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