From f56fa25d54760928504cd253c33948d01e04e985 Mon Sep 17 00:00:00 2001 From: Florian Fontan Date: Wed, 11 Nov 2026 13:08:22 +0100 Subject: [PATCH] irregular: enumerate the minimal infeasible subsets bottom-up in the Benders decomposition The minimal infeasible subsets of an infeasible bin were only searched top-down, from the infeasible subset returned by the tree search on relative positions: the largest items of the bin. The subsets made of other items were only found at later iterations, when the master problem proposed them in another bin. The subsets of the items of an infeasible bin are now enumerated bottom-up, by increasing binary order over the items sorted by decreasing area (A, B, BA, C, CA, CB, CBA, D...): a subset is only checked if all its subsets with one item less fit, so that each subset proven not to fit is a minimal infeasible subset. The subsets contained in a selection known to fit are not solved. The enumeration stops after 16 subproblem solves or 16 cuts, and falls back to the top-down search if it finds nothing. In exact mode, the subproblems are solved through the automatic selection of the algorithms, which runs the tree search on relative positions together with the algorithms which quickly find a packing when the items fit. The tree search on relative positions gets a parameter to solve the set of all the items directly, without its growing subsets of the largest items, used for the subsets of the bottom-up enumeration. --- include/packingsolver/irregular/optimize.hpp | 22 ++ python/src/irregular.cpp | 3 + src/irregular/benders_decomposition.cpp | 217 +++++++++++++++++- src/irregular/benders_decomposition.hpp | 29 +++ src/irregular/main.cpp | 6 + src/irregular/optimize.cpp | 5 + .../tree_search_relative_positions.cpp | 3 +- .../tree_search_relative_positions.hpp | 8 + 8 files changed, 281 insertions(+), 12 deletions(-) diff --git a/include/packingsolver/irregular/optimize.hpp b/include/packingsolver/irregular/optimize.hpp index 0c4063141..231b756ec 100644 --- a/include/packingsolver/irregular/optimize.hpp +++ b/include/packingsolver/irregular/optimize.hpp @@ -316,6 +316,13 @@ struct OptimizeParameters: packingsolver::Parameters */ bool use_tree_search_relative_positions = false; + /** + * For the 'Feasibility' objective, solve the increasing subsets of the + * largest items one after the other in the tree search on relative + * positions. If 'false', only the set of all the items is solved. + */ + bool tree_search_relative_positions_growing_subsets = true; + /** * Use the global nonlinear programming algorithm (only for the * 'Feasibility' objective with a single convex bin; only selected @@ -427,6 +434,21 @@ struct OptimizeParameters: packingsolver::Parameters */ NodeId benders_decomposition_subproblem_tree_search_queue_size = 128; + /** + * Maximum number of subproblem solves of the bottom-up enumeration of the + * minimal infeasible subsets of an infeasible bin in the Benders + * decomposition algorithm with the tree search on relative positions (0 + * disables it). + */ + Counter benders_decomposition_bottom_up_maximum_number_of_subproblem_solves = 16; + + /** + * Maximum number of cuts of the bottom-up enumeration of the minimal + * infeasible subsets of an infeasible bin in the Benders decomposition + * algorithm. + */ + Counter benders_decomposition_bottom_up_maximum_number_of_cuts = 16; + /* * Parameters for anytime mode */ diff --git a/python/src/irregular.cpp b/python/src/irregular.cpp index b89364ea4..c176ac0b5 100644 --- a/python/src/irregular.cpp +++ b/python/src/irregular.cpp @@ -673,6 +673,9 @@ void bind_irregular(nb::module_& m) .def_rw("use_global_nonlinear_programming", &OptimizeParameters::use_global_nonlinear_programming) .def_rw("use_benders_decomposition", &OptimizeParameters::use_benders_decomposition) .def_rw("benders_decomposition_use_tree_search_relative_positions", &OptimizeParameters::benders_decomposition_use_tree_search_relative_positions) + .def_rw("benders_decomposition_bottom_up_maximum_number_of_subproblem_solves", &OptimizeParameters::benders_decomposition_bottom_up_maximum_number_of_subproblem_solves) + .def_rw("benders_decomposition_bottom_up_maximum_number_of_cuts", &OptimizeParameters::benders_decomposition_bottom_up_maximum_number_of_cuts) + .def_rw("tree_search_relative_positions_growing_subsets", &OptimizeParameters::tree_search_relative_positions_growing_subsets) .def_rw("use_sequential_single_knapsack", &OptimizeParameters::use_sequential_single_knapsack) .def_rw("use_sequential_value_correction", &OptimizeParameters::use_sequential_value_correction) .def_rw("use_dichotomic_search", &OptimizeParameters::use_dichotomic_search) diff --git a/src/irregular/benders_decomposition.cpp b/src/irregular/benders_decomposition.cpp index 020203d09..52661cd60 100644 --- a/src/irregular/benders_decomposition.cpp +++ b/src/irregular/benders_decomposition.cpp @@ -148,14 +148,21 @@ OptimizationMode subproblem_optimization_mode( /** * Result of the tree search on relative positions for a selection of items, - * through the cache: it solves the subproblem of the selection. + * through the cache: it solves the subproblem of the selection. In 'Exact' + * mode, through the automatic selection of the algorithms, which runs the + * tree search on relative positions together with the algorithms which + * quickly find a packing when the items fit. + * + * 'growing_subsets': see + * 'OptimizeParameters::tree_search_relative_positions_growing_subsets'. */ const TreeSearchRelativePositionsResult& tree_search_relative_positions_result( const Instance& instance, BinTypeId bin_type_id, const std::vector>& selected_items, const BendersDecompositionParameters& parameters, - SubproblemCache& subproblem_cache) + SubproblemCache& subproblem_cache, + bool growing_subsets = true) { auto key = std::make_pair(bin_type_id, selected_items); auto it = subproblem_cache.results.find(key); @@ -171,8 +178,16 @@ const TreeSearchRelativePositionsResult& tree_search_relative_positions_result( OptimizeParameters sub_parameters; sub_parameters.verbosity_level = 0; sub_parameters.timer = parameters.timer; - sub_parameters.optimization_mode = subproblem_optimization_mode(parameters); - sub_parameters.use_tree_search_relative_positions = true; + if (parameters.optimization_mode == OptimizationMode::Exact) { + // The automatic selection runs the tree search on relative + // positions, which proves that the items don't fit, together with + // the algorithms which quickly find a packing when they fit. + sub_parameters.optimization_mode = OptimizationMode::Exact; + } else { + sub_parameters.optimization_mode = subproblem_optimization_mode(parameters); + sub_parameters.use_tree_search_relative_positions = true; + } + sub_parameters.tree_search_relative_positions_growing_subsets = growing_subsets; irregular::Output sub_output = optimize(sub_instance, sub_parameters); TreeSearchRelativePositionsResult result; @@ -533,6 +548,165 @@ std::vector enumerate_minimal_infeasible_subsets( return subsets; } +/** + * Enumerate minimal infeasible subsets of a selection proven not to fit, + * bottom-up. + * + * The units of the selection (one per copy) are sorted by decreasing area, + * and its subsets are enumerated in the order A, B, BA, C, CA, CB, CBA, D... + * (the binary order of their characteristic vectors). Every subset comes + * before its supersets, so a subset is only checked if all its subsets with + * one item less fit: then, a subset proven not to fit is a minimal + * infeasible subset. All the subsets of the largest items are enumerated + * first, and the enumeration stops after + * 'bottom_up_maximum_number_of_subproblem_solves' subproblem solves or + * 'bottom_up_maximum_number_of_cuts' minimal infeasible subsets: all the + * minimal infeasible subsets of a small selection are found, and some of the + * ones of a large selection. + * + * A subset fits without solving anything if it is contained in a selection + * known to fit ('feasible_selections', the selections packed so far). + * Otherwise, its subproblem is solved (see + * 'tree_search_relative_positions_result'), without the growing subsets of + * the largest items in the tree search on relative positions, since its + * subsets are already known to fit. A subset which isn't proven infeasible is + * considered to fit to check the larger subsets: a subset proven infeasible + * is still a valid cut, only maybe not minimal. + */ +std::vector enumerate_minimal_infeasible_subsets_bottom_up( + const Instance& instance, + BinTypeId bin_type_id, + const std::vector>& selected_items, + std::vector>>& feasible_selections, + const BendersDecompositionParameters& parameters, + SubproblemCache& subproblem_cache, + BendersDecompositionOutput& output) +{ + // Units sorted by decreasing area; the copies of an item type are + // consecutive. + std::vector units; + for (const std::pair& p: selected_items) + for (ItemPos copy = 0; copy < p.second; ++copy) + units.push_back(p.first); + std::sort( + units.begin(), + units.end(), + [&instance](ItemTypeId item_type_id_1, ItemTypeId item_type_id_2) + { + double area_1 = instance.item_type(item_type_id_1).area_scaled; + double area_2 = instance.item_type(item_type_id_2).area_scaled; + if (area_1 != area_2) + return area_1 > area_2; + return item_type_id_1 < item_type_id_2; + }); + // Subsets are bit masks over the first units. + const std::size_t maximum_number_of_units = 20; + std::size_t number_of_units = (std::min)(units.size(), maximum_number_of_units); + // First unit of the group of copies of each unit. + std::vector group_start(number_of_units, 0); + for (std::size_t pos = 1; pos < number_of_units; ++pos) { + group_start[pos] = (units[pos] == units[pos - 1])? + group_start[pos - 1]: pos; + } + // The copies of an item type are identical: a subset is represented by + // its canonical mask, whose units of each group form a prefix. + auto canonical = [&group_start, number_of_units](uint32_t mask) + { + uint32_t result = 0; + std::size_t pos = 0; + while (pos < number_of_units) { + std::size_t start = group_start[pos]; + std::size_t end = pos; + while (end < number_of_units && group_start[end] == start) + ++end; + std::size_t count = 0; + for (std::size_t pos_2 = start; pos_2 < end; ++pos_2) + if (mask & ((uint32_t)1 << pos_2)) + ++count; + for (std::size_t pos_2 = start; pos_2 < start + count; ++pos_2) + result |= ((uint32_t)1 << pos_2); + pos = end; + } + return result; + }; + auto to_selection = [&units, number_of_units](uint32_t mask) + { + std::vector> selection; + for (std::size_t pos = 0; pos < number_of_units; ++pos) { + if (!(mask & ((uint32_t)1 << pos))) + continue; + if (!selection.empty() && selection.back().first == units[pos]) { + selection.back().second++; + } else { + selection.push_back({units[pos], 1}); + } + } + std::sort(selection.begin(), selection.end()); + return selection; + }; + + // Subsets known to fit. + std::vector feasible((std::size_t)1 << number_of_units, false); + feasible[0] = true; + std::vector subsets; + Counter number_of_solves = 0; + for (uint32_t mask = 1; mask < ((uint32_t)1 << number_of_units); ++mask) { + if (parameters.timer.needs_to_end()) + break; + if (canonical(mask) != mask) + continue; + // All the subsets with one item less must fit. + bool all_feasible = true; + for (std::size_t pos = 0; pos < number_of_units; ++pos) { + if (!(mask & ((uint32_t)1 << pos))) + continue; + if (!feasible[canonical(mask & ~((uint32_t)1 << pos))]) { + all_feasible = false; + break; + } + } + if (!all_feasible) + continue; + + output.number_of_bottom_up_checks++; + std::vector> selection = to_selection(mask); + bool known_feasible = false; + for (const auto& feasible_selection: feasible_selections) { + if (contains(feasible_selection, selection)) { + known_feasible = true; + break; + } + } + if (known_feasible) { + feasible[mask] = true; + continue; + } + if (number_of_solves >= parameters.bottom_up_maximum_number_of_subproblem_solves) + break; + number_of_solves++; + output.number_of_bottom_up_solves++; + const TreeSearchRelativePositionsResult& result = tree_search_relative_positions_result( + instance, + bin_type_id, + selection, + parameters, + subproblem_cache, + false); + if (parameters.timer.needs_to_end()) + break; + if (result.feasibility == SelectionFeasibility::ProvenInfeasible) { + subsets.push_back({selection, true}); + if ((Counter)subsets.size() >= parameters.bottom_up_maximum_number_of_cuts) + break; + } else { + if (result.packing != nullptr) + feasible_selections.push_back(selection); + feasible[mask] = true; + } + } + return subsets; +} + /** Add a cut as a new resource of a bin type of the master problem. */ void add_cut_as_resource( onedimensional::InstanceBuilder& master_instance_builder, @@ -710,6 +884,11 @@ BendersDecompositionOutput packingsolver::irregular::benders_decomposition( instance.number_of_bin_types()); std::vector>>> proven_cut_items_by_bin_type( instance.number_of_bin_types()); + // Selections known to fit, per bin type: the bins packed so far and the + // selections packed by the bottom-up enumeration of the minimal + // infeasible subsets. + std::vector>>> feasible_selections_by_bin_type( + instance.number_of_bin_types()); SubproblemCache subproblem_cache; @@ -986,6 +1165,7 @@ BendersDecompositionOutput packingsolver::irregular::benders_decomposition( // The 'copies' identical bins of the master solution share // the same packing. solution.append_bin(packing, 0, master_bin.copies); + feasible_selections_by_bin_type[master_bin.bin_type_id].push_back(selected_items); continue; } all_bins_feasible = false; @@ -1000,13 +1180,28 @@ BendersDecompositionOutput packingsolver::irregular::benders_decomposition( // infeasible items. A cut which isn't proven may still help the master // problem converge, but then its bound isn't a bound anymore. auto minimal_infeasible_subsets_begin = std::chrono::steady_clock::now(); - std::vector subsets = enumerate_minimal_infeasible_subsets( - instance, - master_bin.bin_type_id, - infeasible_items, - feasibility == SelectionFeasibility::ProvenInfeasible, - parameters, - subproblem_cache); + std::vector subsets; + if (parameters.subproblem_use_tree_search_relative_positions + && feasibility == SelectionFeasibility::ProvenInfeasible + && parameters.bottom_up_maximum_number_of_subproblem_solves > 0) { + subsets = enumerate_minimal_infeasible_subsets_bottom_up( + instance, + master_bin.bin_type_id, + selected_items, + feasible_selections_by_bin_type[master_bin.bin_type_id], + parameters, + subproblem_cache, + output); + } + if (subsets.empty()) { + subsets = enumerate_minimal_infeasible_subsets( + instance, + master_bin.bin_type_id, + infeasible_items, + feasibility == SelectionFeasibility::ProvenInfeasible, + parameters, + subproblem_cache); + } output.minimal_infeasible_subsets_time += elapsed_time(minimal_infeasible_subsets_begin); for (const MinimalInfeasibleSubset& subset: subsets) { if (parameters.subproblem_use_tree_search_relative_positions diff --git a/src/irregular/benders_decomposition.hpp b/src/irregular/benders_decomposition.hpp index f579bfcdf..81605a24e 100644 --- a/src/irregular/benders_decomposition.hpp +++ b/src/irregular/benders_decomposition.hpp @@ -72,6 +72,15 @@ struct BendersDecompositionOutput: Output /** Time spent computing the incompatible pairs and triples. */ double incompatible_subsets_time = 0.0; + /** + * Number of selections checked by the bottom-up enumeration of the + * minimal infeasible subsets (see + * 'BendersDecompositionParameters::bottom_up_maximum_number_of_subproblem_solves'), + * and number of them which needed a subproblem solve. + */ + Counter number_of_bottom_up_checks = 0; + Counter number_of_bottom_up_solves = 0; + virtual nlohmann::json to_json() const override { nlohmann::json json = Output::to_json(); @@ -86,6 +95,8 @@ struct BendersDecompositionOutput: Output json["NumberOfIncompatiblePairs"] = number_of_incompatible_pairs; json["NumberOfIncompatibleTriples"] = number_of_incompatible_triples; json["IncompatibleSubsetsTime"] = incompatible_subsets_time; + json["NumberOfBottomUpChecks"] = number_of_bottom_up_checks; + json["NumberOfBottomUpSolves"] = number_of_bottom_up_solves; return json; } @@ -110,6 +121,8 @@ struct BendersDecompositionOutput: Output << std::setw(width) << std::left << "Number of incompatible pairs: " << number_of_incompatible_pairs << std::endl << std::setw(width) << std::left << "Number of incompatible triples: " << number_of_incompatible_triples << std::endl << std::setw(width) << std::left << "Incompatible subsets time (s): " << incompatible_subsets_time << std::endl + << std::setw(width) << std::left << "Number of bottom-up checks: " << number_of_bottom_up_checks << std::endl + << std::setw(width) << std::left << "Number of bottom-up solves: " << number_of_bottom_up_solves << std::endl ; } }; @@ -147,6 +160,22 @@ struct BendersDecompositionParameters: packingsolver::Parameters(), "enable column-generation") ("use-benders-decomposition,", po::value(), "enable Benders decomposition algorithm") ("benders-decomposition-use-tree-search-relative-positions,", po::value(), "use the tree search on relative positions in the subproblems of the Benders decomposition algorithm") + ("benders-decomposition-bottom-up-maximum-number-of-subproblem-solves,", po::value(), "maximum number of subproblem solves of the bottom-up enumeration of the minimal infeasible subsets of a bin in the Benders decomposition algorithm (0 disables it)") + ("benders-decomposition-bottom-up-maximum-number-of-cuts,", po::value(), "maximum number of cuts of the bottom-up enumeration of the minimal infeasible subsets of a bin in the Benders decomposition algorithm") ("use-tree-search-relative-positions,", po::value(), "use the tree search on relative positions (Feasibility objective with a single bin, open dimension objectives)") ("use-global-nonlinear-programming,", po::value(), "use the global nonlinear programming algorithm (Feasibility objective with a single convex bin; requires SCIP)") ("use-dichotomic-search,", po::value(), "enable dichotomic search") @@ -209,6 +211,10 @@ int main(int argc, char *argv[]) parameters.use_benders_decomposition = vm["use-benders-decomposition"].as(); if (vm.count("benders-decomposition-use-tree-search-relative-positions")) parameters.benders_decomposition_use_tree_search_relative_positions = vm["benders-decomposition-use-tree-search-relative-positions"].as(); + if (vm.count("benders-decomposition-bottom-up-maximum-number-of-subproblem-solves")) + parameters.benders_decomposition_bottom_up_maximum_number_of_subproblem_solves = vm["benders-decomposition-bottom-up-maximum-number-of-subproblem-solves"].as(); + if (vm.count("benders-decomposition-bottom-up-maximum-number-of-cuts")) + parameters.benders_decomposition_bottom_up_maximum_number_of_cuts = vm["benders-decomposition-bottom-up-maximum-number-of-cuts"].as(); if (vm.count("use-tree-search-relative-positions")) parameters.use_tree_search_relative_positions = vm["use-tree-search-relative-positions"].as(); if (vm.count("use-global-nonlinear-programming")) diff --git a/src/irregular/optimize.cpp b/src/irregular/optimize.cpp index acd48fae9..c8de1e604 100644 --- a/src/irregular/optimize.cpp +++ b/src/irregular/optimize.cpp @@ -928,6 +928,10 @@ void optimize_benders_decomposition( = parameters.not_anytime_benders_decomposition_number_of_iterations; bd_parameters.subproblem_queue_size = parameters.benders_decomposition_subproblem_tree_search_queue_size; + bd_parameters.bottom_up_maximum_number_of_subproblem_solves + = parameters.benders_decomposition_bottom_up_maximum_number_of_subproblem_solves; + bd_parameters.bottom_up_maximum_number_of_cuts + = parameters.benders_decomposition_bottom_up_maximum_number_of_cuts; // In 'Exact' mode, the tree search on relative positions is used so that // the cuts are proven. bd_parameters.subproblem_use_tree_search_relative_positions @@ -957,6 +961,7 @@ void optimize_tree_search_relative_positions( tsrp_parameters.timer.add_end_boolean(&algorithm_formatter.end_boolean()); if (parameters.optimization_mode == OptimizationMode::NotAnytimeSequential) tsrp_parameters.number_of_threads = 1; + tsrp_parameters.growing_subsets = parameters.tree_search_relative_positions_growing_subsets; tsrp_parameters.new_solution_callback = [&algorithm_formatter]( const irregular::Output& output) { algorithm_formatter.update_solution( diff --git a/src/irregular/tree_search_relative_positions.cpp b/src/irregular/tree_search_relative_positions.cpp index f63bff35b..0c42e7ed3 100644 --- a/src/irregular/tree_search_relative_positions.cpp +++ b/src/irregular/tree_search_relative_positions.cpp @@ -1790,7 +1790,8 @@ TreeSearchRelativePositionsOutput packingsolver::irregular::tree_search_relative }; if (!open_dimension) { - for (ItemPos number_of_items = 1; + for (ItemPos number_of_items = (parameters.growing_subsets)? + 1: (std::max)((ItemPos)1, (ItemPos)item_type_ids.size()); number_of_items <= (ItemPos)item_type_ids.size(); ++number_of_items) { if (parameters.timer.needs_to_end()) diff --git a/src/irregular/tree_search_relative_positions.hpp b/src/irregular/tree_search_relative_positions.hpp index 4ababb5ba..0482dc5f7 100644 --- a/src/irregular/tree_search_relative_positions.hpp +++ b/src/irregular/tree_search_relative_positions.hpp @@ -109,6 +109,14 @@ struct TreeSearchRelativePositionsParameters: packingsolver::Parameters::infinity(); + + /** + * For the 'Feasibility' objective, solve the increasing subsets of the + * largest items one after the other (see 'tree_search_relative_positions'). + * If 'false', only the set of all the items is solved, e.g. when all its + * subsets are already known to fit. + */ + bool growing_subsets = true; }; /**