-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWL_Validator.cpp
More file actions
295 lines (267 loc) · 8.92 KB
/
Copy pathWL_Validator.cpp
File metadata and controls
295 lines (267 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// File WL_Validator.cpp
// Validates the solution of the Warehouse Location Problem
// with Store Incompatibilities. NOTE: indexes in the input file and
// the output list-format files are 1-based, in the code they are 0-based
// Compile with:
// g++ -o WL_Validator.exe WL_Validator.cpp
// Run with:
// ./WL_Validator.exe <input_file> <solution_file>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
class WL_Input
{
public:
WL_Input(string file_name);
unsigned Stores() const { return stores; }
unsigned Warehouses() const { return warehouses; }
unsigned Capacity(unsigned w) const { return capacity[w]; }
unsigned FixedCost(unsigned w) const { return fixed_cost[w]; }
unsigned AmountOfGoods(unsigned s) const { return amount_of_goods[s]; }
unsigned SupplyCost(unsigned s, unsigned w) const { return supply_cost[s][w]; }
unsigned StoreIncompatibilities() const { return store_incompatibilities.size(); }
pair<unsigned, unsigned> StoreIncompatibility(unsigned i) const { return store_incompatibilities[i]; }
private:
unsigned stores, warehouses;
vector<unsigned> capacity;
vector<unsigned> fixed_cost;
vector<unsigned> amount_of_goods;
vector<vector<unsigned>> supply_cost;
vector<pair<unsigned, unsigned>> store_incompatibilities;
};
class WL_Output
{
public:
WL_Output(const WL_Input& i, string file_name);
unsigned Supply(unsigned s, unsigned w) const { return supply[s][w]; }
unsigned Load(unsigned w) const { return load[w]; }
unsigned ResidualCapacity(unsigned w) const { return in.Capacity(w) - load[w]; }
unsigned AssignedGoods(unsigned s) const { return assigned_goods[s]; }
unsigned ResidualAmount(unsigned s) const { return in.AmountOfGoods(s) - assigned_goods[s]; }
bool Compatible(unsigned w, unsigned s) const { return compatible[w][s]; }
void Assign(unsigned s, unsigned w, unsigned q); // assign q goods of s to w
unsigned ComputeCost() const;
unsigned ComputeSupplyCost() const;
unsigned ComputeOpeningCost() const;
unsigned ComputeViolations() const;
void PrintCosts(ostream& os) const;
void PrintViolations(ostream& os) const;
private:
const WL_Input& in;
vector<vector<unsigned>> supply; // main data
vector<unsigned> assigned_goods; // quantity of goods of each store already assigned to warehouses
vector<unsigned> load; // quantity of goods of each warehouse assigned to stores
vector<vector<bool>> compatible; // warehouse/store compatibility matrix based on current assignment
// NOTE: opening is implicit, based on load > 0
};
WL_Input::WL_Input(string file_name)
{
const unsigned MAX_DIM = 100;
unsigned w, s, s2;
char ch, buffer[MAX_DIM];
ifstream is(file_name);
if(!is)
{
cerr << "Cannot open input file " << file_name << endl;
exit(1);
}
is >> buffer >> ch >> warehouses >> ch;
is >> buffer >> ch >> stores >> ch;
capacity.resize(warehouses);
fixed_cost.resize(warehouses);
amount_of_goods.resize(stores);
supply_cost.resize(stores,vector<unsigned>(warehouses));
// read capacity
is.ignore(MAX_DIM,'['); // read "... Capacity = ["
for (w = 0; w < warehouses; w++)
is >> capacity[w] >> ch;
// read fixed costs
is.ignore(MAX_DIM,'['); // read "... FixedCosts = ["
for (w = 0; w < warehouses; w++)
is >> fixed_cost[w] >> ch;
// read goods
is.ignore(MAX_DIM,'['); // read "... Goods = ["
for (s = 0; s < stores; s++)
is >> amount_of_goods[s] >> ch;
// read supply costs
is.ignore(MAX_DIM,'['); // read "... SupplyCost = ["
is >> ch; // read first '|'
for (s = 0; s < stores; s++)
{
for (w = 0; w < warehouses; w++)
is >> supply_cost[s][w] >> ch;
}
is >> ch >> ch;
// read store incompatibilities
unsigned incompatibilities;
is >> buffer >> ch >> incompatibilities >> ch;
store_incompatibilities.resize(incompatibilities);
is.ignore(MAX_DIM,'['); // read "... IncompatiblePairs = ["
for (unsigned i = 0; i < incompatibilities; i++)
{
is >> ch >> s >> ch >> s2;
store_incompatibilities[i].first = s - 1;
store_incompatibilities[i].second = s2 - 1;
}
is >> ch >> ch;
}
WL_Output::WL_Output(const WL_Input& my_in, string file_name)
: in(my_in), supply(in.Stores(),vector<unsigned>(in.Warehouses(),0)),
assigned_goods(in.Stores(),0), load(in.Warehouses(),0),
compatible(in.Warehouses(),vector<bool>(in.Stores(),true))
{
unsigned s, w, q;
char ch;
ifstream is(file_name);
if(!is)
{
cerr << "Cannot open solution file " << file_name << endl;
exit(1);
}
is >> ch;
if (ch == '[') // matrix format
{
for (s = 0; s < in.Stores(); s++)
{
is >> ch;
for (w = 0; w < in.Warehouses(); w++)
{
is >> q >> ch;
Assign(s,w,q);
}
}
is >> ch;
}
else // list format
{
while (is >> ch >> s >> ch >> w >> ch >> q >> ch >> ch)
{
Assign(s-1,w-1,q);
}
is >> ch;
}
}
void WL_Output::Assign(unsigned s, unsigned w, unsigned q)
{
unsigned i;
if (s >= in.Stores())
{
cerr << "Store " << s << " out of range" << endl;
exit(1);
}
if (w >= in.Warehouses())
{
cerr << "Warehouse " << w << " out of range" << endl;
exit(1);
}
if (assigned_goods[s] + q > in.AmountOfGoods(s))
{
cerr << "Quantity " << q << " out of range for store " << s << endl;
exit(1);
}
supply[s][w] += q;
assigned_goods[s] += q;
load[w] += q;
for (i = 0; i < in.StoreIncompatibilities(); i++)
{
if (in.StoreIncompatibility(i).first == s)
compatible[w][in.StoreIncompatibility(i).second] = false;
else if (in.StoreIncompatibility(i).second == s)
compatible[w][in.StoreIncompatibility(i).first] = false;
}
}
unsigned WL_Output::ComputeCost() const
{
return ComputeSupplyCost() + ComputeOpeningCost();
}
unsigned WL_Output::ComputeSupplyCost() const
{
unsigned s, w, cost = 0;
for (s = 0; s < in.Stores(); s++)
for (w = 0; w < in.Warehouses(); w++)
cost += supply[s][w] * in.SupplyCost(s,w);
return cost;
}
unsigned WL_Output::ComputeOpeningCost() const
{
unsigned w, cost = 0;
for (w = 0; w < in.Warehouses(); w++)
if (load[w] > 0)
cost += in.FixedCost(w);
return cost;
}
unsigned WL_Output::ComputeViolations() const
{
unsigned s, w, i, violations = 0;
for (s = 0; s < in.Stores(); s++)
if (assigned_goods[s] < in.AmountOfGoods(s))
violations++;
for (w = 0; w < in.Warehouses(); w++)
if (load[w] > in.Capacity(w))
violations++;
for (i = 0; i < in.StoreIncompatibilities(); i++)
for (w = 0; w < in.Warehouses(); w++)
if (supply[in.StoreIncompatibility(i).first][w] > 0
&& supply[in.StoreIncompatibility(i).second][w] > 0)
violations++;
return violations;
}
void WL_Output::PrintCosts(ostream& os) const
{
unsigned s, w, cost = 0;
for (s = 0; s < in.Stores(); s++)
for (w = 0; w < in.Warehouses(); w++)
if (supply[s][w] > 0)
{
cost += supply[s][w] * in.SupplyCost(s,w);
os << "Moving " << supply[s][w] << " goods from warehourse " << w+1
<< " to store " << s+1 << ", cost " << supply[s][w] << "x"
<< in.SupplyCost(s,w) << " = " << supply[s][w] * in.SupplyCost(s,w)
<< " (" << cost << ")" << endl;
}
for (w = 0; w < in.Warehouses(); w++)
if (load[w] > 0)
{
cost += in.FixedCost(w);
os << "Opening warehouse " << w+1 << ", cost " << in.FixedCost(w)
<< " (" << cost << ")" << endl;
}
}
void WL_Output::PrintViolations(ostream& os) const
{
unsigned s, w, i;
for (s = 0; s < in.Stores(); s++)
if (assigned_goods[s] < in.AmountOfGoods(s))
os << "Goods of store " << s+1 << " are not moved completely (ammount = "
<< in.AmountOfGoods(s) << ", moved = " << assigned_goods[s] << ")" << endl;
for (w = 0; w < in.Warehouses(); w++)
if (load[w] > in.Capacity(w))
os << "Goods of warehouses " << w+1 << " exceed its capacity (capacity = "
<< in.Capacity(w) << ", moved = " << load[w] << ")" << endl;
for (i = 0; i < in.StoreIncompatibilities(); i++)
for (w = 0; w < in.Warehouses(); w++)
if (supply[in.StoreIncompatibility(i).first][w] > 0
&& supply[in.StoreIncompatibility(i).second][w] > 0)
os << "Warehouses " << w+1 << " supplies incompatible stores "
<< in.StoreIncompatibility(i).first+1 << " and "
<< in.StoreIncompatibility(i).second+1 << endl;
}
int main(int argc, char* argv[])
{
string instance;
if (argc != 3)
{
cerr << "Usage: " << argv[0] << " <input_file> <solution_file>" << endl
<< "Input file in .dzn format, solution file either in matrix format (within []) or list format (within {})." << endl;
exit(1);
}
WL_Input in(argv[1]);
WL_Output out(in, argv[2]);
out.PrintCosts(cout);
out.PrintViolations(cout);
cout << "Number of violations: " << out.ComputeViolations() << endl;
cout << "Cost: " << out.ComputeCost() << " = " << out.ComputeSupplyCost() << " (supply cost) + "
<< out.ComputeOpeningCost() << " (opening cost)" << endl << endl;
return 0;
}