Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data/irregular/users/2026-11-08_many_item_types_1000.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions data/irregular/users/2026-11-08_many_item_types_2000.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions include/packingsolver/irregular/optimize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,13 @@ struct OptimizeParameters: packingsolver::Parameters<Instance, Solution, Output>
/** Initial maximum approximation ratio. */
double initial_maximum_approximation_ratio = 0.10;

/**
* Number of item types the tree search expands at each node: the biggest
* ones with at least one insertion. A node's cost and memory otherwise
* grow with the number of item types. -1 (the default): all of them.
*/
Counter tree_search_maximum_number_of_item_types = -1;

/** Decrease factor for the maximum approximation ratio. */
double maximum_approximation_ratio_factor = 0.75;

Expand Down
3 changes: 3 additions & 0 deletions src/irregular/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ int main(int argc, char *argv[])
("use-local-search-linear-programming,", po::value<bool>(), "enable local search algorithm with the linear programming engine")
("use-local-search-nonlinear-programming,", po::value<bool>(), "enable local search algorithm with the nonlinear programming engine (requires Knitro)")
("use-local-search-direct-trigonometry,", po::value<bool>(), "enable local search algorithm with the direct trigonometry engine")
("tree-search-maximum-number-of-item-types,", po::value<Counter>(), "item types the tree search expands at each node (-1: all)")
("use-local-search-black-box-optimization,", po::value<bool>(), "enable local search algorithm with the black-box optimization engine (requires Knitro)")
("local-search-weighting-scheme,", po::value<LocalSearchWeightingScheme>(), "set the weighting scheme of the local search: multiplicative, additive")
("use-sequential-single-knapsack,", po::value<bool>(), "enable sequential-single-knapsack")
Expand Down Expand Up @@ -195,6 +196,8 @@ int main(int argc, char *argv[])
parameters.use_local_search_linear_programming = vm["use-local-search-linear-programming"].as<bool>();
if (vm.count("use-local-search-nonlinear-programming"))
parameters.use_local_search_nonlinear_programming = vm["use-local-search-nonlinear-programming"].as<bool>();
if (vm.count("tree-search-maximum-number-of-item-types"))
parameters.tree_search_maximum_number_of_item_types = vm["tree-search-maximum-number-of-item-types"].as<Counter>();
if (vm.count("use-local-search-direct-trigonometry"))
parameters.use_local_search_direct_trigonometry = vm["use-local-search-direct-trigonometry"].as<bool>();
if (vm.count("use-local-search-black-box-optimization"))
Expand Down
1 change: 1 addition & 0 deletions src/irregular/optimize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ void optimize_tree_search(
ts_parameters.not_anytime_maximum_approximation_ratio = parameters.not_anytime_maximum_approximation_ratio;
ts_parameters.maximum_approximation_ratio_factor = parameters.maximum_approximation_ratio_factor;
ts_parameters.minimum_maximum_approximation_ratio = parameters.minimum_maximum_approximation_ratio;
ts_parameters.maximum_number_of_item_types = parameters.tree_search_maximum_number_of_item_types;
ts_parameters.json_search_tree_path = parameters.json_search_tree_path;
ts_parameters.new_solution_callback = [&algorithm_formatter](
const irregular::Output& ts_output)
Expand Down
141 changes: 119 additions & 22 deletions src/irregular/tree_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ BranchingScheme::BranchingScheme(
parameters.maximum_approximation_ratio)),
parameters_(parameters)
{
item_type_ids_by_area_.resize(instance.number_of_item_types());
std::iota(item_type_ids_by_area_.begin(), item_type_ids_by_area_.end(), 0);
std::stable_sort(
item_type_ids_by_area_.begin(),
item_type_ids_by_area_.end(),
[&instance](ItemTypeId item_type_id_1, ItemTypeId item_type_id_2)
{
return instance.item_type(item_type_id_1).area_orig
> instance.item_type(item_type_id_2).area_orig;
});

// Compute bin_type_ids_ and previous_bins_area_.
//
// For the Knapsack objective, bins are considered by increasing space,
Expand Down Expand Up @@ -1032,28 +1043,52 @@ void BranchingScheme::insertions(
}

// Create insertions on the supporting parts of the last inserted item.
// Loop through each supporting part of the last inserted item.
for (ShapePos supporting_part_pos: bb_bin_type.trapezoid_sets[parent->trapezoid_set_id].supporting_parts) {
const Support& supporting_part = bb_bin_type.supporting_parts[supporting_part_pos];
//std::cout << "supporting_part " << supporting_part.shape.to_string(0) << std::endl;

// Loop through each supported part of each remaining item.
for (TrapezoidSetId supported_trapezoid_set_id: valid_trapezoid_set_ids) {
//std::cout << "trapezoid_set_id " << trapezoid_set_id << std::endl;
const TrapezoidSet& supported_trapezoid_set = trapezoid_sets[supported_trapezoid_set_id];
for (ShapePos supported_part_pos: supported_trapezoid_set.supported_parts) {
if (time_limit_reached(parent->children_insertions.size()))
return;
const Support& supported_part = bb_bin_type.supported_parts[supported_part_pos];

insertion_trapezoid_set(
parent,
supported_trapezoid_set_id,
supporting_part_pos,
parent->x,
parent->y,
supported_part_pos,
Direction::Any);
if (parameters_.maximum_number_of_item_types >= 0) {
auto insert_set = [&](TrapezoidSetId supported_trapezoid_set_id)
{
for (ShapePos supporting_part_pos: bb_bin_type.trapezoid_sets[parent->trapezoid_set_id].supporting_parts) {
for (ShapePos supported_part_pos: trapezoid_sets[supported_trapezoid_set_id].supported_parts) {
if (time_limit_reached(parent->children_insertions.size()))
return false;
insertion_trapezoid_set(
parent,
supported_trapezoid_set_id,
supporting_part_pos,
parent->x,
parent->y,
supported_part_pos,
Direction::Any);
}
}
return true;
};
if (!insert_biggest_item_types(parent, trapezoid_sets, valid_trapezoid_set_ids, insert_set))
return;
} else {
// Create insertions on the supporting parts of the last inserted item.
// Loop through each supporting part of the last inserted item.
for (ShapePos supporting_part_pos: bb_bin_type.trapezoid_sets[parent->trapezoid_set_id].supporting_parts) {
const Support& supporting_part = bb_bin_type.supporting_parts[supporting_part_pos];
//std::cout << "supporting_part " << supporting_part.shape.to_string(0) << std::endl;

// Loop through each supported part of each remaining item.
for (TrapezoidSetId supported_trapezoid_set_id: valid_trapezoid_set_ids) {
//std::cout << "trapezoid_set_id " << trapezoid_set_id << std::endl;
const TrapezoidSet& supported_trapezoid_set = trapezoid_sets[supported_trapezoid_set_id];
for (ShapePos supported_part_pos: supported_trapezoid_set.supported_parts) {
if (time_limit_reached(parent->children_insertions.size()))
return;
const Support& supported_part = bb_bin_type.supported_parts[supported_part_pos];

insertion_trapezoid_set(
parent,
supported_trapezoid_set_id,
supporting_part_pos,
parent->x,
parent->y,
supported_part_pos,
Direction::Any);
}
}
}
}
Expand Down Expand Up @@ -1091,6 +1126,41 @@ void BranchingScheme::insertions(
}

// Create insertions on the supporting parts of the bin.
if (parameters_.maximum_number_of_item_types >= 0) {
auto insert_set = [&](TrapezoidSetId supported_trapezoid_set_id)
{
const TrapezoidSet& supported_trapezoid_set = trapezoid_sets[supported_trapezoid_set_id];
const ItemType& supported_item_type = instance().item_type(supported_trapezoid_set.item_type_id);
for (ShapePos supporting_part_pos: bb_bin_type.bin_supporting_part_ids) {
const Support& supporting_part = bb_bin_type.supporting_parts[supporting_part_pos];
for (ShapePos supported_part_pos: supported_trapezoid_set.supported_parts) {
if (time_limit_reached(parent->children_insertions.size()))
return false;
const Support& supported_part = bb_bin_type.supported_parts[supported_part_pos];
const ItemShape& item_shape = supported_item_type.shapes[supported_part.item_shape_pos];
if (supporting_part.defect_id >= 0
&& instance().can_contain(
item_shape.quality_rule,
bin_type.defects[supporting_part.defect_id].type)) {
return false;
}
insertion_trapezoid_set(
parent,
supported_trapezoid_set_id,
supporting_part_pos,
0.0,
0.0,
supported_part_pos,
new_bin_direction,
new_bin_pos);
}
}
return true;
};
if (!insert_biggest_item_types(parent, trapezoid_sets, valid_trapezoid_set_ids, insert_set))
return;
continue;
}
for (ShapePos supporting_part_pos: bb_bin_type.bin_supporting_part_ids) {
const Support& supporting_part = bb_bin_type.supporting_parts[supporting_part_pos];

Expand Down Expand Up @@ -1135,6 +1205,32 @@ void BranchingScheme::insertions(
}
}

bool BranchingScheme::insert_biggest_item_types(
const std::shared_ptr<Node>& parent,
const std::vector<TrapezoidSet>& trapezoid_sets,
const std::vector<TrapezoidSetId>& valid_trapezoid_set_ids,
const std::function<bool(TrapezoidSetId)>& insert_set) const
{
std::vector<std::vector<TrapezoidSetId>> item_type_trapezoid_set_ids(instance().number_of_item_types());
for (TrapezoidSetId trapezoid_set_id: valid_trapezoid_set_ids)
item_type_trapezoid_set_ids[trapezoid_sets[trapezoid_set_id].item_type_id].push_back(trapezoid_set_id);
Counter number_of_item_types = 0;
for (ItemTypeId item_type_id: item_type_ids_by_area_) {
if (item_type_trapezoid_set_ids[item_type_id].empty())
continue;
ItemPos number_of_insertions = parent->children_insertions.size();
for (TrapezoidSetId trapezoid_set_id: item_type_trapezoid_set_ids[item_type_id])
if (!insert_set(trapezoid_set_id))
return false;
// An item type without an insertion does not count: smaller ones are tried instead.
if ((ItemPos)parent->children_insertions.size() > number_of_insertions
&& ++number_of_item_types >= parameters_.maximum_number_of_item_types) {
break;
}
}
return true;
}

void BranchingScheme::update_insertion(
const std::shared_ptr<Node>& parent,
const Insertion& insertion_orig) const
Expand Down Expand Up @@ -2247,6 +2343,7 @@ const packingsolver::irregular::TreeSearchOutput packingsolver::irregular::tree_
BranchingScheme::Parameters branching_scheme_parameters;
branching_scheme_parameters.guide_id = guide_id;
branching_scheme_parameters.direction = direction;
branching_scheme_parameters.maximum_number_of_item_types = parameters.maximum_number_of_item_types;
if (parameters.optimization_mode == OptimizationMode::Anytime
|| parameters.optimization_mode == OptimizationMode::Exact) {
branching_scheme_parameters.maximum_approximation_ratio
Expand Down
20 changes: 20 additions & 0 deletions src/irregular/tree_search.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "optimizationtools/utils/utils.hpp"
#include "optimizationtools/utils/timer.hpp"

#include <functional>
#include <sstream>
#include <cmath>

Expand Down Expand Up @@ -240,6 +241,9 @@ class BranchingScheme

double maximum_approximation_ratio = 0.05;

/** Item types expanded at each node, biggest first among those with an insertion; -1: all. */
Counter maximum_number_of_item_types = -1;

/**
* Timer to check against inside insertions() and children(), so a
* single node whose candidate insertions are numerous on a complex
Expand Down Expand Up @@ -272,6 +276,17 @@ class BranchingScheme
void insertions(
const std::shared_ptr<Node>& parent) const;

/**
* Insert the biggest item types of 'valid_trapezoid_set_ids' through
* 'insert_set', until 'maximum_number_of_item_types' of them got an
* insertion; false if 'insert_set' asked to stop.
*/
bool insert_biggest_item_types(
const std::shared_ptr<Node>& parent,
const std::vector<TrapezoidSet>& trapezoid_sets,
const std::vector<TrapezoidSetId>& valid_trapezoid_set_ids,
const std::function<bool(TrapezoidSetId)>& insert_set) const;

Node child_tmp(
const std::shared_ptr<Node>& parent,
const Insertion& insertion) const;
Expand Down Expand Up @@ -485,6 +500,9 @@ class BranchingScheme
*/
std::vector<BinTypeId> bin_type_ids_;

/** Item types by decreasing area: the order 'maximum_number_of_item_types' takes them in. */
std::vector<ItemTypeId> item_type_ids_by_area_;

/** Total area of the bins preceding each position, in the same order as 'bin_type_ids_'. */
std::vector<AreaDbl> previous_bins_area_;

Expand Down Expand Up @@ -717,6 +735,8 @@ struct TreeSearchParameters: packingsolver::Parameters<Instance, Solution, Outpu
double maximum_approximation_ratio_factor = 1.0;

double minimum_maximum_approximation_ratio = 0.0;

Counter maximum_number_of_item_types = -1;
};

const TreeSearchOutput tree_search(
Expand Down
Loading