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 "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
00111 {
00112
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
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
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
00135 for (int i=0; i<n_suppliers; i++)
00136 count(this, supplier, i, IRT_LQ, capacity[i]);
00137
00138
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
00191