Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.0
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
38using namespace Gecode;
39
56class Grocery : public Script {
57protected:
61 static const int s = 711;
63 static const int p = 711 * 100 * 100 * 100;
64public:
66 Grocery(const Options& opt)
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
80 }
81
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
103int
104main(int argc, char* argv[]) {
105 Options opt("Grocery");
106 opt.iterations(20);
107 opt.parse(argc,argv);
109 return 0;
110}
111
112// STATISTICS: example-any
113
struct Gecode::@603::NNF::@65::@66 b
For binary nodes (and, or, eqv)
struct Gecode::@603::NNF::@65::@67 a
For atomic nodes.
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
Example: Grocery puzzle
Definition grocery.cpp:56
int main(int argc, char *argv[])
Main-function.
Definition grocery.cpp:104
static const int p
Decimal product of prices.
Definition grocery.cpp:63
IntVarArray abcd
The price of each item.
Definition grocery.cpp:59
static const int s
Sum and product of prices.
Definition grocery.cpp:61
virtual Space * copy(void)
Copy during cloning.
Definition grocery.cpp:89
virtual void print(std::ostream &os) const
Print solution.
Definition grocery.cpp:95
Grocery(const Options &opt)
The actual model.
Definition grocery.cpp:66
Grocery(Grocery &s)
Constructor for cloning s.
Definition grocery.cpp:83
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_LQ
Less or equal ( )
Definition int.hh:928
Gecode toplevel namespace
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition var.hpp:96
IntValBranch INT_VAL_SPLIT_MAX(void)
Select values greater than mean of smallest and largest value.
Definition val.hpp:80