Generated on Wed Nov 1 15:04:28 2006 for Gecode by doxygen 1.4.5

support.icc

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Christian Schulte <schulte@gecode.org>
00004  *
00005  *  Copyright:
00006  *     Christian Schulte, 2004
00007  *
00008  *  Last modified:
00009  *     $Date: 2006-10-25 13:51:24 +0200 (Wed, 25 Oct 2006) $ by $Author: schulte $
00010  *     $Revision: 3787 $
00011  *
00012  *  This file is part of Gecode, the generic constraint
00013  *  development environment:
00014  *     http://www.gecode.org
00015  *
00016  *  See the file "LICENSE" for information on usage and
00017  *  redistribution of this file, and for a
00018  *     DISCLAIMER OF ALL WARRANTIES.
00019  *
00020  */
00021 
00022 inline
00023 Options::Options(const char* n)
00024   : icl(ICL_DEF),
00025     c_d(Search::Config::c_d),
00026     a_d(Search::Config::a_d),
00027     mode(EM_SOLUTION),
00028     quiet(false),
00029     samples(1),
00030     iterations(1),
00031     solutions(1),
00032     fails(-1),
00033     time(-1),
00034     naive(false),
00035     size(0),
00036     name(n) {}
00037 
00038 namespace {
00041   class FailTimeStop : public Search::Stop {
00042   private:
00043     Search::TimeStop *ts;
00044     Search::FailStop *fs;
00045     FailTimeStop(int fails, int time) {
00046       ts = new Search::TimeStop(time);
00047       fs = new Search::FailStop(fails);
00048     }
00049   public:
00050     bool stop(const Search::Statistics& s) {
00051       return fs->stop(s) || ts->stop(s);
00052     }
00054     static Search::Stop* create(int fails, int time) {
00055       if (fails < 0 && time < 0) return NULL;
00056       if (fails < 0) return new Search::TimeStop( time);
00057       if (time  < 0) return new Search::FailStop(fails);
00058       return new FailTimeStop(fails, time);
00059     }
00060   };
00061 }
00062 
00063 
00064 template <class Script, template<class> class Engine>
00065 void
00066 Example::run(const Options& o) {
00067   using namespace std;
00068   try {
00069     if (!o.quiet)
00070       cout << o.name << endl;
00071     switch (o.mode) {
00072     case EM_SOLUTION:
00073       {
00074         Timer t;
00075         int i = o.solutions;
00076         t.start();
00077         Script* s = new Script(o);
00078         unsigned int n_p = 0;
00079         unsigned int n_b = 0;
00080         if (s->status() != SS_FAILED) {
00081           n_p = s->propagators();
00082           n_b = s->branchings();
00083         }
00084         Search::Stop* stop = FailTimeStop::create(o.fails, o.time);
00085         Engine<Script> e(s,o.c_d,o.a_d,stop);
00086         delete s;
00087         do {
00088           Example* ex = e.next();
00089           if (ex == NULL)
00090             break;
00091           ex->print();
00092           delete ex;
00093         } while (--i != 0);
00094         Search::Statistics stat = e.statistics();
00095         cout << endl;
00096         cout << "Initial" << endl
00097              << "\tpropagators:   " << n_p << endl
00098              << "\tbranchings:    " << n_b << endl
00099              << endl
00100              << "Summary" << endl
00101              << "\truntime:       " << t.stop() << endl
00102              << "\tsolutions:     " << abs(static_cast<int>(o.solutions) - i) << endl
00103              << "\tpropagations:  " << stat.propagate << endl
00104              << "\tfailures:      " << stat.fail << endl
00105              << "\tclones:        " << stat.clone << endl
00106              << "\tcommits:       " << stat.commit << endl
00107              << "\tpeak memory:   "
00108              << static_cast<int>((stat.memory+1023) / 1024) << " KB"
00109              << endl;
00110       }
00111       break;
00112     case EM_STAT:
00113       {
00114         int i = o.solutions;
00115         Script* s = new Script(o);
00116         unsigned int n_p = 0;
00117         unsigned int n_b = 0;
00118         if (s->status() != SS_FAILED) {
00119           n_p = s->propagators();
00120           n_b = s->branchings();
00121         }
00122         Engine<Script> e(s,o.c_d,o.a_d);
00123         delete s;
00124         do {
00125           Example* ex = e.next();
00126           if (ex == NULL)
00127             break;
00128           delete ex;
00129         } while (--i != 0);
00130         Search::Statistics stat = e.statistics();
00131         cout << endl
00132              << "\tpropagators:   " << n_p << endl
00133              << "\tbranchings:    " << n_b << endl
00134              << "\tsolutions:     " << abs(static_cast<int>(o.solutions) - i) << endl
00135              << "\tpropagations:  " << stat.propagate << endl
00136              << "\tfailures:      " << stat.fail << endl
00137              << "\tclones:        " << stat.clone << endl
00138              << "\tcommits:       " << stat.commit << endl
00139              << "\tpeak memory:   "
00140              << static_cast<int>((stat.memory+1023) / 1024) << " KB"
00141              << endl;
00142       }
00143       break;
00144     case EM_TIME:
00145       {
00146         Timer t;
00147         GECODE_AUTOARRAY(double,ts,o.samples);
00148         for (int s = o.samples; s--; ) {
00149           t.start();
00150           for (int k = o.iterations; k--; ) {
00151             unsigned int i = o.solutions;
00152             Script* s = new Script(o);
00153             Engine<Script> e(s,o.c_d,o.a_d);
00154             delete s;
00155             do {
00156               Example* ex = e.next();
00157               if (ex == NULL)
00158                 break;
00159               delete ex;
00160             } while (--i != 0);
00161           }
00162           ts[s] = t.stop() / o.iterations;
00163         }
00164         double m = am(ts,o.samples);
00165         double d = dev(ts,o.samples) * 100.0;
00166         if (o.quiet) {
00167           cout << showpoint << setprecision(6) << m << " "
00168                << setprecision(2) << d
00169                << endl;
00170         } else {
00171           cout << "\tRuntime: "
00172                << setw(20) << right
00173                << showpoint << fixed
00174                << setprecision(6) << m << "ms"
00175                << setprecision(2) << " (" << d << "% deviation)"
00176                << endl;
00177         }
00178       }
00179       break;
00180     }
00181   } catch (Exception e) {
00182     cout << "Exception: " << e.what() << "." << endl
00183          << "Stopping..." << endl;
00184   }
00185 }
00186 
00187 // STATISTICS: example-any