38#ifndef GECODE_FLATZINC_HH
39#define GECODE_FLATZINC_HH
45#ifdef GECODE_HAS_SET_VARS
48#ifdef GECODE_HAS_FLOAT_VARS
58#if !defined(GECODE_STATIC_LIBS) && \
59 (defined(__CYGWIN__) || defined(__MINGW32__) || defined(_MSC_VER))
61#ifdef GECODE_BUILD_FLATZINC
62#define GECODE_FLATZINC_EXPORT __declspec( dllexport )
64#define GECODE_FLATZINC_EXPORT __declspec( dllimport )
69#ifdef GECODE_GCC_HAS_CLASS_VISIBILITY
71#define GECODE_FLATZINC_EXPORT __attribute__ ((visibility("default")))
75#define GECODE_FLATZINC_EXPORT
81#ifndef GECODE_BUILD_FLATZINC
82#define GECODE_LIBRARY_NAME "FlatZinc"
110 std::vector<std::string> iv_names;
112 std::vector<std::string> bv_names;
113#ifdef GECODE_HAS_FLOAT_VARS
115 std::vector<std::string> fv_names;
117#ifdef GECODE_HAS_SET_VARS
119 std::vector<std::string> sv_names;
122 void printElem(std::ostream& out,
126#ifdef GECODE_HAS_SET_VARS
130#ifdef GECODE_HAS_FLOAT_VARS
135 void printElemDiff(std::ostream& out,
141#ifdef GECODE_HAS_SET_VARS
146#ifdef GECODE_HAS_FLOAT_VARS
159#ifdef GECODE_HAS_SET_VARS
163#ifdef GECODE_HAS_FLOAT_VARS
172#ifdef GECODE_HAS_SET_VARS
176#ifdef GECODE_HAS_FLOAT_VARS
187 const std::string&
intVarName(
int i)
const {
return iv_names[i]; }
189 const std::string&
boolVarName(
int i)
const {
return bv_names[i]; }
190#ifdef GECODE_HAS_FLOAT_VARS
194#ifdef GECODE_HAS_SET_VARS
196 const std::string&
setVarName(
int i)
const {
return sv_names[i]; }
200 std::map<int,int>& iv, std::map<int,int>& bv,
201 std::map<int,int>& sv, std::map<int,int>& fv);
204 int& optVar,
bool optVarIsInt,
207#ifdef GECODE_HAS_SET_VARS
211#ifdef GECODE_HAS_FLOAT_VARS
258#ifdef GECODE_HAS_CPPROFILER
267 _solutions(
"n",
"number of solutions (0 = all, -1 = one/best)",-1),
269 _threads(
"p",
"number of threads (0 = #processing units)",
271 _free(
"f",
"free search, no need to follow search-specification"),
272 _decay(
"decay",
"decay factor",0.99),
275 _node(
"node",
"node cutoff (0 = none, solution mode)"),
276 _fail(
"fail",
"failure cutoff (0 = none, solution mode)"),
277 _time(
"time",
"time (in ms) cutoff (0 = none, solution mode)"),
278 _time_limit(
"t",
"time (in ms) cutoff (0 = none, solution mode)"),
279 _seed(
"r",
"random seed",0),
281 _r_base(
"restart-base",
"base for geometric restart sequence",1.5),
282 _r_scale(
"restart-scale",
"scale factor for restart sequence",250),
283 _r_limit(
"restart-limit",
"restart cutoff (0 = none, solution mode)"),
284 _nogoods(
"nogoods",
"whether to use no-goods from restarts",false),
285 _nogoods_limit(
"nogoods-limit",
"depth limit for no-good extraction",
287 _interrupt(
"interrupt",
"whether to catch Ctrl-C (true) or not (false)",
289 _step(
"step",
"step distance for float optimization",0.0),
291 _stat(
"s",
"emit statistics"),
292 _output(
"o",
"file to send output to")
294#ifdef GECODE_HAS_CPPROFILER
296 _profiler(
"cp-profiler",
"use this execution id and port (comma separated) with CP-profiler")
319#ifdef GECODE_HAS_CPPROFILER
324 void parse(
int& argc,
char* argv[]) {
337 std::cerr <<
"Gecode FlatZinc interpreter" << std::endl
338 <<
" - FlatZinc text interface"
339 << std::endl << std::endl;
347 unsigned int c_d(
void)
const {
return _c_d.value(); }
348 unsigned int a_d(
void)
const {
return _a_d.value(); }
349 unsigned long long int node(
void)
const {
return _node.value(); }
350 unsigned long long int fail(
void)
const {
return _fail.value(); }
376#ifdef GECODE_HAS_CPPROFILER
397 const std::string& rel0,
398 const std::string& rel1,
399 const std::vector<std::string>& n);
402 unsigned int a,
int i,
int n, std::ostream& o)
const;
403#ifdef GECODE_HAS_FLOAT_VARS
407 std::ostream& o)
const;
417 class FlatZincSpaceInitData;
468 template<
template<
class>
class Engine>
470 runEngine(std::ostream& out,
const Printer&
p,
473 template<
template<
class>
class Engine,
474 template<
class,
template<
class>
class>
class Meta>
476 runMeta(std::ostream& out, const
Printer&
p,
479 branchWithPlugin(AST::Node* ann);
498#ifdef GECODE_HAS_FLOAT_VARS
504#ifdef GECODE_HAS_SET_VARS
507#ifdef GECODE_HAS_FLOAT_VARS
525 return static_cast<OnRestartHandle&
>(SharedHandle::operator =(handle));
530 if (
object() ==
nullptr) {
544#ifdef GECODE_HAS_SET_VARS
548#ifdef GECODE_HAS_FLOAT_VARS
564#ifdef GECODE_HAS_SET_VARS
572#ifdef GECODE_HAS_FLOAT_VARS
591 void init(
int intVars,
int boolVars,
int setVars,
int floatVars);
622#ifdef GECODE_HAS_CPPROFILER
663 std::ostream& err = std::cerr);
704#ifdef GECODE_HAS_SET_VARS
711#ifdef GECODE_HAS_FLOAT_VARS
729 const std::string msg;
731 Error(
const std::string& where,
const std::string& what)
732 : msg(where+
": "+what) {}
734 : msg(make_message(where, what, ann)) {}
735 const std::string&
toString(
void)
const {
return msg; }
737 static std::string make_message(
const std::string &where,
const std::string &what,
AST::Array *
const ann) {
738 std::ostringstream result;
739 result << where <<
": " << what;
741 std::vector<std::string> names = get_constraint_names(ann);
742 if (names.size() > 1) {
743 result <<
" in constraints ";
744 for (
unsigned int i = 0; i < names.size(); ++i) {
745 result <<
'\"' << names[i] <<
'\"';
746 if (i < names.size() - 1) {
751 }
else if (names.size() == 1) {
752 result <<
" in constraint " <<
'\"' << names[0] <<
'\"';
757 static std::vector<std::string> get_constraint_names(AST::Array *
const ann) {
758 std::vector<std::string> result;
760 for (
const auto & i : ann->a) {
762 auto nested_result = get_constraint_names(i->getArray());
763 result.insert(result.end(), nested_result.begin(), nested_result.end());
764 }
else if (i->isCall(
"mzn_constraint_name")) {
765 result.emplace_back(i->getCall()->args->getString());
780 Printer& p, std::ostream& err = std::cerr,
790 Printer& p, std::ostream& err = std::cerr,
Base class for script options.
BaseOptions(const char *s)
Initialize options for script with name s.
void add(Driver::BaseOption &o)
Add new option o.
virtual void help(void)
Print help text.
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc).
Passing Boolean variables.
Boolean integer variables.
Base-class for branchers.
Deterministic finite automaton (DFA).
String-valued option (integer value defined by strings).
Unsigned long long integer option.
A node in a FlatZinc abstract syntax tree.
Access to the model-local blackbox context while posting and searching.
Specification for Boolean variables.
Error(const std::string &where, const std::string &what)
Error(const std::string &where, const std::string &what, AST::Array *const ann)
const std::string & toString(void) const
Options for running FlatZinc models
Gecode::Driver::BoolOption _interrupt
Whether to catch SIGINT.
Gecode::Driver::BoolOption _nogoods
Whether to use no-goods.
Gecode::Driver::ProfilerOption _profiler
Use this execution id for the CP-profiler.
Gecode::Driver::BoolOption _allSolutions
Return all solutions.
Gecode::Driver::BoolOption _stat
Emit statistics.
void allSolutions(bool b)
Script mode to run.
bool allSolutions(void) const
Script mode to run.
Gecode::Driver::BoolOption _free
Use free search.
FlatZincOptions(const char *s)
Constructor.
int seed(void) const
Script mode to run.
double decay(void) const
Script mode to run.
unsigned int nogoods_limit(void) const
Script mode to run.
Gecode::Driver::UnsignedIntOption _nogoods_limit
Depth limit for extracting no-goods.
const char * output(void) const
Script mode to run.
RestartMode restart(void) const
Script mode to run.
Gecode::Driver::UnsignedIntOption _r_scale
Restart scale factor.
double restart_base(void) const
Script mode to run.
virtual void help(void)
Script mode to run.
int solutions(void) const
Script mode to run.
bool nogoods(void) const
Script mode to run.
double step(void) const
Script mode to run.
Gecode::Driver::DoubleOption _step
Step option.
Gecode::ScriptMode mode(void) const
Script mode to run.
bool interrupt(void) const
Script mode to run.
Gecode::Driver::StringValueOption _output
Output file.
double time(void) const
Script mode to run.
void parse(int &argc, char *argv[])
Script mode to run.
unsigned int profiler_port(void) const
Script mode to run.
void restart(RestartMode rm)
Script mode to run.
Gecode::Driver::DoubleOption _r_base
Restart base.
Gecode::Driver::UnsignedIntOption _c_d
Copy recomputation distance.
int profiler_id(void) const
Script mode to run.
void restart_scale(int i)
Script mode to run.
Gecode::Driver::UnsignedLongLongIntOption _fail
Cutoff for number of failures.
unsigned long long int fail(void) const
Script mode to run.
Gecode::Driver::DoubleOption _time_limit
Cutoff for time (for compatibility with flatzinc command line).
Gecode::Driver::StringOption _mode
Script mode to run.
unsigned int restart_scale(void) const
Script mode to run.
unsigned int a_d(void) const
Script mode to run.
unsigned int c_d(void) const
Script mode to run.
Gecode::Driver::UnsignedLongLongIntOption _r_limit
Cutoff for number of restarts.
unsigned long long int restart_limit(void) const
Script mode to run.
bool profiler_info(void) const
Script mode to run.
Gecode::Driver::UnsignedIntOption _a_d
Adaptive recomputation distance.
unsigned long long int node(void) const
Script mode to run.
bool free(void) const
Script mode to run.
Gecode::Driver::DoubleOption _time
Cutoff for time.
Gecode::Driver::DoubleOption _threads
How many threads to use.
Gecode::Driver::IntOption _seed
Random seed.
Gecode::Driver::IntOption _solutions
How many solutions.
Gecode::Driver::UnsignedLongLongIntOption _node
Cutoff for number of nodes.
Gecode::Driver::DoubleOption _decay
Decay option.
double threads(void) const
Script mode to run.
Gecode::Driver::StringOption _restart
Restart method option.
void restart_base(double d)
Script mode to run.
std::vector< bool > last_val_bool
Last assigned values for different types of decisions.
std::vector< IntSet > last_val_set
std::vector< FloatVal > last_val_float
std::vector< int > last_val_int
bool on_restart_status
Whether the last on_restart_iv should be set to the restart status.
std::vector< std::pair< int, int > > uniform_range_int
Inclusive ranges to assign random values.
bool mark_complete
Marker set to true if solve process can be marked as complete.
std::vector< std::pair< FloatVal, FloatVal > > uniform_range_float
int on_restart_iv_sol
Number of sol calls for which variables are stored in on_restart_iv. In the array you first find this...
OnRestartHandle(const OnRestartHandle &handle)
virtual ~OnRestartHandle()
A space that can be initialized with a FlatZinc model.
void createBranchers(Printer &p, AST::Node *ann, FlatZincOptions &opt, bool ignoreUnknown, std::ostream &err=std::cerr)
Create branchers corresponding to the solve item annotations.
Gecode::IntVarArray iv_lns
The integer variables used in LNS.
Meth method(void) const
Return whether to solve a satisfaction or optimization problem.
void shrinkArrays(Printer &p)
Remove all variables not needed for output.
void compare(const FlatZincSpace &s, std::ostream &out, const Printer &p) const
Compare this space with space s and print the differences on out using p.
unsigned int _lns
Percentage of variables to keep in LNS (or 0 for no LNS).
void maximize(int var, bool isInt, AST::Array *annotation)
Post that integer variable var should be maximized.
IntVarArgs arg2intvarargs(AST::Node *arg, int offset=0)
Convert arg to IntVarArgs.
Gecode::FloatVarArray fv
The float variables.
Rnd _random
Random number generator.
Gecode::FloatVarArray on_restart_fv
On Restart Tracked Floating Point Variables.
void newBoolVar(BoolVarSpec *vs)
Create new Boolean variable from specification.
FloatVar arg2FloatVar(AST::Node *n)
Convert n to FloatVar.
void print(std::ostream &out, const Printer &p) const
Produce output on out using p.
std::vector< bool > fv_introduced
Indicates whether a float variable is introduced by mzn2fzn.
AST::Array * _solveAnnotations
Annotations on the solve item.
int _optVar
Index of the variable to optimize.
BranchInformation branchInfo
Information for printing branches.
void run(std::ostream &out, const Printer &p, const FlatZincOptions &opt, Gecode::Support::Timer &t_total)
Run the search.
virtual Gecode::Space * copy(void)
Copy function.
DFA getSharedDFA(DFA &a)
Share DFA a if possible.
void newFloatVar(FloatVarSpec *vs)
Create new float variable from specification.
SetVar arg2SetVar(AST::Node *n)
Convert n to SetVar.
int * iv_boolalias
Indicates whether an integer variable aliases a Boolean variable.
void postConstraints(std::vector< ConExpr * > &ces)
Post a constraint specified by ce.
SetVarArgs arg2setvarargs(AST::Node *arg, int offset=0, int doffset=0, const IntSet &od=IntSet::empty)
Convert n to SetVarArgs.
bool _optVarIsInt
Whether variable to optimize is integer (or float).
FlatZincSpace(FlatZincSpace &)
Copy constructor.
Gecode::BoolVarArray bv
The Boolean variables.
void newSetVar(SetVarSpec *vs)
Create new set variable from specification.
bool optVarIsInt(void) const
Return whether variable used for optimization is integer (or float).
Gecode::SetVarArray on_restart_sv
On Restart Tracked Set Variables.
std::vector< bool > iv_introduced
Indicates whether an integer variable is introduced by mzn2fzn.
virtual bool slave(const MetaInfo &mi)
Slave function for restarts.
IntVar arg2IntVar(AST::Node *n)
Convert n to IntVar.
std::string getDomains(const Printer &p) const
Get string representing the domains of variables (for cpprofiler).
FlatZincSpaceInitData * _initData
Initialisation data (only used for posting constraints).
std::vector< bool > sv_introduced
Indicates whether a set variable is introduced by mzn2fzn.
IntSet arg2intset(AST::Node *n)
Convert n to IntSet.
std::vector< bool > bv_introduced
Indicates whether a Boolean variable is introduced by mzn2fzn.
Gecode::FloatVarArray fv_aux
The introduced float variables.
void minimize(int var, bool isInt, AST::Array *annotation)
Post that integer variable var should be minimized.
IntArgs arg2boolargs(AST::Node *arg, int offset=0)
Convert arg (array of Booleans) to IntArgs.
Gecode::SetVarArray sv_aux
The introduced set variables.
int aliasBool2Int(int iv)
Return linked Boolean variable for integer variable iv.
int floatVarCount
Number of float variables.
int boolVarCount
Number of Boolean variables.
Gecode::SetVarArray sv
The set variables.
int setVarCount
Number of set variables.
~FlatZincSpace(void)
Destructor.
OnRestartHandle restart_data
Gecode::IntVarArray on_restart_iv
On Restart Tracked Integer Variables.
int optVar(void) const
Return index of variable used for optimization.
FloatVarArgs arg2floatvarargs(AST::Node *arg, int offset=0)
Convert n to FloatVarArgs.
IntSharedArray arg2boolsharedarray(AST::Node *arg, int offset=0)
Convert arg (array of integers) to IntSharedArray.
void solve(AST::Array *annotation)
Post the solve item.
Gecode::FloatNum step
Step by which a next solution has to have lower cost.
IntSharedArray _lnsInitialSolution
Initial solution to start the LNS (or nullptr for no LNS).
Gecode::BoolVarArray bv_aux
The introduced Boolean variables.
IntSharedArray arg2intsharedarray(AST::Node *arg, int offset=0)
Convert arg (array of integers) to IntSharedArray.
virtual void constrain(const Space &s)
Implement optimization.
BoolVarArgs arg2boolvarargs(AST::Node *arg, int offset=0, int siv=-1)
Convert arg to BoolVarArgs.
void aliasBool2Int(int iv, int bv)
Link integer variable iv to Boolean variable bv.
AST::Array * solveAnnotations(void) const
Return the solve item annotations.
Gecode::IntVarArray iv_aux
The introduced integer variables.
Gecode::IntVarArray iv
The integer variables.
void init(int intVars, int boolVars, int setVars, int floatVars)
Initialize space with given number of variables.
Gecode::BoolVarArray on_restart_bv
On Restart Tracked Boolean Variables.
bool isBoolArray(AST::Node *b, int &singleInt)
Check if b is array of Booleans (or has a single integer).
IntPropLevel ann2ipl(AST::Node *ann)
Convert ann to integer propagation level.
void newIntVar(IntVarSpec *vs)
Create new integer variable from specification.
Meth _method
Whether to solve as satisfaction or optimization problem.
void compare(const Space &s, std::ostream &out) const
Compare this space with space s and print the differences on out.
friend class BlackBoxAccess
int intVarCount
Number of integer variables.
BoolVar arg2BoolVar(AST::Node *n)
Convert n to BoolVar.
TupleSet arg2tupleset(const IntArgs &a, int noOfVars)
Convert a to TupleSet.
IntSetArgs arg2intsetargs(AST::Node *arg, int offset=0)
Convert arg to IntSetArgs.
FlatZincSpace(Rnd &random=defrnd)
Construct empty space.
FloatValArgs arg2floatargs(AST::Node *arg, int offset=0)
Convert n to FloatValArgs.
bool needAuxVars
Whether the introduced variables still need to be copied.
IntArgs arg2intargs(AST::Node *arg, int offset=0)
Convert arg (array of integers) to IntArgs.
Specification for floating point variables.
Specification for integer variables.
Output support class for FlatZinc interpreter.
const std::string & setVarName(int i) const
void addFloatVarName(const std::string &n)
void printDiff(std::ostream &out, const Gecode::IntVarArray &iv1, const Gecode::IntVarArray &iv2, const Gecode::BoolVarArray &bv1, const Gecode::BoolVarArray &bv2, const Gecode::SetVarArray &sv1, const Gecode::SetVarArray &sv2, const Gecode::FloatVarArray &fv1, const Gecode::FloatVarArray &fv2) const
void addSetVarName(const std::string &n)
void init(AST::Array *output)
const std::string & boolVarName(int i) const
void print(std::ostream &out, const Gecode::IntVarArray &iv, const Gecode::BoolVarArray &bv, const Gecode::SetVarArray &sv, const Gecode::FloatVarArray &fv) const
void shrinkArrays(Space &home, int &optVar, bool optVarIsInt, Gecode::IntVarArray &iv, Gecode::BoolVarArray &bv, Gecode::SetVarArray &sv, Gecode::FloatVarArray &fv)
const std::string & intVarName(int i) const
const std::string & floatVarName(int i) const
void addIntVarName(const std::string &n)
void addBoolVarName(const std::string &n)
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)
Specification for set variables.
Value description class for branching.
Passing integer arguments.
static const IntSet empty
Empty set.
Passing integer variables.
SharedHandle(void)
Create shared handle with no object pointing to.
SharedHandle::Object * object(void) const
Access to the shared object.
struct Gecode::Space::@055132133326276162005044145100211202071356247106::@275070317317120154232063063134255170030071110047 p
Data only available during propagation or branching.
Class representing a set of tuples.
#define GECODE_FLATZINC_EXPORT
ScriptMode
Different modes for executing scripts.
RestartMode
Different modes for restart-based search.
@ SM_STAT
Print statistics for script.
@ SM_SOLUTION
Print solution and some statistics.
@ SM_GIST
Run script in Gist.
@ RM_CONSTANT
Restart with constant sequence.
@ RM_LINEAR
Restart with linear sequence.
@ RM_LUBY
Restart with Luby sequence.
@ RM_GEOMETRIC
Restart with geometric sequence.
double FloatNum
Floating point number base type.
IntPropLevel
Propagation levels for integer propagators.
Space(void)
Default constructor.
Interpreter for the FlatZinc language.
GECODE_FLATZINC_EXPORT Rnd defrnd
Uninitialized default random number generator.
GECODE_FLATZINC_EXPORT FlatZincSpace * parse(const std::string &fileName, Printer &p, std::ostream &err=std::cerr, FlatZincSpace *fzs=nullptr, Rnd &rnd=defrnd)
Parse FlatZinc file fileName into fzs and return it.
Gecode toplevel namespace
ArgArray< IntSet > IntSetArgs
Passing set arguments.
SharedArray< int > IntSharedArray
Arrays of integers that can be shared among several element constraints.
#define GECODE_VTABLE_EXPORT