Generated on Mon Sep 17 2012 22:20:32 for Gecode by doxygen 1.8.1.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  * Last modified:
10  * $Date: 2010-10-07 20:52:01 +1100 (Thu, 07 Oct 2010) $ by $Author: schulte $
11  * $Revision: 11473 $
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 
53 class Money : public Script {
54 protected:
56  static const int nl = 8;
59 public:
61  enum {
63  MODEL_CARRY
64  };
66  Money(const Options& opt) : le(*this,nl,0,9) {
67  IntVar
68  s(le[0]), e(le[1]), n(le[2]), d(le[3]),
69  m(le[4]), o(le[5]), r(le[6]), y(le[7]);
70 
71  rel(*this, s, IRT_NQ, 0);
72  rel(*this, m, IRT_NQ, 0);
73 
74  distinct(*this, le, opt.icl());
75 
76  switch (opt.model()) {
77  case MODEL_SINGLE:
78  rel(*this, 1000*s+100*e+10*n+d
79  + 1000*m+100*o+10*r+e
80  == 10000*m+1000*o+100*n+10*e+y,
81  opt.icl());
82  break;
83  case MODEL_CARRY:
84  {
85  IntVar c0(*this,0,1), c1(*this,0,1), c2(*this,0,1), c3(*this,0,1);
86  rel(*this, d+e == y+10*c0, opt.icl());
87  rel(*this, c0+n+r == e+10*c1, opt.icl());
88  rel(*this, c1+e+o == n+10*c2, opt.icl());
89  rel(*this, c2+s+m == o+10*c3, opt.icl());
90  rel(*this, c3 == m, opt.icl());
91  }
92  break;
93  default: GECODE_NEVER;
94  }
95 
96  branch(*this, le, INT_VAR_SIZE_MIN, INT_VAL_MIN);
97  }
99  virtual void
100  print(std::ostream& os) const {
101  os << "\t" << le << std::endl;
102  }
103 
105  Money(bool share, Money& s) : Script(share,s) {
106  le.update(*this, share, s.le);
107  }
109  virtual Space*
110  copy(bool share) {
111  return new Money(share,*this);
112  }
113 };
114 
118 int
119 main(int argc, char* argv[]) {
120  Options opt("SEND+MORE=MONEY");
122  opt.model(Money::MODEL_SINGLE, "single", "use single linear equation");
123  opt.model(Money::MODEL_CARRY, "carry", "use carry");
124  opt.solutions(0);
125  opt.iterations(20000);
126  opt.parse(argc,argv);
127  Script::run<Money,DFS,Options>(opt);
128  return 0;
129 }
130 
131 // STATISTICS: example-any
132