00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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
00114 {
00115
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
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
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
00138 for (int i=0; i<n_suppliers; i++)
00139 count(*this, supplier, i, IRT_LQ, capacity[i]);
00140
00141
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
00192