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 #ifndef __GECODE_DRIVER_HH__
00039 #define __GECODE_DRIVER_HH__
00040
00041 #include <gecode/minimodel.hh>
00042 #include <gecode/search.hh>
00043 #ifdef GECODE_HAS_GIST
00044 #include <gecode/gist.hh>
00045 #endif
00046
00047
00048
00049
00050
00051 #if !defined(GECODE_STATIC_LIBS) && \
00052 (defined(__CYGWIN__) || defined(__MINGW32__) || defined(_MSC_VER))
00053
00054 #ifdef GECODE_BUILD_DRIVER
00055 #define GECODE_DRIVER_EXPORT __declspec( dllexport )
00056 #else
00057 #define GECODE_DRIVER_EXPORT __declspec( dllimport )
00058 #endif
00059
00060 #else
00061
00062 #ifdef GECODE_GCC_HAS_CLASS_VISIBILITY
00063 #define GECODE_DRIVER_EXPORT __attribute__ ((visibility("default")))
00064 #else
00065 #define GECODE_DRIVER_EXPORT
00066 #endif
00067
00068 #endif
00069
00070
00071 #ifndef GECODE_BUILD_DRIVER
00072 #define GECODE_LIBRARY_NAME "Driver"
00073 #include <gecode/support/auto-link.hpp>
00074 #endif
00075
00086 namespace Gecode {
00087
00088
00098 enum ScriptMode {
00099 SM_SOLUTION,
00100 SM_TIME,
00101 SM_STAT,
00102 SM_GIST
00103 };
00104
00109 enum RestartMode {
00110 RM_NONE,
00111 RM_CONSTANT,
00112 RM_LINEAR,
00113 RM_LUBY,
00114 RM_GEOMETRIC
00115 };
00116
00117 class BaseOptions;
00118
00119 namespace Driver {
00124 class GECODE_DRIVER_EXPORT BaseOption {
00125 friend class Gecode::BaseOptions;
00126 protected:
00127 const char* opt;
00128 const char* exp;
00129 BaseOption* next;
00130
00131 char* argument(int argc, char* argv[]) const;
00132 public:
00134 BaseOption(const char* o, const char* e);
00136 virtual int parse(int argc, char* argv[]) = 0;
00138 virtual void help(void) = 0;
00140 virtual ~BaseOption(void);
00142 static char* strdup(const char* s);
00144 static void strdel(const char* s);
00145 };
00146
00151 class GECODE_DRIVER_EXPORT StringValueOption : public BaseOption {
00152 protected:
00153 const char* cur;
00154 public:
00156 StringValueOption(const char* o, const char* e, const char* v=NULL);
00158 void value(const char* v);
00160 const char* value(void) const;
00162 virtual int parse(int argc, char* argv[]);
00164 virtual void help(void);
00166 virtual ~StringValueOption(void);
00167 };
00168
00169
00174 class GECODE_DRIVER_EXPORT StringOption : public BaseOption {
00175 protected:
00177 class Value {
00178 public:
00179 int val;
00180 const char* opt;
00181 const char* help;
00182 Value* next;
00183 };
00184 int cur;
00185 Value* fst;
00186 Value* lst;
00187 public:
00189 StringOption(const char* o, const char* e, int v=0);
00191 void value(int v);
00193 int value(void) const;
00195 void add(int v, const char* o, const char* h = NULL);
00197 virtual int parse(int argc, char* argv[]);
00199 virtual void help(void);
00201 virtual ~StringOption(void);
00202 };
00203
00204
00209 class GECODE_DRIVER_EXPORT IntOption : public BaseOption {
00210 protected:
00211 int cur;
00212 public:
00214 IntOption(const char* o, const char* e, int v=0);
00216 void value(int v);
00218 int value(void) const;
00220 virtual int parse(int argc, char* argv[]);
00222 virtual void help(void);
00223 };
00224
00229 class GECODE_DRIVER_EXPORT UnsignedIntOption : public BaseOption {
00230 protected:
00231 unsigned int cur;
00232 public:
00234 UnsignedIntOption(const char* o, const char* e, unsigned int v=0);
00236 void value(unsigned int v);
00238 unsigned int value(void) const;
00240 virtual int parse(int argc, char* argv[]);
00242 virtual void help(void);
00243 };
00244
00249 class GECODE_DRIVER_EXPORT DoubleOption : public BaseOption {
00250 protected:
00251 double cur;
00252 public:
00254 DoubleOption(const char* o, const char* e, double v=0);
00256 void value(double v);
00258 double value(void) const;
00260 virtual int parse(int argc, char* argv[]);
00262 virtual void help(void);
00263 };
00264
00269 class GECODE_DRIVER_EXPORT BoolOption : public BaseOption {
00270 protected:
00271 bool cur;
00272 public:
00274 BoolOption(const char* o, const char* e, bool v=false);
00276 void value(bool v);
00278 bool value(void) const;
00280 virtual int parse(int argc, char* argv[]);
00282 virtual void help(void);
00283 };
00284
00289 class GECODE_DRIVER_EXPORT IplOption : public BaseOption {
00290 protected:
00291 IntPropLevel cur;
00292 public:
00294 IplOption(IntPropLevel ipl=IPL_DEF);
00296 void value(IntPropLevel l);
00298 IntPropLevel value(void) const;
00300 virtual int parse(int argc, char* argv[]);
00302 virtual void help(void);
00303 };
00304
00309 class GECODE_DRIVER_EXPORT TraceOption : public BaseOption {
00310 protected:
00311 int cur;
00312 public:
00314 TraceOption(int f=0);
00316 void value(int f);
00318 int value(void) const;
00320 virtual int parse(int argc, char* argv[]);
00322 virtual void help(void);
00323 };
00324
00325 }
00326
00331 class GECODE_DRIVER_EXPORT BaseOptions {
00332 protected:
00333 Driver::BaseOption* fst;
00334 Driver::BaseOption* lst;
00335 const char* _name;
00336 public:
00338 BaseOptions(const char* s);
00340 virtual void help(void);
00341
00343 void add(Driver::BaseOption& o);
00351 void parse(int& argc, char* argv[]);
00352
00354 const char* name(void) const;
00356 void name(const char*);
00357
00359 virtual ~BaseOptions(void);
00360 };
00361
00366 class GECODE_DRIVER_EXPORT Options : public BaseOptions {
00367 protected:
00369
00370 Driver::StringOption _model;
00371 Driver::StringOption _symmetry;
00372 Driver::StringOption _propagation;
00373 Driver::IplOption _ipl;
00374 Driver::StringOption _branching;
00375 Driver::DoubleOption _decay;
00376 Driver::UnsignedIntOption _seed;
00377 Driver::DoubleOption _step;
00378
00379
00381
00382 Driver::StringOption _search;
00383 Driver::UnsignedIntOption _solutions;
00384 Driver::DoubleOption _threads;
00385 Driver::UnsignedIntOption _c_d;
00386 Driver::UnsignedIntOption _a_d;
00387 Driver::UnsignedIntOption _d_l;
00388 Driver::UnsignedIntOption _node;
00389 Driver::UnsignedIntOption _fail;
00390 Driver::UnsignedIntOption _time;
00391 Driver::UnsignedIntOption _assets;
00392 Driver::UnsignedIntOption _slice;
00393 Driver::StringOption _restart;
00394 Driver::DoubleOption _r_base;
00395 Driver::UnsignedIntOption _r_scale;
00396 Driver::BoolOption _nogoods;
00397 Driver::UnsignedIntOption _nogoods_limit;
00398 Driver::DoubleOption _relax;
00399 Driver::BoolOption _interrupt;
00400
00401
00403
00404 Driver::StringOption _mode;
00405 Driver::UnsignedIntOption _samples;
00406 Driver::UnsignedIntOption _iterations;
00407 Driver::BoolOption _print_last;
00408 Driver::StringValueOption _out_file;
00409 Driver::StringValueOption _log_file;
00410 Driver::TraceOption _trace;
00411
00412
00413 public:
00415 Options(const char* s);
00416
00418
00419
00420 void model(int v);
00422 void model(int v, const char* o, const char* h = NULL);
00424 int model(void) const;
00425
00427 void symmetry(int v);
00429 void symmetry(int v, const char* o, const char* h = NULL);
00431 int symmetry(void) const;
00432
00434 void propagation(int v);
00436 void propagation(int v, const char* o, const char* h = NULL);
00438 int propagation(void) const;
00439
00441 void ipl(IntPropLevel i);
00443 IntPropLevel ipl(void) const;
00444
00446 void branching(int v);
00448 void branching(int v, const char* o, const char* h = NULL);
00450 int branching(void) const;
00451
00453 void decay(double d);
00455 double decay(void) const;
00456
00458 void seed(unsigned int s);
00460 unsigned int seed(void) const;
00461
00463 void step(double s);
00465 double step(void) const;
00467
00469
00470
00471 void search(int v);
00473 void search(int v, const char* o, const char* h = NULL);
00475 int search(void) const;
00476
00478 void solutions(unsigned int n);
00480 unsigned int solutions(void) const;
00481
00483 void threads(double n);
00485 double threads(void) const;
00486
00488 void c_d(unsigned int d);
00490 unsigned int c_d(void) const;
00491
00493 void a_d(unsigned int d);
00495 unsigned int a_d(void) const;
00496
00498 void d_l(unsigned int d);
00500 unsigned int d_l(void) const;
00501
00503 void node(unsigned int n);
00505 unsigned int node(void) const;
00506
00508 void fail(unsigned int n);
00510 unsigned int fail(void) const;
00511
00513 void time(unsigned int t);
00515 unsigned int time(void) const;
00516
00518 void assets(unsigned int n);
00520 unsigned int assets(void) const;
00521
00523 void slice(unsigned int n);
00525 unsigned int slice(void) const;
00526
00528 void restart(RestartMode r);
00530 RestartMode restart(void) const;
00531
00533 void restart_base(double base);
00535 double restart_base(void) const;
00536
00538 void restart_scale(unsigned int scale);
00540 unsigned int restart_scale(void) const;
00541
00543 void nogoods(bool b);
00545 bool nogoods(void) const;
00546
00548 void nogoods_limit(unsigned int l);
00550 unsigned int nogoods_limit(void) const;
00551
00553 void relax(double d);
00555 double relax(void) const;
00556
00558 void interrupt(bool b);
00560 bool interrupt(void) const;
00562
00564
00565
00566 void mode(ScriptMode em);
00568 ScriptMode mode(void) const;
00569
00571 void samples(unsigned int s);
00573 unsigned int samples(void) const;
00574
00576 void iterations(unsigned int i);
00578 unsigned int iterations(void) const;
00579
00581 void print_last(bool p);
00583 bool print_last(void) const;
00584
00586 void out_file(const char* f);
00588 const char* out_file(void) const;
00589
00591 void log_file(const char* f);
00593 const char* log_file(void) const;
00594
00596 void trace(int f);
00598 int trace(void) const;
00600
00601 #ifdef GECODE_HAS_GIST
00602
00603 class _I {
00604 private:
00606 Support::DynamicArray<Gist::Inspector*,Heap> _click;
00608 unsigned int n_click;
00610 Support::DynamicArray<Gist::Inspector*,Heap> _solution;
00612 unsigned int n_solution;
00614 Support::DynamicArray<Gist::Inspector*,Heap> _move;
00616 unsigned int n_move;
00618 Support::DynamicArray<Gist::Comparator*,Heap> _compare;
00620 unsigned int n_compare;
00621 public:
00623 _I(void);
00625 void click(Gist::Inspector* i);
00627 void solution(Gist::Inspector* i);
00629 void move(Gist::Inspector* i);
00631 void compare(Gist::Comparator* i);
00632
00634 Gist::Inspector* click(unsigned int i) const;
00636 Gist::Inspector* solution(unsigned int i) const;
00638 Gist::Inspector* move(unsigned int i) const;
00640 Gist::Comparator* compare(unsigned int i) const;
00641 } inspect;
00642 #endif
00643 };
00644
00649 class GECODE_DRIVER_EXPORT SizeOptions : public Options {
00650 protected:
00651 unsigned int _size;
00652 public:
00654 SizeOptions(const char* s);
00656 virtual void help(void);
00658 void parse(int& argc, char* argv[]);
00659
00661 void size(unsigned int s);
00663 unsigned int size(void) const;
00664 };
00665
00670 class GECODE_DRIVER_EXPORT InstanceOptions : public Options {
00671 protected:
00672 const char* _inst;
00673 public:
00675 InstanceOptions(const char* s);
00677 virtual void help(void);
00679 void parse(int& argc, char* argv[]);
00680
00682 void instance(const char* s);
00684 const char* instance(void) const;
00686 ~InstanceOptions(void);
00687 };
00688
00689 }
00690
00691 #include <gecode/driver/options.hpp>
00692
00693 namespace Gecode { namespace Driver {
00694
00702 template<class BaseSpace>
00703 class ScriptBase : public BaseSpace {
00704 public:
00706 ScriptBase(const Options& opt);
00708 ScriptBase(bool share, ScriptBase& e);
00710 virtual void print(std::ostream& os) const;
00712 virtual void compare(const Space& home, std::ostream& os) const;
00714 static std::ostream& select_ostream(const char* sn, std::ofstream& ofs);
00724 template<class Script, template<class> class Engine, class Options>
00725 static void run(const Options& opt, Script* s=NULL);
00726 private:
00727 template<class Script, template<class> class Engine, class Options,
00728 template<class, template<class> class> class Meta>
00729 static void runMeta(const Options& opt, Script* s);
00731 explicit ScriptBase(ScriptBase& e);
00732 };
00733
00734 #ifdef GECODE_HAS_FLOAT_VARS
00735
00737 template<class BaseSpace>
00738 class ExtractStepOption : public BaseSpace {
00739 public:
00741 ExtractStepOption(const Options& opt)
00742 : BaseSpace(opt.step()) {}
00744 ExtractStepOption(bool share, BaseSpace& e)
00745 : BaseSpace(share,e) {}
00746 };
00747
00748 #endif
00749
00751 template<class BaseSpace>
00752 class IgnoreStepOption : public BaseSpace {
00753 public:
00755 IgnoreStepOption(const Options&) {}
00757 IgnoreStepOption(bool share, BaseSpace& e)
00758 : BaseSpace(share,e) {}
00759 };
00760
00761
00762 }}
00763
00764 #include <gecode/driver/script.hpp>
00765
00766 namespace Gecode {
00767
00777 typedef Driver::ScriptBase<Driver::IgnoreStepOption<Space> >
00778 Script;
00783 typedef Driver::ScriptBase<Driver::IgnoreStepOption<MinimizeSpace> >
00784 MinimizeScript;
00789 typedef Driver::ScriptBase<Driver::IgnoreStepOption<MaximizeSpace> >
00790 MaximizeScript;
00795 typedef Driver::ScriptBase<Driver::IgnoreStepOption<IntMinimizeSpace> >
00796 IntMinimizeScript;
00801 typedef Driver::ScriptBase<Driver::IgnoreStepOption<IntMaximizeSpace> >
00802 IntMaximizeScript;
00803
00804 #ifdef GECODE_HAS_FLOAT_VARS
00805
00810 typedef Driver::ScriptBase<Driver::ExtractStepOption<FloatMinimizeSpace> >
00811 FloatMinimizeScript;
00816 typedef Driver::ScriptBase<Driver::ExtractStepOption<FloatMaximizeSpace> >
00817 FloatMaximizeScript;
00818
00819 #endif
00820
00821 }
00822
00823 #endif
00824
00825