Generated on Wed Mar 19 07:29:54 2008 for Gecode by doxygen 1.5.5

options.cc

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, 2004
00008  *
00009  *  Last modified:
00010  *     $Date: 2008-02-01 12:10:00 +0100 (Fri, 01 Feb 2008) $ by $Author: schulte $
00011  *     $Revision: 6034 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *
00018  *  Permission is hereby granted, free of charge, to any person obtaining
00019  *  a copy of this software and associated documentation files (the
00020  *  "Software"), to deal in the Software without restriction, including
00021  *  without limitation the rights to use, copy, modify, merge, publish,
00022  *  distribute, sublicense, and/or sell copies of the Software, and to
00023  *  permit persons to whom the Software is furnished to do so, subject to
00024  *  the following conditions:
00025  *
00026  *  The above copyright notice and this permission notice shall be
00027  *  included in all copies or substantial portions of the Software.
00028  *
00029  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00030  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00031  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00032  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00033  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00034  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00035  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00036  *
00037  */
00038 
00039 #include "examples/support.hh"
00040 
00041 #include <iostream>
00042 #include <iomanip>
00043 
00044 #include <cstdlib>
00045 #include <cstring>
00046 
00047 void 
00048 StringOption::add(int v, const char* o, const char* h) {
00049   Value* n = new Value;
00050   n->val  = v;
00051   n->opt  = o;
00052   n->help = h;
00053   n->next = NULL;
00054   if (fst == NULL) {
00055     fst = n;
00056   } else {
00057     lst->next = n; 
00058   }
00059   lst = n;
00060 }
00061 
00062 bool
00063 StringOption::parse(int& argc, char* argv[]) {
00064   if ((argc < 2) || strcmp(argv[1],opt))
00065     return false;
00066   if (argc == 2) {
00067     std::cerr << "Missing argument for option \"" << opt << "\"" << std::endl;
00068     exit(EXIT_FAILURE);
00069   }
00070   for (Value* v = fst; v != NULL; v = v->next)
00071     if (!strcmp(argv[2],v->opt)) {
00072       cur = v->val;
00073       // Remove options
00074       argc -= 2;
00075       for (int i=1; i<argc; i++)
00076         argv[i] = argv[i+2];
00077       return true;
00078     }
00079   std::cerr << "Wrong argument \"" << argv[2] 
00080             << "\" for option \"" << opt << "\"" 
00081             << std::endl;
00082   exit(EXIT_FAILURE);
00083 }
00084 
00085 void 
00086 StringOption::help(void) {
00087   if (fst == NULL)
00088     return;
00089   std::cerr << '\t' << opt << " (";
00090   const char* d = NULL;
00091   for (Value* v = fst; v != NULL; v = v->next) {
00092     std::cerr << v->opt << ((v->next != NULL) ? ", " : "");
00093     if (v->val == cur)
00094       d = v->opt;
00095   }
00096   std::cerr << ")";
00097   if (d != NULL) 
00098     std::cerr << " default: " << d;
00099   std::cerr << std::endl << "\t\t" << exp << std::endl;
00100   for (Value* v = fst; v != NULL; v = v->next)
00101     if (v->help != NULL)
00102       std::cerr << "\t\t  " << v->opt << ": " << v->help << std::endl;
00103 }
00104 
00105 StringOption::~StringOption(void) {
00106   Value* v = fst;
00107   while (v != NULL) {
00108     Value* n = v->next;
00109     delete v;
00110     v = n;
00111   }
00112 }
00113 
00114 
00115 bool
00116 UnsignedIntOption::parse(int& argc, char* argv[]) {
00117   if ((argc < 2) || strcmp(argv[1],opt))
00118     return false;
00119   if (argc == 2) {
00120     std::cerr << "Missing argument for option \"" << opt << "\"" << std::endl;
00121     exit(EXIT_FAILURE);
00122   }
00123   cur = atoi(argv[2]);
00124   // Remove options
00125   argc -= 2;
00126   for (int i=1; i<argc; i++)
00127     argv[i] = argv[i+2];
00128   return true;
00129 }
00130 
00131 void 
00132 UnsignedIntOption::help(void) {
00133   using namespace std;
00134   cerr << '\t' << opt << " (unsigned int) default: " << cur << endl
00135        << "\t\t" << exp << endl;
00136 }
00137 
00138 
00139 
00140 Options::Options(const char* n)
00141   : fst(NULL), lst(NULL),
00142 
00143     _name(n),
00144 
00145     _model("-model","model variants"),
00146     _propagation("-propagation","propagation variants"),
00147     _pk("-pk","propagation kind",PK_DEF),
00148     _icl("-icl","integer consistency level",ICL_DEF),
00149     _branching("-branching","branching variants"),
00150     _sac("-sac", "run singleton arc consistency at root node", SAC_NONE),
00151     
00152     _search("-search","search engine variants"),
00153     _solutions("-solutions","number of solutions (0 = all)",1),
00154     _c_d("-c-d","recompution copy distance",Search::Config::c_d),
00155     _a_d("-a-d","recompution adaption distance",Search::Config::a_d),
00156     _fail("-fail","failure cutoff (0 = none, solution mode)"),
00157     _time("-time","time (in ms) cutoff (0 = none, solution mode)"),
00158 
00159     _mode("-mode","how to execute example",EM_SOLUTION),
00160     _samples("-samples","how many samples (time mode)",1),
00161     _iterations("-iterations","iterations per sample (time mode)",1)
00162 {
00163 
00164   _pk.add(PK_DEF, "def"); _pk.add(PK_SPEED, "speed");
00165   _pk.add(PK_MEMORY, "memory");
00166 
00167   _icl.add(ICL_DEF, "def"); _icl.add(ICL_VAL, "val");
00168   _icl.add(ICL_BND, "bnd"); _icl.add(ICL_DOM, "dom");
00169 
00170   _sac.add(SAC_NONE, "none"); _sac.add(SAC_ONE, "one"); 
00171   _sac.add(SAC_FULL, "full");
00172 
00173   _mode.add(EM_SOLUTION, "solution"); 
00174   _mode.add(EM_TIME, "time");
00175   _mode.add(EM_STAT, "stat");
00176 #ifdef GECODE_HAS_GIST
00177   _mode.add(EM_GIST, "gist");
00178 #endif
00179 
00180   add(_model); add(_propagation); add(_pk); add(_icl); add(_branching);  add(_sac);
00181   add(_search); add(_solutions); add(_c_d); add(_a_d); add(_fail); add(_time);
00182   add(_mode); add(_iterations); add(_samples);
00183 }
00184 
00185 void
00186 Options::help(void) {
00187   std::cerr << "Options for " << name() << ":" << std::endl
00188             << "\t-help, --help, -?" << std::endl
00189             << "\t\tprint this help message" << std::endl;
00190   for (BaseOption* o = fst; o != NULL; o = o->next)
00191     o->help();
00192 }
00193 
00194 void
00195 Options::parse(int& argc, char* argv[]) {
00196  next:
00197   for (BaseOption* o = fst; o != NULL; o = o->next)
00198     if (o->parse(argc,argv))
00199       goto next;
00200   if (argc < 2)
00201     return;
00202   if (!strcmp(argv[1],"-help") || !strcmp(argv[1],"--help") ||
00203       !strcmp(argv[1],"-?")) {
00204     help();
00205     exit(EXIT_SUCCESS);
00206   }
00207   return;
00208 }
00209 
00210 
00211 SizeOptions::SizeOptions(const char* e) 
00212   : Options(e), _size(0) {}
00213 
00214 void
00215 SizeOptions::help(void) {
00216   Options::help();
00217   std::cerr << "\t(unsigned int) default: " << size() << std::endl
00218             << "\t\twhich version/size for example" << std::endl;
00219 }
00220 
00221 void
00222 SizeOptions::parse(int& argc, char* argv[]) {
00223   Options::parse(argc,argv);
00224   if (argc < 2)
00225     return;
00226   size(atoi(argv[1]));
00227 }
00228 
00229 
00230 // STATISTICS: example-any