warehouses.cpp
Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Christian Schulte <schulte@gecode.org> 00005 * 00006 * Copyright: 00007 * Christian Schulte, 2005 00008 * 00009 * Last modified: 00010 * $Date: 2010-06-16 13:43:29 +0200 (Wed, 16 Jun 2010) $ by $Author: schulte $ 00011 * $Revision: 11077 $ 00012 * 00013 * This file is part of Gecode, the generic constraint 00014 * development environment: 00015 * http://www.gecode.org 00016 * 00017 * Permission is hereby granted, free of charge, to any person obtaining 00018 * a copy of this software and associated documentation files (the 00019 * "Software"), to deal in the Software without restriction, including 00020 * without limitation the rights to use, copy, modify, merge, publish, 00021 * distribute, sublicense, and/or sell copies of the Software, and to 00022 * permit persons to whom the Software is furnished to do so, subject to 00023 * the following conditions: 00024 * 00025 * The above copyright notice and this permission notice shall be 00026 * included in all copies or substantial portions of the Software. 00027 * 00028 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00029 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00030 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00031 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00032 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00033 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00034 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00035 * 00036 */ 00037 00038 #include <gecode/driver.hh> 00039 #include <gecode/int.hh> 00040 #include <gecode/minimodel.hh> 00041 00042 using namespace Gecode; 00043 00045 const int n_warehouses = 5; 00047 const int n_stores = 10; 00048 00050 const int c_fixed = 30; 00051 00053 const int capacity[n_warehouses] = { 00054 1, 4, 2, 1, 3 00055 }; 00056 00058 const int c_supply[n_stores][n_warehouses] = { 00059 {20, 24, 11, 25, 30}, 00060 {28, 27, 82, 83, 74}, 00061 {74, 97, 71, 96, 70}, 00062 { 2, 55, 73, 69, 61}, 00063 {46, 96, 59, 83, 4}, 00064 {42, 22, 29, 67, 59}, 00065 { 1, 5, 73, 59, 56}, 00066 {10, 73, 13, 43, 96}, 00067 {93, 35, 63, 85, 46}, 00068 {47, 65, 55, 71, 95} 00069 }; 00070 00071 00072 00099 class Warehouses : public MinimizeScript { 00100 protected: 00102 IntVarArray supplier; 00104 BoolVarArray open; 00106 IntVarArray c_store; 00108 IntVar c_total; 00109 public: 00111 Warehouses(const Options&) 00112 : supplier(*this, n_stores, 0, n_warehouses-1), 00113 open(*this, n_warehouses, 0, 1), 00114 c_store(*this, n_stores) { 00115 00116 // A warehouse is open, if it supplies to a store 00117 for (int s=0; s<n_stores; s++) 00118 element(*this, open, supplier[s], 1); 00119 00120 // Compute cost for each warehouse 00121 for (int s=0; s<n_stores; s++) { 00122 IntArgs c(n_warehouses, c_supply[s]); 00123 c_store[s] = expr(*this, element(c, supplier[s])); 00124 } 00125 00126 // Do not exceed capacity 00127 { 00128 IntSetArgs c(n_warehouses); 00129 for (int w=0; w<n_warehouses; w++) 00130 c[w] = IntSet(0,capacity[w]); 00131 count(*this, supplier, c, ICL_DOM); 00132 } 00133 00134 // Compute total cost 00135 c_total = expr(*this, c_fixed*sum(open) + sum(c_store)); 00136 00137 // Branch with largest minimum regret on store cost 00138 branch(*this, c_store, INT_VAR_REGRET_MIN_MAX, INT_VAL_MIN); 00139 00140 // Branch by assigning a supplier to each store 00141 branch(*this, supplier, INT_VAR_NONE, INT_VAL_MIN); 00142 } 00144 virtual IntVar cost(void) const { 00145 return c_total; 00146 } 00148 Warehouses(bool share, Warehouses& s) : MinimizeScript(share,s) { 00149 supplier.update(*this, share, s.supplier); 00150 open.update(*this, share, s.open); 00151 c_store.update(*this, share, s.c_store); 00152 c_total.update(*this, share, s.c_total); 00153 } 00154 00156 virtual Space* 00157 copy(bool share) { 00158 return new Warehouses(share,*this); 00159 } 00161 virtual void 00162 print(std::ostream& os) const { 00163 os << "\tSupplier: " << supplier << std::endl 00164 << "\tOpen warehouses: " << open << std::endl 00165 << "\tStore cost: " << c_store << std::endl 00166 << "\tTotal cost: " << c_total << std::endl 00167 << std::endl; 00168 } 00169 }; 00170 00174 int 00175 main(int argc, char* argv[]) { 00176 Options opt("Warehouses"); 00177 opt.solutions(0); 00178 opt.iterations(10); 00179 opt.parse(argc,argv); 00180 MinimizeScript::run<Warehouses,BAB,Options>(opt); 00181 return 0; 00182 } 00183 00184 // STATISTICS: example-any 00185