-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution_verifier.cpp
295 lines (252 loc) · 9.21 KB
/
solution_verifier.cpp
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
295
#include "solution_verifier.h"
#include "instance.h"
#include "solution.h"
#include <sstream>
#include <algorithm>
#include <set>
/*************************************************************************************************/
/* BP ********************************************************************************************/
/*************************************************************************************************/
bool SolutionVerifier<BP>::verify(const Instance<BP>& inst, const Solution<BP>& sol, std::vector<std::string>* error_msg)
{
bool ret = true;
std::vector<int> levels(sol.total_bins, 0); // size of each bin
for (int i : inst.I) {
int bin = sol.item_to_bins[i];
if (bin >= sol.total_bins) {
if (error_msg) {
std::stringstream ss;
ss << "Detected bin with id " << bin << " but solutions claims that there are only " << sol.total_bins << ".";
error_msg->push_back(ss.str());
}
ret = false;
} else if (bin < 0) {
if (error_msg) {
std::stringstream ss;
ss << "Item " << i << " is not assigned to any bin.";
error_msg->push_back(ss.str());
}
ret = false;
} else {
levels[bin] += inst.s[i];
}
}
for (int bin = 0; bin < sol.total_bins; bin++) {
if (levels[bin] > inst.smax) {
if (error_msg) {
std::stringstream ss;
ss << "Bin " << bin << " of size " << levels[bin] << " exceeds maximum capaicty (" << inst.smax << ").";
error_msg->push_back(ss.str());
}
ret = false;
}
}
return ret;
}
/*************************************************************************************************/
/* MLBP ******************************************************************************************/
/*************************************************************************************************/
bool SolutionVerifier<MLBP>::verify(const Instance<MLBP>& inst, const Solution<MLBP>& sol, std::vector<std::string>* error_msg)
{
bool ret = true;
//1. All items are in the bins of every level
//2. No bin contents exceed its capacity
//3. Once items/bins have been put into the same bin, they have to stay together in all the upcoming bins
std::vector<std::vector<int>> bins; // size of each bin
std::vector<std::vector<bool>> used_binsItems;
for (int k : inst.M) {
bins.push_back(std::vector<int>(inst.n[k], 0));
used_binsItems.push_back(std::vector<bool>(inst.n[k - 1], false));
for (int i = 0; i < inst.n[0]; i++) {
int curr = i;
if (k > 1) {
curr = sol.item_to_bins[k - 2][i];
}
int bin_idx = sol.item_to_bins[k - 1][i];
//1.
if (bin_idx < 0) {
if (error_msg) {
std::stringstream ss;
ss << "Item " << i << " has not been assigned to any bin at level " << k;
error_msg->push_back(ss.str());
}
ret = false;
return ret;
}
if (!used_binsItems[k - 1][curr]) {
used_binsItems[k - 1][curr] = true;
bins[k - 1][bin_idx] += inst.s[k - 1][curr];
}
}
}
for (int k : inst.M) {
for (int j = 0; j < inst.n[k]; j++) {
//2.
if (bins[k - 1][j] > inst.w[k][j]) {
if (error_msg) {
std::stringstream ss;
ss << "Bin " << j << " at level " << k << " with contents of size " << bins[k - 1][j] << " exceeds maximum capaicty (" << inst.w[k][j] << ").";
error_msg->push_back(ss.str());
}
ret = false;
}
}
}
for (int k : inst.M) {
int min = std::min(inst.n[0], inst.n[k]);
std::vector<std::vector<int>> same_bins_set;
for (int i = 0; i < inst.n[k]; i++) {
same_bins_set.push_back(std::vector<int>());
}
for (int i = 0; i < min; i++) {
for (int j = 1; j < same_bins_set[i].size(); j++) {
//3.
if (sol.item_to_bins[k - 1][same_bins_set[i][0]] != sol.item_to_bins[k - 1][same_bins_set[i][j]]) {
if (error_msg) {
std::stringstream ss;
ss << "Items [" << same_bins_set[i][0] << ", " << same_bins_set[i][j] << "]" <<
" were put together in level " << k - 1 << ", but are in seperate bins in level " << k;
error_msg->push_back(ss.str());
}
ret = false;
}
}
}
for (int i = 0; i < min; i++) {
int item_or_bin_idx = sol.item_to_bins[k - 1][i];
same_bins_set[item_or_bin_idx].push_back(i);
}
}
return ret;
}
/*************************************************************************************************/
/* CCMLBP ****************************************************************************************/
/*************************************************************************************************/
bool SolutionVerifier<CCMLBP>::verify(const Instance<CCMLBP>& inst, const Solution<CCMLBP>& sol, std::vector<std::string>* error_msg)
{
bool ret = true;
// TODO
return ret;
}
/*************************************************************************************************/
/* MLBPCC ****************************************************************************************/
/*************************************************************************************************/
bool SolutionVerifier<MLBPCC>::verify(const Instance<MLBPCC>& inst, const Solution<MLBPCC>& sol, std::vector<std::string>* error_msg)
{
bool ret = true;
//1. All items are in the bins of every level
//2. No bin contents exceed its capacity
//3. Once items/bins have been put into the same bin, they have to stay together in all the upcoming bins
//4. Conflicting items cannot share any of the bins
std::vector<std::vector<int>> bins; // size of each bin
std::vector<std::vector<bool>> used_binsItems;
for (int k : inst.M) {
bins.push_back(std::vector<int>(inst.n[k], 0));
used_binsItems.push_back(std::vector<bool>(inst.n[k - 1], false));
for (int i = 0; i < inst.n[0]; i++) {
int curr = i;
if (k > 1) {
curr = sol.item_to_bins[k - 2][i];
}
int bin_idx = sol.item_to_bins[k - 1][i];
//1.
if (bin_idx < 0) {
if (error_msg) {
std::stringstream ss;
ss << "Item " << i << " has not been assigned to any bin at level " << k;
error_msg->push_back(ss.str());
}
ret = false;
return ret;
}
if (!used_binsItems[k - 1][curr]) {
used_binsItems[k - 1][curr] = true;
bins[k - 1][bin_idx] += inst.s[k - 1][curr];
}
}
}
for (int k : inst.M) {
for (int j = 0; j < inst.n[k]; j++) {
//2.
if (bins[k - 1][j] > inst.w[k][j]) {
if (error_msg) {
std::stringstream ss;
ss << "Bin " << j << " at level " << k << " with contents of size " << bins[k - 1][j] << " exceeds maximum capaicty (" << inst.w[k][j] << ").";
error_msg->push_back(ss.str());
}
ret = false;
}
}
}
for (int k : inst.M) {
int min = std::min(inst.n[0], inst.n[k]);
std::vector<std::vector<int>> same_bins_set;
for (int i = 0; i < inst.n[k]; i++) {
same_bins_set.push_back(std::vector<int>());
}
for (int i = 0; i < min; i++) {
for (int j = 1; j < same_bins_set[i].size(); j++) {
//3.
if (sol.item_to_bins[k - 1][same_bins_set[i][0]] != sol.item_to_bins[k - 1][same_bins_set[i][j]]) {
if (error_msg) {
std::stringstream ss;
ss << "Items [" << same_bins_set[i][0] << ", " << same_bins_set[i][j] << "]" <<
" were put together in level " << k - 1 << ", but are in seperate bins in level " << k;
error_msg->push_back(ss.str());
}
ret = false;
}
}
}
for (int i = 0; i < min; i++) {
int item_or_bin_idx = sol.item_to_bins[k - 1][i];
same_bins_set[item_or_bin_idx].push_back(i);
}
}
for (int k : inst.M) {
for (int a = 0; a < inst.n[0] - 1; a++) {
for (int b = a + 1; b < inst.n[0]; b++) {
int bin_of_a = sol.item_to_bins[k-1][a];
int bin_of_b = sol.item_to_bins[k - 1][b];
//4.
if (bin_of_a == bin_of_b && inst.conflict[a][b] == 1) {
if (error_msg) {
std::stringstream ss;
ss << "Items [" << a << " and " << b << "]" <<
" are conflicting and were both put into bin " << bin_of_a << " of the level " << k;
error_msg->push_back(ss.str());
}
ret = false;
}
}
}
}
return ret;
}
/*************************************************************************************************/
/* MLBPPO ****************************************************************************************/
/*************************************************************************************************/
bool SolutionVerifier<MLBPPO>::verify(const Instance<MLBPPO>& inst, const Solution<MLBPPO>& sol, std::vector<std::string>* error_msg)
{
bool ret = true;
// TOD
return ret;
}
/*************************************************************************************************/
/* MLBPTW ****************************************************************************************/
/*************************************************************************************************/
bool SolutionVerifier<MLBPTW>::verify(const Instance<MLBPTW>& inst, const Solution<MLBPTW>& sol, std::vector<std::string>* error_msg)
{
bool ret = true;
// TOD
return ret;
}
/*************************************************************************************************/
/* MLBPFC ****************************************************************************************/
/*************************************************************************************************/
bool SolutionVerifier<MLBPFC>::verify(const Instance<MLBPFC>& inst, const Solution<MLBPFC>& sol, std::vector<std::string>* error_msg)
{
bool ret = true;
// TOD
return ret;
}