driver.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 * 00006 * Copyright: 00007 * Christian Schulte, 2009 00008 * 00009 * Last modified: 00010 * $Date: 2010-10-06 22:44:30 +0200 (Wed, 06 Oct 2010) $ by $Author: schulte $ 00011 * $Revision: 11465 $ 00012 * 00013 * This file is part of Gecode, the generic constraint 00014 * development environment: 00015 * http://www.gecode.org 00016 * 00017 * Permission is hereby granted, free of charge, to any person obtaining 00018 * a copy of this software and associated documentation files (the 00019 * "Software"), to deal in the Software without restriction, including 00020 * without limitation the rights to use, copy, modify, merge, publish, 00021 * distribute, sublicense, and/or sell copies of the Software, and to 00022 * permit persons to whom the Software is furnished to do so, subject to 00023 * the following conditions: 00024 * 00025 * The above copyright notice and this permission notice shall be 00026 * included in all copies or substantial portions of the Software. 00027 * 00028 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00029 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00030 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00031 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00032 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00033 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00034 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 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 * Configure linking 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 // Configure auto-linking 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 00105 class BaseOptions; 00106 00107 namespace Driver { 00112 class GECODE_DRIVER_EXPORT BaseOption { 00113 friend class Gecode::BaseOptions; 00114 protected: 00115 const char* opt; 00116 const char* exp; 00117 BaseOption* next; 00118 public: 00120 BaseOption(const char* o, const char* e); 00122 virtual bool parse(int& argc, char* argv[]) = 0; 00124 virtual void help(void) = 0; 00126 virtual ~BaseOption(void); 00128 static char* strdup(const char* s); 00130 static void strdel(const char* s); 00131 }; 00132 00137 class GECODE_DRIVER_EXPORT StringValueOption : public BaseOption { 00138 protected: 00139 const char* cur; 00140 public: 00142 StringValueOption(const char* o, const char* e, const char* v=NULL); 00144 void value(const char* v); 00146 const char* value(void) const; 00148 virtual bool parse(int& argc, char* argv[]); 00150 virtual void help(void); 00152 virtual ~StringValueOption(void); 00153 }; 00154 00155 00160 class GECODE_DRIVER_EXPORT StringOption : public BaseOption { 00161 protected: 00163 class Value { 00164 public: 00165 int val; 00166 const char* opt; 00167 const char* help; 00168 Value* next; 00169 }; 00170 int cur; 00171 Value* fst; 00172 Value* lst; 00173 public: 00175 StringOption(const char* o, const char* e, int v=0); 00177 void value(int v); 00179 int value(void) const; 00181 void add(int v, const char* o, const char* h = NULL); 00183 virtual bool parse(int& argc, char* argv[]); 00185 virtual void help(void); 00187 virtual ~StringOption(void); 00188 }; 00189 00190 00195 class GECODE_DRIVER_EXPORT IntOption : public BaseOption { 00196 protected: 00197 int cur; 00198 public: 00200 IntOption(const char* o, const char* e, int v=0); 00202 void value(int v); 00204 int value(void) const; 00206 virtual bool parse(int& argc, char* argv[]); 00208 virtual void help(void); 00209 }; 00210 00215 class GECODE_DRIVER_EXPORT UnsignedIntOption : public BaseOption { 00216 protected: 00217 unsigned int cur; 00218 public: 00220 UnsignedIntOption(const char* o, const char* e, unsigned int v=0); 00222 void value(unsigned int v); 00224 unsigned int value(void) const; 00226 virtual bool parse(int& argc, char* argv[]); 00228 virtual void help(void); 00229 }; 00230 00235 class GECODE_DRIVER_EXPORT DoubleOption : public BaseOption { 00236 protected: 00237 double cur; 00238 public: 00240 DoubleOption(const char* o, const char* e, unsigned int v=0); 00242 void value(double v); 00244 double value(void) const; 00246 virtual bool parse(int& argc, char* argv[]); 00248 virtual void help(void); 00249 }; 00250 00255 class GECODE_DRIVER_EXPORT BoolOption : public BaseOption { 00256 protected: 00257 bool cur; 00258 public: 00260 BoolOption(const char* o, const char* e); 00262 void value(bool v); 00264 bool value(void) const; 00266 virtual bool parse(int& argc, char* argv[]); 00268 virtual void help(void); 00269 }; 00270 00271 } 00272 00277 class GECODE_DRIVER_EXPORT BaseOptions { 00278 protected: 00279 Driver::BaseOption* fst; 00280 Driver::BaseOption* lst; 00281 const char* _name; 00282 public: 00284 BaseOptions(const char* s); 00286 virtual void help(void); 00287 00289 void add(Driver::BaseOption& o); 00291 void parse(int& argc, char* argv[]); 00292 00294 const char* name(void) const; 00296 void name(const char*); 00297 00299 virtual ~BaseOptions(void); 00300 }; 00301 00306 class GECODE_DRIVER_EXPORT Options : public BaseOptions { 00307 protected: 00309 00310 Driver::StringOption _model; 00311 Driver::StringOption _symmetry; 00312 Driver::StringOption _propagation; 00313 Driver::StringOption _icl; 00314 Driver::StringOption _branching; 00315 00316 00318 00319 Driver::StringOption _search; 00320 Driver::UnsignedIntOption _solutions; 00321 Driver::DoubleOption _threads; 00322 Driver::UnsignedIntOption _c_d; 00323 Driver::UnsignedIntOption _a_d; 00324 Driver::UnsignedIntOption _node; 00325 Driver::UnsignedIntOption _fail; 00326 Driver::UnsignedIntOption _time; 00327 Driver::StringOption _interrupt; 00328 00329 00331 00332 Driver::StringOption _mode; 00333 Driver::UnsignedIntOption _samples; 00334 Driver::UnsignedIntOption _iterations; 00335 00336 00337 public: 00339 Options(const char* s); 00340 00342 00343 00344 void model(int v); 00346 void model(int v, const char* o, const char* h = NULL); 00348 int model(void) const; 00349 00351 void symmetry(int v); 00353 void symmetry(int v, const char* o, const char* h = NULL); 00355 int symmetry(void) const; 00356 00358 void propagation(int v); 00360 void propagation(int v, const char* o, const char* h = NULL); 00362 int propagation(void) const; 00363 00365 void icl(IntConLevel i); 00367 IntConLevel icl(void) const; 00368 00370 void branching(int v); 00372 void branching(int v, const char* o, const char* h = NULL); 00374 int branching(void) const; 00376 00378 00379 00380 void search(int v); 00382 void search(int v, const char* o, const char* h = NULL); 00384 int search(void) const; 00385 00387 void solutions(unsigned int n); 00389 unsigned int solutions(void) const; 00390 00392 void threads(double n); 00394 double threads(void) const; 00395 00397 void c_d(unsigned int d); 00399 unsigned int c_d(void) const; 00400 00402 void a_d(unsigned int d); 00404 unsigned int a_d(void) const; 00405 00407 void node(unsigned int n); 00409 unsigned int node(void) const; 00410 00412 void fail(unsigned int n); 00414 unsigned int fail(void) const; 00415 00417 void time(unsigned int t); 00419 unsigned int time(void) const; 00420 00422 void interrupt(bool b); 00424 bool interrupt(void) const; 00426 00428 00429 00430 void mode(ScriptMode em); 00432 ScriptMode mode(void) const; 00433 00435 void iterations(unsigned int i); 00437 unsigned int iterations(void) const; 00438 00440 void samples(unsigned int s); 00442 unsigned int samples(void) const; 00444 00445 #ifdef GECODE_HAS_GIST 00446 00447 class _I { 00448 private: 00450 Support::DynamicArray<Gist::Inspector*,Heap> _click; 00452 unsigned int n_click; 00454 Support::DynamicArray<Gist::Inspector*,Heap> _solution; 00456 unsigned int n_solution; 00458 Support::DynamicArray<Gist::Inspector*,Heap> _move; 00460 unsigned int n_move; 00462 Support::DynamicArray<Gist::Comparator*,Heap> _compare; 00464 unsigned int n_compare; 00465 public: 00467 _I(void); 00469 void click(Gist::Inspector* i); 00471 void solution(Gist::Inspector* i); 00473 void move(Gist::Inspector* i); 00475 void compare(Gist::Comparator* i); 00476 00478 Gist::Inspector* click(unsigned int i) const; 00480 Gist::Inspector* solution(unsigned int i) const; 00482 Gist::Inspector* move(unsigned int i) const; 00484 Gist::Comparator* compare(unsigned int i) const; 00485 } inspect; 00486 #endif 00487 }; 00488 00493 class GECODE_DRIVER_EXPORT SizeOptions : public Options { 00494 protected: 00495 unsigned int _size; 00496 public: 00498 SizeOptions(const char* s); 00500 virtual void help(void); 00502 void parse(int& argc, char* argv[]); 00503 00505 void size(unsigned int s); 00507 unsigned int size(void) const; 00508 }; 00509 00514 class GECODE_DRIVER_EXPORT InstanceOptions : public Options { 00515 protected: 00516 const char* _inst; 00517 public: 00519 InstanceOptions(const char* s); 00521 virtual void help(void); 00523 void parse(int& argc, char* argv[]); 00524 00526 void instance(const char* s); 00528 const char* instance(void) const; 00530 ~InstanceOptions(void); 00531 }; 00532 00533 } 00534 00535 #include <gecode/driver/options.hpp> 00536 00537 namespace Gecode { 00538 00539 namespace Driver { 00547 template<class BaseSpace> 00548 class ScriptBase : public BaseSpace { 00549 public: 00551 ScriptBase(void) {} 00553 ScriptBase(bool share, ScriptBase& e) : BaseSpace(share,e) {} 00555 virtual void print(std::ostream& os) const { (void) os; } 00557 virtual void compare(const Space&, std::ostream& os) const { 00558 (void) os; 00559 } 00567 template<class Script, template<class> class Engine, class Options> 00568 static void run(const Options& opt); 00569 private: 00571 explicit ScriptBase(ScriptBase& e); 00572 }; 00573 } 00574 00584 typedef Driver::ScriptBase<Space> Script; 00589 typedef Driver::ScriptBase<MinimizeSpace> MinimizeScript; 00594 typedef Driver::ScriptBase<MaximizeSpace> MaximizeScript; 00595 00596 } 00597 00598 #include <gecode/driver/script.hpp> 00599 00600 #endif 00601 00602 // STATISTICS: driver-any