Skip to content

Commit

Permalink
Fix bug in rectangleguillotine branching scheme with trims and defects
Browse files Browse the repository at this point in the history
  • Loading branch information
fontanf committed Nov 24, 2024
1 parent 11ea714 commit 95eb73f
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class InstanceBuilder

/** Add trims to bin type 'i'. */
void add_trims(
BinTypeId i,
BinTypeId bin_type_id,
Length left_trim,
TrimType left_trim_type,
Length right_trim,
Expand All @@ -92,7 +92,7 @@ class InstanceBuilder

/** Add a defect. */
void add_defect(
BinTypeId i,
BinTypeId bin_type_id,
Length x,
Length y,
Length w,
Expand Down
9 changes: 9 additions & 0 deletions src/boxstacks/instance_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ void InstanceBuilder::add_defect(
Length rect_x,
Length rect_y)
{
if (bin_type_id >= instance_.bin_types_.size()) {
throw std::invalid_argument(
"boxstacks::InstanceBuilder::add_defect"
". bin_type_id: " + std::to_string(bin_type_id)
+ "; instance_.bin_types_.size(): "
+ std::to_string(instance_.bin_types_.size())
+ ".");
}

BinType& bin_type = instance_.bin_types_[bin_type_id];

rectangle::Defect defect;
Expand Down
9 changes: 9 additions & 0 deletions src/rectangle/instance_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ void InstanceBuilder::add_defect(
Length rect_x,
Length rect_y)
{
if (bin_type_id >= instance_.bin_types_.size()) {
throw std::invalid_argument(
"rectangle::InstanceBuilder::add_defect"
". bin_type_id: " + std::to_string(bin_type_id)
+ "; instance_.bin_types_.size(): "
+ std::to_string(instance_.bin_types_.size())
+ ".");
}

BinType& bin_type = instance_.bin_types_[bin_type_id];

Defect defect;
Expand Down
11 changes: 8 additions & 3 deletions src/rectangleguillotine/branching_scheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ const std::vector<BranchingScheme::Insertion>& BranchingScheme::insertions(
Length x = x3_prev(parent, df);
Length y = y2_prev(parent, df);

// Try adding an item
// Try inserting an item
for (StackId s = 0; s < instance().number_of_stacks(); ++s) {
if (parent.pos_stack[s] == instance().stack_size(s))
continue;
Expand Down Expand Up @@ -651,15 +651,20 @@ const std::vector<BranchingScheme::Insertion>& BranchingScheme::insertions(
}
}

if (parent.parent == nullptr || parent.item_type_id_1 != -1 || parent.item_type_id_2 != -1) {
// Try inserting a defect.
if (parent.parent == nullptr
|| parent.item_type_id_1 != -1
|| parent.item_type_id_2 != -1) {
BinTypeId bin_type_id = instance().bin_type_id(i);
const BinType& bin_type = instance().bin_type(bin_type_id);
for (DefectId defect_id = 0;
defect_id < (DefectId)bin_type.defects.size();
++defect_id) {
const Defect& defect = bin_type.defects[defect_id];
if (instance().left(defect, o) >= x && instance().bottom(defect, o) >= y)
if (instance().right(defect, o) >= x
&& instance().top(defect, o) >= y) {
insertion_defect(parent, defect_id, df);
}
}
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/rectangleguillotine/instance_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,25 @@ void InstanceBuilder::add_defect(
Length w,
Length h)
{
if (bin_type_id >= instance_.bin_types_.size()) {
throw std::invalid_argument(
"rectangleguillotine::InstanceBuilder::add_defect"
". bin_type_id: " + std::to_string(bin_type_id)
+ "; instance_.bin_types_.size(): "
+ std::to_string(instance_.bin_types_.size())
+ ".");
}

BinType& bin_type = instance_.bin_types_[bin_type_id];

Defect defect;
defect.id = instance_.bin_types_[bin_type_id].defects.size();
defect.id = bin_type.defects.size();
defect.bin_type_id = bin_type_id;
defect.pos.x = x;
defect.pos.y = y;
defect.rect.w = w;
defect.rect.h = h;
instance_.bin_types_[bin_type_id].defects.push_back(defect);
bin_type.defects.push_back(defect);
}

void InstanceBuilder::add_bin_type(
Expand Down
71 changes: 51 additions & 20 deletions test/rectangleguillotine/branching_scheme/trim_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ TEST(RectangleGuillotineBranchingScheme, BottomTrimSoft)
InstanceBuilder instance_builder;
instance_builder.set_objective(Objective::BinPackingWithLeftovers);
instance_builder.set_roadef2018();
BinTypeId i = instance_builder.add_bin_type(6000, 3210);
BinTypeId bin_type_id = instance_builder.add_bin_type(6000, 3210);
instance_builder.add_trims(
i,
bin_type_id,
0, TrimType::Soft,
0, TrimType::Soft,
20, TrimType::Soft,
0, TrimType::Soft);
instance_builder.add_defect(i, 495, 25, 5, 5);
instance_builder.add_defect(bin_type_id, 495, 25, 5, 5);
instance_builder.add_item_type(1000, 500, -1, 1, true, 0);
Instance instance = instance_builder.build();

Expand Down Expand Up @@ -82,14 +82,14 @@ TEST(RectangleGuillotineBranchingScheme, BottomTrimHard)
InstanceBuilder instance_builder;
instance_builder.set_objective(Objective::BinPackingWithLeftovers);
instance_builder.set_roadef2018();
BinTypeId i = instance_builder.add_bin_type(6000, 3210);
BinTypeId bin_type_id = instance_builder.add_bin_type(6000, 3210);
instance_builder.add_trims(
i,
bin_type_id,
0, TrimType::Soft,
0, TrimType::Soft,
20, TrimType::Hard,
0, TrimType::Soft);
instance_builder.add_defect(i, 495, 25, 5, 5);
instance_builder.add_defect(bin_type_id, 495, 25, 5, 5);
instance_builder.add_item_type(1000, 500, -1, 1, true, 0);
Instance instance = instance_builder.build();

Expand Down Expand Up @@ -130,14 +130,14 @@ TEST(RectangleGuillotineBranchingScheme, LeftTrimSoft)
InstanceBuilder instance_builder;
instance_builder.set_objective(Objective::BinPackingWithLeftovers);
instance_builder.set_roadef2018();
BinTypeId i = instance_builder.add_bin_type(6000, 3210);
BinTypeId bin_type_id = instance_builder.add_bin_type(6000, 3210);
instance_builder.add_trims(
i,
bin_type_id,
20, TrimType::Soft,
0, TrimType::Soft,
0, TrimType::Soft,
0, TrimType::Soft);
instance_builder.add_defect(i, 25, 495, 5, 5);
instance_builder.add_defect(bin_type_id, 25, 495, 5, 5);
instance_builder.add_item_type(500, 1000, -1, 1, true, 0);
Instance instance = instance_builder.build();

Expand Down Expand Up @@ -178,14 +178,14 @@ TEST(RectangleGuillotineBranchingScheme, LeftTrimHard)
InstanceBuilder instance_builder;
instance_builder.set_objective(Objective::BinPackingWithLeftovers);
instance_builder.set_roadef2018();
BinTypeId i = instance_builder.add_bin_type(6000, 3210);
BinTypeId bin_type_id = instance_builder.add_bin_type(6000, 3210);
instance_builder.add_trims(
i,
bin_type_id,
20, TrimType::Hard,
0, TrimType::Soft,
0, TrimType::Soft,
0, TrimType::Soft);
instance_builder.add_defect(i, 25, 495, 5, 5);
instance_builder.add_defect(bin_type_id, 25, 495, 5, 5);
instance_builder.add_item_type(500, 1000, -1, 1, true, 0);
Instance instance = instance_builder.build();

Expand Down Expand Up @@ -224,9 +224,9 @@ TEST(RectangleGuillotineBranchingScheme, TopTrimSoft)
InstanceBuilder instance_builder;
instance_builder.set_objective(Objective::BinPackingWithLeftovers);
instance_builder.set_roadef2018();
BinTypeId i = instance_builder.add_bin_type(6000, 3210);
BinTypeId bin_type_id = instance_builder.add_bin_type(6000, 3210);
instance_builder.add_trims(
i,
bin_type_id,
0, TrimType::Soft,
0, TrimType::Soft,
0, TrimType::Soft,
Expand Down Expand Up @@ -268,9 +268,9 @@ TEST(RectangleGuillotineBranchingScheme, TopTrimHard)
InstanceBuilder instance_builder;
instance_builder.set_objective(Objective::BinPackingWithLeftovers);
instance_builder.set_roadef2018();
BinTypeId i = instance_builder.add_bin_type(6000, 3210);
BinTypeId bin_type_id = instance_builder.add_bin_type(6000, 3210);
instance_builder.add_trims(
i,
bin_type_id,
0, TrimType::Soft,
0, TrimType::Soft,
0, TrimType::Soft,
Expand Down Expand Up @@ -311,9 +311,9 @@ TEST(RectangleGuillotineBranchingScheme, RightTrimSoft)
InstanceBuilder instance_builder;
instance_builder.set_objective(Objective::BinPackingWithLeftovers);
instance_builder.set_roadef2018();
BinTypeId i = instance_builder.add_bin_type(3000, 3210);
BinTypeId bin_type_id = instance_builder.add_bin_type(3000, 3210);
instance_builder.add_trims(
i,
bin_type_id,
0, TrimType::Soft,
20, TrimType::Soft,
0, TrimType::Soft,
Expand Down Expand Up @@ -355,9 +355,9 @@ TEST(RectangleGuillotineBranchingScheme, RightTrimHard)
InstanceBuilder instance_builder;
instance_builder.set_objective(Objective::BinPackingWithLeftovers);
instance_builder.set_roadef2018();
BinTypeId i = instance_builder.add_bin_type(3000, 3210);
BinTypeId bin_type_id = instance_builder.add_bin_type(3000, 3210);
instance_builder.add_trims(
i,
bin_type_id,
0, TrimType::Soft,
20, TrimType::Hard,
0, TrimType::Soft,
Expand All @@ -373,3 +373,34 @@ TEST(RectangleGuillotineBranchingScheme, RightTrimHard)

EXPECT_EQ(branching_scheme.insertions(root), is);
}

TEST(RectangleGuillotineBranchingScheme, TrimAndDefect)
{
InstanceBuilder instance_builder;
instance_builder.set_objective(Objective::BinPackingWithLeftovers);
instance_builder.set_predefined("3EVR");
instance_builder.set_cut_through_defects(true);
instance_builder.set_minimum_waste_length(10);
instance_builder.set_minimum_distance_1_cuts(10);
instance_builder.set_maximum_distance_1_cuts(3210);
instance_builder.set_cut_thickness(3);
BinTypeId bin_type_id = instance_builder.add_bin_type(3210, 2250);
instance_builder.add_trims(
bin_type_id,
10, TrimType::Soft,
10, TrimType::Hard,
10, TrimType::Soft,
10, TrimType::Soft);
instance_builder.add_defect(bin_type_id, 12, 9, 300, 54);
instance_builder.add_item_type(910, 846);
Instance instance = instance_builder.build();

BranchingScheme branching_scheme(instance);
auto root = branching_scheme.root();

std::vector<BranchingScheme::Insertion> is {
{-1, -1, -1, 312, 63, 312, 3200, 2240, 1, 1},
};

EXPECT_EQ(branching_scheme.insertions(root), is);
}

0 comments on commit 95eb73f

Please sign in to comment.