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, 2005 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/// Number of warehouses 41const int n_warehouses = 5; 42/// Number of stores 43const int n_stores = 10; 44 45/// Fixed cost for one warehouse 46const int c_fixed = 30; 47 48/// Capacity of a single warehouse 49const int capacity[n_warehouses] = { 50 1, 4, 2, 1, 3 51}; 52 53/// Cost for supply a store by a warehouse 54const int c_supply[n_stores][n_warehouses] = { 55 {20, 24, 11, 25, 30}, 56 {28, 27, 82, 83, 74}, 57 {74, 97, 71, 96, 70}, 58 { 2, 55, 73, 69, 61}, 59 {46, 96, 59, 83, 4}, 60 {42, 22, 29, 67, 59}, 61 { 1, 5, 73, 59, 56}, 62 {10, 73, 13, 43, 96}, 63 {93, 35, 63, 85, 46}, 64 {47, 65, 55, 71, 95} 65}; 66 67/// Model variants 68enum { 69 MODEL_SUMCOST, ///< Use sum as total cost 70 MODEL_LEXCOST ///< Use lexicographic cost 71}; 72 73/** 74 * \brief %Example: Locating warehouses 75 * 76 * A company needs to construct warehouses to supply stores with 77 * goods. Each warehouse possibly to be constructed has a certain 78 * capacity defining how many stores it can supply. Constructing a 79 * warehouse incurs a fixed cost. Costs for transportation from 80 * warehouses to stores depend on the locations of warehouses and 81 * stores. 82 * 83 * Determine which warehouses should be constructed and which 84 * warehouse should supply which store such that overall cost 85 * (transportation cost plus construction cost) is smallest. 86 * 87 * Taken from: 88 * Pascal Van Hentenryck, The OPL Optmization Programming Language, 89 * The MIT Press, 1999. 90 * 91 * See also problem 34 at http://www.csplib.org/. 92 * 93 * Note that "Modeling and Programming with Gecode" uses this example 94 * as a case study. 95 * 96 * \ingroup Example 97 * 98 */ 99template<class Script> 100class Warehouses : public Script { 101protected: 102 /// Which warehouse supplies a store 103 IntVarArray supplier; 104 /// Is a warehouse open (warehouse needed) 105 BoolVarArray open; 106 /// Cost of a store 107 IntVarArray c_store; 108public: 109 /// Actual model 110 Warehouses(const Options& opt) 111 : Script(opt), 112 supplier(*this, n_stores, 0, n_warehouses-1), 113 open(*this, n_warehouses, 0, 1), 114 c_store(*this, n_stores) { 115 116 // A warehouse is open, if it supplies to a store 117 for (int s=0; s<n_stores; s++) 118 element(*this, open, supplier[s], 1); 119 120 // Compute cost for each warehouse 121 for (int s=0; s<n_stores; s++) { 122 IntArgs c(n_warehouses, c_supply[s]); 123 c_store[s] = expr(*this, element(c, supplier[s])); 124 } 125 126 // Do not exceed capacity 127 { 128 IntSetArgs c(n_warehouses); 129 for (int w=0; w<n_warehouses; w++) 130 c[w] = IntSet(0,capacity[w]); 131 count(*this, supplier, c, IPL_DOM); 132 } 133 134 // Branch with largest minimum regret on store cost 135 branch(*this, c_store, INT_VAR_REGRET_MIN_MAX(), INT_VAL_MIN()); 136 // Branch by assigning a supplier to each store 137 branch(*this, supplier, INT_VAR_NONE(), INT_VAL_MIN()); 138 } 139 /// Constructor for cloning \a s 140 Warehouses(Warehouses& s) : Script(s) { 141 supplier.update(*this, s.supplier); 142 open.update(*this, s.open); 143 c_store.update(*this, s.c_store); 144 } 145}; 146 147 148/// Model with cost defined as sum 149class SumCostWarehouses : public Warehouses<IntMinimizeScript> { 150protected: 151 /// Total cost 152 IntVar c_total; 153public: 154 /// Actual model 155 SumCostWarehouses(const Options& opt) 156 : Warehouses<IntMinimizeScript>(opt) { 157 // Compute total cost 158 c_total = expr(*this, c_fixed*sum(open) + sum(c_store)); 159 } 160 /// Return solution cost 161 virtual IntVar cost(void) const { 162 return c_total; 163 } 164 /// Constructor for cloning \a s 165 SumCostWarehouses(SumCostWarehouses& s) : Warehouses<IntMinimizeScript>(s) { 166 c_total.update(*this, s.c_total); 167 } 168 /// Copy during cloning 169 virtual Space* copy(void) { 170 return new SumCostWarehouses(*this); 171 } 172 /// Print solution 173 virtual void 174 print(std::ostream& os) const { 175 os << "\tSupplier: " << supplier << std::endl 176 << "\tOpen warehouses: " << open << std::endl 177 << "\tStore cost: " << c_store << std::endl 178 << "\tTotal cost: " << c_total << std::endl 179 << std::endl; 180 } 181}; 182 183 184/// Model with cost defined lexicographically 185class LexCostWarehouses : public Warehouses<IntLexMinimizeScript> { 186protected: 187 /// Cost for open warehouses 188 IntVar c_open; 189 /// Cost for stores 190 IntVar c_stores; 191public: 192 /// Actual model 193 LexCostWarehouses(const Options& opt) 194 : Warehouses<IntLexMinimizeScript>(opt) { 195 // Compute costs 196 c_open = expr(*this, sum(open)); 197 c_stores = expr(*this, sum(c_store)); 198 } 199 /// Return solution cost 200 virtual IntVarArgs cost(void) const { 201 return {c_open, c_stores}; 202 } 203 /// Constructor for cloning \a s 204 LexCostWarehouses(LexCostWarehouses& s) 205 : Warehouses<IntLexMinimizeScript>(s) { 206 c_open.update(*this, s.c_open); 207 c_stores.update(*this, s.c_stores); 208 } 209 /// Copy during cloning 210 virtual Space* copy(void) { 211 return new LexCostWarehouses(*this); 212 } 213 /// Print solution 214 virtual void 215 print(std::ostream& os) const { 216 os << "\tSupplier: " << supplier << std::endl 217 << "\tOpen warehouses: " << open << std::endl 218 << "\tOpen cost: " << c_open << std::endl 219 << "\tStores cost: " << c_stores << std::endl 220 << std::endl; 221 } 222}; 223 224/** \brief Main-function 225 * \relates Warehouses 226 */ 227int 228main(int argc, char* argv[]) { 229 Options opt("Warehouses"); 230 opt.model(MODEL_SUMCOST); 231 opt.model(MODEL_SUMCOST, "sum", "use sum of costs"); 232 opt.model(MODEL_LEXCOST, "lex", "use lexicographic cost"); 233 opt.solutions(0); 234 opt.iterations(10); 235 opt.parse(argc,argv); 236 switch (opt.model()) { 237 case MODEL_SUMCOST: 238 IntMinimizeScript::run<SumCostWarehouses,BAB,Options>(opt); 239 break; 240 case MODEL_LEXCOST: 241 IntLexMinimizeScript::run<LexCostWarehouses,BAB,Options>(opt); 242 break; 243 } 244 return 0; 245} 246 247// STATISTICS: example-any 248