/usr/include/gecode/driver/script.hpp is in libgecode-dev 3.7.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2004
*
* Last modified:
* $Date: 2011-06-16 18:27:32 +1000 (Thu, 16 Jun 2011) $ by $Author: tack $
* $Revision: 12052 $
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <iostream>
#include <iomanip>
#ifndef GECODE_THREADS_WINDOWS
#include <csignal>
#endif
namespace Gecode { namespace Driver {
/**
* \brief Stop object based on nodes, failures, and time
*
*/
class Cutoff : public Search::Stop {
private:
Search::NodeStop* ns; ///< Used node stop object
Search::FailStop* fs; ///< Used fail stop object
Search::TimeStop* ts; ///< Used time stop object
GECODE_DRIVER_EXPORT
static bool sigint; ///< Whether search was interrupted using Ctrl-C
/// Initialize stop object
Cutoff(unsigned int node, unsigned int fail, unsigned int time)
: ns((node > 0) ? new Search::NodeStop(node) : NULL),
fs((fail > 0) ? new Search::FailStop(fail) : NULL),
ts((time > 0) ? new Search::TimeStop(time) : NULL) {
sigint = false;
}
public:
/// Reason why search has been stopped
enum {
SR_NODE = 1 << 0, ///< Node limit reached
SR_FAIL = 1 << 1, ///< Fail limit reached
SR_TIME = 1 << 2, ///< Time limit reached
SR_INT = 1 << 3 ///< Interrupted by user
};
/// Test whether search must be stopped
virtual bool stop(const Search::Statistics& s, const Search::Options& o) {
return
sigint ||
((ns != NULL) && ns->stop(s,o)) ||
((fs != NULL) && fs->stop(s,o)) ||
((ts != NULL) && ts->stop(s,o));
}
/// Report reason why search has been stopped
int reason(const Search::Statistics& s, const Search::Options& o) {
return
(((ns != NULL) && ns->stop(s,o)) ? SR_NODE : 0) |
(((fs != NULL) && fs->stop(s,o)) ? SR_FAIL : 0) |
(((ts != NULL) && ts->stop(s,o)) ? SR_TIME : 0) |
(sigint ? SR_INT : 0);
}
/// Create appropriate stop-object
static Search::Stop*
create(unsigned int node, unsigned int fail, unsigned int time,
bool intr) {
if ( (!intr) && (node == 0) && (fail == 0) && (time == 0))
return NULL;
else
return new Cutoff(node,fail,time);
}
#ifdef GECODE_THREADS_WINDOWS
/// Handler for catching Ctrl-C
static BOOL interrupt(DWORD t) {
if (t == CTRL_C_EVENT) {
sigint = true;
installCtrlHandler(false,true);
return true;
}
return false;
}
#else
/// Handler for catching Ctrl-C
static void
interrupt(int) {
sigint = true;
installCtrlHandler(false,true);
}
#endif
/// Install handler for catching Ctrl-C
static void installCtrlHandler(bool install, bool force=false) {
if (force || !sigint) {
#ifdef GECODE_THREADS_WINDOWS
SetConsoleCtrlHandler( (PHANDLER_ROUTINE) interrupt, install);
#else
std::signal(SIGINT, install ? interrupt : SIG_DFL);
#endif
}
}
/// Destructor
~Cutoff(void) {
delete ns; delete fs; delete ts;
}
};
/**
* \brief Get time since start of timer and print user friendly time
* information.
*/
GECODE_DRIVER_EXPORT void
stop(Support::Timer& t, std::ostream& os);
/**
* \brief Compute arithmetic mean of \a n elements in \a t
*/
GECODE_DRIVER_EXPORT double
am(double t[], int n);
/**
* \brief Compute deviation of \a n elements in \a t
*/
GECODE_DRIVER_EXPORT double
dev(double t[], int n);
#ifdef GECODE_HAS_GIST
/**
* \brief Traits class for search engines
*/
template<class Engine>
class GistEngine {
};
/// Specialization for DFS
template<typename S>
class GistEngine<DFS<S> > {
public:
static void explore(S* root, const Gist::Options& opt) {
(void) Gist::dfs(root, opt);
}
};
/// Specialization for BAB
template<typename S>
class GistEngine<BAB<S> > {
public:
static void explore(S* root, const Gist::Options& opt) {
(void) Gist::bab(root, opt);
}
};
/// Specialization for Restart
template<typename S>
class GistEngine<Restart<S> > {
public:
static void explore(S* root, const Gist::Options& opt) {
(void) Gist::bab(root, opt);
}
};
#endif
template<class Space>
template<class Script, template<class> class Engine, class Options>
void
ScriptBase<Space>::run(const Options& o) {
using namespace std;
try {
switch (o.mode()) {
case SM_GIST:
#ifdef GECODE_HAS_GIST
{
Gist::Print<Script> pi(o.name());
Gist::VarComparator<Script> vc(o.name());
Gist::Options opt;
opt.inspect.click(&pi);
opt.inspect.compare(&vc);
opt.clone = false;
opt.c_d = o.c_d();
opt.a_d = o.a_d();
for (int i=0; o.inspect.click(i) != NULL; i++)
opt.inspect.click(o.inspect.click(i));
for (int i=0; o.inspect.solution(i) != NULL; i++)
opt.inspect.solution(o.inspect.solution(i));
for (int i=0; o.inspect.move(i) != NULL; i++)
opt.inspect.move(o.inspect.move(i));
for (int i=0; o.inspect.compare(i) != NULL; i++)
opt.inspect.compare(o.inspect.compare(i));
Script* s = new Script(o);
(void) GistEngine<Engine<Script> >::explore(s, opt);
}
break;
// If Gist is not available, fall through
#endif
case SM_SOLUTION:
{
cout << o.name() << endl;
Support::Timer t;
int i = o.solutions();
t.start();
Script* s = new Script(o);
unsigned int n_p = s->propagators();
unsigned int n_b = s->branchers();
Search::Options so;
so.threads = o.threads();
so.c_d = o.c_d();
so.a_d = o.a_d();
so.stop = Cutoff::create(o.node(),o.fail(), o.time(),
o.interrupt());
so.clone = false;
if (o.interrupt())
Cutoff::installCtrlHandler(true);
Engine<Script> e(s,so);
do {
Script* ex = e.next();
if (ex == NULL)
break;
ex->print(std::cout);
delete ex;
} while (--i != 0);
if (o.interrupt())
Cutoff::installCtrlHandler(false);
Search::Statistics stat = e.statistics();
cout << endl;
if (e.stopped()) {
cout << "Search engine stopped..." << endl
<< "\treason: ";
int r = static_cast<Cutoff*>(so.stop)->reason(stat,so);
if (r & Cutoff::SR_INT)
cout << "user interrupt " << endl;
else {
if (r & Cutoff::SR_NODE)
cout << "node ";
if (r & Cutoff::SR_FAIL)
cout << "fail ";
if (r & Cutoff::SR_TIME)
cout << "time ";
cout << "limit reached" << endl << endl;
}
}
cout << "Initial" << endl
<< "\tpropagators: " << n_p << endl
<< "\tbranchers: " << n_b << endl
<< endl
<< "Summary" << endl
<< "\truntime: ";
stop(t, cout);
cout << endl
<< "\tsolutions: "
<< ::abs(static_cast<int>(o.solutions()) - i) << endl
<< "\tpropagations: " << stat.propagate << endl
<< "\tnodes: " << stat.node << endl
<< "\tfailures: " << stat.fail << endl
<< "\tpeak depth: " << stat.depth << endl
<< "\tpeak memory: "
<< static_cast<int>((stat.memory+1023) / 1024) << " KB"
<< endl;
delete so.stop;
}
break;
case SM_STAT:
{
cout << o.name() << endl;
Support::Timer t;
int i = o.solutions();
t.start();
Script* s = new Script(o);
unsigned int n_p = s->propagators();
unsigned int n_b = s->branchers();
Search::Options so;
so.clone = false;
so.threads = o.threads();
so.c_d = o.c_d();
so.a_d = o.a_d();
so.stop = Cutoff::create(o.node(),o.fail(), o.time(),
o.interrupt());
if (o.interrupt())
Cutoff::installCtrlHandler(true);
Engine<Script> e(s,so);
do {
Script* ex = e.next();
if (ex == NULL)
break;
delete ex;
} while (--i != 0);
if (o.interrupt())
Cutoff::installCtrlHandler(false);
Search::Statistics stat = e.statistics();
cout << endl
<< "\tpropagators: " << n_p << endl
<< "\tbranchers: " << n_b << endl
<< "\truntime: ";
stop(t, cout);
cout << endl
<< "\tsolutions: "
<< ::abs(static_cast<int>(o.solutions()) - i) << endl
<< "\tpropagations: " << stat.propagate << endl
<< "\tnodes: " << stat.node << endl
<< "\tfailures: " << stat.fail << endl
<< "\tpeak depth: " << stat.depth << endl
<< "\tpeak memory: "
<< static_cast<int>((stat.memory+1023) / 1024) << " KB"
<< endl;
}
break;
case SM_TIME:
{
cout << o.name() << endl;
Support::Timer t;
double* ts = new double[o.samples()];
bool stopped = false;
for (unsigned int s = o.samples(); !stopped && s--; ) {
t.start();
for (unsigned int k = o.iterations(); !stopped && k--; ) {
unsigned int i = o.solutions();
Script* s = new Script(o);
Search::Options so;
so.clone = false;
so.threads = o.threads();
so.c_d = o.c_d();
so.a_d = o.a_d();
so.stop = Cutoff::create(o.node(),o.fail(), o.time(), false);
Engine<Script> e(s,so);
do {
Script* ex = e.next();
if (ex == NULL)
break;
delete ex;
} while (--i != 0);
if (e.stopped())
stopped = true;
delete so.stop;
}
ts[s] = t.stop() / o.iterations();
}
if (stopped) {
cout << "\tSTOPPED" << endl;
} else {
double m = am(ts,o.samples());
double d = dev(ts,o.samples()) * 100.0;
cout << "\tRuntime: "
<< setw(20) << right
<< showpoint << fixed
<< setprecision(6) << m << "ms"
<< setprecision(2) << " (" << d << "% deviation)"
<< endl;
}
delete [] ts;
}
break;
}
} catch (Exception e) {
cerr << "Exception: " << e.what() << "." << endl
<< "Stopping..." << endl;
exit(EXIT_FAILURE);
}
}
}}
// STATISTICS: driver-any
|