00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #ifndef __GECODE_SEARCH_HH__
00041 #define __GECODE_SEARCH_HH__
00042
00043 #include <ctime>
00044
00045 #include "gecode/kernel.hh"
00046
00047
00048
00049
00050
00051 #if !defined(GECODE_STATIC_LIBS) && \
00052 (defined(__CYGWIN__) || defined(__MINGW32__) || defined(_MSC_VER))
00053
00054 #ifdef GECODE_BUILD_SEARCH
00055 #define GECODE_SEARCH_EXPORT __declspec( dllexport )
00056 #else
00057 #define GECODE_SEARCH_EXPORT __declspec( dllimport )
00058 #endif
00059
00060 #else
00061
00062 #ifdef GECODE_GCC_HAS_CLASS_VISIBILITY
00063 #define GECODE_SEARCH_EXPORT __attribute__ ((visibility("default")))
00064 #else
00065 #define GECODE_SEARCH_EXPORT
00066 #endif
00067
00068 #endif
00069
00070 namespace Gecode {
00071
00073 namespace Search {
00074
00080 namespace Config {
00082 const unsigned int c_d = 8;
00084 const unsigned int a_d = 2;
00085 }
00086
00091 class Statistics {
00092 public:
00094 unsigned long int propagate;
00096 unsigned long int fail;
00098 unsigned long int clone;
00100 unsigned long int commit;
00102 size_t memory;
00104 Statistics(void);
00105 };
00106
00107
00122 class GECODE_SEARCH_EXPORT Stop {
00123 public:
00125 Stop(void);
00127 virtual bool stop(const Statistics& s) = 0;
00129 virtual ~Stop(void);
00130 };
00131
00137 class GECODE_SEARCH_EXPORT MemoryStop : public Stop {
00138 protected:
00140 size_t l;
00141 public:
00143 MemoryStop(size_t l);
00145 size_t limit(void) const;
00147 void limit(size_t l);
00149 virtual bool stop(const Statistics& s);
00150 };
00151
00160 class GECODE_SEARCH_EXPORT FailStop : public Stop {
00161 protected:
00163 unsigned long int l;
00164 public:
00166 FailStop(unsigned long int l);
00168 unsigned long int limit(void) const;
00170 void limit(unsigned long int l);
00172 virtual bool stop(const Statistics& s);
00173 };
00174
00179 class GECODE_SEARCH_EXPORT TimeStop : public Stop {
00180 protected:
00182 clock_t s;
00184 unsigned long int l;
00185 public:
00187 TimeStop(unsigned long int l);
00189 unsigned long int limit(void) const;
00191 void limit(unsigned long int l);
00193 void reset(void);
00195 virtual bool stop(const Statistics& s);
00196 };
00197
00224 class Options {
00225 public:
00227 unsigned int c_d;
00229 unsigned int a_d;
00231 Stop* stop;
00233 GECODE_SEARCH_EXPORT static const Options def;
00235 Options(void);
00236 };
00237
00241 class EngineCtrl : public Statistics {
00242 protected:
00244 Stop* st;
00246 bool _stopped;
00248 size_t mem_space;
00250 size_t mem_cur;
00252 size_t mem_total;
00253 public:
00255 EngineCtrl(Stop* st, size_t sz);
00257 void start(void);
00259 bool stop(size_t sz);
00261 bool stopped(void) const;
00263 void push(const Space* s, const BranchingDesc* d);
00265 void constrained(const Space* s1, const Space* s2);
00267 void adapt(const Space* s);
00269 void pop(const Space* s, const BranchingDesc* d);
00271 void lao(const Space* s);
00273 void current(const Space* s);
00275 void reset(const Space* s);
00277 void reset(void);
00278 };
00279
00284 class ReCoNode {
00285 protected:
00287 Space* _space;
00289 unsigned int _alt;
00291 const BranchingDesc* _desc;
00292 public:
00294 ReCoNode(Space* s, Space* c);
00295
00297 Space* space(void) const;
00299 void space(Space* s);
00300
00302 const BranchingDesc* desc(void) const;
00303
00305 unsigned int alt(void) const;
00307 bool rightmost(void) const;
00309 void next(void);
00310
00312 void dispose(void);
00313 };
00314
00315
00329 class ReCoStack {
00330 private:
00332 Support::DynamicStack<ReCoNode> ds;
00334 const unsigned int a_d;
00335 public:
00337 ReCoStack(unsigned int a_d);
00339 const BranchingDesc* push(Space* s, Space* c);
00341 template <class DescType>
00342 const BranchingDesc* nextDesc(EngineCtrl& s, int& alt,
00343 int& closedDescs);
00345 template <class DescType, bool inclusive>
00346 void closeBranch(EngineCtrl& s);
00348 bool next(EngineCtrl& s);
00350 int lc(Space*& s) const;
00352 void unwind(int l);
00354 void commit(Space* s, int i) const;
00362 template <bool constrained>
00363 Space* recompute(unsigned int& d, EngineCtrl& s);
00365 int entries(void) const;
00367 size_t stacksize(void) const;
00369 void reset(void);
00370 };
00371
00376 class DfsEngine : public EngineCtrl {
00377 private:
00379 ReCoStack rcs;
00381 Space* cur;
00383 const unsigned int c_d;
00385 unsigned int d;
00386 public:
00394 DfsEngine(unsigned int c_d, unsigned int a_d, Stop* st, size_t sz);
00396 void init(Space* s);
00398 void reset(Space* s);
00400 void reset(void);
00402 Space* explore(void);
00404 size_t stacksize(void) const;
00406 ~DfsEngine(void);
00407 };
00408
00417 class GECODE_SEARCH_EXPORT DFS {
00418 protected:
00420 DfsEngine e;
00421 public:
00430 DFS(Space* s, unsigned int c_d, unsigned int a_d, Stop* st, size_t sz);
00432 Space* next(void);
00434 Statistics statistics(void) const;
00436 bool stopped(void) const;
00437 };
00438
00439 }
00440
00448 template <class T>
00449 class DFS : public Search::DFS {
00450 public:
00452 DFS(T* s, const Search::Options& o=Search::Options::def);
00454 T* next(void);
00455 };
00456
00458 template <class T>
00459 T* dfs(T* s, const Search::Options& o=Search::Options::def);
00460
00461
00462 namespace Search {
00463
00468 class ProbeEngine : public EngineCtrl {
00469 protected:
00471 class ProbeNode {
00472 private:
00474 Space* _space;
00476 const BranchingDesc* _desc;
00478 unsigned int _alt;
00479 public:
00481 ProbeNode(Space* s, const BranchingDesc* d, unsigned int a);
00483 Space* space(void) const;
00485 const BranchingDesc* desc(void) const;
00487 unsigned int alt(void) const;
00489 void next(void);
00490 };
00492 Support::DynamicStack<ProbeNode> ds;
00494 Space* cur;
00496 unsigned int d;
00498 bool exhausted;
00499 public:
00501 ProbeEngine(Stop* st, size_t s);
00503 void init(Space* s, unsigned int d);
00505 void reset(Space* s, unsigned int d);
00507 size_t stacksize(void) const;
00509 ~ProbeEngine(void);
00511 Space* explore(void);
00513 bool done(void) const;
00514 };
00515
00519 class GECODE_SEARCH_EXPORT LDS {
00520 protected:
00521 Space* root;
00522 unsigned int d_cur;
00523 unsigned int d_max;
00524 bool no_solution;
00525 ProbeEngine e;
00526 public:
00533 LDS(Space* s, unsigned int d, Stop* st, size_t sz);
00535 Space* next(void);
00537 Statistics statistics(void) const;
00539 bool stopped(void) const;
00541 ~LDS(void);
00542 };
00543
00544 }
00545
00550 template <class T>
00551 class LDS : public Search::LDS {
00552 public:
00554 LDS(T* s, unsigned int d,
00555 const Search::Options& o=Search::Options::def);
00557 T* next(void);
00558 };
00559
00564 template <class T>
00565 T* lds(T* s, unsigned int d,
00566 const Search::Options& o=Search::Options::def);
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577 namespace Search {
00578
00582 class BabEngine : public EngineCtrl {
00583 public:
00585 enum ExploreStatus {
00586 ES_SOLUTION,
00587 ES_CONSTRAIN
00588 };
00589 private:
00591 ReCoStack rcs;
00593 Space* cur;
00595 int mark;
00597 ExploreStatus es;
00599 Space* best;
00601 const unsigned int c_d;
00603 unsigned int d;
00604 public:
00612 BabEngine(unsigned int c_d, unsigned int a_d, Stop* st, size_t sz);
00614 void init(Space* s);
00626 GECODE_SEARCH_EXPORT
00627 ExploreStatus explore(Space*& s1, Space*& s2);
00629 size_t stacksize(void) const;
00631 ~BabEngine(void);
00632 };
00633
00643 class GECODE_SEARCH_EXPORT BAB {
00644 protected:
00646 BabEngine e;
00647 public:
00656 BAB(Space* s, unsigned int c_d, unsigned int a_d, Stop* st, size_t sz);
00658 bool stopped(void) const;
00660 Statistics statistics(void) const;
00661 };
00662
00663 }
00664
00676 template <class T>
00677 class BAB : public Search::BAB {
00678 public:
00680 BAB(T* s, const Search::Options& o=Search::Options::def);
00682 T* next(void);
00683 };
00684
00697 template <class T>
00698 T* bab(T* s, const Search::Options& o=Search::Options::def);
00699
00700
00701
00713 template <class T>
00714 class Restart : public DFS<T> {
00715 protected:
00717 Space* root;
00719 Space* best;
00720 public:
00722 Restart(T* s, const Search::Options& o=Search::Options::def);
00724 ~Restart(void);
00726 T* next(void);
00727 };
00728
00740 template <class T>
00741 T* restart(T* s, const Search::Options& o=Search::Options::def);
00742
00743 }
00744
00745 #include "gecode/search/statistics.icc"
00746 #include "gecode/search/stop.icc"
00747 #include "gecode/search/options.icc"
00748 #include "gecode/search/engine-ctrl.icc"
00749
00750 #include "gecode/search/reco-stack.icc"
00751
00752 #include "gecode/search/dfs.icc"
00753 #include "gecode/search/lds.icc"
00754 #include "gecode/search/bab.icc"
00755 #include "gecode/search/restart.icc"
00756
00757 #endif
00758
00759