Generated on Sat Feb 7 2015 02:01:13 for Gecode by doxygen 1.8.9.1
options.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2004
8  *
9  * Last modified:
10  * $Date: 2013-10-15 00:43:28 +0200 (Tue, 15 Oct 2013) $ by $Author: tack $
11  * $Revision: 14024 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  *
18  * Permission is hereby granted, free of charge, to any person obtaining
19  * a copy of this software and associated documentation files (the
20  * "Software"), to deal in the Software without restriction, including
21  * without limitation the rights to use, copy, modify, merge, publish,
22  * distribute, sublicense, and/or sell copies of the Software, and to
23  * permit persons to whom the Software is furnished to do so, subject to
24  * the following conditions:
25  *
26  * The above copyright notice and this permission notice shall be
27  * included in all copies or substantial portions of the Software.
28  *
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36  *
37  */
38 
39 #include <gecode/driver.hh>
40 
41 #include <iostream>
42 #include <iomanip>
43 
44 #include <cstdlib>
45 #include <cstring>
46 
47 namespace Gecode {
48 
49  namespace Driver {
50 
51  /*
52  * Option baseclass
53  *
54  */
55  char*
56  BaseOption::strdup(const char* s) {
57  if (s == NULL)
58  return NULL;
59  char* d = heap.alloc<char>(static_cast<unsigned long int>(strlen(s)+1));
60  (void) strcpy(d,s);
61  return d;
62  }
63 
64  void
65  BaseOption::strdel(const char* s) {
66  if (s == NULL)
67  return;
68  heap.rfree(const_cast<char*>(s));
69  }
70 
71  char*
72  BaseOption::argument(int argc, char* argv[]) const {
73  if ((argc < 2) || strcmp(argv[1],opt))
74  return NULL;
75  if (argc == 2) {
76  std::cerr << "Missing argument for option \"" << opt << "\""
77  << std::endl;
78  exit(EXIT_FAILURE);
79  }
80  return argv[2];
81  }
82 
83  BaseOption::BaseOption(const char* o, const char* e)
84  : opt(strdup(o)), exp(strdup(e)) {}
85 
87  strdel(opt);
88  strdel(exp);
89  }
90 
91 
92  StringValueOption::StringValueOption(const char* o, const char* e,
93  const char* v)
94  : BaseOption(o,e), cur(strdup(v)) {}
95  void
96  StringValueOption::value(const char* v) {
97  strdel(cur);
98  cur = strdup(v);
99  }
100  int
101  StringValueOption::parse(int argc, char* argv[]) {
102  if (char* a = argument(argc,argv)) {
103  cur = strdup(a);
104  return 2;
105  }
106  return 0;
107  }
108  void
110  std::cerr << '\t' << opt << " (string) default: "
111  << ((cur == NULL) ? "NONE" : cur) << std::endl
112  << "\t\t" << exp << std::endl;
113  }
115  strdel(cur);
116  }
117 
118 
119 
120  void
121  StringOption::add(int v, const char* o, const char* h) {
122  Value* n = new Value;
123  n->val = v;
124  n->opt = strdup(o);
125  n->help = strdup(h);
126  n->next = NULL;
127  if (fst == NULL) {
128  fst = n;
129  } else {
130  lst->next = n;
131  }
132  lst = n;
133  }
134  int
135  StringOption::parse(int argc, char* argv[]) {
136  if (char* a = argument(argc,argv)) {
137  for (Value* v = fst; v != NULL; v = v->next)
138  if (!strcmp(a,v->opt)) {
139  cur = v->val;
140  return 2;
141  }
142  std::cerr << "Wrong argument \"" << a
143  << "\" for option \"" << opt << "\""
144  << std::endl;
145  exit(EXIT_FAILURE);
146  }
147  return 0;
148  }
149  void
151  if (fst == NULL)
152  return;
153  std::cerr << '\t' << opt << " (";
154  const char* d = NULL;
155  for (Value* v = fst; v != NULL; v = v->next) {
156  std::cerr << v->opt << ((v->next != NULL) ? ", " : "");
157  if (v->val == cur)
158  d = v->opt;
159  }
160  std::cerr << ")";
161  if (d != NULL)
162  std::cerr << " default: " << d;
163  std::cerr << std::endl << "\t\t" << exp << std::endl;
164  for (Value* v = fst; v != NULL; v = v->next)
165  if (v->help != NULL)
166  std::cerr << "\t\t " << v->opt << ": " << v->help << std::endl;
167  }
168 
170  Value* v = fst;
171  while (v != NULL) {
172  strdel(v->opt);
173  strdel(v->help);
174  Value* n = v->next;
175  delete v;
176  v = n;
177  }
178  }
179 
180 
181  int
182  IntOption::parse(int argc, char* argv[]) {
183  if (char* a = argument(argc,argv)) {
184  cur = atoi(a);
185  return 2;
186  }
187  return 0;
188  }
189 
190  void
192  std::cerr << '\t' << opt << " (int) default: " << cur << std::endl
193  << "\t\t" << exp << std::endl;
194  }
195 
196 
197  int
198  UnsignedIntOption::parse(int argc, char* argv[]) {
199  if (char* a = argument(argc,argv)) {
200  cur = static_cast<unsigned int>(atoi(a));
201  return 2;
202  }
203  return 0;
204  }
205 
206  void
208  std::cerr << '\t' << opt << " (unsigned int) default: "
209  << cur << std::endl
210  << "\t\t" << exp << std::endl;
211  }
212 
213 
214  int
215  DoubleOption::parse(int argc, char* argv[]) {
216  if (char* a = argument(argc,argv)) {
217  cur = atof(a);
218  return 2;
219  }
220  return 0;
221  }
222 
223  void
225  using namespace std;
226  cerr << '\t' << opt << " (double) default: " << cur << endl
227  << "\t\t" << exp << endl;
228  }
229 
230  int
231  BoolOption::parse(int argc, char* argv[]) {
232  if ((argc < 2) || strcmp(argv[1],opt))
233  return 0;
234  if (argc == 2) {
235  // Option without argument
236  cur = true;
237  return 1;
238  } else if (!strcmp(argv[2],"true") || !strcmp(argv[2],"1")) {
239  cur = true;
240  return 2;
241  } else if (!strcmp(argv[2],"false") || !strcmp(argv[2],"0")) {
242  cur = false;
243  return 2;
244  } else {
245  // Option without argument
246  cur = true;
247  return 1;
248  }
249  return 0;
250  }
251 
252  void
254  using namespace std;
255  cerr << '\t' << opt << " (optional: false, 0, true, 1) default: "
256  << (cur ? "true" : "false") << endl
257  << "\t\t" << exp << endl;
258  }
259 
260 
261  }
262 
263  void
265  o.next = NULL;
266  if (fst == NULL) {
267  fst=&o;
268  } else {
269  lst->next=&o;
270  }
271  lst=&o;
272  }
274  : fst(NULL), lst(NULL),
275  _name(Driver::BaseOption::strdup(n)) {}
276 
277  void
278  BaseOptions::name(const char* n) {
281  }
282 
283  void
285  std::cerr << "Gecode configuration information:" << std::endl
286  << " - Version: " << GECODE_VERSION << std::endl
287  << " - Variable types: ";
288 #ifdef GECODE_HAS_INT_VARS
289  std::cerr << "BoolVar IntVar ";
290 #endif
291 #ifdef GECODE_HAS_SET_VARS
292  std::cerr << "SetVar ";
293 #endif
294 #ifdef GECODE_HAS_FLOAT_VARS
295  std::cerr << "FloatVar "
296  << std::endl
297  << " - Trigonometric and transcendental float constraints: ";
298 #ifdef GECODE_HAS_MPFR
299  std::cerr << "enabled";
300 #else
301  std::cerr << "disabled";
302 #endif
303 #endif
304  std::cerr << std::endl;
305  std::cerr << " - Thread support: ";
306 #ifdef GECODE_HAS_THREADS
307  if (Support::Thread::npu() == 1)
308  std::cerr << "enabled (1 processing unit)";
309  else
310  std::cerr << "enabled (" << Support::Thread::npu()
311  << " processing units)";
312 #else
313  std::cerr << "disabled";
314 #endif
315  std::cerr << std::endl
316  << " - Gist support: ";
317 #ifdef GECODE_HAS_GIST
318  std::cerr << "enabled";
319 #else
320  std::cerr << "disabled";
321 #endif
322  std::cerr << std::endl << std::endl
323  << "Options for " << name() << ":" << std::endl
324  << "\t-help, --help, -?" << std::endl
325  << "\t\tprint this help message" << std::endl;
326  for (Driver::BaseOption* o = fst; o != NULL; o = o->next)
327  o->help();
328  }
329 
330  void
331  BaseOptions::parse(int& argc, char* argv[]) {
332  int c = argc;
333  char** v = argv;
334  next:
335  for (Driver::BaseOption* o = fst; o != NULL; o = o->next)
336  if (int a = o->parse(c,v)) {
337  c -= a; v += a;
338  goto next;
339  }
340  if (c >= 2) {
341  if (!strcmp(v[1],"-help") || !strcmp(v[1],"--help") ||
342  !strcmp(v[1],"-?")) {
343  help();
344  exit(EXIT_SUCCESS);
345  }
346  }
347  // Copy remaining arguments
348  argc = c;
349  for (int i=1; i<argc; i++)
350  argv[i] = v[i];
351  return;
352  }
353 
356  }
357 
358 
359  Options::Options(const char* n)
360  : BaseOptions(n),
361 
362  _model("-model","model variants"),
363  _symmetry("-symmetry","symmetry variants"),
364  _propagation("-propagation","propagation variants"),
365  _icl("-icl","integer consistency level",ICL_DEF),
366  _branching("-branching","branching variants"),
367  _decay("-decay","decay factor",1.0),
368 
369  _search("-search","search engine variants"),
370  _solutions("-solutions","number of solutions (0 = all)",1),
371  _threads("-threads","number of threads (0 = #processing units)",
372  Search::Config::threads),
373  _c_d("-c-d","recomputation commit distance",Search::Config::c_d),
374  _a_d("-a-d","recomputation adaptation distance",Search::Config::a_d),
375  _node("-node","node cutoff (0 = none, solution mode)"),
376  _fail("-fail","failure cutoff (0 = none, solution mode)"),
377  _time("-time","time (in ms) cutoff (0 = none, solution mode)"),
378  _restart("-restart","restart sequence type",RM_NONE),
379  _r_base("-restart-base","base for geometric restart sequence",1.5),
380  _r_scale("-restart-scale","scale factor for restart sequence",250),
381  _nogoods("-nogoods","whether to use no-goods from restarts",false),
382  _nogoods_limit("-nogoods-limit","depth limit for no-good extraction",
383  Search::Config::nogoods_limit),
384  _interrupt("-interrupt","whether to catch Ctrl-C (true) or not (false)",
385  true),
386 
387  _mode("-mode","how to execute script",SM_SOLUTION),
388  _samples("-samples","how many samples (time mode)",1),
389  _iterations("-iterations","iterations per sample (time mode)",1),
390  _print_last("-print-last",
391  "whether to only print the last solution (solution mode)",
392  false),
393  _out_file("-file-sol", "where to print solutions "
394  "(supports stdout, stdlog, stderr)","stdout"),
395  _log_file("-file-stat", "where to print statistics "
396  "(supports stdout, stdlog, stderr)","stdout")
397  {
398 
399  _icl.add(ICL_DEF, "def"); _icl.add(ICL_VAL, "val");
400  _icl.add(ICL_BND, "bnd"); _icl.add(ICL_DOM, "dom");
401 
402  _mode.add(SM_SOLUTION, "solution");
403  _mode.add(SM_TIME, "time");
404  _mode.add(SM_STAT, "stat");
405  _mode.add(SM_GIST, "gist");
406 
407  _restart.add(RM_NONE,"none");
408  _restart.add(RM_CONSTANT,"constant");
409  _restart.add(RM_LINEAR,"linear");
410  _restart.add(RM_LUBY,"luby");
411  _restart.add(RM_GEOMETRIC,"geometric");
412 
421  }
422 
423 
425  : Options(e), _size(0) {}
426 
427  void
429  Options::help();
430  std::cerr << "\t(unsigned int) default: " << size() << std::endl
431  << "\t\twhich version/size for script" << std::endl;
432  }
433 
434  void
435  SizeOptions::parse(int& argc, char* argv[]) {
436  Options::parse(argc,argv);
437  if (argc < 2)
438  return;
439  size(static_cast<unsigned int>(atoi(argv[1])));
440  }
441 
442 
443 
445  : Options(e), _inst(NULL) {}
446 
447  void
448  InstanceOptions::instance(const char* s) {
451  }
452 
453  void
455  Options::help();
456  std::cerr << "\t(string) default: " << instance() << std::endl
457  << "\t\twhich instance for script" << std::endl;
458  }
459 
460  void
461  InstanceOptions::parse(int& argc, char* argv[]) {
462  Options::parse(argc,argv);
463  if (argc < 2)
464  return;
465  instance(argv[1]);
466  }
467 
470  }
471 
472 }
473 
474 // STATISTICS: driver-any
Driver::UnsignedIntOption _c_d
Copy recomputation distance.
Definition: driver.hh:343
Restart with linear sequence.
Definition: driver.hh:112
Driver::BoolOption _interrupt
Whether to catch SIGINT.
Definition: driver.hh:353
virtual ~StringValueOption(void)
Destructor.
Definition: options.cpp:114
int cur
Current value.
Definition: driver.hh:184
Options(const char *s)
Initialize options for script with name s.
Definition: options.cpp:359
Driver::UnsignedIntOption _iterations
How many iterations per sample.
Definition: driver.hh:360
StringValueOption(const char *o, const char *e, const char *v=NULL)
Initialize for option o and explanation e and default value v.
Definition: options.cpp:92
static unsigned int npu(void)
Return number of processing units (1 if information not available)
Definition: none.hpp:80
Driver::DoubleOption _decay
Decay option.
Definition: driver.hh:335
unsigned int cur
Current value.
Definition: driver.hh:231
virtual void help(void)
Print help text.
Definition: options.cpp:428
virtual void help(void)
Print help text.
Definition: options.cpp:150
virtual int parse(int argc, char *argv[])
Parse option at first position and return number of parsed arguments.
Definition: options.cpp:198
const char * exp
Short explanation.
Definition: driver.hh:128
void rfree(void *p)
Free memory block starting at p.
Definition: heap.hpp:355
Value * next
Next option value.
Definition: driver.hh:182
const char * opt
String for option value.
Definition: driver.hh:180
virtual void help(void)
Print help text.
Definition: options.cpp:253
Driver::DoubleOption _threads
How many threads to use.
Definition: driver.hh:342
Value propagation or consistency (naive)
Definition: int.hh:938
Driver::UnsignedIntOption _nogoods_limit
Limit for no-good extraction.
Definition: driver.hh:352
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:435
Driver::StringOption _restart
Restart method option.
Definition: driver.hh:348
Driver::BoolOption _nogoods
Whether to use no-goods.
Definition: driver.hh:351
void add(int v, const char *o, const char *h=NULL)
Add option value for value v, string o, and help text h.
Definition: options.cpp:121
Value * lst
Last option value.
Definition: driver.hh:186
Base class for options.
Definition: driver.hh:124
Restart with Luby sequence.
Definition: driver.hh:113
static void strdel(const char *s)
Delete heap-allocated copy of string s.
Definition: options.cpp:65
No restarts.
Definition: driver.hh:110
Driver::DoubleOption _r_base
Restart base.
Definition: driver.hh:349
void add(Driver::BaseOption &o)
Add new option o.
Definition: options.cpp:264
Heap heap
The single global heap.
Definition: heap.cpp:49
Gecode::IntSet d(v, 7)
Driver::StringOption _model
General model options.
Definition: driver.hh:330
Gecode::FloatVal c(-8, 8)
T * alloc(long unsigned int n)
Allocate block of n objects of type T from heap.
Definition: heap.hpp:400
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Options opt
The options.
Definition: test.cpp:101
Driver::UnsignedIntOption _fail
Cutoff for number of failures.
Definition: driver.hh:346
Print solution and some statistics.
Definition: driver.hh:99
const char * help
Optional help text.
Definition: driver.hh:181
Driver::UnsignedIntOption _samples
How many samples.
Definition: driver.hh:359
#define GECODE_VERSION
Definition: config.hpp:126
Driver::StringValueOption _log_file
Where to print statistics.
Definition: driver.hh:363
const unsigned int a_d
Create a clone during recomputation if distance is greater than a_d (adaptive distance) ...
Definition: search.hh:99
Value * fst
First option value.
Definition: driver.hh:185
virtual ~BaseOption(void)
Destructor.
Definition: options.cpp:86
double cur
Current value.
Definition: driver.hh:251
Driver::StringOption _propagation
Propagation options.
Definition: driver.hh:332
Base class for script options.
Definition: driver.hh:291
const char * instance(void) const
Return instance name.
Definition: options.hpp:480
const char * _name
Script name.
Definition: driver.hh:295
Measure average runtime.
Definition: driver.hh:100
Driver::StringOption _search
Search options.
Definition: driver.hh:340
Driver::BoolOption _print_last
Print only last solution found.
Definition: driver.hh:361
BaseOption * next
Next option Check for option and return its argument.
Definition: driver.hh:129
Driver::StringOption _symmetry
General symmetry options.
Definition: driver.hh:331
Driver::UnsignedIntOption _a_d
Adaptive recomputation distance.
Definition: driver.hh:344
Driver::UnsignedIntOption _time
Cutoff for time.
Definition: driver.hh:347
Driver::StringOption _mode
Script mode to run.
Definition: driver.hh:358
const double threads
Number of threads to use.
Definition: search.hh:95
int val
Value for an option value.
Definition: driver.hh:179
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:331
const int v[7]
Definition: distinct.cpp:207
virtual ~StringOption(void)
Destructor.
Definition: options.cpp:169
virtual int parse(int argc, char *argv[])
Parse option at first position and return number of parsed arguments.
Definition: options.cpp:231
Print statistics for script.
Definition: driver.hh:101
virtual void help(void)
Print help text.
Definition: options.cpp:191
Restart with geometric sequence.
Definition: driver.hh:114
const char * opt
String for option (including hyphen)
Definition: driver.hh:127
static char * strdup(const char *s)
Create heap-allocated copy of string s.
Definition: options.cpp:56
Driver::StringValueOption _out_file
Where to print solutions.
Definition: driver.hh:362
Driver::BaseOption * fst
First registered option.
Definition: driver.hh:293
virtual int parse(int argc, char *argv[])
Parse option at first position and return number of parsed arguments.
Definition: options.cpp:215
virtual int parse(int argc, char *argv[])
Parse option at first position and return number of parsed arguments.
Definition: options.cpp:135
InstanceOptions(const char *s)
Initialize options for script with name s.
Definition: options.cpp:444
const char * _inst
Instance string.
Definition: driver.hh:590
const char * value(void) const
Return current option value.
Definition: options.hpp:50
The default consistency for a constraint.
Definition: int.hh:941
virtual void help(void)
Print help text.
Definition: options.cpp:109
Driver::UnsignedIntOption _solutions
How many solutions.
Definition: driver.hh:341
virtual void help(void)
Print help text.
Definition: options.cpp:207
Driver::StringOption _branching
Branching options.
Definition: driver.hh:334
Run script in Gist.
Definition: driver.hh:102
const char * cur
Current value.
Definition: driver.hh:153
const unsigned int c_d
Create a clone after every c_d commits (commit distance)
Definition: search.hh:97
SizeOptions(const char *s)
Initialize options for script with name s.
Definition: options.cpp:424
unsigned int size(void) const
Return size.
Definition: options.hpp:471
~InstanceOptions(void)
Destructor.
Definition: options.cpp:468
BaseOption(const char *o, const char *e)
Initialize for option o and explanation e.
Definition: options.cpp:83
virtual void help(void)
Print help text.
Definition: options.cpp:224
Bounds propagation or consistency.
Definition: int.hh:939
Driver::StringOption _icl
Integer consistency level.
Definition: driver.hh:333
int cur
Current value.
Definition: driver.hh:211
Gecode toplevel namespace
virtual int parse(int argc, char *argv[])
Parse option at first position and return number of parsed arguments.
Definition: options.cpp:101
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:461
const unsigned int nogoods_limit
Depth limit for no-good generation during search.
Definition: search.hh:107
virtual ~BaseOptions(void)
Destructor.
Definition: options.cpp:354
void exp(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:144
BaseOptions(const char *s)
Initialize options for script with name s.
Definition: options.cpp:273
char * argument(int argc, char *argv[]) const
Definition: options.cpp:72
const char * name(void) const
Return name of script.
Definition: options.hpp:144
Options for scripts
Definition: driver.hh:326
Restart with constant sequence.
Definition: driver.hh:111
Driver::UnsignedIntOption _r_scale
Restart scale factor.
Definition: driver.hh:350
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.
Domain propagation or consistency.
Definition: int.hh:940
virtual int parse(int argc, char *argv[])
Parse option at first position and return number of parsed arguments.
Definition: options.cpp:182
Driver::UnsignedIntOption _node
Cutoff for number of nodes.
Definition: driver.hh:345
virtual void help(void)
Print help text.
Definition: options.cpp:454
virtual void help(void)
Print help text.
Definition: options.cpp:284