Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.0
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
38using namespace Gecode;
39
48class Donald : public Script {
49private:
51 static const int nl = 10;
53 IntVarArray le;
54public:
56 enum {
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
117int
118main(int argc, char* argv[]) {
119 Options opt("Donald+Gerald=Robert");
120 opt.model(Donald::MODEL_SINGLE);
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);
127 return 0;
128}
129
130// STATISTICS: example-any
131
NNF * l
Left subtree.
struct Gecode::@603::NNF::@65::@66 b
For binary nodes (and, or, eqv)
NodeType t
Type of node.
int n
Number of negative literals for node type.
struct Gecode::@603::NNF::@65::@67 a
For atomic nodes.
Example: DONALD+GERALD=ROBERT puzzle
Definition donald.cpp:48
Donald(const Options &opt)
Actual model.
Definition donald.cpp:61
int main(int argc, char *argv[])
Main-function.
Definition donald.cpp:118
Donald(Donald &s)
Constructor for cloning s.
Definition donald.cpp:97
virtual void print(std::ostream &os) const
Print solution.
Definition donald.cpp:107
@ MODEL_CARRY
Use carries.
Definition donald.cpp:58
@ MODEL_SINGLE
Use single linear equation.
Definition donald.cpp:57
virtual Space * copy(void)
Copy during cloning.
Definition donald.cpp:102
Parametric base-class for scripts.
Definition driver.hh:729
static void run(const Options &opt, Script *s=NULL)
Definition script.hpp:290
Integer variable array.
Definition int.hh:763
Integer variables.
Definition int.hh:371
Options for scripts
Definition driver.hh:366
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
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Branch over x with variable selection vars and value selection vals.
Definition branch.cpp:39
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
@ IRT_NQ
Disequality ( )
Definition int.hh:927
Gecode toplevel namespace
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition set.hh:767
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl=IPL_DEF)
Post propagator for for all .
Definition distinct.cpp:46
IntValBranch INT_VAL_MAX(void)
Select largest value.
Definition val.hpp:65
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl=nullptr)
Select variable with smallest domain size.
Definition var.hpp:206
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56