-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbranching_scheme.hpp
386 lines (306 loc) · 11.2 KB
/
branching_scheme.hpp
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#pragma once
#include "packingsolver/onedimensional/solution.hpp"
#include "optimizationtools/utils/utils.hpp"
#include <sstream>
namespace packingsolver
{
namespace onedimensional
{
/**
* Branching scheme class for problem of type "onedimensional".
*/
class BranchingScheme
{
public:
/*
* Sub-structures
*/
struct Insertion
{
/**
* Id of the inserted item type.
*
* '-1' if no item is inserted.
*/
ItemTypeId item_type_id;
/** 'true' iff the item is inserted in a new bin. */
bool new_bin;
bool operator==(const Insertion& insertion) const;
bool operator!=(const Insertion& insertion) const { return !(*this == insertion); }
};
/**
* Node structure of the branching scheme.
*/
struct Node
{
/** Id of the node. */
NodeId id = -1;
/**
* Pointer to the parent of the node,
* 'nullptr' if the node is the root.
*/
std::shared_ptr<Node> parent = nullptr;
/** Last inserted item type. */
ItemTypeId item_type_id = -1;
/** For each item type, number of copies in the node. */
std::vector<ItemPos> item_number_of_copies;
/** Number of bins in the node. */
BinPos number_of_bins = 0;
/** Number of items in the node. */
ItemPos number_of_items = 0;
/** Item length. */
Volume item_length = 0;
/** Squared item length. */
Volume squared_item_length = 0;
/** Current length. */
Volume current_length = 0;
/** Waste. */
Volume waste = 0;
/** Maximum xe of all items in the last bin. */
Length xe_max = 0;
/** Maximum xs of all items in the last bin. */
Length xs_max = 0;
/** Profit. */
Profit profit = 0;
/** Length in the last bin. */
Weight last_bin_length = 0;
/** Weight of the last bin. */
Weight last_bin_weight = 0;
/** Number of items in the last bin. */
ItemPos last_bin_number_of_items = 0;
/** Maximum number of items in the last bin. */
ItemPos last_bin_maximum_number_of_items = -1;
/**
* Remaining weight allowed in the last bin to satisfy the maximum
* weight after constraint.
*/
Weight last_bin_remaiing_weight = -1;
};
struct Parameters
{
/** Guide. */
GuideId guide_id = 0;
};
/** Constructor. */
BranchingScheme(
const Instance& instance,
const Parameters& parameters);
/** Get instance. */
inline const Instance& instance() const { return instance_; }
/** Get parameters. */
inline const Parameters& parameters() const { return parameters_; }
/*
* Branching scheme methods
*/
const std::vector<Insertion>& insertions(
const std::shared_ptr<Node>& parent) const;
Node child_tmp(
const std::shared_ptr<Node>& parent,
const Insertion& insertion) const;
std::shared_ptr<Node> child(
const std::shared_ptr<Node>& parent,
const Insertion& insertion) const
{
return std::shared_ptr<Node>(new Node(child_tmp(parent, insertion)));
}
const std::shared_ptr<Node> root() const;
std::vector<std::shared_ptr<Node>> children(
const std::shared_ptr<Node>& parent) const;
inline bool operator()(
const std::shared_ptr<Node>& node_1,
const std::shared_ptr<Node>& node_2) const;
inline bool leaf(
const std::shared_ptr<Node>& node) const
{
return node->number_of_items == instance_.number_of_items();
}
bool bound(
const std::shared_ptr<Node>& node_1,
const std::shared_ptr<Node>& node_2) const;
bool better(
const std::shared_ptr<Node>& node_1,
const std::shared_ptr<Node>& node_2) const;
bool equals(
const std::shared_ptr<Node>& node_1,
const std::shared_ptr<Node>& node_2) const
{
(void)node_1;
(void)node_2;
return false;
}
/*
* Dominances
*/
inline bool comparable(const std::shared_ptr<Node>&) const { return true; }
struct NodeHasher
{
std::hash<ItemPos> hasher;
const BranchingScheme& branching_scheme;
NodeHasher(const BranchingScheme& branching_scheme): branching_scheme(branching_scheme) { }
inline bool operator()(
const std::shared_ptr<Node>& node_1,
const std::shared_ptr<Node>& node_2) const
{
return node_1->item_number_of_copies == node_2->item_number_of_copies;
}
inline std::size_t operator()(
const std::shared_ptr<Node>& node) const
{
if (branching_scheme.instance().unbounded_knapsack())
return 0;
size_t hash = 0;
for (ItemPos s: node->item_number_of_copies)
optimizationtools::hash_combine(hash, hasher(s));
return hash;
}
};
inline NodeHasher node_hasher() const { return NodeHasher(*this); }
bool dominates(
const std::shared_ptr<Node>& node_1,
const std::shared_ptr<Node>& node_2) const;
/*
* Outputs
*/
std::string display(const std::shared_ptr<Node>& node) const
{
std::stringstream ss;
ss << node->waste;
return ss.str();
}
Solution to_solution(
const std::shared_ptr<Node>& node) const;
private:
/** Instance. */
const Instance& instance_;
/** Parameters. */
Parameters parameters_;
mutable Counter node_id_ = 0;
mutable std::vector<Insertion> insertions_;
/*
* Private methods
*/
/** Get the percentage of item inserted into a node. */
inline double item_percentage(const Node& node) const { return (double)node.number_of_items / instance_.number_of_items(); }
/** Get the mean length of a node. */
inline double mean_length(const Node& node) const { return (double)node.current_length / node.number_of_items; }
/** Get the mean item length of a node; */
inline double mean_item_length(const Node& node) const { return (double)node.item_length / node.number_of_items; }
/** Get the mean squared item length of a node. */
inline double mean_squared_item_length(const Node& node) const { return (double)node.squared_item_length / node.number_of_items; }
/** Get the mean remaining item length of a node. */
inline double mean_remaining_item_length(const Node& node) const { return (double)remaining_item_length(node) / (instance_.number_of_items() - node.number_of_items); }
/** Get the remaining item length of a node. */
inline double remaining_item_length(const Node& node) const { return instance_.item_length() - node.item_length; }
/** Get the waste percentage of a node. */
inline double waste_percentage(const Node& node) const { return (double)node.waste / node.current_length; }
/** Get the waste ratio of a node. */
inline double waste_ratio(const Node& node) const { return (double)node.waste / node.item_length; }
/** Get the knapsack upper bound of a node. */
inline Profit ubkp(const Node& node) const;
/*
* Insertions
*/
/** Insertion of one item in the same bin. */
void insertion_item_same_bin(
const std::shared_ptr<Node>& parent,
ItemTypeId item_type_id) const;
/** Insertion of an item in a new bin. */
void insertion_item_new_bin(
const std::shared_ptr<Node>& parent,
ItemTypeId item_type_id) const;
};
std::ostream& operator<<(
std::ostream& os,
const BranchingScheme::Insertion& insertion);
std::ostream& operator<<(
std::ostream& os,
const BranchingScheme::Node& node);
////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// Inlined methods ////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Profit BranchingScheme::ubkp(const Node& node) const
{
Area remaining_item_length = instance_.item_length() - node.item_length;
Area remaining_length = instance_.bin_length() - node.current_length;
if (remaining_length >= remaining_item_length) {
return instance_.item_profit();
} else {
ItemTypeId item_type_id = instance_.largest_efficiency_item_type_id();
return node.profit + remaining_length
* instance_.item_type(item_type_id).profit
/ instance_.item_type(item_type_id).length;
}
}
inline bool BranchingScheme::operator()(
const std::shared_ptr<Node>& node_1,
const std::shared_ptr<Node>& node_2) const
{
switch (parameters_.guide_id) {
case 0: {
double guide_1 = (double)node_1->current_length / node_1->item_length;
double guide_2 = (double)node_2->current_length / node_2->item_length;
if (guide_1 != guide_2)
return guide_1 < guide_2;
break;
} case 1: {
double guide_1 = (double)node_1->current_length
/ node_1->item_length
/ mean_item_length(*node_1);
double guide_2 = (double)node_2->current_length
/ node_2->item_length
/ mean_item_length(*node_2);
if (guide_1 != guide_2)
return guide_1 < guide_2;
break;
} case 2: {
if ((0.1 + waste_percentage(*node_1)) / mean_item_length(*node_1)
!= (0.1 + waste_percentage(*node_2)) / mean_item_length(*node_2))
return (0.1 + waste_percentage(*node_1)) / mean_item_length(*node_1)
< (0.1 + waste_percentage(*node_2)) / mean_item_length(*node_2);
break;
} case 3: {
if ((0.1 + waste_percentage(*node_1)) / mean_squared_item_length(*node_1)
!= (0.1 + waste_percentage(*node_2)) / mean_squared_item_length(*node_2))
return (0.1 + waste_percentage(*node_1)) / mean_squared_item_length(*node_1)
< (0.1 + waste_percentage(*node_2)) / mean_squared_item_length(*node_2);
break;
} case 4: {
double guide_1 = (double)node_1->current_length / node_1->profit;
double guide_2 = (double)node_2->current_length / node_2->profit;
if (guide_1 != guide_2)
return guide_1 < guide_2;
break;
} case 5: {
double guide_1 = (double)node_1->current_length
/ node_1->profit
/ mean_item_length(*node_1);
double guide_2 = (double)node_2->current_length
/ node_2->profit
/ mean_item_length(*node_2);
if (guide_1 != guide_2)
return guide_1 < guide_2;
break;
} case 6: {
if (node_1->waste != node_2->waste)
return node_1->waste < node_2->waste;
break;
} case 7: {
auto ub1 = ubkp(*node_1);
auto ub2 = ubkp(*node_2);
if (ub1 != ub2)
return ub1 > ub2;
break;
} case 8: {
auto ub1 = ubkp(*node_1);
auto ub2 = ubkp(*node_2);
if (ub1 != ub2)
return ub1 > ub2;
if (node_1->waste != node_2->waste)
return node_1->waste < node_2->waste;
break;
}
}
return node_1->id < node_2->id;
}
}
}