Generated on Mon Jul 6 18:09:02 2009 for Gecode by doxygen 1.5.9

options.cpp

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: 2009-05-14 04:16:05 +0200 (Thu, 14 May 2009) $ by $Author: tack $
00011  *     $Revision: 9102 $
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 <gecode/driver.hh>
00040 
00041 #include <gecode/support/thread.hh>
00042 
00043 #include <iostream>
00044 #include <iomanip>
00045 
00046 #include <cstdlib>
00047 #include <cstring>
00048 
00049 namespace Gecode {
00050 
00051   namespace Driver {
00052 
00053     void
00054     StringOption::add(int v, const char* o, const char* h) {
00055       Value* n = new Value;
00056       n->val  = v;
00057       n->opt  = o;
00058       n->help = h;
00059       n->next = NULL;
00060       if (fst == NULL) {
00061         fst = n;
00062       } else {
00063         lst->next = n;
00064       }
00065       lst = n;
00066     }
00067     
00068     bool
00069     StringOption::parse(int& argc, char* argv[]) {
00070       if ((argc < 2) || strcmp(argv[1],opt))
00071         return false;
00072       if (argc == 2) {
00073         std::cerr << "Missing argument for option \"" << opt << "\"" << std::endl;
00074         exit(EXIT_FAILURE);
00075       }
00076       for (Value* v = fst; v != NULL; v = v->next)
00077         if (!strcmp(argv[2],v->opt)) {
00078           cur = v->val;
00079           // Remove options
00080           argc -= 2;
00081           for (int i=1; i<argc; i++)
00082             argv[i] = argv[i+2];
00083           return true;
00084         }
00085       std::cerr << "Wrong argument \"" << argv[2]
00086                 << "\" for option \"" << opt << "\""
00087                 << std::endl;
00088       exit(EXIT_FAILURE);
00089     }
00090     
00091     void
00092     StringOption::help(void) {
00093       if (fst == NULL)
00094         return;
00095       std::cerr << '\t' << opt << " (";
00096       const char* d = NULL;
00097       for (Value* v = fst; v != NULL; v = v->next) {
00098         std::cerr << v->opt << ((v->next != NULL) ? ", " : "");
00099         if (v->val == cur)
00100           d = v->opt;
00101       }
00102       std::cerr << ")";
00103       if (d != NULL)
00104         std::cerr << " default: " << d;
00105       std::cerr << std::endl << "\t\t" << exp << std::endl;
00106       for (Value* v = fst; v != NULL; v = v->next)
00107         if (v->help != NULL)
00108           std::cerr << "\t\t  " << v->opt << ": " << v->help << std::endl;
00109     }
00110     
00111     StringOption::~StringOption(void) {
00112       Value* v = fst;
00113       while (v != NULL) {
00114         Value* n = v->next;
00115         delete v;
00116         v = n;
00117       }
00118     }
00119     
00120     
00121     bool
00122     IntOption::parse(int& argc, char* argv[]) {
00123       if ((argc < 2) || strcmp(argv[1],opt))
00124         return false;
00125       if (argc == 2) {
00126         std::cerr << "Missing argument for option \"" << opt << "\"" << std::endl;
00127         exit(EXIT_FAILURE);
00128       }
00129       cur = atoi(argv[2]);
00130       // Remove options
00131       argc -= 2;
00132       for (int i=1; i<argc; i++)
00133         argv[i] = argv[i+2];
00134       return true;
00135     }
00136     
00137     void
00138     IntOption::help(void) {
00139       using namespace std;
00140       cerr << '\t' << opt << " (int) default: " << cur << endl
00141            << "\t\t" << exp << endl;
00142     }
00143   
00144 
00145     bool
00146     UnsignedIntOption::parse(int& argc, char* argv[]) {
00147       if ((argc < 2) || strcmp(argv[1],opt))
00148         return false;
00149       if (argc == 2) {
00150         std::cerr << "Missing argument for option \"" << opt << "\"" << std::endl;
00151         exit(EXIT_FAILURE);
00152       }
00153       cur = atoi(argv[2]);
00154       // Remove options
00155       argc -= 2;
00156       for (int i=1; i<argc; i++)
00157         argv[i] = argv[i+2];
00158       return true;
00159     }
00160     
00161     void
00162     UnsignedIntOption::help(void) {
00163       using namespace std;
00164       cerr << '\t' << opt << " (unsigned int) default: " << cur << endl
00165            << "\t\t" << exp << endl;
00166     }
00167   
00168 
00169     bool
00170     DoubleOption::parse(int& argc, char* argv[]) {
00171       if ((argc < 2) || strcmp(argv[1],opt))
00172         return false;
00173       if (argc == 2) {
00174         std::cerr << "Missing argument for option \"" << opt << "\"" << std::endl;
00175         exit(EXIT_FAILURE);
00176       }
00177       cur = atof(argv[2]);
00178       // Remove options
00179       argc -= 2;
00180       for (int i=1; i<argc; i++)
00181         argv[i] = argv[i+2];
00182       return true;
00183     }
00184     
00185     void
00186     DoubleOption::help(void) {
00187       using namespace std;
00188       cerr << '\t' << opt << " (double) default: " << cur << endl
00189            << "\t\t" << exp << endl;
00190     }
00191 
00192     bool
00193     BoolOption::parse(int& argc, char* argv[]) {
00194       if ((argc < 2) || strcmp(argv[1],opt)) {
00195         return false;
00196       }
00197       // Remove options
00198       argc--;
00199       for (int i=1; i<argc; i++)
00200         argv[i] = argv[i+1];
00201       cur = true;
00202       return true;
00203     }
00204 
00205     void 
00206     BoolOption::help(void) {
00207       using namespace std;
00208       cerr << '\t' << opt << endl << "\t\t" << exp << endl;
00209     }
00210 
00211   
00212   }
00213 
00214   BaseOptions::BaseOptions(const char* n)
00215     : fst(NULL), lst(NULL), _name(n) {}
00216 
00217   void
00218   BaseOptions::help(void) {
00219     std::cerr << "Gecode configuration information:" << std::endl
00220               << " - Version: " << GECODE_VERSION << std::endl
00221               << " - Variable types: ";
00222 #ifdef GECODE_HAS_INT_VARS
00223     std::cerr << "BoolVar IntVar ";
00224 #endif
00225 #ifdef GECODE_HAS_SET_VARS
00226     std::cerr << "SetVar";
00227 #endif
00228     std::cerr << std::endl
00229               << " - Thread support: ";
00230 #ifdef GECODE_HAS_THREADS
00231     std::cerr << "enabled (" << Support::Thread::npu() << " processing units)";
00232 #else
00233     std::cerr << "disabled";
00234 #endif
00235     std::cerr << std::endl
00236               << " - Gist support: ";
00237 #ifdef GECODE_HAS_GIST
00238     std::cerr << "enabled";
00239 #else
00240     std::cerr << "disabled";
00241 #endif
00242     std::cerr << std::endl << std::endl
00243               << "Options for " << name() << ":" << std::endl
00244               << "\t-help, --help, -?" << std::endl
00245               << "\t\tprint this help message" << std::endl;
00246     for (Driver::BaseOption* o = fst; o != NULL; o = o->next)
00247       o->help();
00248   }
00249 
00250   void
00251   BaseOptions::parse(int& argc, char* argv[]) {
00252   next:
00253     for (Driver::BaseOption* o = fst; o != NULL; o = o->next)
00254       if (o->parse(argc,argv))
00255         goto next;
00256     if (argc < 2)
00257       return;
00258     if (!strcmp(argv[1],"-help") || !strcmp(argv[1],"--help") ||
00259         !strcmp(argv[1],"-?")) {
00260       help();
00261       exit(EXIT_SUCCESS);
00262     }
00263     return;
00264   }
00265   
00266 
00267   Options::Options(const char* n)
00268     : BaseOptions(n),
00269       
00270       _model("-model","model variants"),
00271       _propagation("-propagation","propagation variants"),
00272       _icl("-icl","integer consistency level",ICL_DEF),
00273       _branching("-branching","branching variants"),
00274       
00275       _search("-search","search engine variants"),
00276       _solutions("-solutions","number of solutions (0 = all)",1),
00277       _threads("-threads","number of threads (0 = #processing units)",
00278                Search::Config::threads),
00279       _c_d("-c-d","recomputation commit distance",Search::Config::c_d),
00280       _a_d("-a-d","recomputation adaption distance",Search::Config::a_d),
00281       _node("-node","node cutoff (0 = none, solution mode)"),
00282       _fail("-fail","failure cutoff (0 = none, solution mode)"),
00283       _time("-time","time (in ms) cutoff (0 = none, solution mode)"),
00284       
00285       _mode("-mode","how to execute script",SM_SOLUTION),
00286       _samples("-samples","how many samples (time mode)",1),
00287       _iterations("-iterations","iterations per sample (time mode)",1)
00288   {
00289     
00290     _icl.add(ICL_DEF, "def"); _icl.add(ICL_VAL, "val");
00291     _icl.add(ICL_BND, "bnd"); _icl.add(ICL_DOM, "dom");
00292     
00293     _mode.add(SM_SOLUTION, "solution");
00294     _mode.add(SM_TIME, "time");
00295     _mode.add(SM_STAT, "stat");
00296     _mode.add(SM_GIST, "gist");
00297     
00298     add(_model); add(_propagation); add(_icl); add(_branching);
00299     add(_search); add(_solutions); add(_threads); add(_c_d); add(_a_d);
00300     add(_node); add(_fail); add(_time);
00301     add(_mode); add(_iterations); add(_samples);
00302   }
00303 
00304   
00305   SizeOptions::SizeOptions(const char* e)
00306     : Options(e), _size(0) {}
00307   
00308   void
00309   SizeOptions::help(void) {
00310     Options::help();
00311     std::cerr << "\t(unsigned int) default: " << size() << std::endl
00312               << "\t\twhich version/size for script" << std::endl;
00313   }
00314 
00315   void
00316   SizeOptions::parse(int& argc, char* argv[]) {
00317     Options::parse(argc,argv);
00318     if (argc < 2)
00319       return;
00320     size(atoi(argv[1]));
00321   }
00322 
00323 }
00324 
00325 // STATISTICS: driver-any