Skip to content

Commit e669a8c

Browse files
committed
Improve box branching scheme
1 parent 62275af commit e669a8c

File tree

2 files changed

+23
-301
lines changed

2 files changed

+23
-301
lines changed

src/box/branching_scheme.cpp

Lines changed: 23 additions & 241 deletions
Original file line numberDiff line numberDiff line change
@@ -165,116 +165,6 @@ void BranchingScheme::update_uncovered_item(
165165
}
166166
}
167167

168-
void BranchingScheme::update_y_uncovered_item(
169-
std::vector<YUncoveredItem>& y_uncovered_items,
170-
const YUncoveredItem& y_uncovered_item,
171-
const UncoveredItem& ref) const
172-
{
173-
if (ref.ys > y_uncovered_item.y
174-
|| ref.ye < y_uncovered_item.y
175-
|| ref.zs >= y_uncovered_item.ze
176-
|| ref.ze <= y_uncovered_item.zs) {
177-
y_uncovered_items.push_back(y_uncovered_item);
178-
return;
179-
}
180-
bool has_z_low = (ref.zs > y_uncovered_item.zs);
181-
bool has_z_middle = (ref.x < y_uncovered_item.xe);
182-
bool has_z_high = (ref.ze < y_uncovered_item.ze);
183-
if (has_z_low) {
184-
YUncoveredItem y_uncovered_item_new = y_uncovered_item;
185-
y_uncovered_item_new.ze = ref.zs;
186-
y_uncovered_items.push_back(y_uncovered_item_new);
187-
}
188-
if (has_z_middle) {
189-
YUncoveredItem y_uncovered_item_new = y_uncovered_item;
190-
y_uncovered_item_new.zs = (std::max)(y_uncovered_item.zs, ref.zs);
191-
y_uncovered_item_new.ze = (std::min)(y_uncovered_item.ze, ref.ze);
192-
y_uncovered_item_new.xs = (std::max)(y_uncovered_item.xs, ref.x);
193-
y_uncovered_items.push_back(y_uncovered_item_new);
194-
}
195-
if (has_z_high) {
196-
YUncoveredItem y_uncovered_item_new = y_uncovered_item;
197-
y_uncovered_item_new.zs = ref.ze;
198-
y_uncovered_items.push_back(y_uncovered_item_new);
199-
}
200-
}
201-
202-
void BranchingScheme::update_z_uncovered_item(
203-
std::vector<ZUncoveredItem>& z_uncovered_items,
204-
const ZUncoveredItem& z_uncovered_item,
205-
const UncoveredItem& ref) const
206-
{
207-
if (ref.zs > z_uncovered_item.z
208-
|| ref.ze < z_uncovered_item.z
209-
|| ref.ys >= z_uncovered_item.ye
210-
|| ref.ye <= z_uncovered_item.ys) {
211-
z_uncovered_items.push_back(z_uncovered_item);
212-
return;
213-
}
214-
bool has_y_low = (ref.ys > z_uncovered_item.ys);
215-
bool has_y_middle = (ref.x < z_uncovered_item.xe);
216-
bool has_y_high = (ref.ye < z_uncovered_item.ye);
217-
if (has_y_low) {
218-
ZUncoveredItem z_uncovered_item_new = z_uncovered_item;
219-
z_uncovered_item_new.ye = ref.ys;
220-
if (z_uncovered_item_new.ys == z_uncovered_item_new.ye) {
221-
throw std::logic_error(
222-
FUNC_SIGNATURE + ": "
223-
"'z_uncovered_item_new.ys' must be different from 'z_uncovered_item_new.ye' (y_low); "
224-
"z_uncovered_item_new.ys: " + std::to_string(z_uncovered_item_new.ys) + "; "
225-
"z_uncovered_item_new.ye: " + std::to_string(z_uncovered_item_new.ye) + "; "
226-
"z_uncovered_item.ys: " + std::to_string(z_uncovered_item.ys) + "; "
227-
"z_uncovered_item.ye: " + std::to_string(z_uncovered_item.ye) + "; "
228-
"xe: " + std::to_string(ref.x) + "; "
229-
"ys: " + std::to_string(ref.ys) + "; "
230-
"ye: " + std::to_string(ref.ye) + "; "
231-
"zs: " + std::to_string(ref.zs) + "; "
232-
"ze: " + std::to_string(ref.ze) + ".");
233-
}
234-
z_uncovered_items.push_back(z_uncovered_item_new);
235-
}
236-
if (has_y_middle) {
237-
ZUncoveredItem z_uncovered_item_new = z_uncovered_item;
238-
z_uncovered_item_new.ys = (std::max)(z_uncovered_item.ys, ref.ys);
239-
z_uncovered_item_new.ye = (std::min)(z_uncovered_item.ye, ref.ye);
240-
z_uncovered_item_new.xs = (std::max)(z_uncovered_item.xs, ref.x);
241-
if (z_uncovered_item_new.ys == z_uncovered_item_new.ye) {
242-
throw std::logic_error(
243-
FUNC_SIGNATURE + ": "
244-
"'z_uncovered_item_new.ys' must be different from 'z_uncovered_item_new.ye' (y_middle); "
245-
"z_uncovered_item_new.ys: " + std::to_string(z_uncovered_item_new.ys) + "; "
246-
"z_uncovered_item_new.ye: " + std::to_string(z_uncovered_item_new.ye) + "; "
247-
"z_uncovered_item.ys: " + std::to_string(z_uncovered_item.ys) + "; "
248-
"z_uncovered_item.ye: " + std::to_string(z_uncovered_item.ye) + "; "
249-
"xe: " + std::to_string(ref.x) + "; "
250-
"ys: " + std::to_string(ref.ys) + "; "
251-
"ye: " + std::to_string(ref.ye) + "; "
252-
"zs: " + std::to_string(ref.zs) + "; "
253-
"ze: " + std::to_string(ref.ze) + ".");
254-
}
255-
z_uncovered_items.push_back(z_uncovered_item_new);
256-
}
257-
if (has_y_high) {
258-
ZUncoveredItem z_uncovered_item_new = z_uncovered_item;
259-
z_uncovered_item_new.ys = ref.ye;
260-
if (z_uncovered_item_new.ys == z_uncovered_item_new.ye) {
261-
throw std::logic_error(
262-
FUNC_SIGNATURE + ": "
263-
"'z_uncovered_item_new.ys' must be different from 'z_uncovered_item_new.ye' (y_high); "
264-
"z_uncovered_item_new.ys: " + std::to_string(z_uncovered_item_new.ys) + "; "
265-
"z_uncovered_item_new.ye: " + std::to_string(z_uncovered_item_new.ye) + "; "
266-
"z_uncovered_item.ys: " + std::to_string(z_uncovered_item.ys) + "; "
267-
"z_uncovered_item.ye: " + std::to_string(z_uncovered_item.ye) + "; "
268-
"xe: " + std::to_string(ref.x) + "; "
269-
"ys: " + std::to_string(ref.ys) + "; "
270-
"ye: " + std::to_string(ref.ye) + "; "
271-
"zs: " + std::to_string(ref.zs) + "; "
272-
"ze: " + std::to_string(ref.ze) + ".");
273-
}
274-
z_uncovered_items.push_back(z_uncovered_item_new);
275-
}
276-
}
277-
278168
BranchingScheme::Node BranchingScheme::child_tmp(
279169
const std::shared_ptr<Node>& pparent,
280170
const Insertion& insertion) const
@@ -333,20 +223,6 @@ BranchingScheme::Node BranchingScheme::child_tmp(
333223
new_uncovered_item.zs = zs;
334224
new_uncovered_item.ze = ze;
335225

336-
YUncoveredItem new_y_uncovered_item;
337-
new_y_uncovered_item.xs = xs;
338-
new_y_uncovered_item.xe = xe;
339-
new_y_uncovered_item.y = ye;
340-
new_y_uncovered_item.zs = zs;
341-
new_y_uncovered_item.ze = ze;
342-
343-
ZUncoveredItem new_z_uncovered_item;
344-
new_z_uncovered_item.xs = xs;
345-
new_z_uncovered_item.xe = xe;
346-
new_z_uncovered_item.ys = ys;
347-
new_z_uncovered_item.ye = ye;
348-
new_z_uncovered_item.z = ze;
349-
350226
// Update uncovered_items.
351227
if (insertion.new_bin > 0) { // New bin.
352228
{
@@ -359,60 +235,11 @@ BranchingScheme::Node BranchingScheme::child_tmp(
359235
update_uncovered_item(node.uncovered_items, uncovered_item, ys, ye, zs, ze);
360236
}
361237
node.uncovered_items.push_back(new_uncovered_item);
362-
{
363-
YUncoveredItem y_uncovered_item;
364-
y_uncovered_item.xs = 0;
365-
y_uncovered_item.xe = bin_type.box.x;
366-
y_uncovered_item.y = 0;
367-
y_uncovered_item.zs = 0;
368-
y_uncovered_item.ze = bin_type.box.z;
369-
update_y_uncovered_item(node.y_uncovered_items, y_uncovered_item, new_uncovered_item);
370-
}
371-
node.y_uncovered_items.push_back(new_y_uncovered_item);
372-
{
373-
ZUncoveredItem z_uncovered_item;
374-
z_uncovered_item.xs = 0;
375-
z_uncovered_item.xe = bin_type.box.x;
376-
z_uncovered_item.ys = 0;
377-
z_uncovered_item.ye = bin_type.box.y;
378-
z_uncovered_item.z = 0;
379-
update_z_uncovered_item(node.z_uncovered_items, z_uncovered_item, new_uncovered_item);
380-
}
381-
node.z_uncovered_items.push_back(new_z_uncovered_item);
382-
383238
} else {
384239
// Compute node.uncovered_item.
385240
for (const UncoveredItem& uncovered_item: parent.uncovered_items)
386241
update_uncovered_item(node.uncovered_items, uncovered_item, ys, ye, zs, ze);
387242
node.uncovered_items.push_back(new_uncovered_item);
388-
389-
// Compute node.y_uncovered_item.
390-
for (const YUncoveredItem& y_uncovered_item: parent.y_uncovered_items)
391-
update_y_uncovered_item(node.y_uncovered_items, y_uncovered_item, new_uncovered_item);
392-
std::vector<YUncoveredItem> y_uncovered_items = {new_y_uncovered_item};
393-
std::vector<YUncoveredItem> y_uncovered_items_tmp;
394-
for (const UncoveredItem& uncovered_item: parent.uncovered_items) {
395-
for (const YUncoveredItem& y_uncovered_item: y_uncovered_items)
396-
update_y_uncovered_item(y_uncovered_items_tmp, y_uncovered_item, uncovered_item);
397-
y_uncovered_items.swap(y_uncovered_items_tmp);
398-
y_uncovered_items_tmp.clear();
399-
}
400-
for (const YUncoveredItem& y_uncovered_item: y_uncovered_items)
401-
node.y_uncovered_items.push_back(y_uncovered_item);
402-
403-
// Compute node.z_uncovered_item.
404-
for (ZUncoveredItem z_uncovered_item: parent.z_uncovered_items)
405-
update_z_uncovered_item(node.z_uncovered_items, z_uncovered_item, new_uncovered_item);
406-
std::vector<ZUncoveredItem> z_uncovered_items = {new_z_uncovered_item};
407-
std::vector<ZUncoveredItem> z_uncovered_items_tmp;
408-
for (const UncoveredItem& uncovered_item: parent.uncovered_items) {
409-
for (const ZUncoveredItem& z_uncovered_item: z_uncovered_items)
410-
update_z_uncovered_item(z_uncovered_items_tmp, z_uncovered_item, uncovered_item);
411-
z_uncovered_items.swap(z_uncovered_items_tmp);
412-
z_uncovered_items_tmp.clear();
413-
}
414-
for (const ZUncoveredItem& z_uncovered_item: z_uncovered_items)
415-
node.z_uncovered_items.push_back(z_uncovered_item);
416243
}
417244

418245
// Compute item_number_of_copies, number_of_items, items_area,
@@ -492,12 +319,12 @@ const std::vector<BranchingScheme::Insertion>& BranchingScheme::insertions(
492319
const BinType& bin_type = instance.bin_type(bin_type_id);
493320

494321
// For all y uncovered item.
495-
for (ItemPos y_uncovered_item_pos = 0;
496-
y_uncovered_item_pos < (ItemPos)parent->y_uncovered_items.size();
322+
for (ItemPos y_uncovered_item_pos = -1;
323+
y_uncovered_item_pos < (ItemPos)parent->uncovered_items.size();
497324
++y_uncovered_item_pos) {
498325
// For all z uncovered item.
499-
for (ItemPos z_uncovered_item_pos = 0;
500-
z_uncovered_item_pos < (ItemPos)parent->z_uncovered_items.size();
326+
for (ItemPos z_uncovered_item_pos = -1;
327+
z_uncovered_item_pos < (ItemPos)parent->uncovered_items.size();
501328
++z_uncovered_item_pos) {
502329
// For all remaining items.
503330
for (ItemTypeId item_type_id = 0;
@@ -571,8 +398,8 @@ const std::vector<BranchingScheme::Insertion>& BranchingScheme::insertions(
571398
item_type_id,
572399
rotation,
573400
new_bin,
574-
0, // y_uncovered_item_pos
575-
0); // z_uncovered_item_pos
401+
-1, // y_uncovered_item_pos
402+
-1); // z_uncovered_item_pos
576403
}
577404
}
578405
}
@@ -611,15 +438,8 @@ void BranchingScheme::insertion_item(
611438
Length xi = bin_type.box.x;
612439
Length yi = bin_type.box.y;
613440
Length zi = bin_type.box.z;
614-
Length zs = 0;
615-
Length ys = 0;
616-
if (new_bin == 0) { // Same bin
617-
ys = parent->y_uncovered_items[y_uncovered_item_pos].y;
618-
zs = parent->z_uncovered_items[z_uncovered_item_pos].z;
619-
} else { // new bin.
620-
ys = 0;
621-
zs = 0;
622-
}
441+
Length ys = (y_uncovered_item_pos >= 0)? parent->uncovered_items[y_uncovered_item_pos].ye: 0;
442+
Length zs = (z_uncovered_item_pos >= 0)? parent->uncovered_items[z_uncovered_item_pos].ze: 0;
623443
Length ye = ys + box.y;
624444
Length ze = zs + box.z;
625445
// Check bin y.
@@ -642,20 +462,22 @@ void BranchingScheme::insertion_item(
642462
}
643463

644464
// Check contact with y and z uncovered items.
645-
if (new_bin == 0) { // Same bin
646-
if (ye <= parent->z_uncovered_items[z_uncovered_item_pos].ys) {
647-
//std::cout << "ye " << ye << " <= zuys " << parent->z_uncovered_items[z_uncovered_item_pos].ys << std::endl;
465+
if (z_uncovered_item_pos >= 0) {
466+
if (ye <= parent->uncovered_items[z_uncovered_item_pos].ys) {
467+
//std::cout << "ye " << ye << " <= zuys " << parent->uncovered_items[z_uncovered_item_pos].ys << std::endl;
648468
return;
649469
}
650-
if (ys >= parent->z_uncovered_items[z_uncovered_item_pos].ye) {
651-
//std::cout << "ys " << ys << " <= zuye " << parent->z_uncovered_items[z_uncovered_item_pos].ye << std::endl;
470+
if (ys >= parent->uncovered_items[z_uncovered_item_pos].ye) {
471+
//std::cout << "ys " << ys << " <= zuye " << parent->uncovered_items[z_uncovered_item_pos].ye << std::endl;
652472
return;
653473
}
654-
if (ze <= parent->y_uncovered_items[y_uncovered_item_pos].zs) {
474+
}
475+
if (y_uncovered_item_pos >= 0) {
476+
if (ze <= parent->uncovered_items[y_uncovered_item_pos].zs) {
655477
//std::cout << "ze " << ze << " <= yuzs " << parent->y_uncovered_items[y_uncovered_item_pos].zs << std::endl;
656478
return;
657479
}
658-
if (zs >= parent->y_uncovered_items[y_uncovered_item_pos].ze) {
480+
if (zs >= parent->uncovered_items[y_uncovered_item_pos].ze) {
659481
//std::cout << "zs " << zs << " <= yuze " << parent->y_uncovered_items[y_uncovered_item_pos].ze << std::endl;
660482
return;
661483
}
@@ -664,8 +486,6 @@ void BranchingScheme::insertion_item(
664486
// Compute xs.
665487
Length xs = 0;
666488
if (new_bin == 0) {
667-
xs = std::max(xs, parent->y_uncovered_items[y_uncovered_item_pos].xs - box.x);
668-
xs = std::max(xs, parent->z_uncovered_items[z_uncovered_item_pos].xs - box.x);
669489
for (const UncoveredItem& uncovered_item: parent->uncovered_items) {
670490
if (uncovered_item.ye <= ys || uncovered_item.ys >= ye)
671491
continue;
@@ -684,21 +504,15 @@ void BranchingScheme::insertion_item(
684504
}
685505

686506
// Check contact with y and z uncovered items.
687-
if (new_bin == 0) {
688-
if (xe <= parent->y_uncovered_items[y_uncovered_item_pos].xs) {
689-
//std::cout << "xe " << xe << " <= yuxs " << parent->y_uncovered_items[y_uncovered_item_pos].xs << std::endl;
690-
return;
691-
}
692-
if (xs >= parent->y_uncovered_items[y_uncovered_item_pos].xe) {
507+
if (y_uncovered_item_pos >= 0) {
508+
if (xs >= parent->uncovered_items[y_uncovered_item_pos].x) {
693509
//std::cout << "xs " << xs << " <= yuxe " << parent->y_uncovered_items[y_uncovered_item_pos].xe << std::endl;
694510
return;
695511
}
696-
if (xe <= parent->z_uncovered_items[z_uncovered_item_pos].xs) {
697-
//std::cout << "xe " << xe << " <= zuxs " << parent->z_uncovered_items[z_uncovered_item_pos].xs << std::endl;
698-
return;
699-
}
700-
if (xs >= parent->z_uncovered_items[z_uncovered_item_pos].xe) {
701-
//std::cout << "xs " << xs << " <= zuxe " << parent->z_uncovered_items[z_uncovered_item_pos].xe << std::endl;
512+
}
513+
if (z_uncovered_item_pos >= 0) {
514+
if (xs >= parent->uncovered_items[z_uncovered_item_pos].x) {
515+
//std::cout << "xs " << xs << " <= zuxe " << parent->uncovered_items[z_uncovered_item_pos].xe << std::endl;
702516
return;
703517
}
704518
}
@@ -1009,32 +823,6 @@ std::ostream& packingsolver::box::operator<<(
1009823
return os;
1010824
}
1011825

1012-
std::ostream& packingsolver::box::operator<<(
1013-
std::ostream& os,
1014-
const BranchingScheme::YUncoveredItem& y_uncovered_item)
1015-
{
1016-
os << "y " << y_uncovered_item.y
1017-
<< " xs " << y_uncovered_item.xs
1018-
<< " xe " << y_uncovered_item.xe
1019-
<< " zs " << y_uncovered_item.zs
1020-
<< " ze " << y_uncovered_item.ze
1021-
;
1022-
return os;
1023-
}
1024-
1025-
std::ostream& packingsolver::box::operator<<(
1026-
std::ostream& os,
1027-
const BranchingScheme::ZUncoveredItem& z_uncovered_item)
1028-
{
1029-
os << "z " << z_uncovered_item.z
1030-
<< " xs " << z_uncovered_item.xs
1031-
<< " xe " << z_uncovered_item.xe
1032-
<< " ys " << z_uncovered_item.ys
1033-
<< " ye " << z_uncovered_item.ye
1034-
;
1035-
return os;
1036-
}
1037-
1038826
bool BranchingScheme::UncoveredItem::operator==(
1039827
const UncoveredItem& uncovered_item) const
1040828
{
@@ -1091,12 +879,6 @@ std::ostream& packingsolver::box::operator<<(
1091879
os << "- uncovered_items" << std::endl;
1092880
for (const BranchingScheme::UncoveredItem& uncovered_item: node.uncovered_items)
1093881
os << " - " << uncovered_item << std::endl;
1094-
os << "- y_uncovered_items" << std::endl;
1095-
for (const BranchingScheme::YUncoveredItem& y_uncovered_item: node.y_uncovered_items)
1096-
os << " - " << y_uncovered_item << std::endl;
1097-
os << "- z_uncovered_items" << std::endl;
1098-
for (const BranchingScheme::ZUncoveredItem& z_uncovered_item: node.z_uncovered_items)
1099-
os << " - " << z_uncovered_item << std::endl;
1100882

1101883
return os;
1102884
}

0 commit comments

Comments
 (0)