Skip to content

Commit

Permalink
Clean rectangleguillotine::BranchingScheme code
Browse files Browse the repository at this point in the history
  • Loading branch information
fontanf committed Jan 12, 2025
1 parent ed338fc commit bf4582a
Show file tree
Hide file tree
Showing 10 changed files with 440 additions and 667 deletions.
399 changes: 81 additions & 318 deletions include/packingsolver/rectangleguillotine/instance.hpp

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/rectangleguillotine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ target_sources(PackingSolver_rectangleguillotine PRIVATE
instance_builder.cpp
solution.cpp
solution_builder.cpp
instance_flipper.cpp
algorithm_formatter.cpp
optimize.cpp
branching_scheme.cpp)
Expand Down
541 changes: 215 additions & 326 deletions src/rectangleguillotine/branching_scheme.cpp

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions src/rectangleguillotine/branching_scheme.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "packingsolver/rectangleguillotine/solution.hpp"
#include "rectangleguillotine/instance_flipper.hpp"

#include "optimizationtools/utils/utils.hpp"

Expand Down Expand Up @@ -360,6 +361,8 @@ class BranchingScheme
/** Instance. */
const Instance& instance_;

InstanceFlipper instance_flipper_;

/** Parameters. */
Parameters parameters_;

Expand Down Expand Up @@ -392,6 +395,10 @@ class BranchingScheme
* Private methods
*/

const Instance& instance(CutOrientation cut_orientation) const { return (cut_orientation == CutOrientation::Vertical)? instance_: instance_flipper_.flipped_instance(); }

const Instance& instance(Depth df) const { return (std::abs(df) % 2 == 0)? instance_flipper_.flipped_instance(): instance_; }

/**
* Return true iff s1 and s2 contains identical objects in the same order.
*/
Expand Down Expand Up @@ -538,15 +545,16 @@ inline bool BranchingScheme::dominates(
if (f1.i < f2.i)
return true;
if (f1.i == f2.i && f1.o == f2.o) {
const Instance& instance = this->instance(f1.o);
if (f1.x1_curr <= f2.x1_prev)
return true;
if (f1.x1_prev <= f2.x1_prev
&& f1.x1_curr <= f2.x1_curr
&& f1.y2_curr <= f2.y2_prev)
return true;
BinTypeId bin_type_id = instance().bin_type_id(f1.i);
const BinType& bin_type = instance().bin_type(bin_type_id);
Length h = instance().height(bin_type, f1.o);
BinTypeId bin_type_id = instance.bin_type_id(f1.i);
const BinType& bin_type = instance.bin_type(bin_type_id);
Length h = bin_type.rect.h;
if (f1.y2_curr != h
&& f1.x1_prev <= f2.x1_prev
&& f1.x3_curr <= f2.x3_curr
Expand Down
9 changes: 5 additions & 4 deletions src/rectangleguillotine/instance.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "packingsolver/rectangleguillotine/instance.hpp"

#include "packingsolver/rectangleguillotine/instance_builder.hpp"

#include <iostream>
#include <fstream>
#include <iomanip>
Expand Down Expand Up @@ -180,8 +182,7 @@ std::ostream& packingsolver::rectangleguillotine::operator<<(
const Defect& defect)
{
os
<< "id " << defect.id
<< " bin_type_id " << defect.bin_type_id
<< "defect"
<< " x " << defect.pos.x
<< " y " << defect.pos.y
<< " w " << defect.rect.w
Expand Down Expand Up @@ -291,7 +292,7 @@ void Instance::write(
const Defect& defect = bin_type.defects[defect_id];
f_defects
<< defect_id << ","
<< defect.bin_type_id << ","
<< bin_type_id << ","
<< defect.pos.x << ","
<< defect.pos.y << ","
<< defect.rect.w << ","
Expand Down Expand Up @@ -431,7 +432,7 @@ std::ostream& Instance::format(
const Defect& defect = bin_type.defects[defect_id];
os
<< std::setw(12) << defect_id
<< std::setw(12) << defect.bin_type_id
<< std::setw(12) << bin_type_id
<< std::setw(12) << defect.pos.x
<< std::setw(12) << defect.pos.y
<< std::setw(12) << defect.rect.w
Expand Down
2 changes: 0 additions & 2 deletions src/rectangleguillotine/instance_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,6 @@ void InstanceBuilder::add_defect(
BinType& bin_type = instance_.bin_types_[bin_type_id];

Defect defect;
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;
Expand Down
88 changes: 88 additions & 0 deletions src/rectangleguillotine/instance_flipper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "rectangleguillotine/instance_flipper.hpp"

#include "packingsolver/rectangleguillotine/instance_builder.hpp"
#include "rectangleguillotine/solution_builder.hpp"

using namespace packingsolver;
using namespace packingsolver::rectangleguillotine;

Instance InstanceFlipper::flip(const Instance& instance)
{
// Build flipped instance.
InstanceBuilder flipped_instance_builder;
if (instance.objective() == Objective::OpenDimensionY) {
flipped_instance_builder.set_objective(Objective::OpenDimensionX);
} else {
flipped_instance_builder.set_objective(instance.objective());
}
rectangleguillotine::Parameters flipped_instance_parameters = instance.parameters();
flipped_instance_parameters.first_stage_orientation = CutOrientation::Vertical;
flipped_instance_builder.set_parameters(flipped_instance_parameters);
for (BinTypeId bin_type_id = 0;
bin_type_id < instance.number_of_bin_types();
++bin_type_id) {
const BinType& bin_type = instance.bin_type(bin_type_id);
BinType bin_type_new = bin_type;
bin_type_new.rect.w = bin_type.rect.h;
bin_type_new.rect.h = bin_type.rect.w;
bin_type_new.left_trim = bin_type.bottom_trim;
bin_type_new.left_trim_type = bin_type.bottom_trim_type;
bin_type_new.right_trim = bin_type.top_trim;
bin_type_new.right_trim_type = bin_type.top_trim_type;
bin_type_new.bottom_trim = bin_type.left_trim;
bin_type_new.bottom_trim_type = bin_type.left_trim_type;
bin_type_new.top_trim = bin_type.right_trim;
bin_type_new.top_trim_type = bin_type.right_trim_type;
for (DefectId defect_id = 0;
defect_id < (DefectId)bin_type.defects.size();
++defect_id) {
bin_type_new.defects[defect_id].pos.x = bin_type.defects[defect_id].pos.y;
bin_type_new.defects[defect_id].pos.y = bin_type.defects[defect_id].pos.x;
bin_type_new.defects[defect_id].rect.w = bin_type.defects[defect_id].rect.h;
bin_type_new.defects[defect_id].rect.h = bin_type.defects[defect_id].rect.w;
}
flipped_instance_builder.add_bin_type(
bin_type_new,
bin_type.copies,
bin_type.copies_min);
}
for (ItemTypeId item_type_id = 0;
item_type_id < instance.number_of_item_types();
++item_type_id) {
const ItemType& item_type = instance.item_type(item_type_id);
ItemType item_type_new = item_type;
item_type_new.rect.w = item_type.rect.h;
item_type_new.rect.h = item_type.rect.w;
flipped_instance_builder.add_item_type(
item_type_new,
item_type.profit,
item_type.copies);
}
return flipped_instance_builder.build();
}

Solution InstanceFlipper::unflip_solution(const Solution& flipped_solution) const
{
SolutionBuilder solution_builder(instance_orig_);
for (BinPos bin_pos = 0;
bin_pos < flipped_solution.number_of_different_bins();
++bin_pos) {
const SolutionBin& flipped_bin = flipped_solution.bin(bin_pos);
solution_builder.add_bin(
flipped_bin.bin_type_id,
flipped_bin.copies,
CutOrientation::Horizontal);
for (const SolutionNode& flipped_node: flipped_bin.nodes) {
if (flipped_node.d <= 0)
continue;
if (flipped_node.d % 2 == 1) {
solution_builder.add_node(flipped_node.d, flipped_node.r);
} else {
solution_builder.add_node(flipped_node.d, flipped_node.t);
}
if (flipped_node.item_type_id >= 0)
solution_builder.set_last_node_item(flipped_node.item_type_id);
}
}
return solution_builder.build();
}
33 changes: 33 additions & 0 deletions src/rectangleguillotine/instance_flipper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include "packingsolver/rectangleguillotine/solution.hpp"

namespace packingsolver
{
namespace rectangleguillotine
{

class InstanceFlipper
{
public:

InstanceFlipper(const Instance& instance):
instance_orig_(instance),
instance_flipped_(flip(instance)) { }

const Instance& flipped_instance() const { return instance_flipped_; }

Solution unflip_solution(const Solution& solution) const;

private:

Instance flip(const Instance& instance);

const Instance& instance_orig_;

Instance instance_flipped_;

};

}
}
15 changes: 5 additions & 10 deletions src/rectangleguillotine/solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ void Solution::update_indicators(
node.r,
node.b,
node.t,
bin_type,
CutOrientation::Vertical);
bin_type);
if (k != -1) {
std::cout << "defects_feasible = false" << std::endl;
defects_feasible_ = false;
Expand All @@ -211,26 +210,22 @@ void Solution::update_indicators(
node.l,
node.b,
node.t,
bin_type,
CutOrientation::Vertical);
bin_type);
DefectId kr = instance().x_intersects_defect(
node.r,
node.b,
node.t,
bin_type,
CutOrientation::Vertical);
bin_type);
DefectId kb = instance().y_intersects_defect(
node.l,
node.r,
node.b,
bin_type,
CutOrientation::Vertical);
bin_type);
DefectId kt = instance().y_intersects_defect(
node.l,
node.r,
node.t,
bin_type,
CutOrientation::Vertical);
bin_type);
if (kl != -1 || kr != -1 || kb != -1 || kt != -1) {
cut_through_defects_feasible_ = false;
feasible_ = false;
Expand Down
5 changes: 1 addition & 4 deletions test/rectangleguillotine/branching_scheme/insertion_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1271,17 +1271,14 @@ TEST(RectangleGuillotineBranchingScheme, PlateRotationI)
instance_builder.set_objective(Objective::BinPackingWithLeftovers);
instance_builder.set_roadef2018();
instance_builder.set_first_stage_orientation(CutOrientation::Horizontal);
instance_builder.add_item_type(500, 1000, -1, 1, false, 0);
instance_builder.add_item_type(500, 1000, -1, 1, true, 0);
instance_builder.add_bin_type(6000, 3210);
Instance instance = instance_builder.build();

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

std::vector<BranchingScheme::Insertion> is {
//{0, -1, -1, 1000, 500, 1000, 3500, 3210, 0, 0},
//{0, -1, -1, 500, 1000, 500, 3500, 3210, 0, 0},
{0, -1, -2, 500, 1000, 500, 3210, 6000, 0, 0},
{0, -1, -2, 1000, 500, 1000, 3210, 6000, 0, 0},
};

Expand Down

0 comments on commit bf4582a

Please sign in to comment.