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 #include <gecode/driver.hh>
00035 #include <gecode/int.hh>
00036 #include <gecode/minimodel.hh>
00037
00038 using namespace Gecode;
00039
00041 const int n_warehouses = 5;
00043 const int n_stores = 10;
00044
00046 const int c_fixed = 30;
00047
00049 const int capacity[n_warehouses] = {
00050 1, 4, 2, 1, 3
00051 };
00052
00054 const int c_supply[n_stores][n_warehouses] = {
00055 {20, 24, 11, 25, 30},
00056 {28, 27, 82, 83, 74},
00057 {74, 97, 71, 96, 70},
00058 { 2, 55, 73, 69, 61},
00059 {46, 96, 59, 83, 4},
00060 {42, 22, 29, 67, 59},
00061 { 1, 5, 73, 59, 56},
00062 {10, 73, 13, 43, 96},
00063 {93, 35, 63, 85, 46},
00064 {47, 65, 55, 71, 95}
00065 };
00066
00068 enum {
00069 MODEL_SUMCOST,
00070 MODEL_LEXCOST
00071 };
00072
00099 template<class Script>
00100 class Warehouses : public Script {
00101 protected:
00103 IntVarArray supplier;
00105 BoolVarArray open;
00107 IntVarArray c_store;
00108 public:
00110 Warehouses(const Options& opt)
00111 : Script(opt),
00112 supplier(*this, n_stores, 0, n_warehouses-1),
00113 open(*this, n_warehouses, 0, 1),
00114 c_store(*this, n_stores) {
00115
00116
00117 for (int s=0; s<n_stores; s++)
00118 element(*this, open, supplier[s], 1);
00119
00120
00121 for (int s=0; s<n_stores; s++) {
00122 IntArgs c(n_warehouses, c_supply[s]);
00123 c_store[s] = expr(*this, element(c, supplier[s]));
00124 }
00125
00126
00127 {
00128 IntSetArgs c(n_warehouses);
00129 for (int w=0; w<n_warehouses; w++)
00130 c[w] = IntSet(0,capacity[w]);
00131 count(*this, supplier, c, IPL_DOM);
00132 }
00133
00134
00135 branch(*this, c_store, INT_VAR_REGRET_MIN_MAX(), INT_VAL_MIN());
00136
00137 branch(*this, supplier, INT_VAR_NONE(), INT_VAL_MIN());
00138 }
00140 Warehouses(Warehouses& s) : Script(s) {
00141 supplier.update(*this, s.supplier);
00142 open.update(*this, s.open);
00143 c_store.update(*this, s.c_store);
00144 }
00145 };
00146
00147
00149 class SumCostWarehouses : public Warehouses<IntMinimizeScript> {
00150 protected:
00152 IntVar c_total;
00153 public:
00155 SumCostWarehouses(const Options& opt)
00156 : Warehouses<IntMinimizeScript>(opt) {
00157
00158 c_total = expr(*this, c_fixed*sum(open) + sum(c_store));
00159 }
00161 virtual IntVar cost(void) const {
00162 return c_total;
00163 }
00165 SumCostWarehouses(SumCostWarehouses& s) : Warehouses<IntMinimizeScript>(s) {
00166 c_total.update(*this, s.c_total);
00167 }
00169 virtual Space* copy(void) {
00170 return new SumCostWarehouses(*this);
00171 }
00173 virtual void
00174 print(std::ostream& os) const {
00175 os << "\tSupplier: " << supplier << std::endl
00176 << "\tOpen warehouses: " << open << std::endl
00177 << "\tStore cost: " << c_store << std::endl
00178 << "\tTotal cost: " << c_total << std::endl
00179 << std::endl;
00180 }
00181 };
00182
00183
00185 class LexCostWarehouses : public Warehouses<IntLexMinimizeScript> {
00186 protected:
00188 IntVar c_open;
00190 IntVar c_stores;
00191 public:
00193 LexCostWarehouses(const Options& opt)
00194 : Warehouses<IntLexMinimizeScript>(opt) {
00195
00196 c_open = expr(*this, sum(open));
00197 c_stores = expr(*this, sum(c_store));
00198 }
00200 virtual IntVarArgs cost(void) const {
00201 IntVarArgs c(2);
00202 c[0] = c_open; c[1] = c_stores;
00203 return c;
00204 }
00206 LexCostWarehouses(LexCostWarehouses& s)
00207 : Warehouses<IntLexMinimizeScript>(s) {
00208 c_open.update(*this, s.c_open);
00209 c_stores.update(*this, s.c_stores);
00210 }
00212 virtual Space* copy(void) {
00213 return new LexCostWarehouses(*this);
00214 }
00216 virtual void
00217 print(std::ostream& os) const {
00218 os << "\tSupplier: " << supplier << std::endl
00219 << "\tOpen warehouses: " << open << std::endl
00220 << "\tOpen cost: " << c_open << std::endl
00221 << "\tStores cost: " << c_stores << std::endl
00222 << std::endl;
00223 }
00224 };
00225
00229 int
00230 main(int argc, char* argv[]) {
00231 Options opt("Warehouses");
00232 opt.model(MODEL_SUMCOST);
00233 opt.model(MODEL_SUMCOST, "sum", "use sum of costs");
00234 opt.model(MODEL_LEXCOST, "lex", "use lexicographic cost");
00235 opt.solutions(0);
00236 opt.iterations(10);
00237 opt.parse(argc,argv);
00238 switch (opt.model()) {
00239 case MODEL_SUMCOST:
00240 IntMinimizeScript::run<SumCostWarehouses,BAB,Options>(opt);
00241 break;
00242 case MODEL_LEXCOST:
00243 IntLexMinimizeScript::run<LexCostWarehouses,BAB,Options>(opt);
00244 break;
00245 }
00246 return 0;
00247 }
00248
00249
00250