this repo has no description
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 40/** 41 * \brief %Example: %Grocery puzzle 42 * 43 * A kid goes into a grocery store and buys four items. The cashier 44 * charges $7.11, the kid pays and is about to leave when the cashier 45 * calls the kid back, and says ''Hold on, I multiplied the four items 46 * instead of adding them; I'll try again; Hah, with adding them the 47 * price still comes to $7.11''. What were the prices of the four items? 48 * 49 * The model is taken from: Christian Schulte, Gert Smolka, Finite Domain 50 * Constraint Programming in Oz. A Tutorial. 2001. 51 * Available from: http://www.mozart-oz.org/documentation/fdt/ 52 * 53 * \ingroup Example 54 * 55 */ 56class Grocery : public Script { 57protected: 58 /// The price of each item 59 IntVarArray abcd; 60 /// Sum and product of prices 61 static const int s = 711; 62 /// Decimal product of prices 63 static const int p = 711 * 100 * 100 * 100; 64public: 65 /// The actual model 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 79 branch(*this, abcd, INT_VAR_NONE(), INT_VAL_SPLIT_MAX()); 80 } 81 82 /// Constructor for cloning \a s 83 Grocery(Grocery& s) : Script(s) { 84 abcd.update(*this, s.abcd); 85 } 86 87 /// Copy during cloning 88 virtual Space* 89 copy(void) { 90 return new Grocery(*this); 91 } 92 93 /// Print solution 94 virtual void 95 print(std::ostream& os) const { 96 os << "\t" << abcd << std::endl; 97 } 98}; 99 100/** \brief Main-function 101 * \relates Grocery 102 */ 103int 104main(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