Repository navigation
Expand file tree
/
Copy pathbin_packing_with_conflicts.hpp
More file actions
368 lines (312 loc) · 10.1 KB
/
Copy pathbin_packing_with_conflicts.hpp
File metadata and controls
368 lines (312 loc) · 10.1 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/**
* Bin packing problem with conflicts
*
* Input:
* - a capacity c
* - n items; for each item j = 1..n, a weight wⱼ
* - a graph G such that each node corresponds to an item
* Problem:
* - Pack all items into bins such that:
* - the total weight of the items of a bin does not exceed the capacity c
* - if there exists an edge between vertex j₁ and vertex j₂ in G, then item
* j₁ and item j₂ must not be in the same bin
* Objective:
* - Minimize the number of bin used.
*
*/
#pragma once
#include "optimizationtools/containers/indexed_set.hpp"
#include "optimizationtools/utils/utils.hpp"
#include <fstream>
#include <iostream>
#include <iomanip>
namespace orproblems
{
namespace bin_packing_with_conflicts
{
using ItemId = int64_t;
using Weight = int64_t;
using BinId = int64_t;
/**
* Structure for an item.
*/
struct Item
{
/** Weight of the item. */
Weight weight;
/** Conflicting items. */
std::vector<ItemId> neighbors;
};
/**
* Instance class for a 'bin_packing_with_conflicts' problem.
*/
class Instance
{
public:
/*
* Getters
*/
/** Get the number of items. */
ItemId number_of_items() const { return items_.size(); }
/** Get an item. */
const Item& item(ItemId item_id) const { return items_[item_id]; }
/** Get the capacity of the bins. */
Weight capacity() const { return capacity_; }
/*
* Outputs
*/
/** Print the instance. */
void format(
std::ostream& os,
int verbosity_level = 1) const
{
if (verbosity_level >= 1) {
os << "Number of items: " << number_of_items() << std::endl;
os << "Capacity: " << capacity() << std::endl;
}
// Print items.
if (verbosity_level >= 2) {
os << std::endl
<< std::setw(12) << "Item"
<< std::setw(12) << "Weight"
<< std::setw(12) << "# confl."
<< std::endl
<< std::setw(12) << "----"
<< std::setw(12) << "------"
<< std::setw(12) << "--------"
<< std::endl;
for (ItemId item_id = 0; item_id < number_of_items(); ++item_id) {
const Item& item = this->item(item_id);
os
<< std::setw(12) << item_id
<< std::setw(12) << item.weight
<< std::setw(12) << item.neighbors.size()
<< std::endl;
}
}
// Print conflicts.
if (verbosity_level >= 3) {
os << std::endl
<< std::setw(12) << "Item 1"
<< std::setw(12) << "Item 2"
<< std::endl
<< std::setw(12) << "------"
<< std::setw(12) << "------"
<< std::endl;
for (ItemId item_id = 0; item_id < number_of_items(); ++item_id) {
for (ItemId item_id_neighbor: item(item_id).neighbors) {
os
<< std::setw(12) << item_id
<< std::setw(12) << item_id_neighbor
<< std::endl;
}
}
}
}
/** Check a certificate. */
std::pair<bool, BinId> check(
const std::string& certificate_path,
std::ostream& os,
int verbosity_level = 1) const
{
std::ifstream file(certificate_path);
if (!file.good()) {
throw std::runtime_error(
"Unable to open file \"" + certificate_path + "\".");
}
if (verbosity_level >= 2) {
os << std::endl
<< std::setw(12) << "Bin"
<< std::setw(12) << "Item"
<< std::setw(12) << "Weight"
<< std::endl
<< std::setw(12) << "---"
<< std::setw(12) << "----"
<< std::setw(12) << "------"
<< std::endl;
}
BinId number_of_bins;
optimizationtools::IndexedSet items(number_of_items());
ItemId current_bin_number_of_items;
optimizationtools::IndexedSet current_bin_items(number_of_items());
ItemId item_id = -1;
ItemId number_of_duplicates = 0;
BinId number_of_overweighted_bins = 0;
ItemId number_of_conflict_violations = 0;
file >> number_of_bins;
for (BinId bin_pos = 0; bin_pos < number_of_bins; ++bin_pos) {
Weight weight = 0;
current_bin_items.clear();
file >> current_bin_number_of_items;
for (ItemId item_pos = 0;
item_pos < current_bin_number_of_items;
++item_pos) {
file >> item_id;
const Item& item = this->item(item_id);
// Check conflicts.
for (ItemId item_id_neighbor: item.neighbors) {
if (current_bin_items.contains(item_id_neighbor)) {
number_of_conflict_violations++;
if (verbosity_level >= 2) {
os << "Item " << item_id
<< " is packed with conflict item "
<< item_id_neighbor << "." << std::endl;
}
}
}
current_bin_items.add(item_id);
// Check duplicates.
if (items.contains(item_id)) {
number_of_duplicates++;
if (verbosity_level >= 2) {
os << "Item " << item_id
<< " has already been packed." << std::endl;
}
}
items.add(item_id);
weight += item.weight;
if (verbosity_level >= 2) {
os
<< std::setw(12) << bin_pos
<< std::setw(12) << item_id
<< std::setw(12) << weight
<< std::endl;
}
}
// Check capacity.
if (weight > capacity()) {
number_of_overweighted_bins++;
if (verbosity_level >= 2) {
os << "Bin " << bin_pos
<< " is overweighted." << std::endl;
}
}
}
bool feasible
= (items.size() == this->number_of_items())
&& (number_of_duplicates == 0)
&& (number_of_overweighted_bins == 0)
&& (number_of_conflict_violations == 0);
if (verbosity_level >= 2)
os << std::endl;
if (verbosity_level >= 1) {
os
<< "Number of items: " << items.size() << " / " << this->number_of_items() << std::endl
<< "Number of duplicates: " << number_of_duplicates << std::endl
<< "Number of conflict violations: " << number_of_conflict_violations << std::endl
<< "Number of overweighted bins: " << number_of_overweighted_bins << std::endl
<< "Feasible: " << feasible << std::endl
<< "Number of bins: " << number_of_bins << std::endl
;
}
return {feasible, number_of_bins};
}
private:
/*
* Private methods
*/
/** Constructor to build an instance manually. */
Instance() { }
/*
* Private attributes
*/
/** Items. */
std::vector<Item> items_;
/** Capacity of the bins. */
Weight capacity_;
friend class InstanceBuilder;
};
class InstanceBuilder
{
public:
/** Constructor. */
InstanceBuilder() { }
/** Set the capacity of the bins. */
void set_capacity(Weight capacity) { instance_.capacity_ = capacity; }
/** Add an item. */
void add_item(Weight weight = 1)
{
Item item;
item.weight = weight;
instance_.items_.push_back(item);
}
/** Set the weight of an item. */
void set_item_weight(
ItemId item_id,
Weight weight)
{
instance_.items_[item_id].weight = weight;
}
/** Add a conflict. */
void add_conflict(
ItemId item_id_1,
ItemId item_id_2)
{
instance_.items_[item_id_1].neighbors.push_back(item_id_2);
instance_.items_[item_id_2].neighbors.push_back(item_id_1);
}
/** Build an instance from a file. */
void read(
const std::string& instance_path,
const std::string& format = "")
{
std::ifstream file(instance_path);
if (!file.good()) {
throw std::runtime_error(
"Unable to open file \"" + instance_path + "\".");
}
if (format == "" || format == "default") {
read_default(file);
} else {
throw std::invalid_argument(
"Unknown instance format \"" + format + "\".");
}
file.close();
}
/*
* Build
*/
/** Build the instance. */
Instance build()
{
return std::move(instance_);
}
private:
/*
* Private methods
*/
/** Read an instance from a file in 'default' format. */
void read_default(std::ifstream& file)
{
std::string tmp;
std::vector<std::string> line;
ItemId number_of_items;
Weight capacity;
file >> number_of_items >> capacity;
for (ItemId item_id = 0;
item_id < number_of_items;
++item_id) {
add_item();
}
set_capacity(capacity);
Weight weight = -1;
ItemId item_id_tmp = -1;
for (ItemId item_id = 0; item_id < number_of_items; ++item_id) {
file >> item_id_tmp >> weight;
set_item_weight(item_id, weight);
getline(file, tmp);
line = optimizationtools::split(tmp, ' ');
for (std::string s: line) {
ItemId item_id_2 = std::stol(s) - 1;
add_conflict(item_id, item_id_2);
}
}
}
/*
* Private attributes
*/
/** Instance. */
Instance instance_;
};
}
}