Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
warehouses.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2005
8  *
9  * Last modified:
10  * $Date$ by $Author$
11  * $Revision$
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include <gecode/driver.hh>
39 #include <gecode/int.hh>
40 #include <gecode/minimodel.hh>
41 
42 using namespace Gecode;
43 
45 const int n_warehouses = 5;
47 const int n_stores = 10;
48 
50 const int c_fixed = 30;
51 
53 const int capacity[n_warehouses] = {
54  1, 4, 2, 1, 3
55 };
56 
59  {20, 24, 11, 25, 30},
60  {28, 27, 82, 83, 74},
61  {74, 97, 71, 96, 70},
62  { 2, 55, 73, 69, 61},
63  {46, 96, 59, 83, 4},
64  {42, 22, 29, 67, 59},
65  { 1, 5, 73, 59, 56},
66  {10, 73, 13, 43, 96},
67  {93, 35, 63, 85, 46},
68  {47, 65, 55, 71, 95}
69 };
70 
72 enum {
75 };
76 
103 template<class Script>
104 class Warehouses : public Script {
105 protected:
112 public:
115  : Script(opt),
116  supplier(*this, n_stores, 0, n_warehouses-1),
117  open(*this, n_warehouses, 0, 1),
118  c_store(*this, n_stores) {
119 
120  // A warehouse is open, if it supplies to a store
121  for (int s=0; s<n_stores; s++)
122  element(*this, open, supplier[s], 1);
123 
124  // Compute cost for each warehouse
125  for (int s=0; s<n_stores; s++) {
127  c_store[s] = expr(*this, element(c, supplier[s]));
128  }
129 
130  // Do not exceed capacity
131  {
133  for (int w=0; w<n_warehouses; w++)
134  c[w] = IntSet(0,capacity[w]);
135  count(*this, supplier, c, IPL_DOM);
136  }
137 
138  // Branch with largest minimum regret on store cost
139  branch(*this, c_store, INT_VAR_REGRET_MIN_MAX(), INT_VAL_MIN());
140  // Branch by assigning a supplier to each store
141  branch(*this, supplier, INT_VAR_NONE(), INT_VAL_MIN());
142  }
145  supplier.update(*this, s.supplier);
146  open.update(*this, s.open);
147  c_store.update(*this, s.c_store);
148  }
149 };
150 
151 
153 class SumCostWarehouses : public Warehouses<IntMinimizeScript> {
154 protected:
157 public:
160  : Warehouses<IntMinimizeScript>(opt) {
161  // Compute total cost
162  c_total = expr(*this, c_fixed*sum(open) + sum(c_store));
163  }
165  virtual IntVar cost(void) const {
166  return c_total;
167  }
170  c_total.update(*this, s.c_total);
171  }
173  virtual Space* copy(void) {
174  return new SumCostWarehouses(*this);
175  }
177  virtual void
178  print(std::ostream& os) const {
179  os << "\tSupplier: " << supplier << std::endl
180  << "\tOpen warehouses: " << open << std::endl
181  << "\tStore cost: " << c_store << std::endl
182  << "\tTotal cost: " << c_total << std::endl
183  << std::endl;
184  }
185 };
186 
187 
189 class LexCostWarehouses : public Warehouses<IntLexMinimizeScript> {
190 protected:
195 public:
199  // Compute costs
200  c_open = expr(*this, sum(open));
201  c_stores = expr(*this, sum(c_store));
202  }
204  virtual IntVarArgs cost(void) const {
205  IntVarArgs c(2);
206  c[0] = c_open; c[1] = c_stores;
207  return c;
208  }
212  c_open.update(*this, s.c_open);
213  c_stores.update(*this, s.c_stores);
214  }
216  virtual Space* copy(void) {
217  return new LexCostWarehouses(*this);
218  }
220  virtual void
221  print(std::ostream& os) const {
222  os << "\tSupplier: " << supplier << std::endl
223  << "\tOpen warehouses: " << open << std::endl
224  << "\tOpen cost: " << c_open << std::endl
225  << "\tStores cost: " << c_stores << std::endl
226  << std::endl;
227  }
228 };
229 
233 int
234 main(int argc, char* argv[]) {
235  Options opt("Warehouses");
236  opt.model(MODEL_SUMCOST);
237  opt.model(MODEL_SUMCOST, "sum", "use sum of costs");
238  opt.model(MODEL_LEXCOST, "lex", "use lexicographic cost");
239  opt.solutions(0);
240  opt.iterations(10);
241  opt.parse(argc,argv);
242  switch (opt.model()) {
243  case MODEL_SUMCOST:
244  IntMinimizeScript::run<SumCostWarehouses,BAB,Options>(opt);
245  break;
246  case MODEL_LEXCOST:
247  IntLexMinimizeScript::run<LexCostWarehouses,BAB,Options>(opt);
248  break;
249  }
250  return 0;
251 }
252 
253 // STATISTICS: example-any
254 
BoolVarArray open
Is a warehouse open (warehouse needed)
Definition: warehouses.cpp:109
const int c_supply[n_stores][n_warehouses]
Cost for supply a store by a warehouse.
Definition: warehouses.cpp:58
virtual void print(std::ostream &os) const
Print solution.
Definition: warehouses.cpp:221
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition: var.hpp:100
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf, FloatVarValPrint vvp)
Branch over x with variable selection vars and value selection vals.
Definition: branch.cpp:43
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntPropLevel)
Post propagator for .
Definition: count.cpp:44
void update(Space &home, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1060
IntVar c_stores
Cost for stores.
Definition: warehouses.cpp:194
virtual IntVar cost(void) const
Return solution cost.
Definition: warehouses.cpp:165
virtual IntVarArgs cost(void) const
Return solution cost.
Definition: warehouses.cpp:204
virtual void print(std::ostream &os) const
Print solution.
Definition: warehouses.cpp:178
Integer variable array.
Definition: int.hh:742
Model with cost defined lexicographically.
Definition: warehouses.cpp:189
IntVarBranch INT_VAR_REGRET_MIN_MAX(BranchTbl tbl)
Select variable with largest min-regret.
Definition: var.hpp:295
virtual Space * copy(void)
Copy during cloning.
Definition: warehouses.cpp:173
LexCostWarehouses(LexCostWarehouses &s)
Constructor for cloning s.
Definition: warehouses.cpp:210
Computation spaces.
Definition: core.hpp:1668
Parametric base-class for scripts.
Definition: driver.hh:733
void iterations(unsigned int i)
Set default number of iterations.
Definition: options.hpp:465
int main(int argc, char *argv[])
Main-function.
Definition: warehouses.cpp:234
const int n_warehouses
Number of warehouses.
Definition: warehouses.cpp:45
Gecode::FloatVal c(-8, 8)
Model with cost defined as sum.
Definition: warehouses.cpp:153
Options opt
The options.
Definition: test.cpp:101
Example: Locating warehouses
Definition: warehouses.cpp:104
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition: val.hpp:59
void update(Space &home, VarImpVar< VarImp > &y)
Update this variable to be a clone of variable y.
Definition: var.hpp:128
Integer sets.
Definition: int.hh:174
Use lexicographic cost.
Definition: warehouses.cpp:74
Passing integer variables.
Definition: int.hh:637
Passing integer arguments.
Definition: int.hh:608
Warehouses(const Options &opt)
Actual model.
Definition: warehouses.cpp:114
BoolVar expr(Home home, const BoolExpr &e, IntPropLevel ipl)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:632
Boolean variable array.
Definition: int.hh:787
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:544
Warehouses(Warehouses &s)
Constructor for cloning s.
Definition: warehouses.cpp:144
IntVarArray supplier
Which warehouse supplies a store.
Definition: warehouses.cpp:107
SumCostWarehouses(const Options &opt)
Actual model.
Definition: warehouses.cpp:159
Integer variables.
Definition: int.hh:351
SumCostWarehouses(SumCostWarehouses &s)
Constructor for cloning s.
Definition: warehouses.cpp:169
Domain propagation Options: basic versus advanced propagation.
Definition: int.hh:958
Use sum as total cost.
Definition: warehouses.cpp:73
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition: options.hpp:287
const int n_stores
Number of stores.
Definition: warehouses.cpp:47
virtual Space * copy(void)
Copy during cloning.
Definition: warehouses.cpp:216
void model(int v)
Set default model value.
Definition: options.hpp:181
Gecode toplevel namespace
LinFloatExpr sum(const FloatVarArgs &x)
Construct linear float expression as sum of float variables.
Definition: float-expr.cpp:548
LexCostWarehouses(const Options &opt)
Actual model.
Definition: warehouses.cpp:197
const int capacity[n_warehouses]
Capacity of a single warehouse.
Definition: warehouses.cpp:53
IntVar c_open
Cost for open warehouses.
Definition: warehouses.cpp:192
IntVar c_total
Total cost.
Definition: warehouses.cpp:156
Options for scripts
Definition: driver.hh:370
const int c_fixed
Fixed cost for one warehouse.
Definition: warehouses.cpp:50
void element(Home home, IntSharedArray c, IntVar x0, IntVar x1, IntPropLevel)
Post domain consistent propagator for .
Definition: element.cpp:43
IntVarArray c_store
Cost of a store.
Definition: warehouses.cpp:111