44#ifndef GECODE_SEARCH_HH
45#define GECODE_SEARCH_HH
48#include <initializer_list>
57#if !defined(GECODE_STATIC_LIBS) && \
58 (defined(__CYGWIN__) || defined(__MINGW32__) || defined(_MSC_VER))
60#ifdef GECODE_BUILD_SEARCH
61#define GECODE_SEARCH_EXPORT __declspec( dllexport )
63#define GECODE_SEARCH_EXPORT __declspec( dllimport )
68#ifdef GECODE_GCC_HAS_CLASS_VISIBILITY
69#define GECODE_SEARCH_EXPORT __attribute__ ((visibility("default")))
71#define GECODE_SEARCH_EXPORT
77#ifndef GECODE_BUILD_SEARCH
78#define GECODE_LIBRARY_NAME "Search"
83namespace Gecode {
namespace Search {
117 const unsigned int c_d = 8;
119 const unsigned int a_d = 2;
127 const unsigned int d_l = 5;
145namespace Gecode {
namespace Search {
177namespace Gecode {
namespace Search {
179 class WrapTraceRecorder;
181 class EdgeTraceRecorder;
224 bool meta(
void)
const;
229 unsigned int wfst(
void)
const;
231 unsigned int wlst(
void)
const;
233 unsigned int workers(
void)
const;
238 unsigned int efst(
void)
const;
240 unsigned int elst(
void)
const;
242 unsigned int engines(
void)
const;
258 void init(
unsigned int wid,
unsigned int nid,
unsigned int a);
260 void init(
unsigned int wid,
unsigned int nid,
unsigned int a,
269 operator bool(
void)
const;
271 unsigned int wid(
void)
const;
273 unsigned int nid(
void)
const;
277 std::string
string(
void)
const;
301 unsigned int wid,
unsigned int nid,
306 unsigned int wid(
void)
const;
308 unsigned int nid(
void)
const;
318 unsigned int pending;
324 unsigned int n_active;
332 void worker(
unsigned int& wid,
unsigned int&
eid);
338 void _round(
unsigned int eid);
350 unsigned int workers(
void)
const;
352 unsigned int engines(
void)
const;
356 unsigned int eid(
unsigned int wid)
const;
405#ifdef GECODE_HAS_CPPROFILER
414namespace Gecode {
namespace CPProfiler {
470namespace Gecode {
namespace Search {
483 virtual unsigned long long int operator ()(
void)
const = 0;
485 virtual unsigned long long int operator ++(
void) = 0;
512 unsigned long long int min,
unsigned long long int max,
513 unsigned long long int n);
533 unsigned long long int c;
538 virtual unsigned long long int operator ()(
void)
const;
540 virtual unsigned long long int operator ++(
void);
552 unsigned long long int n;
557 virtual unsigned long long int operator ()(
void)
const;
559 virtual unsigned long long int operator ++(
void);
569 unsigned long long int i;
573 static const unsigned long long int n_start = 63U;
577 static unsigned long long int log(
unsigned long long int i);
579 static unsigned long long int luby(
unsigned long long int i);
584 virtual unsigned long long int operator ()(
void)
const;
586 virtual unsigned long long int operator ++(
void);
605 virtual unsigned long long int operator ()(
void)
const;
607 virtual unsigned long long int operator ++(
void);
619 unsigned long long int min;
621 unsigned long long int n;
625 unsigned long long int cur;
629 unsigned long long int min,
unsigned long long int max,
630 unsigned long long int n);
632 virtual unsigned long long int operator ()(
void)
const;
634 virtual unsigned long long int operator ++(
void);
648 unsigned long long int n;
653 virtual unsigned long long int operator ()(
void)
const;
655 virtual unsigned long long int operator ++(
void);
674 virtual unsigned long long int operator ()(
void)
const;
676 virtual unsigned long long int operator ++(
void);
692 unsigned long long int i;
694 unsigned long long int n;
699 virtual unsigned long long int operator ()(
void)
const;
701 virtual unsigned long long int operator ++(
void);
710namespace Gecode {
namespace Search {
788namespace Gecode {
namespace Search {
839 std::atomic<unsigned long long int>
l;
848 unsigned long long int limit(
void)
const;
850 void limit(
unsigned long long int l);
866 std::atomic<unsigned long long int>
l;
875 unsigned long long int limit(
void)
const;
877 void limit(
unsigned long long int l);
888 using clock = std::chrono::steady_clock;
891 std::atomic<clock_rep>
t0;
893 std::atomic<double>
l;
902 double limit(
void)
const;
918 std::atomic<unsigned long long int>
l;
927 unsigned long long int limit(
void)
const;
929 void limit(
unsigned long long int l);
938namespace Gecode {
namespace Search {
965namespace Gecode {
namespace Search {
970 template<
class,
class>
972 template<
class,
template<
class>
class>
981 virtual T*
next(
void);
985 virtual bool stopped(
void)
const;
999namespace Gecode {
namespace Search {
1002 template<
class T,
class E>
1003 Engine*
build(Space* s,
const Options& opt);
1005 template<
class T,
template<
class>
class E>
1006 Engine*
build(Space* s,
const Options& opt);
1023 bool best(
void)
const;
1053 explicit SEBs(
int n);
1055 SEBs(
const std::vector<SEB>& x);
1057 SEBs(std::initializer_list<SEB> x);
1059 template<
class InputIterator>
1060 SEBs(InputIterator first, InputIterator last);
1195 template<
class T,
template<
class>
class E = DFS>
1202 static const bool best = E<T>::best;
1223 template<
class T,
template<
class>
class E>
1227 template<
class T,
template<
class>
class E>
1234namespace Gecode {
namespace Search {
namespace Meta {
1237 template<
class T,
template<
class>
class E>
1241 template<
class T,
template<
class>
class E>
1245#ifdef GECODE_HAS_THREADS
1248 template<
class T,
template<
class>
class E>
1252 template<
class T,
template<
class>
class E>
1279 template<
class T,
template<
class>
class E = DFS>
1291 static const bool best = E<T>::best;
1311 template<
class T,
template<
class>
class E>
BAB(T *s, const Search::Options &o=Search::Options::def)
Initialize engine for space s and options o.
static const bool best
Whether engine does best solution search.
Class to send solution information to CPProfiler.
virtual ~GetInfo(void)
Delete.
virtual std::string getInfo(const Space &home) const =0
Return info for a space.
virtual void done(void)
All workers are done.
virtual ~CPProfilerSearchTracer(void)
Delete.
virtual void round(unsigned int eid)
The engine with id eid goes to a next round (restart or next iteration in LDS).
virtual void skip(const EdgeInfo &ei)
The engine skips an edge.
virtual void node(const EdgeInfo &ei, const NodeInfo &ni)
The engine creates a new node with information ei and ni.
virtual void init(void)
The search engine initializes.
CPProfilerSearchTracer(int eid, std::string name, unsigned int port=Search::Config::cpprofiler_port, const GetInfo *pgi=nullptr)
Initialize.
Choice for performing commit
DFS(T *s, const Search::Options &o=Search::Options::def)
Initialize search engine for space s with options o.
static const bool best
Whether engine does best solution search.
Base class for heap allocated objects.
static const bool best
Whether engine does best solution search.
LDS(T *s, const Search::Options &o=Search::Options::def)
Initialize engine for space s and options o.
No-goods recorded from restarts.
void build(T *s, SEBs &sebs, const Search::Options &o)
The actual build function.
PBS(T *s, const Search::Options &o=Search::Options::def)
Initialize with engines running copies of s with options o.
static const bool best
Whether engine does best solution search.
static const bool best
Whether engine does best solution search.
RBS(T *s, const Search::Options &o)
Initialize engine for space s and options o.
Passing search engine builder arguments.
SEBs(void)
Allocate empty array.
void invalidate(void)
Invalidate edge information (for stealing).
unsigned int alternative(void) const
Return number of alternative.
unsigned int _a
Number of alternative.
unsigned int _nid
The parent node id.
unsigned int wid(void) const
Return parent worker id.
std::string string(void) const
Return string for alternative.
unsigned int nid(void) const
Return parent node id.
EdgeInfo(void)
Initialize as non existing.
unsigned int _wid
The parent worker id (edge does not exist if UINT_MAX).
std::string _s
String corresponding to alternative.
Information about an engine.
bool meta(void) const
Return whether engine is a meta engine.
unsigned int wfst(void) const
Return id of first worker.
unsigned int _lst
Last worker or engine.
EngineInfo(void)
Do not initialize.
unsigned int workers(void) const
Return number of workers.
EngineType _type
The engine type.
EngineType type(void) const
Return engine type.
unsigned int efst(void) const
Return id of first engine.
unsigned int engines(void) const
Return number of engines.
unsigned int _fst
First worker or engine.
unsigned int elst(void) const
Return id of last engine.
unsigned int wlst(void) const
Return id of last worker plus one.
unsigned int _wid
The worker id.
const Choice * _c
The corresponding choice (nullptr if type is not BRANCH).
NodeType _nt
The node type.
NodeType type(void) const
Return node type.
const Choice & choice(void) const
Return corresponding choice.
unsigned int nid(void) const
Return node id.
unsigned int wid(void) const
Return worker id.
const Space & space(void) const
Return corresponding space.
unsigned int _nid
The node id.
const Space & _s
The corresponding space.
NodeInfo(NodeType nt, unsigned int wid, unsigned int nid, const Space &s, const Choice *c=nullptr)
Initialize node info.
Support for tracing search.
unsigned int eid(unsigned int wid) const
Return the engine id of a worker with id wid.
virtual void init(void)=0
The search engine initializes.
EngineType
Which type of engine.
@ BAB
Engine is a BAB engine.
@ DFS
Engine is a DFS engine.
@ AOE
Unspecified engine (any other engine).
@ PBS
Engine is a PBS engine.
@ RBS
Engine is a RBS engine.
@ LDS
Engine is a LDS engine.
unsigned int engines(void) const
Return number of engines.
virtual ~SearchTracer(void)
Delete.
virtual void node(const EdgeInfo &ei, const NodeInfo &ni)=0
The engine creates a new node with information ei and ni.
virtual void round(unsigned int eid)=0
The engine with id eid goes to a next round (restart or next iteration in LDS).
unsigned int workers(void) const
Return number of workers.
virtual void skip(const EdgeInfo &ei)=0
The engine skips an edge.
SearchTracer(void)
Initialize.
virtual void done(void)=0
All workers are done.
Base-class for search engines.
virtual bool stopped(void) const
Check whether engine has been stopped.
virtual T * next(void)
Return next solution (nullptr, if none exists or search has been stopped).
virtual Statistics statistics(void) const
Return statistics.
virtual ~Base(void)
Destructor.
friend Engine * build(Space *, const Options &)
Build an engine of type E for a script T.
Base(Engine *e=nullptr)
Constructor.
Engine * e
The actual search engine.
A class for building search engines.
const bool b
Whether engine to be built is a best solution search engine.
Options opt
Stored and already expanded options.
Builder(const Options &opt, bool best)
Initialize with options opt and best solution search support.
bool best(void) const
Whether engine is a best solution search engine.
Options & options(void)
Provide access to options.
unsigned long long int n
How many number to take from the first.
CutoffAppend(Cutoff *c1, unsigned long long int n, Cutoff *c2)
Constructor.
Cutoff * c1
First cutoff generators.
Cutoff * c2
Second cutoff generators.
CutoffConstant(unsigned long long int c)
Constructor.
unsigned long long int c
Constant.
double n
Current cutoff value.
CutoffGeometric(unsigned long long int scale, double base)
Constructor.
double scale
Scale factor.
unsigned long long int n
Next number in sequence.
CutoffLinear(unsigned long long int scale)
Constructor.
unsigned long long int scale
Scale factor.
static const unsigned long long int n_start
Number of pre-computed luby values.
unsigned long long int i
Iteration number.
static unsigned long long int log(unsigned long long int i)
Compute binary logarithm of i.
CutoffLuby(unsigned long long int scale)
Constructor.
static unsigned long int start[n_start]
Precomputed luby-values.
unsigned long long int scale
Scale factor.
static unsigned long long int luby(unsigned long long int i)
Compute Luby number for step i.
Cutoff * c1
First cutoff generator.
Cutoff * c2
Second cutoff generator.
CutoffMerge(Cutoff *c1, Cutoff *c2)
Constructor.
CutoffRandom(unsigned int seed, unsigned long long int min, unsigned long long int max, unsigned long long int n)
Constructor.
unsigned long long int n
Random values.
unsigned long long int step
Step size.
unsigned long long int cur
Current value.
unsigned long long int min
Minimum cutoff value.
Support::RandomGenerator rnd
Random number generator.
Cutoff * c
Actual cutoff generator.
unsigned long long int cutoff
CutoffRepeat(Cutoff *c, unsigned long long int n)
Constructor.
Base class for cutoff generators for restart-based meta engine.
static Cutoff * linear(unsigned long long int scale=Config::slice)
Create generator for linear sequence scaled by scale.
static Cutoff * append(Cutoff *c1, unsigned long long int n, Cutoff *c2)
Append cutoff values from c2 after n values from c1.
static Cutoff * rnd(unsigned int seed, unsigned long long int min, unsigned long long int max, unsigned long long int n)
Cutoff(void)
Default constructor.
static Cutoff * repeat(Cutoff *c, unsigned long long int n)
Create generator that repeats n times each cutoff value from c.
static Cutoff * merge(Cutoff *c1, Cutoff *c2)
Merge cutoff values from c1 with values from c2.
static Cutoff * constant(unsigned long long int scale=Config::slice)
Create generator for constant sequence with constant s.
static Cutoff * luby(unsigned long long int scale=Config::slice)
Create generator for luby sequence with scale-factor scale.
static Cutoff * geometric(unsigned long long int scale=Config::slice, double base=Config::base)
Recorder for a search tracer with edge information.
Search engine implementation interface
virtual void constrain(const Space &b)
Constrain future solutions to be better than b (raises exception).
virtual Statistics statistics(void) const =0
Return statistics.
virtual Space * next(void)=0
Return next solution (nullptr, if none exists or search has been stopped).
virtual void reset(Space *s)
Reset engine to restart at space s (does nothing).
virtual bool stopped(void) const =0
Check whether engine has been stopped.
virtual NoGoods & nogoods(void)
Return no-goods (the no-goods are empty).
std::atomic< unsigned long long int > l
Failure limit.
virtual bool stop(const Statistics &s, const Options &o)
Return true if failure limit is exceeded.
FailStop(unsigned long long int l)
Stop if failure limit l is exceeded.
unsigned long long int limit(void) const
Return current limit.
NodeStop(unsigned long long int l)
Stop if node limit l is exceeded.
unsigned long long int limit(void) const
Return current limit.
virtual bool stop(const Statistics &s, const Options &o)
Return true if node limit is exceeded.
std::atomic< unsigned long long int > l
Node limit.
static const Options def
Default options.
unsigned int c_d
Create a clone after every c_d commits (commit distance).
bool clone
Whether engines create a clone when being initialized.
Options(void)
Initialize with default values.
unsigned int d_l
Discrepancy limit (for LDS).
Cutoff * cutoff
Cutoff for restart-based search.
Options expand(void) const
Expand with real number of threads.
unsigned int a_d
Create a clone during recomputation if distance is greater than a_d (adaptive distance).
Stop * stop
Stop object for stopping search.
SearchTracer * tracer
Tracer object for tracing search.
unsigned int assets
Number of assets (engines) in a portfolio.
unsigned int slice
Size of a slice in a portfolio (in number of failures).
unsigned int nogoods_limit
Depth limit for extraction of no-goods.
double threads
Number of threads to use.
std::atomic< unsigned long long int > l
Restart limit.
virtual bool stop(const Statistics &s, const Options &o)
Return true if failure limit is exceeded.
RestartStop(unsigned long long int l)
Stop if restart limit l is exceeded.
unsigned long long int limit(void) const
Return current limit.
Statistics(void)
Initialize.
unsigned long int restart
Number of restarts.
Statistics & operator+=(const Statistics &s)
Increment by statistics s.
unsigned long int depth
Maximum depth of search stack.
unsigned long long int fail
Number of failed nodes in search tree.
unsigned long long int node
Number of nodes expanded.
Statistics operator+(const Statistics &s)
Return sum with s.
unsigned long int nogood
Number of no-goods posted.
Base-class for Stop-object.
static Stop * node(unsigned long long int l)
Stop if node limit l has been exceeded.
Stop(void)
Default constructor.
virtual bool stop(const Statistics &s, const Options &o)=0
Stop search, if returns true.
static Stop * time(double l)
Stop if time limit l (in milliseconds) has been exceeded.
static Stop * fail(unsigned long long int l)
Stop if failure limit l has been exceeded.
static Stop * restart(unsigned long long int l)
Stop if restart limit l has been exceeded.
clock::time_point::rep clock_rep
virtual bool stop(const Statistics &s, const Options &o)
Return true if time limit is exceeded.
std::chrono::steady_clock clock
TimeStop(double l)
Stop if search exceeds l milliseconds (from creation of this object).
double limit(void) const
Return current limit in milliseconds.
std::atomic< double > l
Current limit in milliseconds.
std::atomic< clock_rep > t0
Clock representation at the start of timing.
void reset(void)
Reset time to zero.
Simple recorder for a search tracer.
Recorder for engine events (for access control).
StatusStatistics(void)
Initialize.
virtual void init(void)
The search engine initializes.
StdSearchTracer(std::ostream &os=std::cerr)
Initialize with output stream os.
virtual ~StdSearchTracer(void)
Delete.
virtual void round(unsigned int eid)
The engine with id eid goes to a next round (restart or next iteration in LDS).
virtual void done(void)
All workers are done.
static StdSearchTracer def
Default tracer (printing to std::cerr).
static const char * t2s[EngineType::AOE+1]
Map engine type to string.
virtual void node(const EdgeInfo &ei, const NodeInfo &ni)
The engine creates a new node with information ei and ni.
virtual void skip(const EdgeInfo &ei)
The engine skips an edge.
std::ostream & os
Output stream to use.
Array with arbitrary number of elements.
A mutex for mutual exclausion among several threads.
LinearCongruentialGenerator< 2147483647, 48271, 44488, 3399 > RandomGenerator
Default values for linear congruential generator.
T * pbs(T *s, const Search::Options &o=Search::Options::def)
Run a portfolio of search engines.
T * bab(T *s, const Search::Options &o=Search::Options::def)
Perform depth-first branch-and-bound search for subclass T of space s and options o.
T * lds(T *s, const Search::Options &o=Search::Options::def)
Invoke limited-discrepancy search for s as root node and optionso.
T * rbs(T *s, const Search::Options &o)
Perform restart-based search.
Code that is specific to the CPProfiler.
const unsigned int initial_delay
Initial delay in milliseconds for all but first worker thread.
const unsigned int d_l
Default discrepancy limit for LDS.
const unsigned int a_d
Create a clone during recomputation if distance is greater than a_d (adaptive distance).
const double base
Base for geometric restart sequence.
const unsigned int c_d
Create a clone after every c_d commits (commit distance).
const unsigned int slice
Size of a slice in a portfolio and scale factor for restarts(in number of failures).
const unsigned int cpprofiler_port
Default port for CPProfiler.
const unsigned int nogoods_limit
Depth limit for no-good generation during search.
const unsigned int steal_limit
Minimal number of open nodes for stealing.
const double threads
Number of threads to use.
const bool clone
Whether engines create a clone when being initialized.
Parallel search engine implementations
Sequential search engine implementations
Engine * build(Space *s, const Options &opt)
Build an engine of type E for a script T.
Gecode toplevel namespace
Search::Builder * SEB
Type for a search engine builder.
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
T * dfs(T *s, const Search::Options &o=Search::Options::def)
Invoke depth-first search engine for subclass T of space s with options o.
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
#define GECODE_SEARCH_EXPORT