Generated on Tue Apr 18 10:22:09 2017 for Gecode by doxygen 1.6.3

search.hh

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *     Guido Tack <tack@gecode.org>
00006  *
00007  *  Copyright:
00008  *     Christian Schulte, 2002
00009  *     Guido Tack, 2004
00010  *
00011  *  Last modified:
00012  *     $Date: 2016-10-25 12:52:26 +0200 (Tue, 25 Oct 2016) $ by $Author: schulte $
00013  *     $Revision: 15233 $
00014  *
00015  *  This file is part of Gecode, the generic constraint
00016  *  development environment:
00017  *     http://www.gecode.org
00018  *
00019  *  Permission is hereby granted, free of charge, to any person obtaining
00020  *  a copy of this software and associated documentation files (the
00021  *  "Software"), to deal in the Software without restriction, including
00022  *  without limitation the rights to use, copy, modify, merge, publish,
00023  *  distribute, sublicense, and/or sell copies of the Software, and to
00024  *  permit persons to whom the Software is furnished to do so, subject to
00025  *  the following conditions:
00026  *
00027  *  The above copyright notice and this permission notice shall be
00028  *  included in all copies or substantial portions of the Software.
00029  *
00030  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00031  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00032  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00033  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00034  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00035  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00036  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00037  *
00038  */
00039 
00040 #ifndef __GECODE_SEARCH_HH__
00041 #define __GECODE_SEARCH_HH__
00042 
00043 #include <gecode/kernel.hh>
00044 
00045 /*
00046  * Configure linking
00047  *
00048  */
00049 #if !defined(GECODE_STATIC_LIBS) && \
00050     (defined(__CYGWIN__) || defined(__MINGW32__) || defined(_MSC_VER))
00051 
00052 #ifdef GECODE_BUILD_SEARCH
00053 #define GECODE_SEARCH_EXPORT __declspec( dllexport )
00054 #else
00055 #define GECODE_SEARCH_EXPORT __declspec( dllimport )
00056 #endif
00057 
00058 #else
00059 
00060 #ifdef GECODE_GCC_HAS_CLASS_VISIBILITY
00061 #define GECODE_SEARCH_EXPORT __attribute__ ((visibility("default")))
00062 #else
00063 #define GECODE_SEARCH_EXPORT
00064 #endif
00065 
00066 #endif
00067 
00068 // Configure auto-linking
00069 #ifndef GECODE_BUILD_SEARCH
00070 #define GECODE_LIBRARY_NAME "Search"
00071 #include <gecode/support/auto-link.hpp>
00072 #endif
00073 
00074 
00075 namespace Gecode { namespace Search {
00076 
00078   namespace Sequential {}
00079 
00081   namespace Parallel {}
00082 
00084   namespace Meta {}
00085 
00086   namespace Meta {
00087 
00089     namespace Sequential {}
00090 
00092     namespace Parallel {}
00093 
00094   }
00095 
00096 
00102   namespace Config {
00104     const bool clone = true;
00106     const double threads = 1.0;
00107 
00109     const unsigned int c_d = 8;
00111     const unsigned int a_d = 2;
00112 
00114     const unsigned int steal_limit = 3;
00116     const unsigned int initial_delay = 5;
00117 
00119     const unsigned int d_l = 5;
00120 
00122     const double base = 1.5;
00124     const unsigned int slice = 250;
00125 
00127     const unsigned int nogoods_limit = 128;
00128   }
00129 
00130 }}
00131 
00132 #include <gecode/search/exception.hpp>
00133 
00134 namespace Gecode { namespace Search {
00135 
00140   class Statistics : public StatusStatistics {
00141   public:
00143     unsigned long int fail;
00145     unsigned long int node;
00147     unsigned long int depth;
00149     unsigned long int restart;
00151     unsigned long int nogood;
00153     Statistics(void);
00155     void reset(void);
00157     Statistics operator +(const Statistics& s);
00159     Statistics& operator +=(const Statistics& s);
00160   };
00161 
00162 }}
00163 
00164 #include <gecode/search/statistics.hpp>
00165 
00166 namespace Gecode { namespace Search {
00167 
00172   class GECODE_SEARCH_EXPORT Cutoff : public HeapAllocated {
00173   public:
00175 
00176 
00177     Cutoff(void);
00179     virtual unsigned long int operator ()(void) const = 0;
00181     virtual unsigned long int operator ++(void) = 0;
00183     virtual ~Cutoff(void);
00185 
00186 
00187 
00188     static Cutoff*
00189     constant(unsigned long int scale=Config::slice);
00191     static Cutoff*
00192     linear(unsigned long int scale=Config::slice);
00196     static Cutoff*
00197     geometric(unsigned long int scale=Config::slice, double base=Config::base);
00199     static Cutoff*
00200     luby(unsigned long int scale=Config::slice);
00205     static Cutoff*
00206     rnd(unsigned int seed,
00207         unsigned long int min, unsigned long int max,
00208         unsigned long int n);
00210     static Cutoff*
00211     append(Cutoff* c1, unsigned long int n, Cutoff* c2);
00213     static Cutoff*
00214     merge(Cutoff* c1, Cutoff* c2);
00216     static Cutoff*
00217     repeat(Cutoff* c, unsigned long int n);
00219   };
00220 
00225   class GECODE_SEARCH_EXPORT CutoffConstant : public Cutoff {
00226   protected:
00228     unsigned long int c;
00229   public:
00231     CutoffConstant(unsigned long int c);
00233     virtual unsigned long int operator ()(void) const;
00235     virtual unsigned long int operator ++(void);
00236   };
00237 
00242   class GECODE_SEARCH_EXPORT CutoffLinear : public Cutoff {
00243   protected:
00245     unsigned long int scale;
00247     unsigned long int n;
00248   public:
00250     CutoffLinear(unsigned long int scale);
00252     virtual unsigned long int operator ()(void) const;
00254     virtual unsigned long int operator ++(void);
00255   };
00256 
00261   class GECODE_SEARCH_EXPORT CutoffLuby : public Cutoff {
00262   protected:
00264     unsigned long int i;
00266     unsigned long int scale;
00268     static const unsigned long int n_start = 63U;
00270     static unsigned long int start[n_start];
00272     static unsigned long int log(unsigned long int i);
00274     static unsigned long int luby(unsigned long int i);
00275   public:
00277     CutoffLuby(unsigned long int scale);
00279     virtual unsigned long int operator ()(void) const;
00281     virtual unsigned long int operator ++(void);
00282   };
00283 
00288   class GECODE_SEARCH_EXPORT CutoffGeometric : public Cutoff {
00289   protected:
00291     double n;
00293     double scale;
00295     double base;
00296   public:
00298     CutoffGeometric(unsigned long int scale, double base);
00300     virtual unsigned long int operator ()(void) const;
00302     virtual unsigned long int operator ++(void);
00303   };
00304 
00309   class GECODE_SEARCH_EXPORT CutoffRandom : public Cutoff {
00310   protected:
00312     Support::RandomGenerator rnd;
00314     unsigned long int min;
00316     unsigned long int n;
00318     unsigned long int step;
00320     unsigned long int cur;
00321   public:
00323     CutoffRandom(unsigned int seed,
00324                  unsigned long int min, unsigned long int max,
00325                  unsigned long int n);
00327     virtual unsigned long int operator ()(void) const;
00329     virtual unsigned long int operator ++(void);
00330   };
00331 
00336   class GECODE_SEARCH_EXPORT CutoffAppend : public Cutoff {
00337   protected:
00339     Cutoff* c1;
00341     Cutoff* c2;
00343     unsigned long int n;
00344   public:
00346     CutoffAppend(Cutoff* c1, unsigned long int n, Cutoff* c2);
00348     virtual unsigned long int operator ()(void) const;
00350     virtual unsigned long int operator ++(void);
00352     virtual ~CutoffAppend(void);
00353   };
00354 
00359   class GECODE_SEARCH_EXPORT CutoffMerge : public Cutoff {
00360   protected:
00362     Cutoff* c1;
00364     Cutoff* c2;
00365   public:
00367     CutoffMerge(Cutoff* c1, Cutoff* c2);
00369     virtual unsigned long int operator ()(void) const;
00371     virtual unsigned long int operator ++(void);
00373     virtual ~CutoffMerge(void);
00374   };
00375 
00380   class GECODE_SEARCH_EXPORT CutoffRepeat : public Cutoff {
00381   protected:
00383     Cutoff* c;
00384     // Current cutoff
00385     unsigned int cutoff;
00386     // Iteration
00387     unsigned long int i;
00388     // Number of repetitions
00389     unsigned long int n;
00390   public:
00392     CutoffRepeat(Cutoff* c, unsigned long int n);
00394     virtual unsigned long int operator ()(void) const;
00396     virtual unsigned long int operator ++(void);
00398     virtual ~CutoffRepeat(void);
00399   };
00400 
00401 }}
00402 
00403 #include <gecode/search/cutoff.hpp>
00404 
00405 namespace Gecode { namespace Search {
00406 
00407     class Stop;
00408 
00446     class Options {
00447     public:
00449       bool clone;
00451       double threads;
00453       unsigned int c_d;
00455       unsigned int a_d;
00457       unsigned int d_l;
00459       bool share_rbs;
00461       bool share_pbs;
00463       unsigned int assets;
00465       unsigned int slice;
00467       unsigned int nogoods_limit;
00469       Stop* stop;
00471       Cutoff* cutoff;
00473       GECODE_SEARCH_EXPORT static const Options def;
00475       Options(void);
00477       GECODE_SEARCH_EXPORT Options
00478       expand(void) const;
00479     };
00480 
00481 }}
00482 
00483 #include <gecode/search/options.hpp>
00484 
00485 namespace Gecode { namespace Search {
00486 
00501   class GECODE_SEARCH_EXPORT Stop : public HeapAllocated {
00502   public:
00504 
00505 
00506     Stop(void);
00508     virtual bool stop(const Statistics& s, const Options& o) = 0;
00510     virtual ~Stop(void);
00512 
00513 
00514 
00515     static Stop* node(unsigned long int l);
00517     static Stop* fail(unsigned long int l);
00519     static Stop* time(unsigned long int l);
00521   };
00522 
00531   class GECODE_SEARCH_EXPORT NodeStop : public Stop {
00532   protected:
00534     unsigned long int l;
00535   public:
00537     NodeStop(unsigned long int l);
00539     unsigned long int limit(void) const;
00541     void limit(unsigned long int l);
00543     virtual bool stop(const Statistics& s, const Options& o);
00544   };
00545 
00554   class GECODE_SEARCH_EXPORT FailStop : public Stop {
00555   protected:
00557     unsigned long int l;
00558   public:
00560     FailStop(unsigned long int l);
00562     unsigned long int limit(void) const;
00564     void limit(unsigned long int l);
00566     virtual bool stop(const Statistics& s, const Options& o);
00567   };
00568 
00573   class GECODE_SEARCH_EXPORT TimeStop : public Stop {
00574   protected:
00576     Support::Timer t;
00578     unsigned long int l;
00579   public:
00581     TimeStop(unsigned long int l);
00583     unsigned long int limit(void) const;
00585     void limit(unsigned long int l);
00587     void reset(void);
00589     virtual bool stop(const Statistics& s, const Options& o);
00590   };
00591 
00592 }}
00593 
00594 #include <gecode/search/stop.hpp>
00595 
00596 namespace Gecode { namespace Search {
00597 
00601   class GECODE_SEARCH_EXPORT Engine : public HeapAllocated {
00602   public:
00604     virtual Space* next(void) = 0;
00606     virtual Statistics statistics(void) const = 0;
00608     virtual bool stopped(void) const = 0;
00610     virtual void constrain(const Space& b);
00612     virtual void reset(Space* s);
00614     virtual NoGoods& nogoods(void);
00616     virtual ~Engine(void);
00617   };
00618 
00619 }}
00620 
00621 #include <gecode/search/engine.hpp>
00622 
00623 namespace Gecode { namespace Search {
00624 
00626   template<class T>
00627   class Base : public HeapAllocated {
00628     template<class, class>
00629     friend Engine* build(Space*, const Options&);
00630     template<class, template<class> class>
00631     friend Engine* build(Space*, const Options&);
00632   protected:
00634     Engine* e;
00636     Base(Engine* e = NULL);
00637   public:
00639     virtual T* next(void);
00641     virtual Statistics statistics(void) const;
00643     virtual bool stopped(void) const;
00645     virtual ~Base(void);
00646   private:
00648     Base(const Base&);
00650     Base& operator =(const Base&);
00651   };
00652 
00653 }}
00654 
00655 #include <gecode/search/base.hpp>
00656 
00657 namespace Gecode { namespace Search {
00658 
00660   template<class T, class E>
00661   Engine* build(Space* s, const Options& opt);
00663   template<class T, template<class> class E>
00664   Engine* build(Space* s, const Options& opt);
00665 
00667   class GECODE_SEARCH_EXPORT Builder : public HeapAllocated {
00668   protected:
00670     Options opt;
00672     const bool b;
00673   public:
00675     Builder(const Options& opt, bool best);
00677     Options& options(void);
00679     const Options& options(void) const;
00681     bool best(void) const;
00683     virtual Engine* operator() (Space* s) const = 0;
00685     virtual ~Builder(void);
00686   };
00687 
00688 }}
00689 
00690 #include <gecode/search/build.hpp>
00691 
00692 namespace Gecode {
00693 
00695   typedef Search::Builder* SEB;
00696 
00697 }
00698 
00699 #include <gecode/search/traits.hpp>
00700 
00701 namespace Gecode {
00702 
00704   class SEBs : public PrimArgArray<SEB> {
00705   public:
00707 
00708 
00709     SEBs(void);
00711     explicit SEBs(int n);
00713     SEBs(const std::vector<SEB>& x);
00715     template<class InputIterator>
00716     SEBs(InputIterator first, InputIterator last);
00718     SEBs(const PrimArgArray<SEB>& a);
00720     GECODE_SEARCH_EXPORT
00721     SEBs(int n, SEB b0, ...);
00723   };
00724 
00725 }
00726 
00727 #include <gecode/search/sebs.hpp>
00728 
00729 namespace Gecode {
00730 
00738   template<class T>
00739   class DFS : public Search::Base<T> {
00740   public:
00742     DFS(T* s, const Search::Options& o=Search::Options::def);
00744     static const bool best = false;
00745   };
00746 
00748   template<class T>
00749   T* dfs(T* s, const Search::Options& o=Search::Options::def);
00750 
00752   template<class T>
00753   SEB dfs(const Search::Options& o=Search::Options::def);
00754 
00755 }
00756 
00757 #include <gecode/search/dfs.hpp>
00758 
00759 namespace Gecode {
00760 
00772   template<class T>
00773   class BAB : public Search::Base<T> {
00774   public:
00776     BAB(T* s, const Search::Options& o=Search::Options::def);
00778     static const bool best = true;
00779   };
00780 
00793   template<class T>
00794   T* bab(T* s, const Search::Options& o=Search::Options::def);
00795 
00797   template<class T>
00798   SEB bab(const Search::Options& o=Search::Options::def);
00799 
00800 }
00801 
00802 #include <gecode/search/bab.hpp>
00803 
00804 namespace Gecode {
00805 
00810   template<class T>
00811   class LDS : public Search::Base<T> {
00812   public:
00814     LDS(T* s, const Search::Options& o=Search::Options::def);
00816     static const bool best = false;
00817   };
00818 
00823   template<class T>
00824   T* lds(T* s, const Search::Options& o=Search::Options::def);
00825 
00827   template<class T>
00828   SEB lds(const Search::Options& o=Search::Options::def);
00829 
00830 }
00831 
00832 #include <gecode/search/lds.hpp>
00833 
00834 namespace Gecode {
00835 
00854   template<class T, template<class> class E = DFS>
00855   class RBS : public Search::Base<T> {
00856     using Search::Base<T>::e;
00857   public:
00859     RBS(T* s, const Search::Options& o);
00861     static const bool best = E<T>::best;
00862   };
00863 
00882   template<class T, template<class> class E>
00883   T* rbs(T* s, const Search::Options& o);
00884 
00886   template<class T, template<class> class E>
00887   SEB rbs(const Search::Options& o);
00888 
00889 }
00890 
00891 #include <gecode/search/rbs.hpp>
00892 
00893 namespace Gecode { namespace Search { namespace Meta {
00894 
00896   template<class T, template<class> class E>
00897   Engine* sequential(T* master, const Search::Statistics& stat, Options& opt);
00898 
00900   template<class T, template<class> class E>
00901   Engine* sequential(T* master, SEBs& sebs,
00902                      const Search::Statistics& stat, Options& opt, bool best);
00903 
00904 #ifdef GECODE_HAS_THREADS
00905 
00907   template<class T, template<class> class E>
00908   Engine* parallel(T* master, const Search::Statistics& stat, Options& opt);
00909 
00911   template<class T, template<class> class E>
00912   Engine* parallel(T* master, SEBs& sebs,
00913                    const Search::Statistics& stat, Options& opt, bool best);
00914 
00915 #endif
00916 
00917 }}}
00918 
00919 namespace Gecode {
00920 
00938   template<class T, template<class> class E = DFS>
00939   class PBS : public Search::Base<T> {
00940     using Search::Base<T>::e;
00941   protected:
00943     void build(T* s, SEBs& sebs, const Search::Options& o);
00944   public:
00946     PBS(T* s, const Search::Options& o=Search::Options::def);
00948     PBS(T* s, SEBs& sebs, const Search::Options& o=Search::Options::def);
00950     PBS(T* s, SEB seb0, SEB seb1,
00951         const Search::Options& o=Search::Options::def);
00953     PBS(T* s, SEB seb0, SEB seb1, SEB seb2,
00954         const Search::Options& o=Search::Options::def);
00956     PBS(T* s, SEB seb0, SEB seb1, SEB seb2, SEB seb3,
00957         const Search::Options& o=Search::Options::def);
00959     static const bool best = E<T>::best;
00960   };
00961 
00979   template<class T, template<class> class E>
00980   T* pbs(T* s, const Search::Options& o=Search::Options::def);
00981 
00983   template<class T>
00984   SEB pbs(const Search::Options& o=Search::Options::def);
00985 
00986 }
00987 
00988 #include <gecode/search/pbs.hpp>
00989 
00990 #endif
00991 
00992 // STATISTICS: search-other