Generated on Thu Feb 21 2013 23:11:39 for Gecode by doxygen 1.8.3.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: 2010-10-07 07:44:30 +1100 (Thu, 07 Oct 2010) $ by $Author: schulte $
11  * $Revision: 11465 $
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  BaseOption::BaseOption(const char* o, const char* e)
72  : opt(strdup(o)), exp(strdup(e)) {}
73 
75  strdel(opt);
76  strdel(exp);
77  }
78 
79 
80  bool
81  StringValueOption::parse(int& argc, char* argv[]) {
82  if ((argc < 2) || strcmp(argv[1],opt))
83  return false;
84  if (argc == 2) {
85  std::cerr << "Missing argument for option \"" << opt << "\"" << std::endl;
86  exit(EXIT_FAILURE);
87  }
88  cur = strdup(argv[2]);
89  // Remove options
90  argc -= 2;
91  for (int i=1; i<argc; i++)
92  argv[i] = argv[i+2];
93  return true;
94  }
95 
96  void
98  using namespace std;
99  cerr << '\t' << opt << " (string) default: "
100  << ((cur == NULL) ? "NONE" : cur) << endl
101  << "\t\t" << exp << endl;
102  }
103 
105  strdel(cur);
106  }
107 
108 
109 
110  void
111  StringOption::add(int v, const char* o, const char* h) {
112  Value* n = new Value;
113  n->val = v;
114  n->opt = strdup(o);
115  n->help = strdup(h);
116  n->next = NULL;
117  if (fst == NULL) {
118  fst = n;
119  } else {
120  lst->next = n;
121  }
122  lst = n;
123  }
124 
125  bool
126  StringOption::parse(int& argc, char* argv[]) {
127  if ((argc < 2) || strcmp(argv[1],opt))
128  return false;
129  if (argc == 2) {
130  std::cerr << "Missing argument for option \"" << opt << "\"" << std::endl;
131  exit(EXIT_FAILURE);
132  }
133  for (Value* v = fst; v != NULL; v = v->next)
134  if (!strcmp(argv[2],v->opt)) {
135  cur = v->val;
136  // Remove options
137  argc -= 2;
138  for (int i=1; i<argc; i++)
139  argv[i] = argv[i+2];
140  return true;
141  }
142  std::cerr << "Wrong argument \"" << argv[2]
143  << "\" for option \"" << opt << "\""
144  << std::endl;
145  exit(EXIT_FAILURE);
146  }
147 
148  void
150  if (fst == NULL)
151  return;
152  std::cerr << '\t' << opt << " (";
153  const char* d = NULL;
154  for (Value* v = fst; v != NULL; v = v->next) {
155  std::cerr << v->opt << ((v->next != NULL) ? ", " : "");
156  if (v->val == cur)
157  d = v->opt;
158  }
159  std::cerr << ")";
160  if (d != NULL)
161  std::cerr << " default: " << d;
162  std::cerr << std::endl << "\t\t" << exp << std::endl;
163  for (Value* v = fst; v != NULL; v = v->next)
164  if (v->help != NULL)
165  std::cerr << "\t\t " << v->opt << ": " << v->help << std::endl;
166  }
167 
169  Value* v = fst;
170  while (v != NULL) {
171  strdel(v->opt);
172  strdel(v->help);
173  Value* n = v->next;
174  delete v;
175  v = n;
176  }
177  }
178 
179 
180  bool
181  IntOption::parse(int& argc, char* argv[]) {
182  if ((argc < 2) || strcmp(argv[1],opt))
183  return false;
184  if (argc == 2) {
185  std::cerr << "Missing argument for option \"" << opt << "\"" << std::endl;
186  exit(EXIT_FAILURE);
187  }
188  cur = atoi(argv[2]);
189  // Remove options
190  argc -= 2;
191  for (int i=1; i<argc; i++)
192  argv[i] = argv[i+2];
193  return true;
194  }
195 
196  void
198  using namespace std;
199  cerr << '\t' << opt << " (int) default: " << cur << endl
200  << "\t\t" << exp << endl;
201  }
202 
203 
204  bool
205  UnsignedIntOption::parse(int& argc, char* argv[]) {
206  if ((argc < 2) || strcmp(argv[1],opt))
207  return false;
208  if (argc == 2) {
209  std::cerr << "Missing argument for option \"" << opt << "\"" << std::endl;
210  exit(EXIT_FAILURE);
211  }
212  cur = static_cast<unsigned int>(atoi(argv[2]));
213  // Remove options
214  argc -= 2;
215  for (int i=1; i<argc; i++)
216  argv[i] = argv[i+2];
217  return true;
218  }
219 
220  void
222  using namespace std;
223  cerr << '\t' << opt << " (unsigned int) default: " << cur << endl
224  << "\t\t" << exp << endl;
225  }
226 
227 
228  bool
229  DoubleOption::parse(int& argc, char* argv[]) {
230  if ((argc < 2) || strcmp(argv[1],opt))
231  return false;
232  if (argc == 2) {
233  std::cerr << "Missing argument for option \"" << opt << "\"" << std::endl;
234  exit(EXIT_FAILURE);
235  }
236  cur = atof(argv[2]);
237  // Remove options
238  argc -= 2;
239  for (int i=1; i<argc; i++)
240  argv[i] = argv[i+2];
241  return true;
242  }
243 
244  void
246  using namespace std;
247  cerr << '\t' << opt << " (double) default: " << cur << endl
248  << "\t\t" << exp << endl;
249  }
250 
251  bool
252  BoolOption::parse(int& argc, char* argv[]) {
253  if ((argc < 2) || strcmp(argv[1],opt)) {
254  return false;
255  }
256  // Remove options
257  argc--;
258  for (int i=1; i<argc; i++)
259  argv[i] = argv[i+1];
260  cur = true;
261  return true;
262  }
263 
264  void
266  using namespace std;
267  cerr << '\t' << opt << endl << "\t\t" << exp << endl;
268  }
269 
270 
271  }
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  std::cerr << std::endl
295  << " - Thread support: ";
296 #ifdef GECODE_HAS_THREADS
297  if (Support::Thread::npu() == 1)
298  std::cerr << "enabled (1 processing unit)";
299  else
300  std::cerr << "enabled (" << Support::Thread::npu()
301  << " processing units)";
302 #else
303  std::cerr << "disabled";
304 #endif
305  std::cerr << std::endl
306  << " - Gist support: ";
307 #ifdef GECODE_HAS_GIST
308  std::cerr << "enabled";
309 #else
310  std::cerr << "disabled";
311 #endif
312  std::cerr << std::endl << std::endl
313  << "Options for " << name() << ":" << std::endl
314  << "\t-help, --help, -?" << std::endl
315  << "\t\tprint this help message" << std::endl;
316  for (Driver::BaseOption* o = fst; o != NULL; o = o->next)
317  o->help();
318  }
319 
320  void
321  BaseOptions::parse(int& argc, char* argv[]) {
322  next:
323  for (Driver::BaseOption* o = fst; o != NULL; o = o->next)
324  if (o->parse(argc,argv))
325  goto next;
326  if (argc < 2)
327  return;
328  if (!strcmp(argv[1],"-help") || !strcmp(argv[1],"--help") ||
329  !strcmp(argv[1],"-?")) {
330  help();
331  exit(EXIT_SUCCESS);
332  }
333  return;
334  }
335 
338  }
339 
340 
341  Options::Options(const char* n)
342  : BaseOptions(n),
343 
344  _model("-model","model variants"),
345  _symmetry("-symmetry","symmetry variants"),
346  _propagation("-propagation","propagation variants"),
347  _icl("-icl","integer consistency level",ICL_DEF),
348  _branching("-branching","branching variants"),
349 
350  _search("-search","search engine variants"),
351  _solutions("-solutions","number of solutions (0 = all)",1),
352  _threads("-threads","number of threads (0 = #processing units)",
353  Search::Config::threads),
354  _c_d("-c-d","recomputation commit distance",Search::Config::c_d),
355  _a_d("-a-d","recomputation adaptation distance",Search::Config::a_d),
356  _node("-node","node cutoff (0 = none, solution mode)"),
357  _fail("-fail","failure cutoff (0 = none, solution mode)"),
358  _time("-time","time (in ms) cutoff (0 = none, solution mode)"),
359  _interrupt("-interrupt","whether to catch Ctrl-C (true) or not (false)", true),
360 
361  _mode("-mode","how to execute script",SM_SOLUTION),
362  _samples("-samples","how many samples (time mode)",1),
363  _iterations("-iterations","iterations per sample (time mode)",1)
364  {
365 
366  _icl.add(ICL_DEF, "def"); _icl.add(ICL_VAL, "val");
367  _icl.add(ICL_BND, "bnd"); _icl.add(ICL_DOM, "dom");
368 
369  _mode.add(SM_SOLUTION, "solution");
370  _mode.add(SM_TIME, "time");
371  _mode.add(SM_STAT, "stat");
372  _mode.add(SM_GIST, "gist");
373 
374  _interrupt.add(false, "false");
375  _interrupt.add(true, "true");
376 
378  add(_branching);
382  }
383 
384 
386  : Options(e), _size(0) {}
387 
388  void
390  Options::help();
391  std::cerr << "\t(unsigned int) default: " << size() << std::endl
392  << "\t\twhich version/size for script" << std::endl;
393  }
394 
395  void
396  SizeOptions::parse(int& argc, char* argv[]) {
397  Options::parse(argc,argv);
398  if (argc < 2)
399  return;
400  size(static_cast<unsigned int>(atoi(argv[1])));
401  }
402 
403 
404 
406  : Options(e), _inst(NULL) {}
407 
408  void
409  InstanceOptions::instance(const char* s) {
411  _inst = strdup(s);
412  }
413 
414  void
416  Options::help();
417  std::cerr << "\t(string) default: " << instance() << std::endl
418  << "\t\twhich instance for script" << std::endl;
419  }
420 
421  void
422  InstanceOptions::parse(int& argc, char* argv[]) {
423  Options::parse(argc,argv);
424  if (argc < 2)
425  return;
426  instance(argv[1]);
427  }
428 
431  }
432 
433 }
434 
435 // STATISTICS: driver-any