Generated on Mon Feb 8 2021 00:00:00 for Gecode by doxygen 1.8.20
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  * This file is part of Gecode, the generic constraint
10  * development environment:
11  * http://www.gecode.org
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining
14  * a copy of this software and associated documentation files (the
15  * "Software"), to deal in the Software without restriction, including
16  * without limitation the rights to use, copy, modify, merge, publish,
17  * distribute, sublicense, and/or sell copies of the Software, and to
18  * permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be
22  * included in all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31  *
32  */
33 
34 #include <gecode/driver.hh>
35 #include <gecode/int.hh>
36 #include <gecode/minimodel.hh>
37 
38 using namespace Gecode;
39 
48 class Donald : public Script {
49 private:
51  static const int nl = 10;
53  IntVarArray le;
54 public:
56  enum {
58  MODEL_CARRY
59  };
61  Donald(const Options& opt)
62  : Script(opt), le(*this,nl,0,9) {
63  IntVar
64  d(le[0]), o(le[1]), n(le[2]), a(le[3]), l(le[4]),
65  g(le[5]), e(le[6]), r(le[7]), b(le[8]), t(le[9]);
66  rel(*this, d, IRT_NQ, 0);
67  rel(*this, g, IRT_NQ, 0);
68  rel(*this, r, IRT_NQ, 0);
69 
70  distinct(*this, le, opt.ipl());
71 
72  switch (opt.model()) {
73  case MODEL_SINGLE:
74  rel(*this, 100000*d+10000*o+1000*n+100*a+10*l+d
75  + 100000*g+10000*e+1000*r+100*a+10*l+d
76  == 100000*r+10000*o+1000*b+100*e+10*r+t,
77  opt.ipl());
78  break;
79  case MODEL_CARRY:
80  {
81  IntVar c0(*this,0,1), c1(*this,0,1), c2(*this,0,1),
82  c3(*this,0,1), c4(*this,0,1);
83  rel(*this, d+d == t+10*c0, opt.ipl());
84  rel(*this, c0+l+l == r+10*c1, opt.ipl());
85  rel(*this, c1+a+a == e+10*c2, opt.ipl());
86  rel(*this, c2+n+r == b+10*c3, opt.ipl());
87  rel(*this, c3+o+e == o+10*c4, opt.ipl());
88  rel(*this, c4+d+g == r, opt.ipl());
89  }
90  break;
91  default: GECODE_NEVER;
92  }
93 
94  branch(*this, le, INT_VAR_SIZE_MIN(), INT_VAL_MAX());
95  }
97  Donald(Donald& s) : Script(s) {
98  le.update(*this, s.le);
99  }
101  virtual Space*
102  copy(void) {
103  return new Donald(*this);
104  }
106  virtual void
107  print(std::ostream& os) const {
108  os << "\t" << le << std::endl;;
109  }
110 
111 };
112 
113 
117 int
118 main(int argc, char* argv[]) {
119  Options opt("Donald+Gerald=Robert");
121  opt.model(Donald::MODEL_SINGLE, "single", "use single linear equation");
122  opt.model(Donald::MODEL_CARRY, "carry", "use carry");
123  opt.solutions(0);
124  opt.iterations(1500);
125  opt.parse(argc,argv);
126  Script::run<Donald,DFS,Options>(opt);
127  return 0;
128 }
129 
130 // STATISTICS: example-any
131 
@ MODEL_CARRY
Use carries.
Definition: donald.cpp:58
NodeType t
Type of node.
Definition: bool-expr.cpp:230
Donald(Donald &s)
Constructor for cloning s.
Definition: donald.cpp:97
Computation spaces.
Definition: core.hpp:1742
void 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:39
void iterations(unsigned int i)
Set default number of iterations.
Definition: options.hpp:461
Integer variable array.
Definition: int.hh:763
Donald(const Options &opt)
Actual model.
Definition: donald.cpp:61
Gecode toplevel namespace
Options opt
The options.
Definition: test.cpp:97
Example: DONALD+GERALD=ROBERT puzzle
Definition: donald.cpp:48
Options for scripts
Definition: driver.hh:366
Parametric base-class for scripts.
Definition: driver.hh:729
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:767
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:548
struct Gecode::@602::NNF::@65::@66 b
For binary nodes (and, or, eqv)
Integer variables.
Definition: int.hh:371
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:56
struct Gecode::@602::NNF::@65::@67 a
For atomic nodes.
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl)
Select variable with smallest domain size.
Definition: var.hpp:206
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:43
NNF * l
Left subtree.
Definition: bool-expr.cpp:240
Gecode::IntSet d(v, 7)
int main(int argc, char *argv[])
Main-function.
Definition: donald.cpp:118
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl)
Post propagator for for all .
Definition: distinct.cpp:46
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition: options.hpp:283
@ IRT_NQ
Disequality ( )
Definition: int.hh:927
void model(int v)
Set default model value.
Definition: options.hpp:177
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
virtual void print(std::ostream &os) const
Print solution.
Definition: donald.cpp:107
virtual Space * copy(void)
Copy during cloning.
Definition: donald.cpp:102
IntValBranch INT_VAL_MAX(void)
Select largest value.
Definition: val.hpp:65
void update(Space &home, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1013
@ MODEL_SINGLE
Use single linear equation.
Definition: donald.cpp:57