Generated on Thu Mar 7 2013 10:21:15 for Gecode by doxygen 1.8.3.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: 2010-10-07 20:52:01 +1100 (Thu, 07 Oct 2010) $ by $Author: schulte $
11  * $Revision: 11473 $
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 MinimizeScript {
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) : MinimizeScript(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  MinimizeScript::run<Warehouses,BAB,Options>(opt);
181  return 0;
182 }
183 
184 // STATISTICS: example-any
185