Generated on Sat Feb 7 2015 02:01:13 for Gecode by doxygen 1.8.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  * Last modified:
10  * $Date: 2013-07-08 14:22:40 +0200 (Mon, 08 Jul 2013) $ by $Author: schulte $
11  * $Revision: 13820 $
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  : 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, ICL_DOM);
132  }
133 
134  // Compute total cost
135  c_total = expr(*this, c_fixed*sum(open) + sum(c_store));
136 
137  // Branch with largest minimum regret on store cost
138  branch(*this, c_store, INT_VAR_REGRET_MIN_MAX(), INT_VAL_MIN());
139 
140  // Branch by assigning a supplier to each store
141  branch(*this, supplier, INT_VAR_NONE(), INT_VAL_MIN());
142  }
144  virtual IntVar cost(void) const {
145  return c_total;
146  }
148  Warehouses(bool share, Warehouses& s) : IntMinimizeScript(share,s) {
149  supplier.update(*this, share, s.supplier);
150  open.update(*this, share, s.open);
151  c_store.update(*this, share, s.c_store);
152  c_total.update(*this, share, s.c_total);
153  }
154 
156  virtual Space*
157  copy(bool share) {
158  return new Warehouses(share,*this);
159  }
161  virtual void
162  print(std::ostream& os) const {
163  os << "\tSupplier: " << supplier << std::endl
164  << "\tOpen warehouses: " << open << std::endl
165  << "\tStore cost: " << c_store << std::endl
166  << "\tTotal cost: " << c_total << std::endl
167  << std::endl;
168  }
169 };
170 
174 int
175 main(int argc, char* argv[]) {
176  Options opt("Warehouses");
177  opt.solutions(0);
178  opt.iterations(10);
179  opt.parse(argc,argv);
180  IntMinimizeScript::run<Warehouses,BAB,Options>(opt);
181  return 0;
182 }
183 
184 // STATISTICS: example-any
185 
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
Warehouses(const Options &)
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:157
Integer variable array.
Definition: int.hh:741
Computation spaces.
Definition: core.hpp:1362
Parametric base-class for scripts.
Definition: driver.hh:622
void iterations(unsigned int i)
Set default number of iterations.
Definition: options.hpp:385
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:175
const int n_warehouses
Number of warehouses.
Definition: warehouses.cpp:45
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)
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
virtual void print(std::ostream &os) const
Print solution.
Definition: warehouses.cpp:162
Integer sets.
Definition: int.hh:171
void element(Home home, IntSharedArray c, IntVar x0, IntVar x1, IntConLevel)
Post domain consistent propagator for .
Definition: element.cpp:43
Passing integer arguments.
Definition: int.hh:607
Boolean variable array.
Definition: int.hh:786
Warehouses(bool share, Warehouses &s)
Constructor for cloning s.
Definition: warehouses.cpp:148
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:331
BoolVar expr(Home home, const BoolExpr &e, IntConLevel icl)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:632
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntConLevel)
Post propagator for .
Definition: count.cpp:44
Integer variables.
Definition: int.hh:350
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:243
BoolVarArray open
Is a warehouse open (warehouse needed)
Definition: warehouses.cpp:104
virtual IntVar cost(void) const
Return solution cost.
Definition: warehouses.cpp:144
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
BrancherHandle 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 for scripts
Definition: driver.hh:326
const int c_fixed
Fixed cost for one warehouse.
Definition: warehouses.cpp:50
Domain propagation or consistency.
Definition: int.hh:940