Generated on Thu Mar 16 2017 03:24:14 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: 2015-09-11 16:29:45 +0200 (Fri, 11 Sep 2015) $ by $Author: schulte $
11  * $Revision: 14672 $
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 
71 
72 
99 class Warehouses : public IntMinimizeScript {
100 protected:
109 public:
112  : IntMinimizeScript(opt),
113  supplier(*this, n_stores, 0, n_warehouses-1),
114  open(*this, n_warehouses, 0, 1),
115  c_store(*this, n_stores) {
116 
117  // A warehouse is open, if it supplies to a store
118  for (int s=0; s<n_stores; s++)
119  element(*this, open, supplier[s], 1);
120 
121  // Compute cost for each warehouse
122  for (int s=0; s<n_stores; s++) {
124  c_store[s] = expr(*this, element(c, supplier[s]));
125  }
126 
127  // Do not exceed capacity
128  {
130  for (int w=0; w<n_warehouses; w++)
131  c[w] = IntSet(0,capacity[w]);
132  count(*this, supplier, c, IPL_DOM);
133  }
134 
135  // Compute total cost
136  c_total = expr(*this, c_fixed*sum(open) + sum(c_store));
137 
138  // Branch with largest minimum regret on store cost
139  branch(*this, c_store, INT_VAR_REGRET_MIN_MAX(), INT_VAL_MIN());
140 
141  // Branch by assigning a supplier to each store
142  branch(*this, supplier, INT_VAR_NONE(), INT_VAL_MIN());
143  }
145  virtual IntVar cost(void) const {
146  return c_total;
147  }
149  Warehouses(bool share, Warehouses& s) : IntMinimizeScript(share,s) {
150  supplier.update(*this, share, s.supplier);
151  open.update(*this, share, s.open);
152  c_store.update(*this, share, s.c_store);
153  c_total.update(*this, share, s.c_total);
154  }
155 
157  virtual Space*
158  copy(bool share) {
159  return new Warehouses(share,*this);
160  }
162  virtual void
163  print(std::ostream& os) const {
164  os << "\tSupplier: " << supplier << std::endl
165  << "\tOpen warehouses: " << open << std::endl
166  << "\tStore cost: " << c_store << std::endl
167  << "\tTotal cost: " << c_total << std::endl
168  << std::endl;
169  }
170 };
171 
175 int
176 main(int argc, char* argv[]) {
177  Options opt("Warehouses");
178  opt.solutions(0);
179  opt.iterations(10);
180  opt.parse(argc,argv);
181  IntMinimizeScript::run<Warehouses,BAB,Options>(opt);
182  return 0;
183 }
184 
185 // STATISTICS: example-any
186 
const int c_supply[n_stores][n_warehouses]
Cost for supply a store by a warehouse.
Definition: warehouses.cpp:58
IntVarArray supplier
Which warehouse supplies a store.
Definition: warehouses.cpp:102
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition: var.hpp:108
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntPropLevel)
Post propagator for .
Definition: count.cpp:44
Warehouses(const Options &opt)
Actual model.
Definition: warehouses.cpp:111
void update(Space &home, bool share, VarImpVar< VarImp > &y)
Update this variable to be a clone of variable y.
Definition: var.hpp:128
virtual Space * copy(bool share)
Copy during cloning.
Definition: warehouses.cpp:158
Integer variable array.
Definition: int.hh:742
virtual IntVar cost(void) const
Return solution cost.
Definition: warehouses.cpp:145
Computation spaces.
Definition: core.hpp:1672
Parametric base-class for scripts.
Definition: driver.hh:703
void iterations(unsigned int i)
Set default number of iterations.
Definition: options.hpp:465
IntVarBranch INT_VAR_REGRET_MIN_MAX(BranchTbl tbl)
Select variable with largest min-regret.
Definition: var.hpp:277
int main(int argc, char *argv[])
Main-function.
Definition: warehouses.cpp:176
const int n_warehouses
Number of warehouses.
Definition: warehouses.cpp:45
virtual void print(std::ostream &os) const
Print solution.
Definition: warehouses.cpp:163
void update(Space &, bool share, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1072
Gecode::FloatVal c(-8, 8)
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
Options opt
The options.
Definition: test.cpp:101
IntVarArray c_store
Cost of a store.
Definition: warehouses.cpp:106
Example: Locating warehouses
Definition: warehouses.cpp:99
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition: val.hpp:68
Integer sets.
Definition: int.hh:172
Passing integer arguments.
Definition: int.hh:608
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
Warehouses(bool share, Warehouses &s)
Constructor for cloning s.
Definition: warehouses.cpp:149
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:477
Integer variables.
Definition: int.hh:351
Domain propagation Preferences: prefer speed or memory.
Definition: int.hh:946
IntVar c_total
Total cost.
Definition: warehouses.cpp:108
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition: options.hpp:287
BoolVarArray open
Is a warehouse open (warehouse needed)
Definition: warehouses.cpp:104
const int n_stores
Number of stores.
Definition: warehouses.cpp:47
Gecode toplevel namespace
LinFloatExpr sum(const FloatVarArgs &x)
Construct linear float expression as sum of float variables.
Definition: float-expr.cpp:548
const int capacity[n_warehouses]
Capacity of a single warehouse.
Definition: warehouses.cpp:53
Options for scripts
Definition: driver.hh:366
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