Generated on Mon Aug 25 11:35:33 2008 for Gecode by doxygen 1.5.6

warehouses.cc

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: 2008-02-06 18:48:22 +0100 (Wed, 06 Feb 2008) $ by $Author: schulte $
00011  *     $Revision: 6102 $
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 "examples/support.hh"
00039 #include "gecode/minimodel.hh"
00040 
00042 const int n_suppliers = 5;
00044 const int n_stores = 10;
00045 
00047 const int building_cost = 30;
00048 
00050 const int capacity[n_suppliers] = {
00051   1, 4, 2, 1, 3
00052 };
00053 
00055 const int cost_matrix[n_stores][n_suppliers] = {
00056   {20, 24, 11, 25, 30},
00057   {28, 27, 82, 83, 74},
00058   {74, 97, 71, 96, 70},
00059   { 2, 55, 73, 69, 61},
00060   {46, 96, 59, 83,  4},
00061   {42, 22, 29, 67, 59},
00062   { 1,  5, 73, 59, 56},
00063   {10, 73, 13, 43, 96},
00064   {93, 35, 63, 85, 46},
00065   {47, 65, 55, 71, 95}
00066 };
00067 
00068 
00069 
00093 class Warehouses : public Example {
00094 protected:
00096   IntVarArray supplier;
00098   BoolVarArray open;
00100   IntVarArray cost;
00102   IntVar total;
00103 public:
00105   Warehouses(const Options&)
00106     : supplier(this, n_stores, 0, n_suppliers-1),
00107       open(this, n_suppliers, 0, 1),
00108       cost(this, n_stores, 0, Int::Limits::max),
00109       total(this, 0, Int::Limits::max) {
00110     // Compute total cost
00111     {
00112       // Opening cost
00113       IntArgs c(n_suppliers);
00114       for (int i=0; i<n_suppliers; i++)
00115         c[i]=building_cost;
00116       IntVar oc(this, 0, Int::Limits::max);
00117       linear(this, c, open, IRT_EQ, oc);
00118       // Total cost of stores
00119       IntVarArgs tc(n_stores+1);
00120       for (int i=0; i<n_stores; i++)
00121         tc[i]=cost[i];
00122       tc[n_stores] = oc;
00123       linear(this, tc, IRT_EQ, total);
00124     }
00125 
00126     // Compute cost for store
00127     for (int i=0; i<n_stores; i++) {
00128       IntArgs c(n_suppliers);
00129       for (int j=0; j<n_suppliers; j++)
00130         c[j] = cost_matrix[i][j];
00131       element(this, c, supplier[i], cost[i]);
00132     }
00133 
00134     // Do not exceed capacity
00135     for (int i=0; i<n_suppliers; i++)
00136       count(this, supplier, i, IRT_LQ, capacity[i]);
00137 
00138     // A warehouse is open, if it supplies to a shop
00139     for (int i=0; i<n_suppliers; i++) {
00140       BoolVarArgs store_by_supplier(n_stores);
00141       for (int j=0; j<n_stores; j++)
00142         store_by_supplier[j] = post(this, ~(supplier[j] == i));
00143       rel(this, BOT_OR, store_by_supplier, open[i]);
00144     }
00145 
00146     branch(this, cost, INT_VAR_REGRET_MIN_MAX, INT_VAL_MIN);
00147   }
00148 
00150   void
00151   constrain(Space* s) {
00152     rel(this, total, IRT_LE, static_cast<Warehouses*>(s)->total.val());
00153   }
00155   Warehouses(bool share, Warehouses& s) : Example(share,s) {
00156     supplier.update(this, share, s.supplier);
00157     open.update(this, share, s.open);
00158     cost.update(this, share, s.cost);
00159     total.update(this, share, s.total);
00160   }
00161 
00163   virtual Space*
00164   copy(bool share) {
00165     return new Warehouses(share,*this);
00166   }
00168   virtual void
00169   print(std::ostream& os) {
00170     os << "\tSupplier: " << supplier << std::endl
00171        << "\tCost: " << cost << std::endl
00172        << "\tTotal cost: " << total << std::endl
00173        << std::endl;
00174   }
00175 };
00176 
00180 int
00181 main(int argc, char* argv[]) {
00182   Options opt("Warehouses");
00183   opt.solutions(0);
00184   opt.iterations(10);
00185   opt.parse(argc,argv);
00186   Example::run<Warehouses,BAB,Options>(opt);
00187   return 0;
00188 }
00189 
00190 // STATISTICS: example-any
00191