00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "examples/support.hh"
00023 #include "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
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
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
00111 for (int i=0; i<n_suppliers; i++)
00112 count(this, supplier, i, IRT_LQ, capacity[i]);
00113
00114
00115 for (int i=0; i<n_suppliers; i++) {
00116 BoolVarArgs store_by_supplier(n_stores);
00117 IntVar n_supplied(this, 0, n_stores);
00118 for (int j=0; j<n_stores; j++)
00119 store_by_supplier[j] = post(this, ~(supplier[j] == i));
00120 linear(this, store_by_supplier, IRT_EQ, n_supplied);
00121 BoolVar b(this, 0, 1);
00122 rel(this, open[i], IRT_EQ, 1, b);
00123 rel(this, n_supplied, IRT_GR, 0, b);
00124 }
00125
00126 branch(this, cost, BVAR_REGRET_MIN_MAX, BVAL_MIN);
00127 }
00128
00130 void
00131 constrain(Space* s) {
00132 rel(this, total, IRT_LE, static_cast<Warehouses*>(s)->total.val());
00133 }
00135 Warehouses(bool share, Warehouses& s) : Example(share,s) {
00136 supplier.update(this, share, s.supplier);
00137 open.update(this, share, s.open);
00138 cost.update(this, share, s.cost);
00139 total.update(this, share, s.total);
00140 }
00141
00143 virtual Space*
00144 copy(bool share) {
00145 return new Warehouses(share,*this);
00146 }
00148 virtual void
00149 print(void) {
00150 std::cout << "\tSupplier: {";
00151 for (int i=0; i<n_stores; i++) {
00152 std::cout << supplier[i] << ((i<n_stores-1)?", ":"};\n");
00153 }
00154 std::cout << "\tCost: {";
00155 for (int i=0; i<n_stores; i++) {
00156 std::cout << cost[i] << ((i<n_stores-1)?", ":"};\n");
00157 }
00158 std::cout << "\tTotal cost: " << total << std::endl;
00159 std::cout << std::endl;
00160 }
00161 };
00162
00166 int
00167 main(int argc, char** argv) {
00168 Options opt("Warehouses");
00169 opt.solutions = 0;
00170 opt.iterations = 10;
00171 opt.naive = true;
00172 opt.parse(argc,argv);
00173 Example::run<Warehouses,BAB>(opt);
00174 return 0;
00175 }
00176
00177
00178