Generated on Sat Feb 7 2015 02:01:11 for Gecode by doxygen 1.8.9.1
donald.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2001
8  *
9  * Last modified:
10  * $Date: 2013-07-08 14:22:40 +0200 (Mon, 08 Jul 2013) $ by $Author: schulte $
11  * $Revision: 13820 $
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 #include <gecode/driver.hh>
39 #include <gecode/int.hh>
40 #include <gecode/minimodel.hh>
41 
42 using namespace Gecode;
43 
52 class Donald : public Script {
53 private:
55  static const int nl = 10;
57  IntVarArray le;
58 public:
60  enum {
62  MODEL_CARRY
63  };
65  Donald(const Options& opt) :
66  le(*this,nl,0,9) {
67  IntVar
68  d(le[0]), o(le[1]), n(le[2]), a(le[3]), l(le[4]),
69  g(le[5]), e(le[6]), r(le[7]), b(le[8]), t(le[9]);
70  rel(*this, d, IRT_NQ, 0);
71  rel(*this, g, IRT_NQ, 0);
72  rel(*this, r, IRT_NQ, 0);
73 
74  distinct(*this, le, opt.icl());
75 
76  switch (opt.model()) {
77  case MODEL_SINGLE:
78  rel(*this, 100000*d+10000*o+1000*n+100*a+10*l+d
79  + 100000*g+10000*e+1000*r+100*a+10*l+d
80  == 100000*r+10000*o+1000*b+100*e+10*r+t,
81  opt.icl());
82  break;
83  case MODEL_CARRY:
84  {
85  IntVar c0(*this,0,1), c1(*this,0,1), c2(*this,0,1),
86  c3(*this,0,1), c4(*this,0,1);
87  rel(*this, d+d == t+10*c0, opt.icl());
88  rel(*this, c0+l+l == r+10*c1, opt.icl());
89  rel(*this, c1+a+a == e+10*c2, opt.icl());
90  rel(*this, c2+n+r == b+10*c3, opt.icl());
91  rel(*this, c3+o+e == o+10*c4, opt.icl());
92  rel(*this, c4+d+g == r, opt.icl());
93  }
94  break;
95  default: GECODE_NEVER;
96  }
97 
98  branch(*this, le, INT_VAR_SIZE_MIN(), INT_VAL_MAX());
99  }
101  Donald(bool share, Donald& s) : Script(share,s) {
102  le.update(*this, share, s.le);
103  }
105  virtual Space*
106  copy(bool share) {
107  return new Donald(share,*this);
108  }
110  virtual void
111  print(std::ostream& os) const {
112  os << "\t" << le << std::endl;;
113  }
114 
115 };
116 
117 
121 int
122 main(int argc, char* argv[]) {
123  Options opt("Donald+Gerald=Robert");
125  opt.model(Donald::MODEL_SINGLE, "single", "use single linear equation");
126  opt.model(Donald::MODEL_CARRY, "carry", "use carry");
127  opt.solutions(0);
128  opt.iterations(1500);
129  opt.parse(argc,argv);
130  Script::run<Donald,DFS,Options>(opt);
131  return 0;
132 }
133 
134 // STATISTICS: example-any
135 
virtual void print(std::ostream &os) const
Print solution.
Definition: donald.cpp:111
NodeType t
Type of node.
Definition: bool-expr.cpp:234
NNF * l
Left subtree.
Definition: bool-expr.cpp:244
Donald(const Options &opt)
Actual model.
Definition: donald.cpp:65
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl)
Select variable with smallest domain size.
Definition: var.hpp:212
Integer variable array.
Definition: int.hh:741
Computation spaces.
Definition: core.hpp:1362
Parametric base-class for scripts.
Definition: driver.hh:622
void iterations(unsigned int i)
Set default number of iterations.
Definition: options.hpp:385
virtual Space * copy(bool share)
Copy during cloning.
Definition: donald.cpp:106
Gecode::IntSet d(v, 7)
void update(Space &, bool share, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1072
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Options opt
The options.
Definition: test.cpp:101
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
Use single linear equation.
Definition: donald.cpp:61
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:331
IntValBranch INT_VAL_MAX(void)
Select largest value.
Definition: val.hpp:78
Example: DONALD+GERALD=ROBERT puzzle
Definition: donald.cpp:52
Use carries.
Definition: donald.cpp:62
Integer variables.
Definition: int.hh:350
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
void distinct(Home home, const IntVarArgs &x, IntConLevel icl)
Post propagator for for all .
Definition: distinct.cpp:47
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition: options.hpp:243
void model(int v)
Set default model value.
Definition: options.hpp:155
struct Gecode::@518::NNF::@57::@58 b
For binary nodes (and, or, eqv)
Gecode toplevel namespace
Donald(bool share, Donald &s)
Constructor for cloning s.
Definition: donald.cpp:101
Disequality ( )
Definition: int.hh:905
BrancherHandle branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf, FloatVarValPrint vvp)
Branch over x with variable selection vars and value selection vals.
Definition: branch.cpp:43
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
Options for scripts
Definition: driver.hh:326
void icl(IntConLevel i)
Set default integer consistency level.
Definition: options.hpp:194
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.
int main(int argc, char *argv[])
Main-function.
Definition: donald.cpp:122