/usr/include/gecode/flatzinc.hh is in libgecode-dev 4.4.0-5.
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 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 | /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Guido Tack <tack@gecode.org>
*
* Contributing authors:
* Gabriel Hjort Blindell <gabriel.hjort.blindell@gmail.com>
*
* Copyright:
* Guido Tack, 2007-2012
* Gabriel Hjort Blindell, 2012
*
* Last modified:
* $Date: 2015-03-19 11:47:57 +0100 (Thu, 19 Mar 2015) $ by $Author: tack $
* $Revision: 14463 $
*
* 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.
*
*/
#ifndef __GECODE_FLATZINC_HH__
#define __GECODE_FLATZINC_HH__
#include <iostream>
#include <gecode/kernel.hh>
#include <gecode/int.hh>
#ifdef GECODE_HAS_SET_VARS
#include <gecode/set.hh>
#endif
#ifdef GECODE_HAS_FLOAT_VARS
#include <gecode/float.hh>
#endif
#include <map>
/*
* Support for DLLs under Windows
*
*/
#if !defined(GECODE_STATIC_LIBS) && \
(defined(__CYGWIN__) || defined(__MINGW32__) || defined(_MSC_VER))
#ifdef GECODE_BUILD_FLATZINC
#define GECODE_FLATZINC_EXPORT __declspec( dllexport )
#else
#define GECODE_FLATZINC_EXPORT __declspec( dllimport )
#endif
#else
#ifdef GECODE_GCC_HAS_CLASS_VISIBILITY
#define GECODE_FLATZINC_EXPORT __attribute__ ((visibility("default")))
#else
#define GECODE_FLATZINC_EXPORT
#endif
#endif
// Configure auto-linking
#ifndef GECODE_BUILD_FLATZINC
#define GECODE_LIBRARY_NAME "FlatZinc"
#include <gecode/support/auto-link.hpp>
#endif
#include <gecode/driver.hh>
#include <gecode/flatzinc/conexpr.hh>
#include <gecode/flatzinc/ast.hh>
#include <gecode/flatzinc/varspec.hh>
/**
* \namespace Gecode::FlatZinc
* \brief Interpreter for the %FlatZinc language
*
* The Gecode::FlatZinc namespace contains all functionality required
* to parse and solve constraint models written in the %FlatZinc language.
*
*/
namespace Gecode { namespace FlatZinc {
/**
* \brief Output support class for %FlatZinc interpreter
*
*/
class GECODE_FLATZINC_EXPORT Printer {
private:
AST::Array* _output;
void printElem(std::ostream& out,
AST::Node* ai,
const Gecode::IntVarArray& iv,
const Gecode::BoolVarArray& bv
#ifdef GECODE_HAS_SET_VARS
,
const Gecode::SetVarArray& sv
#endif
#ifdef GECODE_HAS_FLOAT_VARS
,
const Gecode::FloatVarArray& fv
#endif
) const;
void printElemDiff(std::ostream& out,
AST::Node* ai,
const Gecode::IntVarArray& iv1,
const Gecode::IntVarArray& iv2,
const Gecode::BoolVarArray& bv1,
const Gecode::BoolVarArray& bv2
#ifdef GECODE_HAS_SET_VARS
,
const Gecode::SetVarArray& sv1,
const Gecode::SetVarArray& sv2
#endif
#ifdef GECODE_HAS_FLOAT_VARS
,
const Gecode::FloatVarArray& fv1,
const Gecode::FloatVarArray& fv2
#endif
) const;
public:
Printer(void) : _output(NULL) {}
void init(AST::Array* output);
void print(std::ostream& out,
const Gecode::IntVarArray& iv,
const Gecode::BoolVarArray& bv
#ifdef GECODE_HAS_SET_VARS
,
const Gecode::SetVarArray& sv
#endif
#ifdef GECODE_HAS_FLOAT_VARS
,
const Gecode::FloatVarArray& fv
#endif
) const;
void printDiff(std::ostream& out,
const Gecode::IntVarArray& iv1, const Gecode::IntVarArray& iv2,
const Gecode::BoolVarArray& bv1, const Gecode::BoolVarArray& bv2
#ifdef GECODE_HAS_SET_VARS
,
const Gecode::SetVarArray& sv1, const Gecode::SetVarArray& sv2
#endif
#ifdef GECODE_HAS_FLOAT_VARS
,
const Gecode::FloatVarArray& fv1,
const Gecode::FloatVarArray& fv2
#endif
) const;
~Printer(void);
void shrinkElement(AST::Node* node,
std::map<int,int>& iv, std::map<int,int>& bv,
std::map<int,int>& sv, std::map<int,int>& fv);
void shrinkArrays(Space& home,
int& optVar, bool optVarIsInt,
Gecode::IntVarArray& iv,
Gecode::BoolVarArray& bv
#ifdef GECODE_HAS_SET_VARS
,
Gecode::SetVarArray& sv
#endif
#ifdef GECODE_HAS_FLOAT_VARS
,
Gecode::FloatVarArray& fv
#endif
);
private:
Printer(const Printer&);
Printer& operator=(const Printer&);
};
/**
* \brief %Options for running %FlatZinc models
*
*/
class FlatZincOptions : public Gecode::BaseOptions {
protected:
/// \name Search options
//@{
Gecode::Driver::IntOption _solutions; ///< How many solutions
Gecode::Driver::BoolOption _allSolutions; ///< Return all solutions
Gecode::Driver::DoubleOption _threads; ///< How many threads to use
Gecode::Driver::BoolOption _free; ///< Use free search
Gecode::Driver::DoubleOption _decay; ///< Decay option
Gecode::Driver::UnsignedIntOption _c_d; ///< Copy recomputation distance
Gecode::Driver::UnsignedIntOption _a_d; ///< Adaptive recomputation distance
Gecode::Driver::UnsignedIntOption _node; ///< Cutoff for number of nodes
Gecode::Driver::UnsignedIntOption _fail; ///< Cutoff for number of failures
Gecode::Driver::UnsignedIntOption _time; ///< Cutoff for time
Gecode::Driver::IntOption _seed; ///< Random seed
Gecode::Driver::StringOption _restart; ///< Restart method option
Gecode::Driver::DoubleOption _r_base; ///< Restart base
Gecode::Driver::UnsignedIntOption _r_scale; ///< Restart scale factor
Gecode::Driver::BoolOption _nogoods; ///< Whether to use no-goods
Gecode::Driver::UnsignedIntOption _nogoods_limit; ///< Depth limit for extracting no-goods
Gecode::Driver::BoolOption _interrupt; ///< Whether to catch SIGINT
Gecode::Driver::DoubleOption _step; ///< Step option
//@}
/// \name Execution options
//@{
Gecode::Driver::StringOption _mode; ///< Script mode to run
Gecode::Driver::BoolOption _stat; ///< Emit statistics
Gecode::Driver::StringValueOption _output; ///< Output file
//@}
public:
/// Constructor
FlatZincOptions(const char* s)
: Gecode::BaseOptions(s),
_solutions("-n","number of solutions (0 = all, -1 = one/best)",-1),
_allSolutions("-a", "return all solutions (equal to -n 0)"),
_threads("-p","number of threads (0 = #processing units)",
Gecode::Search::Config::threads),
_free("-f", "free search, no need to follow search-specification"),
_decay("-decay","decay factor",0.99),
_c_d("-c-d","recomputation commit distance",Gecode::Search::Config::c_d),
_a_d("-a-d","recomputation adaption distance",Gecode::Search::Config::a_d),
_node("-node","node cutoff (0 = none, solution mode)"),
_fail("-fail","failure cutoff (0 = none, solution mode)"),
_time("-time","time (in ms) cutoff (0 = none, solution mode)"),
_seed("-r","random seed",0),
_restart("-restart","restart sequence type",RM_NONE),
_r_base("-restart-base","base for geometric restart sequence",1.5),
_r_scale("-restart-scale","scale factor for restart sequence",250),
_nogoods("-nogoods","whether to use no-goods from restarts",false),
_nogoods_limit("-nogoods-limit","depth limit for no-good extraction",
Search::Config::nogoods_limit),
_interrupt("-interrupt","whether to catch Ctrl-C (true) or not (false)",
true),
_step("-step","step distance for float optimization",0.0),
_mode("-mode","how to execute script",Gecode::SM_SOLUTION),
_stat("-s","emit statistics"),
_output("-o","file to send output to") {
_mode.add(Gecode::SM_SOLUTION, "solution");
_mode.add(Gecode::SM_STAT, "stat");
_mode.add(Gecode::SM_GIST, "gist");
_restart.add(RM_NONE,"none");
_restart.add(RM_CONSTANT,"constant");
_restart.add(RM_LINEAR,"linear");
_restart.add(RM_LUBY,"luby");
_restart.add(RM_GEOMETRIC,"geometric");
add(_solutions); add(_threads); add(_c_d); add(_a_d);
add(_allSolutions);
add(_free);
add(_decay);
add(_node); add(_fail); add(_time); add(_interrupt);
add(_seed);
add(_step);
add(_restart); add(_r_base); add(_r_scale);
add(_nogoods); add(_nogoods_limit);
add(_mode); add(_stat);
add(_output);
}
void parse(int& argc, char* argv[]) {
Gecode::BaseOptions::parse(argc,argv);
if (_allSolutions.value() && _solutions.value()==-1) {
_solutions.value(0);
}
if (_stat.value())
_mode.value(Gecode::SM_STAT);
}
virtual void help(void) {
std::cerr << "Gecode FlatZinc interpreter" << std::endl
<< " - Supported FlatZinc version: " << GECODE_FLATZINC_VERSION
<< std::endl << std::endl;
Gecode::BaseOptions::help();
}
int solutions(void) const { return _solutions.value(); }
bool allSolutions(void) const { return _allSolutions.value(); }
double threads(void) const { return _threads.value(); }
bool free(void) const { return _free.value(); }
unsigned int c_d(void) const { return _c_d.value(); }
unsigned int a_d(void) const { return _a_d.value(); }
unsigned int node(void) const { return _node.value(); }
unsigned int fail(void) const { return _fail.value(); }
unsigned int time(void) const { return _time.value(); }
int seed(void) const { return _seed.value(); }
double step(void) const { return _step.value(); }
const char* output(void) const { return _output.value(); }
Gecode::ScriptMode mode(void) const {
return static_cast<Gecode::ScriptMode>(_mode.value());
}
double decay(void) const { return _decay.value(); }
RestartMode restart(void) const {
return static_cast<RestartMode>(_restart.value());
}
double restart_base(void) const { return _r_base.value(); }
unsigned int restart_scale(void) const { return _r_scale.value(); }
bool nogoods(void) const { return _nogoods.value(); }
unsigned int nogoods_limit(void) const { return _nogoods_limit.value(); }
bool interrupt(void) const { return _interrupt.value(); }
void allSolutions(bool b) { _allSolutions.value(b); }
};
class BranchInformation : public SharedHandle {
public:
/// Constructor
BranchInformation(void);
/// Copy constructor
BranchInformation(const BranchInformation& bi);
/// Initialise for use
void init(void);
/// Add new brancher information
void add(const BrancherHandle& bh,
const std::string& rel0,
const std::string& rel1,
const std::vector<std::string>& n);
/// Output branch information
void print(const BrancherHandle& bh,
int a, int i, int n, std::ostream& o) const;
#ifdef GECODE_HAS_FLOAT_VARS
/// Output branch information
void print(const BrancherHandle& bh,
int a, int i, const FloatNumBranch& nl, std::ostream& o) const;
#endif
};
/**
* \brief A thread-safe random number generator
*
*/
class GECODE_FLATZINC_EXPORT FznRnd {
protected:
/// The actual random number generator
Gecode::Support::RandomGenerator random;
/// A mutex for the random number generator
Gecode::Support::Mutex mutex;
public:
/// Constructor
FznRnd(unsigned int s=1);
/// Returns a random integer from the interval [0..n)
unsigned int operator ()(unsigned int n);
};
/**
* \brief A space that can be initialized with a %FlatZinc model
*
*/
class GECODE_FLATZINC_EXPORT FlatZincSpace : public Space {
public:
enum Meth {
SAT, //< Solve as satisfaction problem
MIN, //< Solve as minimization problem
MAX //< Solve as maximization problem
};
protected:
/// Number of integer variables
int intVarCount;
/// Number of Boolean variables
int boolVarCount;
/// Number of float variables
int floatVarCount;
/// Number of set variables
int setVarCount;
/// Index of the variable to optimize
int _optVar;
/// Whether variable to optimize is integer (or float)
bool _optVarIsInt;
/// Whether to solve as satisfaction or optimization problem
Meth _method;
/// Percentage of variables to keep in LNS (or 0 for no LNS)
unsigned int _lns;
/// Random number generator
FznRnd* _random;
/// Annotations on the solve item
AST::Array* _solveAnnotations;
/// Copy constructor
FlatZincSpace(bool share, FlatZincSpace&);
private:
/// Run the search engine
template<template<class> class Engine>
void
runEngine(std::ostream& out, const Printer& p,
const FlatZincOptions& opt, Gecode::Support::Timer& t_total);
/// Run the meta search engine
template<template<class> class Engine,
template<template<class> class,class> class Meta>
void
runMeta(std::ostream& out, const Printer& p,
const FlatZincOptions& opt, Gecode::Support::Timer& t_total);
void
branchWithPlugin(AST::Node* ann);
public:
/// The integer variables
Gecode::IntVarArray iv;
/// The introduced integer variables
Gecode::IntVarArray iv_aux;
/// The integer variables used in LNS
Gecode::IntVarArray iv_lns;
/// Indicates whether an integer variable is introduced by mzn2fzn
std::vector<bool> iv_introduced;
/// Indicates whether an integer variable aliases a Boolean variable
int* iv_boolalias;
/// The Boolean variables
Gecode::BoolVarArray bv;
/// The introduced Boolean variables
Gecode::BoolVarArray bv_aux;
/// Indicates whether a Boolean variable is introduced by mzn2fzn
std::vector<bool> bv_introduced;
#ifdef GECODE_HAS_SET_VARS
/// The set variables
Gecode::SetVarArray sv;
/// The introduced set variables
Gecode::SetVarArray sv_aux;
/// Indicates whether a set variable is introduced by mzn2fzn
std::vector<bool> sv_introduced;
#endif
#ifdef GECODE_HAS_FLOAT_VARS
/// The float variables
Gecode::FloatVarArray fv;
/// The introduced float variables
Gecode::FloatVarArray fv_aux;
/// Indicates whether a float variable is introduced by mzn2fzn
std::vector<bool> fv_introduced;
/// Step by which a next solution has to have lower cost
Gecode::FloatNum step;
#endif
/// Whether the introduced variables still need to be copied
bool needAuxVars;
/// Construct empty space
FlatZincSpace(FznRnd* random = NULL);
/// Destructor
~FlatZincSpace(void);
/// Initialize space with given number of variables
void init(int intVars, int boolVars, int setVars, int floatVars);
/// Create new integer variable from specification
void newIntVar(IntVarSpec* vs);
/// Link integer variable \a iv to Boolean variable \a bv
void aliasBool2Int(int iv, int bv);
/// Return linked Boolean variable for integer variable \a iv
int aliasBool2Int(int iv);
/// Create new Boolean variable from specification
void newBoolVar(BoolVarSpec* vs);
/// Create new set variable from specification
void newSetVar(SetVarSpec* vs);
/// Create new float variable from specification
void newFloatVar(FloatVarSpec* vs);
/// Post a constraint specified by \a ce
void postConstraints(std::vector<ConExpr*>& ces);
/// Post the solve item
void solve(AST::Array* annotation);
/// Post that integer variable \a var should be minimized
void minimize(int var, bool isInt, AST::Array* annotation);
/// Post that integer variable \a var should be maximized
void maximize(int var, bool isInt, AST::Array* annotation);
/// Run the search
void run(std::ostream& out, const Printer& p,
const FlatZincOptions& opt, Gecode::Support::Timer& t_total);
/// Produce output on \a out using \a p
void print(std::ostream& out, const Printer& p) const;
/// Compare this space with space \a s and print the differences on
/// \a out
void compare(const Space& s, std::ostream& out) const;
/// Compare this space with space \a s and print the differences on
/// \a out using \a p
void compare(const FlatZincSpace& s, std::ostream& out,
const Printer& p) const;
/**
* \brief Remove all variables not needed for output
*
* After calling this function, no new constraints can be posted through
* FlatZinc variable references, and the createBranchers method must
* not be called again.
*
*/
void shrinkArrays(Printer& p);
/// Return whether to solve a satisfaction or optimization problem
Meth method(void) const;
/// Return index of variable used for optimization
int optVar(void) const;
/// Return whether variable used for optimization is integer (or float)
bool optVarIsInt(void) const;
/**
* \brief Create branchers corresponding to the solve item annotations
*
* If \a ignoreUnknown is true, unknown solve item annotations will be
* ignored, otherwise a warning is written to \a err.
*
* The seed for random branchers is given by the \a seed parameter.
*
*/
void createBranchers(AST::Node* ann,
int seed, double decay,
bool ignoreUnknown,
std::ostream& err = std::cerr);
/// Return the solve item annotations
AST::Array* solveAnnotations(void) const;
/// Information for printing branches
BranchInformation branchInfo;
/// Implement optimization
virtual void constrain(const Space& s);
/// Copy function
virtual Gecode::Space* copy(bool share);
virtual bool slave(const CRI& cri);
/// \name AST to variable and value conversion
//@{
/// Convert \a arg (array of integers) to IntArgs
IntArgs arg2intargs(AST::Node* arg, int offset = 0);
/// Convert \a arg (array of Booleans) to IntArgs
IntArgs arg2boolargs(AST::Node* arg, int offset = 0);
/// Convert \a n to IntSet
IntSet arg2intset(AST::Node* n);
/// Convert \a arg to IntSetArgs
IntSetArgs arg2intsetargs(AST::Node* arg, int offset = 0);
/// Convert \a arg to IntVarArgs
IntVarArgs arg2intvarargs(AST::Node* arg, int offset = 0);
/// Convert \a arg to BoolVarArgs
BoolVarArgs arg2boolvarargs(AST::Node* arg, int offset = 0, int siv=-1);
/// Convert \a n to BoolVar
BoolVar arg2BoolVar(AST::Node* n);
/// Convert \a n to IntVar
IntVar arg2IntVar(AST::Node* n);
/// Check if \a b is array of Booleans (or has a single integer)
bool isBoolArray(AST::Node* b, int& singleInt);
#ifdef GECODE_HAS_SET_VARS
/// Convert \a n to SetVar
SetVar arg2SetVar(AST::Node* n);
/// Convert \a n to SetVarArgs
SetVarArgs arg2setvarargs(AST::Node* arg, int offset = 0, int doffset = 0,
const IntSet& od=IntSet::empty);
#endif
#ifdef GECODE_HAS_FLOAT_VARS
/// Convert \a n to FloatValArgs
FloatValArgs arg2floatargs(AST::Node* arg, int offset = 0);
/// Convert \a n to FloatVar
FloatVar arg2FloatVar(AST::Node* n);
/// Convert \a n to FloatVarArgs
FloatVarArgs arg2floatvarargs(AST::Node* arg, int offset = 0);
#endif
/// Convert \a ann to IntConLevel
IntConLevel ann2icl(AST::Node* ann);
//@}
};
/// %Exception class for %FlatZinc errors
class GECODE_VTABLE_EXPORT Error {
private:
const std::string msg;
public:
Error(const std::string& where, const std::string& what)
: msg(where+": "+what) {}
const std::string& toString(void) const { return msg; }
};
/**
* \brief Parse FlatZinc file \a fileName into \a fzs and return it.
*
* Creates a new empty FlatZincSpace if \a fzs is NULL.
*/
GECODE_FLATZINC_EXPORT
FlatZincSpace* parse(const std::string& fileName,
Printer& p, std::ostream& err = std::cerr,
FlatZincSpace* fzs=NULL, FznRnd* rnd=NULL);
/**
* \brief Parse FlatZinc from \a is into \a fzs and return it.
*
* Creates a new empty FlatZincSpace if \a fzs is NULL.
*/
GECODE_FLATZINC_EXPORT
FlatZincSpace* parse(std::istream& is,
Printer& p, std::ostream& err = std::cerr,
FlatZincSpace* fzs=NULL, FznRnd* rnd=NULL);
}}
#endif
// STATISTICS: flatzinc-any
|