Generated on Mon Nov 30 23:53:18 2009 for Gecode by doxygen 1.6.1

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: 2009-04-02 20:58:46 +0200 (Thu, 02 Apr 2009) $ by $Author: schulte $
00011  *     $Revision: 8649 $
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_suppliers = 5;
00047 const int n_stores = 10;
00048 
00050 const int building_cost = 30;
00051 
00053 const int capacity[n_suppliers] = {
00054   1, 4, 2, 1, 3
00055 };
00056 
00058 const int cost_matrix[n_stores][n_suppliers] = {
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 
00096 class Warehouses : public MinimizeScript {
00097 protected:
00099   IntVarArray supplier;
00101   BoolVarArray open;
00103   IntVarArray scost;
00105   IntVar total;
00106 public:
00108   Warehouses(const Options&)
00109     : supplier(*this, n_stores, 0, n_suppliers-1),
00110       open(*this, n_suppliers, 0, 1),
00111       scost(*this, n_stores, 0, Int::Limits::max),
00112       total(*this, 0, Int::Limits::max) {
00113     // Compute total cost
00114     {
00115       // Opening cost
00116       IntArgs c(n_suppliers);
00117       for (int i=0; i<n_suppliers; i++)
00118         c[i]=building_cost;
00119       IntVar oc(*this, 0, Int::Limits::max);
00120       linear(*this, c, open, IRT_EQ, oc);
00121       // Total cost of stores
00122       IntVarArgs tc(n_stores+1);
00123       for (int i=0; i<n_stores; i++)
00124         tc[i]=scost[i];
00125       tc[n_stores] = oc;
00126       linear(*this, tc, IRT_EQ, total);
00127     }
00128 
00129     // Compute cost for store
00130     for (int i=0; i<n_stores; i++) {
00131       IntArgs c(n_suppliers);
00132       for (int j=0; j<n_suppliers; j++)
00133         c[j] = cost_matrix[i][j];
00134       element(*this, c, supplier[i], scost[i]);
00135     }
00136 
00137     // Do not exceed capacity
00138     for (int i=0; i<n_suppliers; i++)
00139       count(*this, supplier, i, IRT_LQ, capacity[i]);
00140 
00141     // A warehouse is open, if it supplies to a shop
00142     for (int i=0; i<n_suppliers; i++) {
00143       BoolVarArgs store_by_supplier(n_stores);
00144       for (int j=0; j<n_stores; j++)
00145         store_by_supplier[j] = post(*this, ~(supplier[j] == i));
00146       rel(*this, BOT_OR, store_by_supplier, open[i]);
00147     }
00148 
00149     branch(*this, scost, INT_VAR_REGRET_MIN_MAX, INT_VAL_MIN);
00150   }
00152   virtual IntVar cost(void) const {
00153     return total;
00154   }
00156   Warehouses(bool share, Warehouses& s) : MinimizeScript(share,s) {
00157     supplier.update(*this, share, s.supplier);
00158     open.update(*this, share, s.open);
00159     scost.update(*this, share, s.scost);
00160     total.update(*this, share, s.total);
00161   }
00162 
00164   virtual Space*
00165   copy(bool share) {
00166     return new Warehouses(share,*this);
00167   }
00169   virtual void
00170   print(std::ostream& os) const {
00171     os << "\tSupplier: " << supplier << std::endl
00172        << "\tCost: " << scost << std::endl
00173        << "\tTotal cost: " << total << std::endl
00174        << std::endl;
00175   }
00176 };
00177 
00181 int
00182 main(int argc, char* argv[]) {
00183   Options opt("Warehouses");
00184   opt.solutions(0);
00185   opt.iterations(10);
00186   opt.parse(argc,argv);
00187   MinimizeScript::run<Warehouses,BAB,Options>(opt);
00188   return 0;
00189 }
00190 
00191 // STATISTICS: example-any
00192