Generated on Thu Apr 11 13:58:54 2019 for Gecode by doxygen 1.6.3

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  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 #include <gecode/driver.hh>
00035 #include <gecode/int.hh>
00036 #include <gecode/minimodel.hh>
00037 
00038 using namespace Gecode;
00039 
00041 const int n_warehouses = 5;
00043 const int n_stores = 10;
00044 
00046 const int c_fixed = 30;
00047 
00049 const int capacity[n_warehouses] = {
00050   1, 4, 2, 1, 3
00051 };
00052 
00054 const int c_supply[n_stores][n_warehouses] = {
00055   {20, 24, 11, 25, 30},
00056   {28, 27, 82, 83, 74},
00057   {74, 97, 71, 96, 70},
00058   { 2, 55, 73, 69, 61},
00059   {46, 96, 59, 83,  4},
00060   {42, 22, 29, 67, 59},
00061   { 1,  5, 73, 59, 56},
00062   {10, 73, 13, 43, 96},
00063   {93, 35, 63, 85, 46},
00064   {47, 65, 55, 71, 95}
00065 };
00066 
00068 enum {
00069   MODEL_SUMCOST, 
00070   MODEL_LEXCOST  
00071 };
00072 
00099 template<class Script>
00100 class Warehouses : public Script {
00101 protected:
00103   IntVarArray supplier;
00105   BoolVarArray open;
00107   IntVarArray c_store;
00108 public:
00110   Warehouses(const Options& opt)
00111     : Script(opt),
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, IPL_DOM);
00132     }
00133 
00134     // Branch with largest minimum regret on store cost
00135     branch(*this, c_store, INT_VAR_REGRET_MIN_MAX(), INT_VAL_MIN());
00136     // Branch by assigning a supplier to each store
00137     branch(*this, supplier, INT_VAR_NONE(), INT_VAL_MIN());
00138   }
00140   Warehouses(Warehouses& s) : Script(s) {
00141     supplier.update(*this, s.supplier);
00142     open.update(*this, s.open);
00143     c_store.update(*this, s.c_store);
00144   }
00145 };
00146 
00147 
00149 class SumCostWarehouses : public Warehouses<IntMinimizeScript> {
00150 protected:
00152   IntVar c_total;
00153 public:
00155   SumCostWarehouses(const Options& opt)
00156     : Warehouses<IntMinimizeScript>(opt) {
00157     // Compute total cost
00158     c_total = expr(*this, c_fixed*sum(open) + sum(c_store));
00159   }
00161   virtual IntVar cost(void) const {
00162     return c_total;
00163   }
00165   SumCostWarehouses(SumCostWarehouses& s) : Warehouses<IntMinimizeScript>(s) {
00166     c_total.update(*this, s.c_total);
00167   }
00169   virtual Space* copy(void) {
00170     return new SumCostWarehouses(*this);
00171   }
00173   virtual void
00174   print(std::ostream& os) const {
00175     os << "\tSupplier:        " << supplier << std::endl
00176        << "\tOpen warehouses: " << open << std::endl
00177        << "\tStore cost:      " << c_store << std::endl
00178        << "\tTotal cost:      " << c_total << std::endl
00179        << std::endl;
00180   }
00181 };
00182 
00183 
00185 class LexCostWarehouses : public Warehouses<IntLexMinimizeScript> {
00186 protected:
00188   IntVar c_open;
00190   IntVar c_stores;
00191 public:
00193   LexCostWarehouses(const Options& opt)
00194     : Warehouses<IntLexMinimizeScript>(opt) {
00195     // Compute costs
00196     c_open = expr(*this, sum(open));
00197     c_stores = expr(*this, sum(c_store));
00198   }
00200   virtual IntVarArgs cost(void) const {
00201     return {c_open, c_stores};
00202   }
00204   LexCostWarehouses(LexCostWarehouses& s)
00205     : Warehouses<IntLexMinimizeScript>(s) {
00206     c_open.update(*this, s.c_open);
00207     c_stores.update(*this, s.c_stores);
00208   }
00210   virtual Space* copy(void) {
00211     return new LexCostWarehouses(*this);
00212   }
00214   virtual void
00215   print(std::ostream& os) const {
00216     os << "\tSupplier:        " << supplier << std::endl
00217        << "\tOpen warehouses: " << open << std::endl
00218        << "\tOpen cost:       " << c_open << std::endl
00219        << "\tStores cost:     " << c_stores << std::endl
00220        << std::endl;
00221   }
00222 };
00223 
00227 int
00228 main(int argc, char* argv[]) {
00229   Options opt("Warehouses");
00230   opt.model(MODEL_SUMCOST);
00231   opt.model(MODEL_SUMCOST, "sum", "use sum of costs");
00232   opt.model(MODEL_LEXCOST, "lex", "use lexicographic cost");
00233   opt.solutions(0);
00234   opt.iterations(10);
00235   opt.parse(argc,argv);
00236   switch (opt.model()) {
00237   case MODEL_SUMCOST:    
00238     IntMinimizeScript::run<SumCostWarehouses,BAB,Options>(opt);
00239     break;
00240   case MODEL_LEXCOST:    
00241     IntLexMinimizeScript::run<LexCostWarehouses,BAB,Options>(opt);
00242     break;
00243   }
00244   return 0;
00245 }
00246 
00247 // STATISTICS: example-any
00248