00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __GECODE_SEARCH_HH__
00025 #define __GECODE_SEARCH_HH__
00026
00027 #include <ctime>
00028
00029 #include "gecode/kernel.hh"
00030
00031
00032
00033
00034
00035
00036 #if !defined(GECODE_STATIC_LIBS) && \
00037 (defined(__CYGWIN__) || defined(__MINGW32__) || defined(_MSC_VER))
00038
00039 #ifdef GECODE_BUILD_SEARCH
00040 #define GECODE_SEARCH_EXPORT __declspec( dllexport )
00041 #else
00042 #define GECODE_SEARCH_EXPORT __declspec( dllimport )
00043 #endif
00044
00045 #else
00046
00047 #ifdef GCC_HASCLASSVISIBILITY
00048
00049 #define GECODE_SEARCH_EXPORT __attribute__ ((visibility("default")))
00050
00051 #else
00052
00053 #define GECODE_SEARCH_EXPORT
00054
00055 #endif
00056 #endif
00057
00058 #include "gecode/support/dynamic-stack.hh"
00059
00060 namespace Gecode {
00061
00063 namespace Search {
00064
00070 namespace Config {
00072 const unsigned int c_d = 8;
00074 const unsigned int a_d = 2;
00075 }
00076
00081 class Statistics {
00082 public:
00084 unsigned long int propagate;
00086 unsigned long int fail;
00088 unsigned long int clone;
00090 unsigned long int commit;
00092 size_t memory;
00094 Statistics(void);
00095 };
00096
00097
00112 class GECODE_SEARCH_EXPORT Stop {
00113 public:
00115 Stop(void);
00117 virtual bool stop(const Statistics& s) = 0;
00119 virtual ~Stop(void);
00120 };
00121
00127 class GECODE_SEARCH_EXPORT MemoryStop : public Stop {
00128 protected:
00130 size_t l;
00131 public:
00133 MemoryStop(size_t l);
00135 size_t limit(void) const;
00137 void limit(size_t l);
00139 virtual bool stop(const Statistics& s);
00140 };
00141
00150 class GECODE_SEARCH_EXPORT FailStop : public Stop {
00151 protected:
00153 unsigned long int l;
00154 public:
00156 FailStop(unsigned long int l);
00158 unsigned long int limit(void) const;
00160 void limit(unsigned long int l);
00162 virtual bool stop(const Statistics& s);
00163 };
00164
00169 class GECODE_SEARCH_EXPORT TimeStop : public Stop {
00170 protected:
00172 clock_t s;
00174 unsigned long int l;
00175 public:
00177 TimeStop(unsigned long int l);
00179 unsigned long int limit(void) const;
00181 void limit(unsigned long int l);
00183 void reset(void);
00185 virtual bool stop(const Statistics& s);
00186 };
00187
00188
00192 class EngineCtrl : public Statistics {
00193 protected:
00195 Stop* st;
00197 bool _stopped;
00199 size_t mem_space;
00201 size_t mem_cur;
00203 size_t mem_total;
00204 public:
00206 EngineCtrl(Stop* st, size_t sz);
00208 void start(void);
00210 bool stop(size_t sz);
00212 bool stopped(void) const;
00214 void push(const Space* s, const BranchingDesc* d);
00216 void constrained(const Space* s1, const Space* s2);
00218 void adapt(const Space* s);
00220 void pop(const Space* s, const BranchingDesc* d);
00222 void lao(const Space* s);
00224 void current(const Space* s);
00226 void reset(const Space* s);
00227 };
00228
00233 class ReCoNode {
00234 protected:
00236 Space* _space;
00238 unsigned int _alt;
00240 const BranchingDesc* _desc;
00241 public:
00243 ReCoNode(Space* s, Space* c);
00244
00246 Space* space(void) const;
00248 void space(Space* s);
00249
00251 const BranchingDesc* desc(void) const;
00252
00254 unsigned int alt(void) const;
00256 bool rightmost(void) const;
00258 void next(void);
00259
00261 void dispose(void);
00262 };
00263
00264
00278 class ReCoStack {
00279 private:
00281 Support::DynamicStack<ReCoNode> ds;
00283 const unsigned int a_d;
00284 public:
00286 ReCoStack(unsigned int a_d);
00288 const BranchingDesc* push(Space* s, Space* c);
00290 bool next(EngineCtrl& s);
00292 int lc(Space*& s) const;
00294 void unwind(int l);
00296 void commit(Space* s, int i) const;
00304 template <bool constrained>
00305 Space* recompute(unsigned int& d, EngineCtrl& s);
00307 int entries(void) const;
00309 size_t stacksize(void) const;
00311 void reset(void);
00312 };
00313
00318 class DfsEngine : public EngineCtrl {
00319 private:
00321 ReCoStack rcs;
00323 Space* cur;
00325 const unsigned int c_d;
00327 unsigned int d;
00328 public:
00336 DfsEngine(unsigned int c_d, unsigned int a_d, Stop* st, size_t sz);
00338 void init(Space* s);
00340 void reset(Space* s);
00342 Space* explore(void);
00344 size_t stacksize(void) const;
00346 ~DfsEngine(void);
00347 };
00348
00357 class GECODE_SEARCH_EXPORT DFS {
00358 protected:
00360 DfsEngine e;
00361 public:
00370 DFS(Space* s, unsigned int c_d, unsigned int a_d, Stop* st, size_t sz);
00372 Space* next(void);
00374 Statistics statistics(void) const;
00376 bool stopped(void) const;
00377 };
00378
00379 }
00380
00388 template <class T>
00389 class DFS : public Search::DFS {
00390 public:
00398 DFS(T* s,
00399 unsigned int c_d=Search::Config::c_d,
00400 unsigned int a_d=Search::Config::a_d,
00401 Search::Stop* st=NULL);
00403 T* next(void);
00404 };
00405
00414 template <class T>
00415 T* dfs(T* s,
00416 unsigned int c_d=Search::Config::c_d,
00417 unsigned int a_d=Search::Config::a_d,
00418 Search::Stop* st=NULL);
00419
00420
00421
00422 namespace Search {
00423
00428 class ProbeEngine : public EngineCtrl {
00429 protected:
00431 class ProbeNode {
00432 private:
00434 Space* _space;
00436 const BranchingDesc* _desc;
00438 unsigned int _alt;
00439 public:
00441 ProbeNode(Space* s, const BranchingDesc* d, unsigned int a);
00443 Space* space(void) const;
00445 const BranchingDesc* desc(void) const;
00447 unsigned int alt(void) const;
00449 void next(void);
00450 };
00452 Support::DynamicStack<ProbeNode> ds;
00454 Space* cur;
00456 unsigned int d;
00457 public:
00459 ProbeEngine(Stop* st, size_t s);
00461 void init(Space* s, unsigned int d);
00463 void reset(Space* s, unsigned int d);
00465 size_t stacksize(void) const;
00467 ~ProbeEngine(void);
00469 Space* explore(void);
00470 };
00471
00475 class GECODE_SEARCH_EXPORT LDS {
00476 protected:
00477 Space* root;
00478 unsigned int d_cur;
00479 unsigned int d_max;
00480 bool no_solution;
00481 ProbeEngine e;
00482 public:
00489 LDS(Space* s, unsigned int d, Stop* st, size_t sz);
00491 Space* next(void);
00493 Statistics statistics(void) const;
00495 bool stopped(void) const;
00497 ~LDS(void);
00498 };
00499
00500 }
00501
00506 template <class T>
00507 class LDS : public Search::LDS {
00508 public:
00514 LDS(T* s, unsigned int d, Search::Stop* st=NULL);
00516 T* next(void);
00517 };
00518
00526 template <class T>
00527 T* lds(T* s,unsigned int d, Search::Stop* st=NULL);
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538 namespace Search {
00539
00543 class BabEngine : public EngineCtrl {
00544 public:
00546 enum ExploreStatus {
00547 ES_SOLUTION,
00548 ES_CONSTRAIN
00549 };
00550 private:
00552 ReCoStack rcs;
00554 Space* cur;
00556 int mark;
00558 ExploreStatus es;
00560 Space* best;
00562 const unsigned int c_d;
00564 unsigned int d;
00565 public:
00573 BabEngine(unsigned int c_d, unsigned int a_d, Stop* st, size_t sz);
00575 void init(Space* s);
00587 GECODE_SEARCH_EXPORT
00588 ExploreStatus explore(Space*& s1, Space*& s2);
00590 size_t stacksize(void) const;
00592 ~BabEngine(void);
00593 };
00594
00604 class GECODE_SEARCH_EXPORT BAB {
00605 protected:
00607 BabEngine e;
00608 public:
00617 BAB(Space* s, unsigned int c_d, unsigned int a_d, Stop* st, size_t sz);
00619 bool stopped(void) const;
00621 Statistics statistics(void) const;
00622 };
00623
00624 }
00625
00630 template <class T>
00631 class BAB : public Search::BAB {
00632 public:
00646 BAB(T* s,
00647 unsigned int c_d=Search::Config::c_d,
00648 unsigned int a_d=Search::Config::a_d,
00649 Search::Stop* st=NULL);
00651 T* next(void);
00652 };
00653
00668 template <class T>
00669 T* bab(T* s,
00670 unsigned int c_d=Search::Config::c_d,
00671 unsigned int a_d=Search::Config::a_d,
00672 Search::Stop* st=NULL);
00673
00674
00675
00680 template <class T>
00681 class Restart : public DFS<T> {
00682 protected:
00684 Space* root;
00686 Space* best;
00687 public:
00701 Restart(T* s,
00702 unsigned int c_d=Search::Config::c_d,
00703 unsigned int a_d=Search::Config::a_d,
00704 Search::Stop* st=NULL);
00706 ~Restart(void);
00708 T* next(void);
00709 };
00710
00724 template <class T>
00725 T* restart(T* s,
00726 unsigned int c_d=Search::Config::c_d,
00727 unsigned int a_d=Search::Config::a_d,
00728 Search::Stop* st=NULL);
00729
00730 }
00731
00732 #include "gecode/search/statistics.icc"
00733 #include "gecode/search/stop.icc"
00734 #include "gecode/search/engine-ctrl.icc"
00735
00736 #include "gecode/search/reco-stack.icc"
00737
00738 #include "gecode/search/dfs.icc"
00739 #include "gecode/search/lds.icc"
00740 #include "gecode/search/bab.icc"
00741 #include "gecode/search/restart.icc"
00742
00743 #endif
00744
00745