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
00285 }
00286
00291 class GECODE_DRIVER_EXPORT BaseOptions {
00292 protected:
00293 Driver::BaseOption* fst;
00294 Driver::BaseOption* lst;
00295 const char* _name;
00296 public:
00298 BaseOptions(const char* s);
00300 virtual void help(void);
00301
00303 void add(Driver::BaseOption& o);
00311 void parse(int& argc, char* argv[]);
00312
00314 const char* name(void) const;
00316 void name(const char*);
00317
00319 virtual ~BaseOptions(void);
00320 };
00321
00326 class GECODE_DRIVER_EXPORT Options : public BaseOptions {
00327 protected:
00329
00330 Driver::StringOption _model;
00331 Driver::StringOption _symmetry;
00332 Driver::StringOption _propagation;
00333 Driver::StringOption _icl;
00334 Driver::StringOption _branching;
00335 Driver::DoubleOption _decay;
00336
00337
00339
00340 Driver::StringOption _search;
00341 Driver::UnsignedIntOption _solutions;
00342 Driver::DoubleOption _threads;
00343 Driver::UnsignedIntOption _c_d;
00344 Driver::UnsignedIntOption _a_d;
00345 Driver::UnsignedIntOption _node;
00346 Driver::UnsignedIntOption _fail;
00347 Driver::UnsignedIntOption _time;
00348 Driver::StringOption _restart;
00349 Driver::DoubleOption _r_base;
00350 Driver::UnsignedIntOption _r_scale;
00351 Driver::BoolOption _nogoods;
00352 Driver::UnsignedIntOption _nogoods_limit;
00353 Driver::BoolOption _interrupt;
00354
00355
00357
00358 Driver::StringOption _mode;
00359 Driver::UnsignedIntOption _samples;
00360 Driver::UnsignedIntOption _iterations;
00361 Driver::BoolOption _print_last;
00362 Driver::StringValueOption _out_file;
00363 Driver::StringValueOption _log_file;
00364
00365
00366 public:
00368 Options(const char* s);
00369
00371
00372
00373 void model(int v);
00375 void model(int v, const char* o, const char* h = NULL);
00377 int model(void) const;
00378
00380 void symmetry(int v);
00382 void symmetry(int v, const char* o, const char* h = NULL);
00384 int symmetry(void) const;
00385
00387 void propagation(int v);
00389 void propagation(int v, const char* o, const char* h = NULL);
00391 int propagation(void) const;
00392
00394 void icl(IntConLevel i);
00396 IntConLevel icl(void) const;
00397
00399 void branching(int v);
00401 void branching(int v, const char* o, const char* h = NULL);
00403 int branching(void) const;
00404
00406 void decay(double d);
00408 double decay(void) const;
00410
00412
00413
00414 void search(int v);
00416 void search(int v, const char* o, const char* h = NULL);
00418 int search(void) const;
00419
00421 void solutions(unsigned int n);
00423 unsigned int solutions(void) const;
00424
00426 void threads(double n);
00428 double threads(void) const;
00429
00431 void c_d(unsigned int d);
00433 unsigned int c_d(void) const;
00434
00436 void a_d(unsigned int d);
00438 unsigned int a_d(void) const;
00439
00441 void node(unsigned int n);
00443 unsigned int node(void) const;
00444
00446 void fail(unsigned int n);
00448 unsigned int fail(void) const;
00449
00451 void time(unsigned int t);
00453 unsigned int time(void) const;
00454
00456 void restart(RestartMode r);
00458 RestartMode restart(void) const;
00459
00461 void restart_base(double base);
00463 double restart_base(void) const;
00464
00466 void restart_scale(unsigned int scale);
00468 unsigned int restart_scale(void) const;
00469
00471 void nogoods(bool b);
00473 bool nogoods(void) const;
00474
00476 void nogoods_limit(unsigned int l);
00478 unsigned int nogoods_limit(void) const;
00479
00481 void interrupt(bool b);
00483 bool interrupt(void) const;
00485
00487
00488
00489 void mode(ScriptMode em);
00491 ScriptMode mode(void) const;
00492
00494 void samples(unsigned int s);
00496 unsigned int samples(void) const;
00497
00499 void iterations(unsigned int i);
00501 unsigned int iterations(void) const;
00502
00504 void print_last(bool p);
00506 bool print_last(void) const;
00507
00509 void out_file(const char* f);
00511 const char* out_file(void) const;
00512
00514 void log_file(const char* f);
00516 const char* log_file(void) const;
00518
00519 #ifdef GECODE_HAS_GIST
00520
00521 class _I {
00522 private:
00524 Support::DynamicArray<Gist::Inspector*,Heap> _click;
00526 unsigned int n_click;
00528 Support::DynamicArray<Gist::Inspector*,Heap> _solution;
00530 unsigned int n_solution;
00532 Support::DynamicArray<Gist::Inspector*,Heap> _move;
00534 unsigned int n_move;
00536 Support::DynamicArray<Gist::Comparator*,Heap> _compare;
00538 unsigned int n_compare;
00539 public:
00541 _I(void);
00543 void click(Gist::Inspector* i);
00545 void solution(Gist::Inspector* i);
00547 void move(Gist::Inspector* i);
00549 void compare(Gist::Comparator* i);
00550
00552 Gist::Inspector* click(unsigned int i) const;
00554 Gist::Inspector* solution(unsigned int i) const;
00556 Gist::Inspector* move(unsigned int i) const;
00558 Gist::Comparator* compare(unsigned int i) const;
00559 } inspect;
00560 #endif
00561 };
00562
00567 class GECODE_DRIVER_EXPORT SizeOptions : public Options {
00568 protected:
00569 unsigned int _size;
00570 public:
00572 SizeOptions(const char* s);
00574 virtual void help(void);
00576 void parse(int& argc, char* argv[]);
00577
00579 void size(unsigned int s);
00581 unsigned int size(void) const;
00582 };
00583
00588 class GECODE_DRIVER_EXPORT InstanceOptions : public Options {
00589 protected:
00590 const char* _inst;
00591 public:
00593 InstanceOptions(const char* s);
00595 virtual void help(void);
00597 void parse(int& argc, char* argv[]);
00598
00600 void instance(const char* s);
00602 const char* instance(void) const;
00604 ~InstanceOptions(void);
00605 };
00606
00607 }
00608
00609 #include <gecode/driver/options.hpp>
00610
00611 namespace Gecode {
00612
00613 namespace Driver {
00621 template<class BaseSpace>
00622 class ScriptBase : public BaseSpace {
00623 public:
00625 ScriptBase(void) {}
00627 ScriptBase(bool share, ScriptBase& e)
00628 : BaseSpace(share,e) {}
00630 virtual void print(std::ostream& os) const { (void) os; }
00632 virtual void compare(const Space&, std::ostream& os) const {
00633 (void) os;
00634 }
00636 static std::ostream& select_ostream(const char* name, std::ofstream& ofs);
00646 template<class Script, template<class> class Engine, class Options>
00647 static void run(const Options& opt, Script* s=NULL);
00648 private:
00649 template<class Script, template<class> class Engine, class Options,
00650 template<template<class> class,class> class Meta>
00651 static void runMeta(const Options& opt, Script* s);
00653 explicit ScriptBase(ScriptBase& e);
00654 };
00655 }
00656
00666 typedef Driver::ScriptBase<Space> Script;
00671 typedef Driver::ScriptBase<MinimizeSpace> MinimizeScript;
00676 typedef Driver::ScriptBase<MaximizeSpace> MaximizeScript;
00681 typedef Driver::ScriptBase<IntMinimizeSpace> IntMinimizeScript;
00686 typedef Driver::ScriptBase<IntMaximizeSpace> IntMaximizeScript;
00687
00688 #ifdef GECODE_HAS_FLOAT_VARS
00689
00694 typedef Driver::ScriptBase<FloatMinimizeSpace> FloatMinimizeScript;
00699 typedef Driver::ScriptBase<FloatMaximizeSpace> FloatMaximizeScript;
00700
00701 #endif
00702
00703 }
00704
00705 #include <gecode/driver/script.hpp>
00706
00707 #endif
00708
00709