Generated on Tue Apr 18 10:21:31 2017 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  *  Last modified:
00010  *     $Date: 2015-09-11 16:29:45 +0200 (Fri, 11 Sep 2015) $ by $Author: schulte $
00011  *     $Revision: 14672 $
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 IntMinimizeScript {
00100 protected:
00102   IntVarArray supplier;
00104   BoolVarArray open;
00106   IntVarArray c_store;
00108   IntVar c_total;
00109 public:
00111   Warehouses(const Options& opt)
00112     : IntMinimizeScript(opt),
00113       supplier(*this, n_stores, 0, n_warehouses-1),
00114       open(*this, n_warehouses, 0, 1),
00115       c_store(*this, n_stores) {
00116 
00117     // A warehouse is open, if it supplies to a store
00118     for (int s=0; s<n_stores; s++)
00119       element(*this, open, supplier[s], 1);
00120 
00121     // Compute cost for each warehouse
00122     for (int s=0; s<n_stores; s++) {
00123       IntArgs c(n_warehouses, c_supply[s]);
00124       c_store[s] = expr(*this, element(c, supplier[s]));
00125     }
00126 
00127     // Do not exceed capacity
00128     {
00129       IntSetArgs c(n_warehouses);
00130       for (int w=0; w<n_warehouses; w++)
00131         c[w] = IntSet(0,capacity[w]);
00132       count(*this, supplier, c, IPL_DOM);
00133     }
00134 
00135     // Compute total cost
00136     c_total = expr(*this, c_fixed*sum(open) + sum(c_store));
00137 
00138     // Branch with largest minimum regret on store cost
00139     branch(*this, c_store, INT_VAR_REGRET_MIN_MAX(), INT_VAL_MIN());
00140 
00141     // Branch by assigning a supplier to each store
00142     branch(*this, supplier, INT_VAR_NONE(), INT_VAL_MIN());
00143   }
00145   virtual IntVar cost(void) const {
00146     return c_total;
00147   }
00149   Warehouses(bool share, Warehouses& s) : IntMinimizeScript(share,s) {
00150     supplier.update(*this, share, s.supplier);
00151     open.update(*this, share, s.open);
00152     c_store.update(*this, share, s.c_store);
00153     c_total.update(*this, share, s.c_total);
00154   }
00155 
00157   virtual Space*
00158   copy(bool share) {
00159     return new Warehouses(share,*this);
00160   }
00162   virtual void
00163   print(std::ostream& os) const {
00164     os << "\tSupplier:        " << supplier << std::endl
00165        << "\tOpen warehouses: " << open << std::endl
00166        << "\tStore cost:      " << c_store << std::endl
00167        << "\tTotal cost:      " << c_total << std::endl
00168        << std::endl;
00169   }
00170 };
00171 
00175 int
00176 main(int argc, char* argv[]) {
00177   Options opt("Warehouses");
00178   opt.solutions(0);
00179   opt.iterations(10);
00180   opt.parse(argc,argv);
00181   IntMinimizeScript::run<Warehouses,BAB,Options>(opt);
00182   return 0;
00183 }
00184 
00185 // STATISTICS: example-any
00186