grocery.cpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2001
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <gecode/driver.hh>
#include <gecode/int.hh>
#include <gecode/minimodel.hh>
using namespace Gecode;
class Grocery : public Script {
protected:
IntVarArray abcd;
static const int s = 711;
static const int p = 711 * 100 * 100 * 100;
public:
Grocery(const Options& opt)
: Script(opt), abcd(*this,4,0,s) {
IntVar a(abcd[0]), b(abcd[1]), c(abcd[2]), d(abcd[3]);
// The sum of all variables is s
rel(*this, a+b+c+d == s, opt.ipl());
// The product of all variables is s (corrected by scale factor)
rel(*this, (a*b)*(c*d) == p, opt.ipl());
// Break symmetries: order the variables
rel(*this, abcd, IRT_LQ);
branch(*this, abcd, INT_VAR_NONE(), INT_VAL_SPLIT_MAX());
}
Grocery(Grocery& s) : Script(s) {
abcd.update(*this, s.abcd);
}
virtual Space*
copy(void) {
return new Grocery(*this);
}
virtual void
print(std::ostream& os) const {
os << "\t" << abcd << std::endl;
}
};
int
main(int argc, char* argv[]) {
Options opt("Grocery");
opt.iterations(20);
opt.parse(argc,argv);
Script::run<Grocery,DFS,Options>(opt);
return 0;
}
// STATISTICS: example-any
Gecode::Driver::ScriptBase< Driver::IgnoreStepOption< Space > >::run
static void run(const Options &opt, Script *s=NULL)
Gecode::IntVarArray
Integer variable array.
Definition int.hh:763
Gecode::IntVar
Integer variables.
Definition int.hh:371
Gecode::Options
Options for scripts
Definition driver.hh:366
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Grocery::main
int main(int argc, char *argv[])
Main-function.
Definition grocery.cpp:104
Grocery::p
static const int p
Decimal product of prices.
Definition grocery.cpp:63
Grocery::abcd
IntVarArray abcd
The price of each item.
Definition grocery.cpp:59
Grocery::s
static const int s
Sum and product of prices.
Definition grocery.cpp:61
Grocery::copy
virtual Space * copy(void)
Copy during cloning.
Definition grocery.cpp:89
Grocery::print
virtual void print(std::ostream &os) const
Print solution.
Definition grocery.cpp:95
Grocery::Grocery
Grocery(const Options &opt)
The actual model.
Definition grocery.cpp:66
Grocery::Grocery
Grocery(Grocery &s)
Constructor for cloning s.
Definition grocery.cpp:83
Test::Options::parse
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
driver.hh
int.hh
Gecode::Script
Driver::ScriptBase< Driver::IgnoreStepOption< Space > > Script
Base-class for scripts.
Definition driver.hh:801
Gecode::branch
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
Gecode::rel
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
Gecode::IRT_LQ
@ IRT_LQ
Less or equal ( ).
Definition int.hh:928
minimodel.hh
Gecode
Gecode toplevel namespace
Gecode::INT_VAR_NONE
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition var.hpp:96
Gecode::INT_VAL_SPLIT_MAX
IntValBranch INT_VAL_SPLIT_MAX(void)
Select values greater than mean of smallest and largest value.
Definition val.hpp:80