Generated on Tue Jan 26 2021 00:00:00 for Gecode by doxygen 1.9.1
money.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 
49 class Money : public Script {
50 protected:
52  static const int nl = 8;
55 public:
57  enum {
59  MODEL_CARRY
60  };
62  Money(const Options& opt) : Script(opt), le(*this,nl,0,9) {
63  IntVar
64  s(le[0]), e(le[1]), n(le[2]), d(le[3]),
65  m(le[4]), o(le[5]), r(le[6]), y(le[7]);
66 
67  if (opt.trace()) {
68  trace(*this, le, opt.trace());
69  trace(*this, opt.trace());
70  }
71 
72  rel(*this, s, IRT_NQ, 0);
73  rel(*this, m, IRT_NQ, 0);
74 
75  distinct(*this, le, opt.ipl());
76 
77  switch (opt.model()) {
78  case MODEL_SINGLE:
79  rel(*this, 1000*s+100*e+10*n+d
80  + 1000*m+100*o+10*r+e
81  == 10000*m+1000*o+100*n+10*e+y,
82  opt.ipl());
83  break;
84  case MODEL_CARRY:
85  {
86  IntVar c0(*this,0,1), c1(*this,0,1), c2(*this,0,1), c3(*this,0,1);
87  rel(*this, d+e == y+10*c0, opt.ipl());
88  rel(*this, c0+n+r == e+10*c1, opt.ipl());
89  rel(*this, c1+e+o == n+10*c2, opt.ipl());
90  rel(*this, c2+s+m == o+10*c3, opt.ipl());
91  rel(*this, c3 == m, opt.ipl());
92  }
93  break;
94  default: GECODE_NEVER;
95  }
96 
97  branch(*this, le, INT_VAR_SIZE_MIN(), INT_VAL_MIN(), nullptr,
98  [](const Space&, const Brancher&, unsigned int a,
99  IntVar, int i, const int& n,
100  std::ostream& o) {
101  static char name[8] = {'s','e','n','d',
102  'm','o','r','y'};
103  o << name[i]
104  << ((a == 0) ? " = " : " != ")
105  << n;
106  });
107  }
109  virtual void
110  print(std::ostream& os) const {
111  os << "\t" << le << std::endl;
112  }
113 
115  Money(Money& s) : Script(s) {
116  le.update(*this, s.le);
117  }
119  virtual Space*
120  copy(void) {
121  return new Money(*this);
122  }
123 };
124 
128 int
129 main(int argc, char* argv[]) {
130  Options opt("SEND+MORE=MONEY");
132  opt.model(Money::MODEL_SINGLE, "single", "use single linear equation");
133  opt.model(Money::MODEL_CARRY, "carry", "use carry");
134  opt.solutions(0);
135  opt.iterations(20000);
136  opt.parse(argc,argv);
137  Script::run<Money,DFS,Options>(opt);
138  return 0;
139 }
140 
141 // STATISTICS: example-any
142 
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
struct Gecode::@602::NNF::@65::@67 a
For atomic nodes.
NNF * r
Right subtree.
Definition: bool-expr.cpp:242
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:548
Base-class for branchers.
Definition: core.hpp:1442
Parametric base-class for scripts.
Definition: driver.hh:729
Integer variable array.
Definition: int.hh:763
Integer variables.
Definition: int.hh:371
Options for scripts
Definition: driver.hh:366
void iterations(unsigned int i)
Set default number of iterations.
Definition: options.hpp:461
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition: options.hpp:283
void model(int v)
Set default model value.
Definition: options.hpp:177
Computation spaces.
Definition: core.hpp:1742
void update(Space &home, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1013
Example: SEND+MORE=MONEY puzzle
Definition: money.cpp:49
int main(int argc, char *argv[])
Main-function.
Definition: money.cpp:129
IntVarArray le
Array of letters.
Definition: money.cpp:54
Money(Money &s)
Constructor for cloning s.
Definition: money.cpp:115
@ MODEL_CARRY
Use carries.
Definition: money.cpp:59
@ MODEL_SINGLE
Use single linear equation.
Definition: money.cpp:58
virtual Space * copy(void)
Copy during cloning.
Definition: money.cpp:120
Money(const Options &opt)
Actual model.
Definition: money.cpp:62
virtual void print(std::ostream &os) const
Print solution.
Definition: money.cpp:110
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl)
Post propagator for for all .
Definition: distinct.cpp:46
Post propagator for SetVar SetOpType SetVar y
Definition: set.hh:767
void trace(Home home, const FloatVarArgs &x, TraceFilter tf, int te, FloatTracer &t)
Create a tracer for float variables.
Definition: trace.cpp:39
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:43
@ IRT_NQ
Disequality ( )
Definition: int.hh:927
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition: val.hpp:55
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl)
Select variable with smallest domain size.
Definition: var.hpp:206
Gecode::IntArgs i({1, 2, 3, 4})
Gecode::IntSet d(v, 7)
Options opt
The options.
Definition: test.cpp:97
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:56