Generated on Thu Mar 7 2013 10:21:16 for Gecode by doxygen 1.8.3.1
flatzinc.hh
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Guido Tack <tack@gecode.org>
5  *
6  * Copyright:
7  * Guido Tack, 2007
8  *
9  * Last modified:
10  * $Date: 2012-03-21 16:25:08 +1100 (Wed, 21 Mar 2012) $ by $Author: tack $
11  * $Revision: 12605 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #ifndef __GECODE_FLATZINC_HH__
39 #define __GECODE_FLATZINC_HH__
40 
41 #include <iostream>
42 
43 #include <gecode/kernel.hh>
44 #include <gecode/int.hh>
45 #ifdef GECODE_HAS_SET_VARS
46 #include <gecode/set.hh>
47 #endif
48 
49 #include <map>
50 
51 /*
52  * Support for DLLs under Windows
53  *
54  */
55 
56 #if !defined(GECODE_STATIC_LIBS) && \
57  (defined(__CYGWIN__) || defined(__MINGW32__) || defined(_MSC_VER))
58 
59 #ifdef GECODE_BUILD_FLATZINC
60 #define GECODE_FLATZINC_EXPORT __declspec( dllexport )
61 #else
62 #define GECODE_FLATZINC_EXPORT __declspec( dllimport )
63 #endif
64 
65 #else
66 
67 #ifdef GECODE_GCC_HAS_CLASS_VISIBILITY
68 
69 #define GECODE_FLATZINC_EXPORT __attribute__ ((visibility("default")))
70 
71 #else
72 
73 #define GECODE_FLATZINC_EXPORT
74 
75 #endif
76 #endif
77 
78 // Configure auto-linking
79 #ifndef GECODE_BUILD_FLATZINC
80 #define GECODE_LIBRARY_NAME "FlatZinc"
82 #endif
83 
84 #include <gecode/driver.hh>
85 
87 #include <gecode/flatzinc/ast.hh>
89 
99 namespace Gecode { namespace FlatZinc {
100 
106  private:
107  AST::Array* _output;
108  void printElem(std::ostream& out,
109  AST::Node* ai,
110  const Gecode::IntVarArray& iv,
111  const Gecode::BoolVarArray& bv
112 #ifdef GECODE_HAS_SET_VARS
113  ,
114  const Gecode::SetVarArray& sv
115 #endif
116  ) const;
117  public:
118  Printer(void) : _output(NULL) {}
119  void init(AST::Array* output);
120 
121  void print(std::ostream& out,
122  const Gecode::IntVarArray& iv,
123  const Gecode::BoolVarArray& bv
124 #ifdef GECODE_HAS_SET_VARS
125  ,
126  const Gecode::SetVarArray& sv
127 #endif
128  ) const;
129 
130  ~Printer(void);
131 
132  void shrinkElement(AST::Node* node,
133  std::map<int,int>& iv, std::map<int,int>& bv,
134  std::map<int,int>& sv);
135 
136  void shrinkArrays(Space& home,
137  int& optVar,
140 #ifdef GECODE_HAS_SET_VARS
141  ,
143 #endif
144  );
145 
146  private:
147  Printer(const Printer&);
148  Printer& operator=(const Printer&);
149  };
150 
156  protected:
158 
170 
171 
173 
177 
178  public:
180  FZ_SEARCH_BAB, //< Branch-and-bound search
181  FZ_SEARCH_RESTART //< Restart search
182  };
184  FlatZincOptions(const char* s)
185  : Gecode::BaseOptions(s),
186  _solutions("-n","number of solutions (0 = all)",1),
187  _allSolutions("-a", "return all solutions (equal to -solutions 0)"),
188  _threads("-p","number of threads (0 = #processing units)",
189  Gecode::Search::Config::threads),
190  _free("--free", "no need to follow search-specification"),
191  _search("-search","search engine variant", FZ_SEARCH_BAB),
192  _c_d("-c-d","recomputation commit distance",Gecode::Search::Config::c_d),
193  _a_d("-a-d","recomputation adaption distance",Gecode::Search::Config::a_d),
194  _node("-node","node cutoff (0 = none, solution mode)"),
195  _fail("-fail","failure cutoff (0 = none, solution mode)"),
196  _time("-time","time (in ms) cutoff (0 = none, solution mode)"),
197  _seed("-r","random seed",0),
198  _mode("-mode","how to execute script",Gecode::SM_SOLUTION),
199  _stat("-s","emit statistics"),
200  _output("-o","file to send output to") {
201 
202  _search.add(FZ_SEARCH_BAB, "bab");
203  _search.add(FZ_SEARCH_RESTART, "restart");
204  _mode.add(Gecode::SM_SOLUTION, "solution");
205  _mode.add(Gecode::SM_STAT, "stat");
206  _mode.add(Gecode::SM_GIST, "gist");
209  add(_free);
210  add(_search);
211  add(_node); add(_fail); add(_time);
212  add(_seed);
213  add(_mode); add(_stat);
214  add(_output);
215  }
216 
217  void parse(int& argc, char* argv[]) {
218  Gecode::BaseOptions::parse(argc,argv);
219  if (_allSolutions.value()) {
220  _solutions.value(0);
221  }
222  if (_stat.value())
224  }
225 
226  virtual void help(void) {
227  std::cerr << "Gecode FlatZinc interpreter" << std::endl
228  << " - Supported FlatZinc version: " << GECODE_FLATZINC_VERSION
229  << std::endl << std::endl;
231  }
232 
233  unsigned int solutions(void) const { return _solutions.value(); }
234  bool allSolutions(void) const { return _allSolutions.value(); }
235  double threads(void) const { return _threads.value(); }
236  bool free(void) const { return _free.value(); }
237  SearchOptions search(void) const {
238  return static_cast<SearchOptions>(_search.value());
239  }
240  unsigned int c_d(void) const { return _c_d.value(); }
241  unsigned int a_d(void) const { return _a_d.value(); }
242  unsigned int node(void) const { return _node.value(); }
243  unsigned int fail(void) const { return _fail.value(); }
244  unsigned int time(void) const { return _time.value(); }
245  int seed(void) const { return _seed.value(); }
246  const char* output(void) const { return _output.value(); }
247  Gecode::ScriptMode mode(void) const {
248  return static_cast<Gecode::ScriptMode>(_mode.value());
249  }
250  };
251 
257  public:
258  enum Meth {
259  SAT, //< Solve as satisfaction problem
260  MIN, //< Solve as minimization problem
261  MAX //< Solve as maximization problem
262  };
263  protected:
270 
272  int _optVar;
273 
276 
279 
281  FlatZincSpace(bool share, FlatZincSpace&);
282  private:
284  template<template<class> class Engine>
285  void
286  runEngine(std::ostream& out, const Printer& p,
287  const FlatZincOptions& opt, Gecode::Support::Timer& t_total);
288  void
289  branchWithPlugin(AST::Node* ann);
290  public:
294  std::vector<bool> iv_introduced;
300  std::vector<bool> bv_introduced;
301 #ifdef GECODE_HAS_SET_VARS
302 
305  std::vector<bool> sv_introduced;
306 #endif
307 
308  FlatZincSpace(void);
309 
311  ~FlatZincSpace(void);
312 
314  void init(int intVars, int boolVars, int setVars);
315 
317  void newIntVar(IntVarSpec* vs);
319  void aliasBool2Int(int iv, int bv);
321  int aliasBool2Int(int iv);
323  void newBoolVar(BoolVarSpec* vs);
325  void newSetVar(SetVarSpec* vs);
326 
328  void postConstraint(const ConExpr& ce, AST::Node* annotation);
329 
331  void solve(AST::Array* annotation);
333  void minimize(int var, AST::Array* annotation);
335  void maximize(int var, AST::Array* annotation);
336 
338  void run(std::ostream& out, const Printer& p,
339  const FlatZincOptions& opt, Gecode::Support::Timer& t_total);
340 
342  void print(std::ostream& out, const Printer& p) const;
343 
352  void shrinkArrays(Printer& p);
353 
355  Meth method(void) const;
356 
358  int optVar(void) const;
359 
369  void createBranchers(AST::Node* ann,
370  int seed,
371  bool ignoreUnknown,
372  std::ostream& err = std::cerr);
373 
375  AST::Array* solveAnnotations(void) const;
376 
378  virtual void constrain(const Space& s);
380  virtual Gecode::Space* copy(bool share);
381  };
382 
384  class Error {
385  private:
386  const std::string msg;
387  public:
388  Error(const std::string& where, const std::string& what)
389  : msg(where+": "+what) {}
390  const std::string& toString(void) const { return msg; }
391  };
392 
399  FlatZincSpace* parse(const std::string& fileName,
400  Printer& p, std::ostream& err = std::cerr,
401  FlatZincSpace* fzs=NULL);
402 
409  FlatZincSpace* parse(std::istream& is,
410  Printer& p, std::ostream& err = std::cerr,
411  FlatZincSpace* fzs=NULL);
412 
413 }}
414 
415 #endif
416 
417 // STATISTICS: flatzinc-any