Generated on Sat Jan 12 2019 20:58:51 for Gecode by doxygen 1.8.13
grocery.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 
56 class Grocery : public Script {
57 protected:
61  static const int s = 711;
63  static const int p = 711 * 100 * 100 * 100;
64 public:
67  : Script(opt), abcd(*this,4,0,s) {
68  IntVar a(abcd[0]), b(abcd[1]), c(abcd[2]), d(abcd[3]);
69 
70  // The sum of all variables is s
71  rel(*this, a+b+c+d == s, opt.ipl());
72 
73  // The product of all variables is s (corrected by scale factor)
74  rel(*this, (a*b)*(c*d) == p, opt.ipl());
75 
76  // Break symmetries: order the variables
77  rel(*this, abcd, IRT_LQ);
78 
79  branch(*this, abcd, INT_VAR_NONE(), INT_VAL_SPLIT_MAX());
80  }
81 
83  Grocery(Grocery& s) : Script(s) {
84  abcd.update(*this, s.abcd);
85  }
86 
88  virtual Space*
89  copy(void) {
90  return new Grocery(*this);
91  }
92 
94  virtual void
95  print(std::ostream& os) const {
96  os << "\t" << abcd << std::endl;
97  }
98 };
99 
103 int
104 main(int argc, char* argv[]) {
105  Options opt("Grocery");
106  opt.iterations(20);
107  opt.parse(argc,argv);
108  Script::run<Grocery,DFS,Options>(opt);
109  return 0;
110 }
111 
112 // STATISTICS: example-any
113 
Example: Grocery puzzle
Definition: grocery.cpp:56
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition: var.hpp:96
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 update(Space &home, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:995
Less or equal ( )
Definition: int.hh:928
Integer variable array.
Definition: int.hh:763
void ipl(IntPropLevel i)
Set default integer propagation level.
Definition: options.hpp:216
virtual Space * copy(void)
Copy during cloning.
Definition: grocery.cpp:89
Computation spaces.
Definition: core.hpp:1701
Parametric base-class for scripts.
Definition: driver.hh:729
void iterations(unsigned int i)
Set default number of iterations.
Definition: options.hpp:461
Gecode::IntSet d(v, 7)
Grocery(const Options &opt)
The actual model.
Definition: grocery.cpp:66
Gecode::FloatVal c(-8, 8)
int main(int argc, char *argv[])
Main-function.
Definition: grocery.cpp:104
IntVarArray abcd
The price of each item.
Definition: grocery.cpp:59
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:232
Options opt
The options.
Definition: test.cpp:97
struct Gecode::@593::NNF::@62::@63 b
For binary nodes (and, or, eqv)
IntValBranch INT_VAL_SPLIT_MAX(void)
Select values greater than mean of smallest and largest value.
Definition: val.hpp:80
virtual void print(std::ostream &os) const
Print solution.
Definition: grocery.cpp:95
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:540
Integer variables.
Definition: int.hh:371
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:43
Grocery(Grocery &s)
Constructor for cloning s.
Definition: grocery.cpp:83
Gecode toplevel namespace
Options for scripts
Definition: driver.hh:366
struct Gecode::@593::NNF::@62::@64 a
For atomic nodes.