Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.0
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
38using namespace Gecode;
39
41const int n_warehouses = 5;
43const int n_stores = 10;
44
46const int c_fixed = 30;
47
49const 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
68enum {
71};
72
99template<class Script>
100class Warehouses : public Script {
101protected:
108public:
110 Warehouses(const Options& opt)
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
136 // Branch by assigning a supplier to each store
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
149class SumCostWarehouses : public Warehouses<IntMinimizeScript> {
150protected:
153public:
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 }
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
185class LexCostWarehouses : public Warehouses<IntLexMinimizeScript> {
186protected:
191public:
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 }
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
227int
228main(int argc, char* argv[]) {
229 Options opt("Warehouses");
230 opt.model(MODEL_SUMCOST);
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:
239 break;
240 case MODEL_LEXCOST:
242 break;
243 }
244 return 0;
245}
246
247// STATISTICS: example-any
248
Boolean variable array.
Definition int.hh:808
Parametric base-class for scripts.
Definition driver.hh:729
static void run(const Options &opt, Script *s=NULL)
Definition script.hpp:290
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
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.
IntVar c_open
Cost for open warehouses.
virtual IntVarArgs cost(void) const
Return solution cost.
IntVar c_stores
Cost for stores.
virtual void print(std::ostream &os) const
Print solution.
LexCostWarehouses(const Options &opt)
Actual model.
LexCostWarehouses(LexCostWarehouses &s)
Constructor for cloning s.
virtual Space * copy(void)
Copy during cloning.
Model with cost defined as sum.
IntVar c_total
Total cost.
virtual Space * copy(void)
Copy during cloning.
virtual void print(std::ostream &os) const
Print solution.
virtual IntVar cost(void) const
Return solution cost.
SumCostWarehouses(SumCostWarehouses &s)
Constructor for cloning s.
SumCostWarehouses(const Options &opt)
Actual model.
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
Example: Locating warehouses
IntVarArray supplier
Which warehouse supplies a store.
int main(int argc, char *argv[])
Main-function.
BoolVarArray open
Is a warehouse open (warehouse needed)
IntVarArray c_store
Cost of a store.
Warehouses(Warehouses &s)
Constructor for cloning s.
Warehouses(const Options &opt)
Actual model.
const int n_stores
Number of stores.
const int c_fixed
Fixed cost for one warehouse.
@ MODEL_LEXCOST
Use lexicographic cost.
@ MODEL_SUMCOST
Use sum as total cost.
const int c_supply[n_stores][n_warehouses]
Cost for supply a store by a warehouse.
const int n_warehouses
Number of warehouses.
const int capacity[n_warehouses]
Capacity of a single warehouse.
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Branch over x with variable selection vars and value selection vals.
Definition branch.cpp:39
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:979
Gecode toplevel namespace
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntPropLevel ipl=IPL_DEF)
Post propagator for .
Definition count.cpp:40
IntVar expr(Home home, const LinIntExpr &e, const IntPropLevels &ipls=IntPropLevels::def)
Post linear expression and return its value.
Definition int-expr.cpp:915
IntVarBranch INT_VAR_REGRET_MIN_MAX(BranchTbl tbl=nullptr)
Select variable with largest min-regret.
Definition var.hpp:291
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition var.hpp:96
void element(Home home, IntSharedArray n, IntVar x0, IntVar x1, IntPropLevel ipl=IPL_DEF)
Post domain consistent propagator for .
Definition element.cpp:39
LinIntExpr sum(const IntVarArgs &x)
Construct linear expression as sum of integer variables.
Definition int-expr.cpp:880
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition val.hpp:55