main page
modules
namespaces
classes
files
Gecode home
Generated on Tue Jan 28 2020 00:00:00 for Gecode by
doxygen
1.8.17
examples
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: 2017-03-17 23:04:57 +0100 (Fri, 17 Mar 2017) $ by $Author: schulte $
11
* $Revision: 15597 $
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;
58
IntVarArray
le
;
59
public
:
61
enum
{
62
MODEL_SINGLE
,
63
MODEL_CARRY
64
};
66
Money
(
const
Options
&
opt
) :
Script
(
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
if
(
opt
.trace()) {
72
trace
(*
this
, le,
opt
.trace());
73
trace
(*
this
,
opt
.trace());
74
}
75
76
rel
(*
this
, s,
IRT_NQ
, 0);
77
rel
(*
this
, m,
IRT_NQ
, 0);
78
79
distinct
(*
this
, le,
opt
.ipl());
80
81
switch
(
opt
.model()) {
82
case
MODEL_SINGLE:
83
rel
(*
this
, 1000*s+100*e+10*
n
+
d
84
+ 1000*m+100*o+10*
r
+e
85
== 10000*m+1000*o+100*
n
+10*e+
y
,
86
opt
.ipl());
87
break
;
88
case
MODEL_CARRY:
89
{
90
IntVar
c0(*
this
,0,1), c1(*
this
,0,1), c2(*
this
,0,1), c3(*
this
,0,1);
91
rel
(*
this
,
d
+e ==
y
+10*c0,
opt
.ipl());
92
rel
(*
this
, c0+
n
+
r
== e+10*c1,
opt
.ipl());
93
rel
(*
this
, c1+e+o ==
n
+10*c2,
opt
.ipl());
94
rel
(*
this
, c2+s+m == o+10*c3,
opt
.ipl());
95
rel
(*
this
, c3 == m,
opt
.ipl());
96
}
97
break
;
98
default
:
GECODE_NEVER
;
99
}
100
101
branch
(*
this
, le,
INT_VAR_SIZE_MIN
(),
INT_VAL_MIN
());
102
}
104
virtual
void
105
print
(std::ostream& os)
const
{
106
os <<
"\t"
<< le << std::endl;
107
}
108
110
Money
(
bool
share,
Money
& s) :
Script
(share,s) {
111
le.
update
(*
this
, share, s.
le
);
112
}
114
virtual
Space
*
115
copy
(
bool
share) {
116
return
new
Money
(share,*
this
);
117
}
118
};
119
123
int
124
main
(
int
argc,
char
* argv[]) {
125
Options
opt
(
"SEND+MORE=MONEY"
);
126
opt
.
model
(
Money::MODEL_SINGLE
);
127
opt
.
model
(
Money::MODEL_SINGLE
,
"single"
,
"use single linear equation"
);
128
opt
.
model
(
Money::MODEL_CARRY
,
"carry"
,
"use carry"
);
129
opt
.
solutions
(0);
130
opt
.
iterations
(20000);
131
opt
.
parse
(argc,argv);
132
Script::run<Money,DFS,Options>(
opt
);
133
return
0;
134
}
135
136
// STATISTICS: example-any
137
Money::copy
virtual Space * copy(bool share)
Copy during cloning.
Definition:
money.cpp:115
Gecode::y
Post propagator for SetVar SetOpType SetVar y
Definition:
set.hh:784
minimodel.hh
int.hh
Money::le
IntVarArray le
Array of letters.
Definition:
money.cpp:58
Gecode::VarArray::update
void update(Space &, bool share, VarArray< Var > &a)
Update array to be a clone of array a.
Definition:
array.hpp:1072
Gecode::Space
Computation spaces.
Definition:
core.hpp:1748
Gecode::branch
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:43
Gecode::Options::iterations
void iterations(unsigned int i)
Set default number of iterations.
Definition:
options.hpp:465
Gecode::INT_VAL_MIN
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition:
val.hpp:59
Gecode::IntVarArray
Integer variable array.
Definition:
int.hh:744
driver.hh
Gecode
Gecode toplevel namespace
Test::opt
Options opt
The options.
Definition:
test.cpp:101
Gecode::Options
Options for scripts
Definition:
driver.hh:366
Money::Money
Money(const Options &opt)
Actual model.
Definition:
money.cpp:66
Gecode::Driver::ScriptBase
Parametric base-class for scripts.
Definition:
driver.hh:703
Gecode::r
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition:
set.hh:784
Gecode::BaseOptions::parse
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition:
options.cpp:510
Gecode::IntVar
Integer variables.
Definition:
int.hh:353
GECODE_NEVER
#define GECODE_NEVER
Assert that this command is never executed.
Definition:
macros.hpp:60
Gecode::INT_VAR_SIZE_MIN
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl)
Select variable with smallest domain size.
Definition:
var.hpp:210
Gecode::rel
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition:
rel.cpp:47
Money::print
virtual void print(std::ostream &os) const
Print solution.
Definition:
money.cpp:105
Test::Int::Distinct::d
Gecode::IntSet d(v, 7)
Money::main
int main(int argc, char *argv[])
Main-function.
Definition:
money.cpp:124
Gecode::trace
void trace(Home home, const FloatVarArgs &x, TraceFilter tf, int te, FloatTracer &t)
Create a tracer for float variables.
Definition:
trace.cpp:43
Money::MODEL_SINGLE
@ MODEL_SINGLE
Use single linear equation.
Definition:
money.cpp:62
Gecode::distinct
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl)
Post propagator for for all .
Definition:
distinct.cpp:50
Gecode::Options::solutions
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition:
options.hpp:287
Gecode::IRT_NQ
@ IRT_NQ
Disequality ( )
Definition:
int.hh:908
Gecode::Options::model
void model(int v)
Set default model value.
Definition:
options.hpp:181
n
int n
Number of negative literals for node type.
Definition:
bool-expr.cpp:238
Money
Example: SEND+MORE=MONEY puzzle
Definition:
money.cpp:53
Money::Money
Money(bool share, Money &s)
Constructor for cloning s.
Definition:
money.cpp:110
Money::MODEL_CARRY
@ MODEL_CARRY
Use carries.
Definition:
money.cpp:63