Generated on Wed Nov 1 15:04:28 2006 for Gecode by doxygen 1.4.5

warehouses.cc

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Christian Schulte <schulte@gecode.org>
00004  *
00005  *  Copyright:
00006  *     Christian Schulte, 2005
00007  *
00008  *  Last modified:
00009  *     $Date: 2006-08-04 16:06:52 +0200 (Fri, 04 Aug 2006) $ by $Author: schulte $
00010  *     $Revision: 3517 $
00011  *
00012  *  This file is part of Gecode, the generic constraint
00013  *  development environment:
00014  *     http://www.gecode.org
00015  *
00016  *  See the file "LICENSE" for information on usage and
00017  *  redistribution of this file, and for a
00018  *     DISCLAIMER OF ALL WARRANTIES.
00019  *
00020  */
00021 
00022 #include "examples/support.hh"
00023 #include "gecode/minimodel.hh"
00024 
00026 static const int n_suppliers = 5;
00028 static const int n_stores = 10;
00029 
00031 static const int building_cost = 30;
00032 
00034 static const int capacity[n_suppliers] = {
00035   1, 4, 2, 1, 3
00036 };
00037 
00039 static const int cost_matrix[n_stores][n_suppliers] = {
00040   {20, 24, 11, 25, 30},
00041   {28, 27, 82, 83, 74},
00042   {74, 97, 71, 96, 70},
00043   { 2, 55, 73, 69, 61},
00044   {46, 96, 59, 83,  4},
00045   {42, 22, 29, 67, 59},
00046   { 1,  5, 73, 59, 56},
00047   {10, 73, 13, 43, 96},
00048   {93, 35, 63, 85, 46},
00049   {47, 65, 55, 71, 95}
00050 };
00051 
00052 
00053 
00071 class Warehouses : public Example {
00072 protected:
00074   IntVarArray supplier;
00076   BoolVarArray open;
00078   IntVarArray cost;
00080   IntVar total;
00081 public:
00083   Warehouses(const Options& opt)
00084     : supplier(this, n_stores, 0, n_suppliers-1),
00085       open(this, n_suppliers, 0, 1),
00086       cost(this, n_stores, 0, Limits::Int::int_max),
00087       total(this, 0, Limits::Int::int_max) {
00088     // Compute total cost
00089     {
00090       IntArgs c(n_stores + n_suppliers);
00091       IntVarArgs x(n_stores + n_suppliers);
00092       for (int i=0; i<n_stores; i++) {
00093         c[i]=1; x[i]=cost[i];
00094       }
00095       for (int i=0; i<n_suppliers; i++) {
00096         c[n_stores+i]=building_cost;
00097         x[n_stores+i]=open[i];
00098       }
00099       linear(this, c, x, IRT_EQ, total);
00100     }
00101 
00102     // Compute cost for store
00103     for (int i=0; i<n_stores; i++) {
00104       IntArgs c(n_suppliers);
00105       for (int j=0; j<n_suppliers; j++)
00106         c[j] = cost_matrix[i][j];
00107       element(this, c, supplier[i], cost[i]);
00108     }
00109 
00110     // Do not exceed capacity
00111     for (int i=0; i<n_suppliers; i++)
00112       count(this, supplier, i, IRT_LQ, capacity[i]);
00113 
00114     // A warehouse is open, if it supplies to a shop
00115     for (int i=0; i<n_suppliers; i++) {
00116       BoolVarArgs store_by_supplier(n_stores);
00117       for (int j=0; j<n_stores; j++)
00118         store_by_supplier[j] = post(this, ~(supplier[j] == i));
00119       BoolVar b(this, 0, 1);
00120       rel(this, open[i], IRT_EQ, 1, b);
00121       linear(this, store_by_supplier, IRT_GR, 0, b);
00122     }
00123 
00124     branch(this, cost, BVAR_REGRET_MIN_MAX, BVAL_MIN);
00125   }
00126 
00128   void
00129   constrain(Space* s) {
00130     rel(this, total, IRT_LE, static_cast<Warehouses*>(s)->total.val());
00131   }
00133   Warehouses(bool share, Warehouses& s) : Example(share,s) {
00134     supplier.update(this, share, s.supplier);
00135     open.update(this, share, s.open);
00136     cost.update(this, share, s.cost);
00137     total.update(this, share, s.total);
00138   }
00139 
00141   virtual Space*
00142   copy(bool share) {
00143     return new Warehouses(share,*this);
00144   }
00146   virtual void
00147   print(void) {
00148     std::cout << "\tSupplier: {";
00149     for (int i=0; i<n_stores; i++) {
00150       std::cout << supplier[i] << ((i<n_stores-1)?", ":"};\n");
00151     }
00152     std::cout << "\tCost: {";
00153     for (int i=0; i<n_stores; i++) {
00154       std::cout << cost[i] << ((i<n_stores-1)?", ":"};\n");
00155     }
00156     std::cout << "\tTotal cost: " << total << std::endl;
00157     std::cout << std::endl;
00158   }
00159 };
00160 
00164 int
00165 main(int argc, char** argv) {
00166   Options opt("Warehouses");
00167   opt.solutions  = 0;
00168   opt.iterations = 10;
00169   opt.naive      = true;
00170   opt.parse(argc,argv);
00171   Example::run<Warehouses,BAB>(opt);
00172   return 0;
00173 }
00174 
00175 // STATISTICS: example-any
00176