From 67826eabc127ff6c68148c5d4cb7558d4f701a0a Mon Sep 17 00:00:00 2001 From: Florian Fontan Date: Tue, 24 Nov 2026 10:30:58 +0100 Subject: [PATCH 1/2] Update the description of the variable-sized bin packing with leftovers objective --- include/packingsolver/algorithms/common.hpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/include/packingsolver/algorithms/common.hpp b/include/packingsolver/algorithms/common.hpp index 9232e93ba..478f7811b 100644 --- a/include/packingsolver/algorithms/common.hpp +++ b/include/packingsolver/algorithms/common.hpp @@ -323,10 +323,9 @@ enum class Objective * Variable-sized bin packing in which the leftovers of the bins are * valued (see 'LeftoverValueType'). * - * If the cost of a bin type is set, the cost of the solution is minimized - * first; otherwise, the value of the stock left after packing the items - * (the leftovers of the bins of the solution and the bins which are not - * used) is maximized directly. The algorithms optimize it as + * The net cost of the solution is minimized: the cost of its bins minus a + * refund for each of their leftovers (see 'Instance::leftover_refund' of + * each problem type). The algorithms optimize it as * 'VariableSizedBinPacking'. */ VariableSizedBinPackingWithLeftovers, From 26e14abbd9e783a665e9a04b2e49866dc3325fa5 Mon Sep 17 00:00:00 2001 From: Florian Fontan Date: Tue, 24 Nov 2026 18:29:53 +0100 Subject: [PATCH 2/2] Apply the leftover post-process to the other objectives The leftover post-process re-optimizes the bins of the best solution as single-bin BinPackingWithLeftovers instances, and keeps a re-optimized bin if its leftovers are better. It was only applied for the BinPackingWithLeftovers objective. It is now also applied for the BinPacking, VariableSizedBinPacking, VariableSizedBinPackingWithLeftovers and Knapsack objectives, in the not-anytime modes: - The last bin post-process remains specific to BinPackingWithLeftovers. - For the other objectives, all the bins are re-optimized ('reoptimize_all_bins_leftovers'), whatever 'leftover_all_bins'. - The post-processed solution has the same value as the best solution: it replaces it in the solution pool if its leftovers are better ('AlgorithmFormatter::update_solution_leftovers', 'SolutionPool::replace_best', 'Solution::leftovers_better'). - The post-process isn't stopped when the best solution is proven optimal, since the leftovers are not part of the proven objective. - Empty bins are not re-optimized. The re-optimization functions are renamed to make clear that they re-optimize the leftovers: 'reoptimize_bin_leftovers', 'reoptimize_non_last_bins_leftovers' and 'reoptimize_all_bins_leftovers'. --- doc/leftovers.rst | 8 ++ include/packingsolver/algorithms/common.hpp | 44 +++++++ .../packingsolver/box/algorithm_formatter.hpp | 12 ++ include/packingsolver/box/optimize.hpp | 5 +- include/packingsolver/box/post_process.hpp | 34 ++++-- include/packingsolver/box/solution.hpp | 12 ++ .../boxstacks/algorithm_formatter.hpp | 12 ++ include/packingsolver/boxstacks/optimize.hpp | 5 +- .../packingsolver/boxstacks/post_process.hpp | 34 ++++-- include/packingsolver/boxstacks/solution.hpp | 12 ++ .../irregular/algorithm_formatter.hpp | 12 ++ include/packingsolver/irregular/optimize.hpp | 5 +- .../packingsolver/irregular/post_process.hpp | 34 ++++-- include/packingsolver/irregular/solution.hpp | 12 ++ .../rectangle/algorithm_formatter.hpp | 12 ++ include/packingsolver/rectangle/optimize.hpp | 5 +- .../packingsolver/rectangle/post_process.hpp | 34 ++++-- include/packingsolver/rectangle/solution.hpp | 12 ++ src/box/algorithm_formatter.cpp | 26 ++++ src/box/optimize.cpp | 105 +++++++++++----- src/box/post_process.cpp | 46 ++++++- src/box/solution.cpp | 12 ++ src/boxstacks/algorithm_formatter.cpp | 26 ++++ src/boxstacks/optimize.cpp | 86 +++++++++---- src/boxstacks/post_process.cpp | 46 ++++++- src/boxstacks/solution.cpp | 12 ++ src/irregular/algorithm_formatter.cpp | 26 ++++ src/irregular/benders_decomposition.cpp | 2 + src/irregular/large_item_first.cpp | 7 +- src/irregular/optimize.cpp | 103 +++++++++++----- src/irregular/post_process.cpp | 46 ++++++- src/irregular/solution.cpp | 21 ++++ src/rectangle/algorithm_formatter.cpp | 26 ++++ src/rectangle/benders_decomposition.cpp | 1 + src/rectangle/optimize.cpp | 112 ++++++++++++----- src/rectangle/post_process.cpp | 46 ++++++- src/rectangle/solution.cpp | 12 ++ test/box/box_test.cpp | 4 +- test/boxstacks/leftover_volume_test.cpp | 4 +- test/rectangle/rectangle_test.cpp | 114 +++++++++++++++++- 40 files changed, 994 insertions(+), 193 deletions(-) diff --git a/doc/leftovers.rst b/doc/leftovers.rst index 3b059b779..3746470f3 100644 --- a/doc/leftovers.rst +++ b/doc/leftovers.rst @@ -106,6 +106,14 @@ For the :code:`variable-sized-bin-packing-with-leftovers` objective, two solutio The solution also reports these values, in the fields ``LeftoverValue``, ``StaircaseLeftoverValue``, ``TotalLeftoverValue`` and ``TotalStaircaseLeftoverValue`` of the output. With the ``squared-area`` value type, ``LeftoverValue`` and ``TotalLeftoverValue`` are squared areas, while the staircase values are areas. +Post-process +------------ + +In the not-anytime optimization modes, for the :ref:`rectangle`, :ref:`box`, :ref:`box-stacks` and :ref:`irregular` problem types, the bins of the best solution are re-packed at the end to improve their leftovers. A re-packed bin keeps the same items, and it is kept only if its leftover is better. + +* With the :code:`bin-packing-with-leftovers` objective, the last bin is re-packed with the tree search, if it contains few items; then, if ``leftover_all_bins`` is enabled, the other bins are re-packed. +* With the :code:`bin-packing`, :code:`variable-sized-bin-packing`, :code:`variable-sized-bin-packing-with-leftovers` and :code:`knapsack` objectives, all the bins are re-packed. Except for :code:`variable-sized-bin-packing-with-leftovers`, the value of the solution doesn't change: the re-packed solution only replaces the best one if the total leftover value of the bins is greater. + .. _leftovers-rectangleguillotine: Rectangle guillotine diff --git a/include/packingsolver/algorithms/common.hpp b/include/packingsolver/algorithms/common.hpp index 478f7811b..4ce8947ec 100644 --- a/include/packingsolver/algorithms/common.hpp +++ b/include/packingsolver/algorithms/common.hpp @@ -341,6 +341,25 @@ inline bool is_variable_sized_bin_packing(Objective objective) || objective == Objective::VariableSizedBinPackingWithLeftovers; } +/** + * Return 'true' iff the leftover post-process applies to an objective: the + * objectives for which all the bins of a solution are packed with a fixed set + * of items. + * + * For 'BinPackingWithLeftovers' and 'VariableSizedBinPackingWithLeftovers', + * better leftovers give a better solution. For the other ones, re-packing the + * items of a bin doesn't change the value of the solution, but it may leave + * better leftovers. + */ +inline bool leftover_post_process_objective(Objective objective) +{ + return objective == Objective::BinPacking + || objective == Objective::BinPackingWithLeftovers + || objective == Objective::VariableSizedBinPacking + || objective == Objective::VariableSizedBinPackingWithLeftovers + || objective == Objective::Knapsack; +} + /** * Get the objective of a onedimensional sub-instance built from an instance * of another problem type: the onedimensional problem type doesn't value the @@ -482,6 +501,31 @@ class SolutionPool return (new_best)? 1: 0; } + /** + * Replace the best solution of the pool by a solution which is equivalent + * to it: with the same value for the objective, and the same item copies. + * + * It is used by the post-processes which improve a solution on a + * criterion which is not part of the objective, e.g. its leftovers. + * + * Return 'false', without changing the pool, if the solution is not + * equivalent to the best solution. + */ + bool replace_best( + const Solution& solution, + const std::string& label = "") + { + Entry entry = {solution, label}; + EntryComparator comparator; + if (comparator(entry, best_) || comparator(best_, entry)) + return false; + solutions_.erase(solutions_.begin()); + solutions_.insert(entry); + best_ = *solutions_.begin(); + worst_ = *std::prev(solutions_.end()); + return true; + } + private: Counter size_max_; diff --git a/include/packingsolver/box/algorithm_formatter.hpp b/include/packingsolver/box/algorithm_formatter.hpp index 57772f9ae..7b18aa10e 100644 --- a/include/packingsolver/box/algorithm_formatter.hpp +++ b/include/packingsolver/box/algorithm_formatter.hpp @@ -45,6 +45,18 @@ class AlgorithmFormatter const Solution& solution, const std::string& s); + /** + * Update the solution with a solution from the leftover post-process. + * + * The solution is added as with 'update_solution' if it is better for + * the objective. If it is equivalent to the best solution for the + * objective but its leftovers are better (see + * 'Solution::leftovers_better'), it replaces the best solution. + */ + void update_solution_leftovers( + const Solution& solution, + const std::string& s); + /** Mark the instance as proven infeasible. */ void update_is_proven_infeasible(); diff --git a/include/packingsolver/box/optimize.hpp b/include/packingsolver/box/optimize.hpp index c461877e6..e741c0c2f 100644 --- a/include/packingsolver/box/optimize.hpp +++ b/include/packingsolver/box/optimize.hpp @@ -232,8 +232,9 @@ struct OptimizeParameters: packingsolver::Parameters std::vector tree_search_directions; /** - * Re-optimize the bins of the best solution at the end, for the - * BinPackingWithLeftovers objective in a not-anytime mode. + * Re-optimize the bins of the best solution at the end, to improve their + * leftovers, in a not-anytime mode, for the objectives of + * 'leftover_post_process_objective'. */ bool post_process = true; diff --git a/include/packingsolver/box/post_process.hpp b/include/packingsolver/box/post_process.hpp index 4e16a02c0..fa588edc9 100644 --- a/include/packingsolver/box/post_process.hpp +++ b/include/packingsolver/box/post_process.hpp @@ -22,24 +22,25 @@ struct GroupIdenticalBinsOutput: packingsolver::Output GroupIdenticalBinsOutput group_identical_bins(const Solution& solution); /** - * Re-optimize the bin at 'bin_pos' of a solution, as its own single-bin - * BinPackingWithLeftovers instance, with 'optimize' and 'parameters' (its - * output and its post-process being disabled). + * Re-optimize the leftovers of the bin at 'bin_pos' of a solution, by solving + * that bin as its own single-bin BinPackingWithLeftovers instance, with + * 'optimize' and 'parameters' (its output and its post-process being + * disabled). * * Return the solution with that bin replaced by the re-optimized one, or the - * solution itself if the re-optimization didn't pack all the items of the - * bin. + * solution itself if the bin is empty or if the re-optimization didn't pack + * all the items of the bin. */ -Solution reoptimize_bin( +Solution reoptimize_bin_leftovers( const Solution& solution, BinPos bin_pos, const OptimizeParameters& parameters = {}); /** - * Re-optimize each bin of a solution except the last one (see - * 'reoptimize_bin'), and keep a re-optimized bin if its leftover value is - * greater, or if its leftover value is the same and its staircase leftover - * value is greater. + * Re-optimize the leftovers of each bin of a solution except the last one + * (see 'reoptimize_bin_leftovers'), and keep a re-optimized bin if its + * leftover value is greater, or if its leftover value is the same and its + * staircase leftover value is greater (see 'Solution::leftovers_better'). * * With the algorithms selected as for any instance, a bin with many items * found quickly is re-optimized quickly too. @@ -48,7 +49,18 @@ Solution reoptimize_bin( * bin in the objective, so the algorithms hardly try to keep the free space * of the other bins in a single region. */ -Solution reoptimize_non_last_bins( +Solution reoptimize_non_last_bins_leftovers( + const Solution& solution, + const OptimizeParameters& parameters = {}); + +/** + * Re-optimize the leftovers of each bin of a solution (see + * 'reoptimize_non_last_bins_leftovers'). + * + * It is used by the leftover post-process for the objectives other than + * 'BinPackingWithLeftovers', for which the last bin plays no special role. + */ +Solution reoptimize_all_bins_leftovers( const Solution& solution, const OptimizeParameters& parameters = {}); diff --git a/include/packingsolver/box/solution.hpp b/include/packingsolver/box/solution.hpp index d064057bd..7f0969203 100644 --- a/include/packingsolver/box/solution.hpp +++ b/include/packingsolver/box/solution.hpp @@ -282,6 +282,18 @@ class Solution */ inline Profit net_cost() const { return net_cost_; } + /** + * Return 'true' iff the leftovers of the solution are better than the + * ones of another solution of the same instance: the total leftover value + * of all the bins of the instance is greater (see + * 'total_leftover_value'), or it is the same and the total staircase + * leftover value is greater. + * + * It doesn't depend on the objective of the instance; it is used by the + * leftover post-process. + */ + bool leftovers_better(const Solution& solution) const; + /** * Get the sum of the staircase leftover values of all the bins of the * instance: the staircase leftover values of the bins of the solution, diff --git a/include/packingsolver/boxstacks/algorithm_formatter.hpp b/include/packingsolver/boxstacks/algorithm_formatter.hpp index f0e6c9d4b..36f171afc 100644 --- a/include/packingsolver/boxstacks/algorithm_formatter.hpp +++ b/include/packingsolver/boxstacks/algorithm_formatter.hpp @@ -45,6 +45,18 @@ class AlgorithmFormatter const Solution& solution, const std::string& s); + /** + * Update the solution with a solution from the leftover post-process. + * + * The solution is added as with 'update_solution' if it is better for + * the objective. If it is equivalent to the best solution for the + * objective but its leftovers are better (see + * 'Solution::leftovers_better'), it replaces the best solution. + */ + void update_solution_leftovers( + const Solution& solution, + const std::string& s); + /** Mark the instance as proven infeasible. */ void update_is_proven_infeasible(); diff --git a/include/packingsolver/boxstacks/optimize.hpp b/include/packingsolver/boxstacks/optimize.hpp index 7fe4aba3d..d311436dc 100644 --- a/include/packingsolver/boxstacks/optimize.hpp +++ b/include/packingsolver/boxstacks/optimize.hpp @@ -201,8 +201,9 @@ struct OptimizeParameters: packingsolver::Parameters std::vector tree_search_guides; /** - * Re-optimize the bins of the best solution at the end, for the - * BinPackingWithLeftovers objective in a not-anytime mode. + * Re-optimize the bins of the best solution at the end, to improve their + * leftovers, in a not-anytime mode, for the objectives of + * 'leftover_post_process_objective'. */ bool post_process = true; diff --git a/include/packingsolver/boxstacks/post_process.hpp b/include/packingsolver/boxstacks/post_process.hpp index 11ccf2f10..cae1c91f0 100644 --- a/include/packingsolver/boxstacks/post_process.hpp +++ b/include/packingsolver/boxstacks/post_process.hpp @@ -22,24 +22,25 @@ struct GroupIdenticalBinsOutput: packingsolver::Output GroupIdenticalBinsOutput group_identical_bins(const Solution& solution); /** - * Re-optimize the bin at 'bin_pos' of a solution, as its own single-bin - * BinPackingWithLeftovers instance, with 'optimize' and 'parameters' (its - * output and its post-process being disabled). + * Re-optimize the leftovers of the bin at 'bin_pos' of a solution, by solving + * that bin as its own single-bin BinPackingWithLeftovers instance, with + * 'optimize' and 'parameters' (its output and its post-process being + * disabled). * * Return the solution with that bin replaced by the re-optimized one, or the - * solution itself if the re-optimization didn't pack all the items of the - * bin. + * solution itself if the bin is empty or if the re-optimization didn't pack + * all the items of the bin. */ -Solution reoptimize_bin( +Solution reoptimize_bin_leftovers( const Solution& solution, BinPos bin_pos, const OptimizeParameters& parameters = {}); /** - * Re-optimize each bin of a solution except the last one (see - * 'reoptimize_bin'), and keep a re-optimized bin if its leftover value is - * greater, or if its leftover value is the same and its staircase leftover - * value is greater. + * Re-optimize the leftovers of each bin of a solution except the last one + * (see 'reoptimize_bin_leftovers'), and keep a re-optimized bin if its + * leftover value is greater, or if its leftover value is the same and its + * staircase leftover value is greater (see 'Solution::leftovers_better'). * * With the algorithms selected as for any instance, a bin with many items * found quickly is re-optimized quickly too. @@ -48,7 +49,18 @@ Solution reoptimize_bin( * bin in the objective, so the algorithms hardly try to keep the free space * of the other bins in a single region. */ -Solution reoptimize_non_last_bins( +Solution reoptimize_non_last_bins_leftovers( + const Solution& solution, + const OptimizeParameters& parameters = {}); + +/** + * Re-optimize the leftovers of each bin of a solution (see + * 'reoptimize_non_last_bins_leftovers'). + * + * It is used by the leftover post-process for the objectives other than + * 'BinPackingWithLeftovers', for which the last bin plays no special role. + */ +Solution reoptimize_all_bins_leftovers( const Solution& solution, const OptimizeParameters& parameters = {}); diff --git a/include/packingsolver/boxstacks/solution.hpp b/include/packingsolver/boxstacks/solution.hpp index 2d51e2242..55f12c2b1 100644 --- a/include/packingsolver/boxstacks/solution.hpp +++ b/include/packingsolver/boxstacks/solution.hpp @@ -306,6 +306,18 @@ class Solution */ inline Profit net_cost() const { return net_cost_; } + /** + * Return 'true' iff the leftovers of the solution are better than the + * ones of another solution of the same instance: the total leftover value + * of all the bins of the instance is greater (see + * 'total_leftover_value'), or it is the same and the total staircase + * leftover value is greater. + * + * It doesn't depend on the objective of the instance; it is used by the + * leftover post-process. + */ + bool leftovers_better(const Solution& solution) const; + /** * Get the sum of the staircase leftover values of all the bins of the * instance: the staircase leftover values of the bins of the solution, diff --git a/include/packingsolver/irregular/algorithm_formatter.hpp b/include/packingsolver/irregular/algorithm_formatter.hpp index 02ac546b7..6a67c8b69 100644 --- a/include/packingsolver/irregular/algorithm_formatter.hpp +++ b/include/packingsolver/irregular/algorithm_formatter.hpp @@ -45,6 +45,18 @@ class AlgorithmFormatter const Solution& solution, const std::string& s); + /** + * Update the solution with a solution from the leftover post-process. + * + * The solution is added as with 'update_solution' if it is better for + * the objective. If it is equivalent to the best solution for the + * objective but its leftovers are better (see + * 'Solution::leftovers_better'), it replaces the best solution. + */ + void update_solution_leftovers( + const Solution& solution, + const std::string& s); + /** * Mark the instance as proven infeasible; the infeasible subset is all * the items. diff --git a/include/packingsolver/irregular/optimize.hpp b/include/packingsolver/irregular/optimize.hpp index 0b97867e2..17165986a 100644 --- a/include/packingsolver/irregular/optimize.hpp +++ b/include/packingsolver/irregular/optimize.hpp @@ -390,8 +390,9 @@ struct OptimizeParameters: packingsolver::Parameters std::vector tree_search_guides; /** - * Re-optimize the bins of the best solution at the end, for the - * BinPackingWithLeftovers objective in a not-anytime mode. + * Re-optimize the bins of the best solution at the end, to improve their + * leftovers, in a not-anytime mode, for the objectives of + * 'leftover_post_process_objective'. */ bool post_process = true; diff --git a/include/packingsolver/irregular/post_process.hpp b/include/packingsolver/irregular/post_process.hpp index 91631f215..582883f94 100644 --- a/include/packingsolver/irregular/post_process.hpp +++ b/include/packingsolver/irregular/post_process.hpp @@ -56,24 +56,25 @@ AnchorOutput anchor( const AnchorParameters& parameters = {}); /** - * Re-optimize the bin at 'bin_pos' of a solution, as its own single-bin - * BinPackingWithLeftovers instance, with 'optimize' and 'parameters' (its - * output and its post-process being disabled). + * Re-optimize the leftovers of the bin at 'bin_pos' of a solution, by solving + * that bin as its own single-bin BinPackingWithLeftovers instance, with + * 'optimize' and 'parameters' (its output and its post-process being + * disabled). * * Return the solution with that bin replaced by the re-optimized one, or the - * solution itself if the re-optimization didn't pack all the items of the - * bin. + * solution itself if the bin is empty or if the re-optimization didn't pack + * all the items of the bin. */ -Solution reoptimize_bin( +Solution reoptimize_bin_leftovers( const Solution& solution, BinPos bin_pos, const OptimizeParameters& parameters = {}); /** - * Re-optimize each bin of a solution except the last one (see - * 'reoptimize_bin'), and keep a re-optimized bin if its leftover value is - * greater, or if its leftover value is the same and its staircase leftover - * value is greater. + * Re-optimize the leftovers of each bin of a solution except the last one + * (see 'reoptimize_bin_leftovers'), and keep a re-optimized bin if its + * leftover value is greater, or if its leftover value is the same and its + * staircase leftover value is greater (see 'Solution::leftovers_better'). * * With the algorithms selected as for any instance, a bin with many items * found quickly, for example by the tree search periodic packing, is @@ -83,7 +84,18 @@ Solution reoptimize_bin( * bin in the objective, so the algorithms hardly try to keep the free space * of the other bins in a single region. */ -Solution reoptimize_non_last_bins( +Solution reoptimize_non_last_bins_leftovers( + const Solution& solution, + const OptimizeParameters& parameters = {}); + +/** + * Re-optimize the leftovers of each bin of a solution (see + * 'reoptimize_non_last_bins_leftovers'). + * + * It is used by the leftover post-process for the objectives other than + * 'BinPackingWithLeftovers', for which the last bin plays no special role. + */ +Solution reoptimize_all_bins_leftovers( const Solution& solution, const OptimizeParameters& parameters = {}); diff --git a/include/packingsolver/irregular/solution.hpp b/include/packingsolver/irregular/solution.hpp index e8cb34dc5..0520fdf66 100644 --- a/include/packingsolver/irregular/solution.hpp +++ b/include/packingsolver/irregular/solution.hpp @@ -216,6 +216,18 @@ class Solution */ inline Profit net_cost() const { return net_cost_; } + /** + * Return 'true' iff the leftovers of the solution are better than the + * ones of another solution of the same instance: the total leftover value + * of all the bins of the instance is greater (see + * 'total_leftover_value'), or it is the same and the total staircase + * leftover value is greater. + * + * It doesn't depend on the objective of the instance; it is used by the + * leftover post-process. + */ + bool leftovers_better(const Solution& solution) const; + /** * Get the sum of the staircase leftover values of all the bins of the * instance: the staircase leftover values of the bins of the solution, diff --git a/include/packingsolver/rectangle/algorithm_formatter.hpp b/include/packingsolver/rectangle/algorithm_formatter.hpp index 5c008ef8b..99e9d2a18 100644 --- a/include/packingsolver/rectangle/algorithm_formatter.hpp +++ b/include/packingsolver/rectangle/algorithm_formatter.hpp @@ -45,6 +45,18 @@ class AlgorithmFormatter const Solution& solution, const std::string& s); + /** + * Update the solution with a solution from the leftover post-process. + * + * The solution is added as with 'update_solution' if it is better for + * the objective. If it is equivalent to the best solution for the + * objective but its leftovers are better (see + * 'Solution::leftovers_better'), it replaces the best solution. + */ + void update_solution_leftovers( + const Solution& solution, + const std::string& s); + /** Mark the instance as proven infeasible. */ void update_is_proven_infeasible(); diff --git a/include/packingsolver/rectangle/optimize.hpp b/include/packingsolver/rectangle/optimize.hpp index 2e5523892..3d2125b04 100644 --- a/include/packingsolver/rectangle/optimize.hpp +++ b/include/packingsolver/rectangle/optimize.hpp @@ -249,8 +249,9 @@ struct OptimizeParameters: packingsolver::Parameters std::vector tree_search_guides; /** - * Re-optimize the bins of the best solution at the end, for the - * BinPackingWithLeftovers objective in a not-anytime mode. + * Re-optimize the bins of the best solution at the end, to improve their + * leftovers, in a not-anytime mode, for the objectives of + * 'leftover_post_process_objective'. */ bool post_process = true; diff --git a/include/packingsolver/rectangle/post_process.hpp b/include/packingsolver/rectangle/post_process.hpp index 55cecc032..95bec246a 100644 --- a/include/packingsolver/rectangle/post_process.hpp +++ b/include/packingsolver/rectangle/post_process.hpp @@ -22,24 +22,25 @@ struct GroupIdenticalBinsOutput: packingsolver::Output GroupIdenticalBinsOutput group_identical_bins(const Solution& solution); /** - * Re-optimize the bin at 'bin_pos' of a solution, as its own single-bin - * BinPackingWithLeftovers instance, with 'optimize' and 'parameters' (its - * output and its post-process being disabled). + * Re-optimize the leftovers of the bin at 'bin_pos' of a solution, by solving + * that bin as its own single-bin BinPackingWithLeftovers instance, with + * 'optimize' and 'parameters' (its output and its post-process being + * disabled). * * Return the solution with that bin replaced by the re-optimized one, or the - * solution itself if the re-optimization didn't pack all the items of the - * bin. + * solution itself if the bin is empty or if the re-optimization didn't pack + * all the items of the bin. */ -Solution reoptimize_bin( +Solution reoptimize_bin_leftovers( const Solution& solution, BinPos bin_pos, const OptimizeParameters& parameters = {}); /** - * Re-optimize each bin of a solution except the last one (see - * 'reoptimize_bin'), and keep a re-optimized bin if its leftover value is - * greater, or if its leftover value is the same and its staircase leftover - * value is greater. + * Re-optimize the leftovers of each bin of a solution except the last one + * (see 'reoptimize_bin_leftovers'), and keep a re-optimized bin if its + * leftover value is greater, or if its leftover value is the same and its + * staircase leftover value is greater (see 'Solution::leftovers_better'). * * With the algorithms selected as for any instance, a bin with many items * found quickly is re-optimized quickly too. @@ -48,7 +49,18 @@ Solution reoptimize_bin( * bin in the objective, so the algorithms hardly try to keep the free space * of the other bins in a single region. */ -Solution reoptimize_non_last_bins( +Solution reoptimize_non_last_bins_leftovers( + const Solution& solution, + const OptimizeParameters& parameters = {}); + +/** + * Re-optimize the leftovers of each bin of a solution (see + * 'reoptimize_non_last_bins_leftovers'). + * + * It is used by the leftover post-process for the objectives other than + * 'BinPackingWithLeftovers', for which the last bin plays no special role. + */ +Solution reoptimize_all_bins_leftovers( const Solution& solution, const OptimizeParameters& parameters = {}); diff --git a/include/packingsolver/rectangle/solution.hpp b/include/packingsolver/rectangle/solution.hpp index 7ca93e81b..c0e1b9fc4 100644 --- a/include/packingsolver/rectangle/solution.hpp +++ b/include/packingsolver/rectangle/solution.hpp @@ -221,6 +221,18 @@ class Solution */ inline Profit net_cost() const { return net_cost_; } + /** + * Return 'true' iff the leftovers of the solution are better than the + * ones of another solution of the same instance: the total leftover value + * of all the bins of the instance is greater (see + * 'total_leftover_value'), or it is the same and the total staircase + * leftover value is greater. + * + * It doesn't depend on the objective of the instance; it is used by the + * leftover post-process. + */ + bool leftovers_better(const Solution& solution) const; + /** * Get the sum of the staircase leftover values of all the bins of the * instance: the staircase leftover values of the bins of the solution, diff --git a/src/box/algorithm_formatter.cpp b/src/box/algorithm_formatter.cpp index 0998723f5..a2302a9d9 100644 --- a/src/box/algorithm_formatter.cpp +++ b/src/box/algorithm_formatter.cpp @@ -242,6 +242,32 @@ void AlgorithmFormatter::update_solution( mutex_.unlock(); } +void AlgorithmFormatter::update_solution_leftovers( + const Solution& solution, + const std::string& s) +{ + mutex_.lock(); + output_.time = parameters_.timer.elapsed_time(); + const Solution& solution_best = output_.solution_pool.best(); + bool updated = false; + if (solution_best < solution) { + updated = (output_.solution_pool.add(solution, s) == 1); + } else if (!(solution < solution_best) + && solution.leftovers_better(solution_best)) { + updated = output_.solution_pool.replace_best(solution, s); + } + if (updated) { + print(s); + if (parameters_.write_json_output) + output_.json["IntermediaryOutputs"].push_back(output_.to_json()); + parameters_.new_solution_callback(output_); + + if (output_.is_proven_optimal()) + end_ = true; + } + mutex_.unlock(); +} + void AlgorithmFormatter::update_is_proven_infeasible() { mutex_.lock(); diff --git a/src/box/optimize.cpp b/src/box/optimize.cpp index 099377b00..4c573ac50 100644 --- a/src/box/optimize.cpp +++ b/src/box/optimize.cpp @@ -273,6 +273,8 @@ void optimize_sequential_single_knapsack( = [&algorithm_formatter, ¶meters, &queue_size, &queue_size_ms](const Instance& kp_instance) { OptimizeParameters kp_parameters; + // The leftovers of the sub-instance don't matter. + kp_parameters.post_process = false; kp_parameters.verbosity_level = 0; kp_parameters.timer = parameters.timer; kp_parameters.timer.add_end_boolean(&algorithm_formatter.end_boolean()); @@ -337,6 +339,8 @@ void optimize_sequential_value_correction( = [&algorithm_formatter, ¶meters](const Instance& kp_instance) { OptimizeParameters kp_parameters; + // The leftovers of the sub-instance don't matter. + kp_parameters.post_process = false; kp_parameters.verbosity_level = 0; kp_parameters.timer = parameters.timer; kp_parameters.timer.add_end_boolean(&algorithm_formatter.end_boolean()); @@ -392,6 +396,8 @@ void optimize_dichotomic_search( = [&algorithm_formatter, ¶meters, &queue_size](const Instance& bpp_instance) { OptimizeParameters bpp_parameters; + // The leftovers of the sub-instance don't matter. + bpp_parameters.post_process = false; bpp_parameters.verbosity_level = 0; bpp_parameters.timer = parameters.timer; bpp_parameters.timer.add_end_boolean(&algorithm_formatter.end_boolean()); @@ -450,6 +456,8 @@ void optimize_column_generation( = [&algorithm_formatter, ¶meters](const Instance& kp_instance, PricingType) { OptimizeParameters kp_parameters; + // The leftovers of the sub-instance don't matter. + kp_parameters.post_process = false; kp_parameters.verbosity_level = 0; kp_parameters.timer = parameters.timer; kp_parameters.timer.add_end_boolean(&algorithm_formatter.end_boolean()); @@ -507,14 +515,15 @@ void optimize_item_types_fit( } /** - * Post-processes for BinPackingWithLeftovers in a not-anytime mode. + * Leftover post-processes in a not-anytime mode. * - * Last bin post-process: re-optimize just the last bin of 'solution_best' - * with the tree search (see 'reoptimize_bin'), in case it packs it into less - * leftover space, whichever algorithm found the solution. It is only applied - * if the last bin contains fewer items than - * 'post_process_last_bin_maximum_number_of_items': the tree search may take - * much longer than the algorithm which found the solution. + * Last bin post-process, for 'BinPackingWithLeftovers' only: re-optimize + * just the last bin of 'solution_best' with the tree search (see + * 'reoptimize_bin_leftovers'), in case it packs it into less leftover space, + * whichever algorithm found the solution. It is only applied if the last bin + * contains fewer items than 'post_process_last_bin_maximum_number_of_items': + * the tree search may take much longer than the algorithm which found the + * solution. * * It is applied to the best solution of each algorithm, not only to the * best solution overall: the re-optimized last bin of a solution which isn't @@ -524,30 +533,57 @@ void optimize_item_types_fit( * after the ones of the last bin in the objective, so the algorithms hardly * try to keep the free space of the other bins in a single region. * Re-optimize each other bin of the best solution, with the tree search with - * maximal spaces (see 'reoptimize_non_last_bins'). It is only + * maximal spaces (see 'reoptimize_non_last_bins_leftovers'). It is only * applied if 'leftover_all_bins' is enabled, since it can only improve the * leftover values of the other bins. + * + * For the other objectives of 'leftover_post_process_objective', no bin + * plays the role of the last bin: only the other bins post-process is + * applied, and it re-optimizes all the bins, whatever 'leftover_all_bins' + * (see 'reoptimize_all_bins_leftovers'). + * + * For the objectives other than 'BinPackingWithLeftovers' and + * 'VariableSizedBinPackingWithLeftovers', the post-processed solution has + * the same value as the best solution: it replaces it if its leftovers are + * better (see 'AlgorithmFormatter::update_solution_leftovers'). */ bool post_process_applies( const Instance& instance, const OptimizeParameters& parameters) { - return instance.objective() == Objective::BinPackingWithLeftovers + return leftover_post_process_objective(instance.objective()) && parameters.optimization_mode != OptimizationMode::Anytime && parameters.optimization_mode != OptimizationMode::Exact && parameters.post_process; } +/** Report a solution of the leftover post-process. */ +void update_post_process_solution( + const Instance& instance, + const Solution& solution, + AlgorithmFormatter& algorithm_formatter) +{ + if (instance.objective() == Objective::BinPackingWithLeftovers) { + algorithm_formatter.update_solution(solution, "post-process"); + } else { + algorithm_formatter.update_solution_leftovers(solution, "post-process"); + } +} + void optimize_last_bin_post_process( const Instance& instance, const OptimizeParameters& parameters, const Solution& solution_best, AlgorithmFormatter& algorithm_formatter) { - if (post_process_applies(instance, parameters) - && solution_best.number_of_bins() > 0 - && solution_best.bin(solution_best.number_of_different_bins() - 1).copies == 1 - && (ItemPos)solution_best.bin(solution_best.number_of_different_bins() - 1).items.size() + if (!post_process_applies(instance, parameters) + || instance.objective() != Objective::BinPackingWithLeftovers + || solution_best.number_of_bins() == 0) { + return; + } + BinPos bin_pos = solution_best.number_of_different_bins() - 1; + if (solution_best.bin(bin_pos).copies == 1 + && (ItemPos)solution_best.bin(bin_pos).items.size() < parameters.post_process_last_bin_maximum_number_of_items) { OptimizeParameters last_bin_parameters; last_bin_parameters.timer = parameters.timer; @@ -555,11 +591,11 @@ void optimize_last_bin_post_process( last_bin_parameters.not_anytime_tree_search_queue_size = parameters.not_anytime_tree_search_queue_size; last_bin_parameters.linear_programming_solver_name = parameters.linear_programming_solver_name; last_bin_parameters.use_tree_search = true; - Solution solution = reoptimize_bin( + Solution solution = reoptimize_bin_leftovers( solution_best, - solution_best.number_of_different_bins() - 1, + bin_pos, last_bin_parameters); - algorithm_formatter.update_solution(solution, "post-process"); + update_post_process_solution(instance, solution, algorithm_formatter); } } @@ -569,20 +605,28 @@ void optimize_other_bins_post_process( const Solution& solution_best, AlgorithmFormatter& algorithm_formatter) { - if (!post_process_applies(instance, parameters) - || !instance.parameters().leftover_all_bins) { + if (!post_process_applies(instance, parameters)) + return; + bool bin_packing_with_leftovers = (instance.objective() == Objective::BinPackingWithLeftovers); + if (bin_packing_with_leftovers + && !instance.parameters().leftover_all_bins) { return; } OptimizeParameters post_process_parameters = parameters; - post_process_parameters.timer.add_end_boolean(&algorithm_formatter.end_boolean()); + // For the objectives which don't value the leftovers, the algorithm + // formatter ends as soon as the best solution is proven optimal, which + // doesn't mean that its leftovers can't be improved: only the time limit + // stops the post-process. + if (bin_packing_with_leftovers) + post_process_parameters.timer.add_end_boolean(&algorithm_formatter.end_boolean()); // A bin may contain many items: the tree search would take much longer // than the algorithm which found it. post_process_parameters.use_tree_search = false; post_process_parameters.use_tree_search_maximal_spaces = true; - Solution solution = reoptimize_non_last_bins( - solution_best, - post_process_parameters); - algorithm_formatter.update_solution(solution, "post-process"); + Solution solution = (bin_packing_with_leftovers)? + reoptimize_non_last_bins_leftovers(solution_best, post_process_parameters): + reoptimize_all_bins_leftovers(solution_best, post_process_parameters); + update_post_process_solution(instance, solution, algorithm_formatter); } } @@ -937,7 +981,9 @@ packingsolver::box::Output packingsolver::box::optimize( // 'NotAnytimeDeterministic' mode, these local formatters forward every // solution and bound to the shared 'algorithm_formatter' as soon as they // get it, instead of only at the end. - bool local = deterministic || post_process_applies(instance, parameters); + bool local = deterministic + || (post_process_applies(instance, parameters) + && instance.objective() == Objective::BinPackingWithLeftovers); if (local && !deterministic) { local_parameters.new_solution_callback = [&algorithm_formatter](const Output& local_output) { @@ -1072,11 +1118,12 @@ packingsolver::box::Output packingsolver::box::optimize( } } - // Last bin post-process, on the best solution of each algorithm, and on - // the best solution overall if it doesn't come from one of them (e.g. - // found before running them). The best solution is copied since the - // post-process may replace it. - if (post_process_applies(instance, parameters)) { + // Last bin post-process, for 'BinPackingWithLeftovers', on the best + // solution of each algorithm, and on the best solution overall if it + // doesn't come from one of them (e.g. found before running them). The + // best solution is copied since the post-process may replace it. + if (post_process_applies(instance, parameters) + && instance.objective() == Objective::BinPackingWithLeftovers) { Solution solution_best = output.solution_pool.best(); bool solution_best_found_by_an_algorithm = false; for (const auto& local_output: local_outputs) { diff --git a/src/box/post_process.cpp b/src/box/post_process.cpp index 5135d8bdb..ab7b0593c 100644 --- a/src/box/post_process.cpp +++ b/src/box/post_process.cpp @@ -14,7 +14,7 @@ GroupIdenticalBinsOutput packingsolver::box::group_identical_bins( return output; } -Solution packingsolver::box::reoptimize_bin( +Solution packingsolver::box::reoptimize_bin_leftovers( const Solution& solution, BinPos bin_pos, const OptimizeParameters& parameters) @@ -28,6 +28,10 @@ Solution packingsolver::box::reoptimize_bin( // Add bin types. const SolutionBin& bin = solution.bin(bin_pos); + // An empty bin has nothing to re-optimize: its sub-instance would have no + // items, and its solution no bin. + if (bin.items.empty()) + return solution; BinTypeId bin_type_id = bin_instance_builder.add_bin_type_from_original_instance(bin.bin_type_id); bin_instance_builder.set_bin_type_copies(bin_type_id, 1); bin_instance_builder.set_bin_type_copies_min(bin_type_id, 0); @@ -87,24 +91,56 @@ Solution packingsolver::box::reoptimize_bin( return new_solution; } -Solution packingsolver::box::reoptimize_non_last_bins( +namespace +{ + +/** + * Re-optimize each of the first 'number_of_bins' bins of a solution, and keep + * a re-optimized bin if its leftovers are better. + */ +Solution reoptimize_bins_leftovers( const Solution& solution, + BinPos number_of_bins, const OptimizeParameters& parameters) { Solution new_solution = solution; for (BinPos bin_pos = 0; - bin_pos < solution.number_of_different_bins() - 1; + bin_pos < number_of_bins; ++bin_pos) { if (parameters.timer.needs_to_end()) break; - Solution bin_solution = reoptimize_bin( + Solution bin_solution = box::reoptimize_bin_leftovers( new_solution, bin_pos, parameters); // Only the bin at 'bin_pos' differs: the comparison is on its leftover // value, then on its staircase leftover value. - if (new_solution < bin_solution) + if (!(bin_solution < new_solution) + && bin_solution.leftovers_better(new_solution)) { new_solution = bin_solution; + } } return new_solution; } + +} + +Solution packingsolver::box::reoptimize_non_last_bins_leftovers( + const Solution& solution, + const OptimizeParameters& parameters) +{ + return reoptimize_bins_leftovers( + solution, + solution.number_of_different_bins() - 1, + parameters); +} + +Solution packingsolver::box::reoptimize_all_bins_leftovers( + const Solution& solution, + const OptimizeParameters& parameters) +{ + return reoptimize_bins_leftovers( + solution, + solution.number_of_different_bins(), + parameters); +} diff --git a/src/box/solution.cpp b/src/box/solution.cpp index e9ae00ca4..a87b23371 100644 --- a/src/box/solution.cpp +++ b/src/box/solution.cpp @@ -249,6 +249,18 @@ double Solution::total_leftover_value() const return value - leftover_value_loss_; } +bool Solution::leftovers_better(const Solution& solution) const +{ + // The total leftover values of all the bins of the instance are compared + // through the value lost in the used bins. + LeftoverValueType leftover_value_type = instance().parameters().leftover_value_type; + if (strictly_greater_leftover_value(leftover_value_type, solution.leftover_value_loss_, leftover_value_loss_)) + return true; + if (strictly_greater_leftover_value(leftover_value_type, leftover_value_loss_, solution.leftover_value_loss_)) + return false; + return staircase_dominated_volume_ < solution.staircase_dominated_volume_; +} + bool Solution::operator<(const Solution& solution) const { // Check feasibility. diff --git a/src/boxstacks/algorithm_formatter.cpp b/src/boxstacks/algorithm_formatter.cpp index 543c1ac70..2b44d0c19 100644 --- a/src/boxstacks/algorithm_formatter.cpp +++ b/src/boxstacks/algorithm_formatter.cpp @@ -224,6 +224,32 @@ void AlgorithmFormatter::update_solution( mutex_.unlock(); } +void AlgorithmFormatter::update_solution_leftovers( + const Solution& solution, + const std::string& s) +{ + mutex_.lock(); + output_.time = parameters_.timer.elapsed_time(); + const Solution& solution_best = output_.solution_pool.best(); + bool updated = false; + if (solution_best < solution) { + updated = (output_.solution_pool.add(solution, s) == 1); + } else if (!(solution < solution_best) + && solution.leftovers_better(solution_best)) { + updated = output_.solution_pool.replace_best(solution, s); + } + if (updated) { + print(s); + if (parameters_.write_json_output) + output_.json["IntermediaryOutputs"].push_back(output_.to_json()); + parameters_.new_solution_callback(output_); + + if (output_.is_proven_optimal()) + end_ = true; + } + mutex_.unlock(); +} + void AlgorithmFormatter::update_is_proven_infeasible() { mutex_.lock(); diff --git a/src/boxstacks/optimize.cpp b/src/boxstacks/optimize.cpp index 127f4b7ec..a48ea91bc 100644 --- a/src/boxstacks/optimize.cpp +++ b/src/boxstacks/optimize.cpp @@ -299,6 +299,8 @@ void optimize_sequential_single_knapsack( const Instance& kp_instance) { OptimizeParameters kp_parameters; + // The leftovers of the sub-instance don't matter. + kp_parameters.post_process = false; kp_parameters.verbosity_level = 0; kp_parameters.timer = parameters.timer; kp_parameters.timer.add_end_boolean(&algorithm_formatter.end_boolean()); @@ -375,6 +377,8 @@ void optimize_sequential_value_correction( = [&algorithm_formatter, ¶meters, &output](const Instance& kp_instance) { OptimizeParameters kp_parameters; + // The leftovers of the sub-instance don't matter. + kp_parameters.post_process = false; kp_parameters.verbosity_level = 0; kp_parameters.timer = parameters.timer; kp_parameters.timer.add_end_boolean(&algorithm_formatter.end_boolean()); @@ -451,31 +455,55 @@ void optimize_item_types_fit( } /** - * Post-processes for BinPackingWithLeftovers in a not-anytime mode. + * Leftover post-processes in a not-anytime mode. * - * Last bin post-process: re-optimize just the last bin of 'solution_best' - * (see 'reoptimize_bin'), in case it packs it into less leftover space. It is - * only applied if the last bin contains fewer items than + * Last bin post-process, for 'BinPackingWithLeftovers' only: re-optimize + * just the last bin of 'solution_best' (see 'reoptimize_bin_leftovers'), in + * case it packs it into less leftover space. It is only applied if the last + * bin contains fewer items than * 'post_process_last_bin_maximum_number_of_items'. * * Other bins post-process: the leftover values of the other bins only come * after the ones of the last bin in the objective, so the algorithms hardly * try to keep the free space of the other bins in a single region. * Re-optimize each other bin of the best solution, with the algorithms - * selected as for any instance (see 'reoptimize_non_last_bins'). It is only - * applied if 'leftover_all_bins' is enabled, since it can only improve the - * leftover values of the other bins. + * selected as for any instance (see 'reoptimize_non_last_bins_leftovers'). + * It is only applied if 'leftover_all_bins' is enabled, since it can only + * improve the leftover values of the other bins. + * + * For the other objectives of 'leftover_post_process_objective', no bin + * plays the role of the last bin: only the other bins post-process is + * applied, and it re-optimizes all the bins, whatever 'leftover_all_bins' + * (see 'reoptimize_all_bins_leftovers'). + * + * For the objectives other than 'BinPackingWithLeftovers' and + * 'VariableSizedBinPackingWithLeftovers', the post-processed solution has + * the same value as the best solution: it replaces it if its leftovers are + * better (see 'AlgorithmFormatter::update_solution_leftovers'). */ bool post_process_applies( const Instance& instance, const OptimizeParameters& parameters) { - return instance.objective() == Objective::BinPackingWithLeftovers + return leftover_post_process_objective(instance.objective()) && parameters.optimization_mode != OptimizationMode::Anytime && parameters.optimization_mode != OptimizationMode::Exact && parameters.post_process; } +/** Report a solution of the leftover post-process. */ +void update_post_process_solution( + const Instance& instance, + const Solution& solution, + AlgorithmFormatter& algorithm_formatter) +{ + if (instance.objective() == Objective::BinPackingWithLeftovers) { + algorithm_formatter.update_solution(solution, "post-process"); + } else { + algorithm_formatter.update_solution_leftovers(solution, "post-process"); + } +} + void optimize_last_bin_post_process( const Instance& instance, const OptimizeParameters& parameters, @@ -483,15 +511,17 @@ void optimize_last_bin_post_process( AlgorithmFormatter& algorithm_formatter) { if (!post_process_applies(instance, parameters) + || instance.objective() != Objective::BinPackingWithLeftovers || solution_best.number_of_bins() == 0) { return; } - const SolutionBin& last_bin = solution_best.bin(solution_best.number_of_different_bins() - 1); - ItemPos last_bin_number_of_items = 0; - for (const SolutionStack& solution_stack: last_bin.stacks) - last_bin_number_of_items += solution_stack.items.size(); - if (last_bin.copies != 1 - || last_bin_number_of_items + BinPos bin_pos = solution_best.number_of_different_bins() - 1; + if (solution_best.bin(bin_pos).copies != 1) + return; + ItemPos bin_number_of_items = 0; + for (const SolutionStack& solution_stack: solution_best.bin(bin_pos).stacks) + bin_number_of_items += solution_stack.items.size(); + if (bin_number_of_items >= parameters.post_process_last_bin_maximum_number_of_items) { return; } @@ -501,11 +531,11 @@ void optimize_last_bin_post_process( last_bin_parameters.optimization_mode = parameters.optimization_mode; last_bin_parameters.not_anytime_tree_search_queue_size = parameters.not_anytime_tree_search_queue_size; last_bin_parameters.linear_programming_solver_name = parameters.linear_programming_solver_name; - Solution solution = reoptimize_bin( + Solution solution = reoptimize_bin_leftovers( solution_best, - solution_best.number_of_different_bins() - 1, + bin_pos, last_bin_parameters); - algorithm_formatter.update_solution(solution, "post-process"); + update_post_process_solution(instance, solution, algorithm_formatter); } void optimize_other_bins_post_process( @@ -514,16 +544,24 @@ void optimize_other_bins_post_process( const Solution& solution_best, AlgorithmFormatter& algorithm_formatter) { - if (!post_process_applies(instance, parameters) - || !instance.parameters().leftover_all_bins) { + if (!post_process_applies(instance, parameters)) + return; + bool bin_packing_with_leftovers = (instance.objective() == Objective::BinPackingWithLeftovers); + if (bin_packing_with_leftovers + && !instance.parameters().leftover_all_bins) { return; } OptimizeParameters post_process_parameters = parameters; - post_process_parameters.timer.add_end_boolean(&algorithm_formatter.end_boolean()); - Solution solution = reoptimize_non_last_bins( - solution_best, - post_process_parameters); - algorithm_formatter.update_solution(solution, "post-process"); + // For the objectives which don't value the leftovers, the algorithm + // formatter ends as soon as the best solution is proven optimal, which + // doesn't mean that its leftovers can't be improved: only the time limit + // stops the post-process. + if (bin_packing_with_leftovers) + post_process_parameters.timer.add_end_boolean(&algorithm_formatter.end_boolean()); + Solution solution = (bin_packing_with_leftovers)? + reoptimize_non_last_bins_leftovers(solution_best, post_process_parameters): + reoptimize_all_bins_leftovers(solution_best, post_process_parameters); + update_post_process_solution(instance, solution, algorithm_formatter); } } diff --git a/src/boxstacks/post_process.cpp b/src/boxstacks/post_process.cpp index ede8ac2e2..de990b71d 100644 --- a/src/boxstacks/post_process.cpp +++ b/src/boxstacks/post_process.cpp @@ -14,7 +14,7 @@ GroupIdenticalBinsOutput packingsolver::boxstacks::group_identical_bins( return output; } -Solution packingsolver::boxstacks::reoptimize_bin( +Solution packingsolver::boxstacks::reoptimize_bin_leftovers( const Solution& solution, BinPos bin_pos, const OptimizeParameters& parameters) @@ -28,6 +28,10 @@ Solution packingsolver::boxstacks::reoptimize_bin( // Add bin types. const SolutionBin& bin = solution.bin(bin_pos); + // An empty bin has nothing to re-optimize: its sub-instance would have no + // items, and its solution no bin. + if (bin.stacks.empty()) + return solution; BinTypeId bin_type_id = bin_instance_builder.add_bin_type_from_original_instance(bin.bin_type_id); bin_instance_builder.set_bin_type_copies(bin_type_id, 1); bin_instance_builder.set_bin_type_copies_min(bin_type_id, 0); @@ -88,24 +92,56 @@ Solution packingsolver::boxstacks::reoptimize_bin( return new_solution; } -Solution packingsolver::boxstacks::reoptimize_non_last_bins( +namespace +{ + +/** + * Re-optimize each of the first 'number_of_bins' bins of a solution, and keep + * a re-optimized bin if its leftovers are better. + */ +Solution reoptimize_bins_leftovers( const Solution& solution, + BinPos number_of_bins, const OptimizeParameters& parameters) { Solution new_solution = solution; for (BinPos bin_pos = 0; - bin_pos < solution.number_of_different_bins() - 1; + bin_pos < number_of_bins; ++bin_pos) { if (parameters.timer.needs_to_end()) break; - Solution bin_solution = reoptimize_bin( + Solution bin_solution = boxstacks::reoptimize_bin_leftovers( new_solution, bin_pos, parameters); // Only the bin at 'bin_pos' differs: the comparison is on its leftover // value, then on its staircase leftover value. - if (new_solution < bin_solution) + if (!(bin_solution < new_solution) + && bin_solution.leftovers_better(new_solution)) { new_solution = bin_solution; + } } return new_solution; } + +} + +Solution packingsolver::boxstacks::reoptimize_non_last_bins_leftovers( + const Solution& solution, + const OptimizeParameters& parameters) +{ + return reoptimize_bins_leftovers( + solution, + solution.number_of_different_bins() - 1, + parameters); +} + +Solution packingsolver::boxstacks::reoptimize_all_bins_leftovers( + const Solution& solution, + const OptimizeParameters& parameters) +{ + return reoptimize_bins_leftovers( + solution, + solution.number_of_different_bins(), + parameters); +} diff --git a/src/boxstacks/solution.cpp b/src/boxstacks/solution.cpp index 7f7d28863..ee50bb143 100644 --- a/src/boxstacks/solution.cpp +++ b/src/boxstacks/solution.cpp @@ -349,6 +349,18 @@ double Solution::total_leftover_value() const return value - leftover_value_loss_; } +bool Solution::leftovers_better(const Solution& solution) const +{ + // The total leftover values of all the bins of the instance are compared + // through the value lost in the used bins. + LeftoverValueType leftover_value_type = instance().parameters().leftover_value_type; + if (strictly_greater_leftover_value(leftover_value_type, solution.leftover_value_loss_, leftover_value_loss_)) + return true; + if (strictly_greater_leftover_value(leftover_value_type, leftover_value_loss_, solution.leftover_value_loss_)) + return false; + return staircase_dominated_volume_ < solution.staircase_dominated_volume_; +} + bool Solution::operator<(const Solution& solution) const { // Check feasibility. diff --git a/src/irregular/algorithm_formatter.cpp b/src/irregular/algorithm_formatter.cpp index d84df8f14..a3ae6eaa2 100644 --- a/src/irregular/algorithm_formatter.cpp +++ b/src/irregular/algorithm_formatter.cpp @@ -248,6 +248,32 @@ void AlgorithmFormatter::update_solution( mutex_.unlock(); } +void AlgorithmFormatter::update_solution_leftovers( + const Solution& solution, + const std::string& s) +{ + mutex_.lock(); + output_.time = parameters_.timer.elapsed_time(); + const Solution& solution_best = output_.solution_pool.best(); + bool updated = false; + if (solution_best < solution) { + updated = (output_.solution_pool.add(solution, s) == 1); + } else if (!(solution < solution_best) + && solution.leftovers_better(solution_best)) { + updated = output_.solution_pool.replace_best(solution, s); + } + if (updated) { + print(s); + if (parameters_.write_json_output) + output_.json["IntermediaryOutputs"].push_back(output_.to_json()); + parameters_.new_solution_callback(output_); + + if (output_.is_proven_optimal()) + end_ = true; + } + mutex_.unlock(); +} + void AlgorithmFormatter::update_is_proven_infeasible() { std::vector> infeasible_subset; diff --git a/src/irregular/benders_decomposition.cpp b/src/irregular/benders_decomposition.cpp index 530a379b9..d9e4721ab 100644 --- a/src/irregular/benders_decomposition.cpp +++ b/src/irregular/benders_decomposition.cpp @@ -176,6 +176,7 @@ const TreeSearchRelativePositionsResult& tree_search_relative_positions_result( selected_items, sub_to_orig); OptimizeParameters sub_parameters; + sub_parameters.post_process = false; sub_parameters.verbosity_level = 0; sub_parameters.timer = parameters.timer; if (parameters.optimization_mode == OptimizationMode::Exact) { @@ -261,6 +262,7 @@ SelectionFeasibility selection_feasibility( selected_items, sub_to_orig); OptimizeParameters sub_parameters; + sub_parameters.post_process = false; sub_parameters.verbosity_level = 0; sub_parameters.timer = parameters.timer; sub_parameters.optimization_mode = subproblem_optimization_mode(parameters); diff --git a/src/irregular/large_item_first.cpp b/src/irregular/large_item_first.cpp index 70b734469..56d3310f6 100644 --- a/src/irregular/large_item_first.cpp +++ b/src/irregular/large_item_first.cpp @@ -75,7 +75,7 @@ LargeItemFirstOutput packingsolver::irregular::large_item_first( } // Build sub-parameters to pass to each sub-problem. - auto make_sub_parameters = [¶meters, &algorithm_formatter]() { + auto make_sub_parameters = [&instance, ¶meters, &algorithm_formatter]() { OptimizeParameters sub_parameters; sub_parameters.seed = parameters.seed; sub_parameters.verbosity_level = 0; @@ -88,6 +88,11 @@ LargeItemFirstOutput packingsolver::irregular::large_item_first( = parameters.not_anytime_tree_search_queue_size; sub_parameters.linear_programming_solver_name = parameters.linear_programming_solver_name; sub_parameters.local_search_weighting_scheme = parameters.local_search_weighting_scheme; + // Only 'BinPackingWithLeftovers' post-processes the solutions of the + // phases; for the other objectives, the leftover post-process is + // applied to the solution of the whole instance. + sub_parameters.post_process = parameters.post_process + && instance.objective() == Objective::BinPackingWithLeftovers; sub_parameters.use_tree_search = parameters.use_tree_search; sub_parameters.use_local_search_linear_programming = parameters.use_local_search_linear_programming; sub_parameters.use_local_search_nonlinear_programming = parameters.use_local_search_nonlinear_programming; diff --git a/src/irregular/optimize.cpp b/src/irregular/optimize.cpp index 43072df2c..6fd6cee54 100644 --- a/src/irregular/optimize.cpp +++ b/src/irregular/optimize.cpp @@ -354,11 +354,12 @@ void optimize_tree_search( /** - * Post-processes for BinPackingWithLeftovers in a not-anytime mode. + * Leftover post-processes in a not-anytime mode. * - * Last bin post-process: re-optimize just the last bin of 'solution_best' - * with the tree search with guides {2, 3} (see 'reoptimize_bin'), in case they - * pack it into less leftover space: the used area of the last bin doesn't + * Last bin post-process, for 'BinPackingWithLeftovers' only: re-optimize + * just the last bin of 'solution_best' with the tree search with guides + * {2, 3} (see 'reoptimize_bin_leftovers'), in case they pack it into less + * leftover space: the used area of the last bin doesn't * have to span the whole width or the whole height of the bin, whichever * algorithm found the solution. It is only applied if the last bin contains * fewer items than 'post_process_last_bin_maximum_number_of_items': the tree @@ -372,30 +373,57 @@ void optimize_tree_search( * after the ones of the last bin in the objective, so the algorithms hardly * try to keep the free space of the other bins in a single region. * Re-optimize each other bin of the best solution, with the algorithms - * selected as for any instance (see 'reoptimize_non_last_bins'). It is only - * applied if 'leftover_all_bins' is enabled, since it can only improve the - * leftover values of the other bins. + * selected as for any instance (see 'reoptimize_non_last_bins_leftovers'). + * It is only applied if 'leftover_all_bins' is enabled, since it can only + * improve the leftover values of the other bins. + * + * For the other objectives of 'leftover_post_process_objective', no bin + * plays the role of the last bin: only the other bins post-process is + * applied, and it re-optimizes all the bins, whatever 'leftover_all_bins' + * (see 'reoptimize_all_bins_leftovers'). + * + * For the objectives other than 'BinPackingWithLeftovers' and + * 'VariableSizedBinPackingWithLeftovers', the post-processed solution has + * the same value as the best solution: it replaces it if its leftovers are + * better (see 'AlgorithmFormatter::update_solution_leftovers'). */ bool post_process_applies( const Instance& instance, const OptimizeParameters& parameters) { - return instance.objective() == Objective::BinPackingWithLeftovers + return leftover_post_process_objective(instance.objective()) && parameters.optimization_mode != OptimizationMode::Anytime && parameters.optimization_mode != OptimizationMode::Exact && parameters.post_process; } +/** Report a solution of the leftover post-process. */ +void update_post_process_solution( + const Instance& instance, + const Solution& solution, + AlgorithmFormatter& algorithm_formatter) +{ + if (instance.objective() == Objective::BinPackingWithLeftovers) { + algorithm_formatter.update_solution(solution, "post-process"); + } else { + algorithm_formatter.update_solution_leftovers(solution, "post-process"); + } +} + void optimize_last_bin_post_process( const Instance& instance, const OptimizeParameters& parameters, const Solution& solution_best, AlgorithmFormatter& algorithm_formatter) { - if (post_process_applies(instance, parameters) - && solution_best.number_of_bins() > 0 - && solution_best.bin(solution_best.number_of_different_bins() - 1).copies == 1 - && (ItemPos)solution_best.bin(solution_best.number_of_different_bins() - 1).items.size() + if (!post_process_applies(instance, parameters) + || instance.objective() != Objective::BinPackingWithLeftovers + || solution_best.number_of_bins() == 0) { + return; + } + BinPos bin_pos = solution_best.number_of_different_bins() - 1; + if (solution_best.bin(bin_pos).copies == 1 + && (ItemPos)solution_best.bin(bin_pos).items.size() < parameters.post_process_last_bin_maximum_number_of_items) { // Only the tree search is run: the guides {2, 3} are specific to it. OptimizeParameters last_bin_parameters; @@ -408,11 +436,11 @@ void optimize_last_bin_post_process( last_bin_parameters.linear_programming_solver_name = parameters.linear_programming_solver_name; last_bin_parameters.local_search_weighting_scheme = parameters.local_search_weighting_scheme; last_bin_parameters.use_tree_search = true; - Solution solution = reoptimize_bin( + Solution solution = reoptimize_bin_leftovers( solution_best, - solution_best.number_of_different_bins() - 1, + bin_pos, last_bin_parameters); - algorithm_formatter.update_solution(solution, "post-process"); + update_post_process_solution(instance, solution, algorithm_formatter); } } @@ -422,16 +450,24 @@ void optimize_other_bins_post_process( const Solution& solution_best, AlgorithmFormatter& algorithm_formatter) { - if (!post_process_applies(instance, parameters) - || !instance.parameters().leftover_all_bins) { + if (!post_process_applies(instance, parameters)) + return; + bool bin_packing_with_leftovers = (instance.objective() == Objective::BinPackingWithLeftovers); + if (bin_packing_with_leftovers + && !instance.parameters().leftover_all_bins) { return; } OptimizeParameters post_process_parameters = parameters; - post_process_parameters.timer.add_end_boolean(&algorithm_formatter.end_boolean()); - Solution solution = reoptimize_non_last_bins( - solution_best, - post_process_parameters); - algorithm_formatter.update_solution(solution, "post-process"); + // For the objectives which don't value the leftovers, the algorithm + // formatter ends as soon as the best solution is proven optimal, which + // doesn't mean that its leftovers can't be improved: only the time limit + // stops the post-process. + if (bin_packing_with_leftovers) + post_process_parameters.timer.add_end_boolean(&algorithm_formatter.end_boolean()); + Solution solution = (bin_packing_with_leftovers)? + reoptimize_non_last_bins_leftovers(solution_best, post_process_parameters): + reoptimize_all_bins_leftovers(solution_best, post_process_parameters); + update_post_process_solution(instance, solution, algorithm_formatter); } void optimize_local_search( @@ -610,6 +646,8 @@ void optimize_sequential_single_knapsack( maximum_approximation_ratio](const Instance& kp_instance) { OptimizeParameters kp_parameters; + // The leftovers of the sub-instance don't matter. + kp_parameters.post_process = false; kp_parameters.seed = parameters.seed; kp_parameters.verbosity_level = 0; kp_parameters.timer = parameters.timer; @@ -714,6 +752,8 @@ void optimize_sequential_value_correction( = [&algorithm_formatter, ¶meters](const Instance& kp_instance) { OptimizeParameters kp_parameters; + // The leftovers of the sub-instance don't matter. + kp_parameters.post_process = false; kp_parameters.seed = parameters.seed; kp_parameters.verbosity_level = 0; kp_parameters.timer = parameters.timer; @@ -774,6 +814,8 @@ void optimize_dichotomic_search( = [&algorithm_formatter, ¶meters, queue_size, maximum_approximation_ratio](const Instance& bpp_instance) { OptimizeParameters bpp_parameters; + // The leftovers of the sub-instance don't matter. + bpp_parameters.post_process = false; bpp_parameters.seed = parameters.seed; bpp_parameters.verbosity_level = 0; bpp_parameters.timer = parameters.timer; @@ -840,6 +882,8 @@ void optimize_column_generation( = [&algorithm_formatter, ¶meters](const Instance& kp_instance, PricingType pricing_type) { OptimizeParameters kp_parameters; + // The leftovers of the sub-instance don't matter. + kp_parameters.post_process = false; kp_parameters.seed = parameters.seed; kp_parameters.verbosity_level = 0; kp_parameters.timer = parameters.timer; @@ -1601,7 +1645,9 @@ packingsolver::irregular::Output packingsolver::irregular::optimize( // 'NotAnytimeDeterministic' mode, these local formatters forward every // solution and bound to the shared 'algorithm_formatter' as soon as they // get it, instead of only at the end. - bool local = deterministic || post_process_applies(instance, parameters); + bool local = deterministic + || (post_process_applies(instance, parameters) + && instance.objective() == Objective::BinPackingWithLeftovers); if (local && !deterministic) { local_parameters.new_solution_callback = [&algorithm_formatter](const Output& local_output) { @@ -1845,11 +1891,12 @@ packingsolver::irregular::Output packingsolver::irregular::optimize( } } - // Last bin post-process, on the best solution of each algorithm, and on - // the best solution overall if it doesn't come from one of them (e.g. - // found before running them). The best solution is copied since the - // post-process may replace it. - if (post_process_applies(instance, parameters)) { + // Last bin post-process, for 'BinPackingWithLeftovers', on the best + // solution of each algorithm, and on the best solution overall if it + // doesn't come from one of them (e.g. found before running them). The + // best solution is copied since the post-process may replace it. + if (post_process_applies(instance, parameters) + && instance.objective() == Objective::BinPackingWithLeftovers) { Solution solution_best = output.solution_pool.best(); bool solution_best_found_by_an_algorithm = false; for (const auto& local_output: local_outputs) { diff --git a/src/irregular/post_process.cpp b/src/irregular/post_process.cpp index 2d3370ee6..dea731641 100644 --- a/src/irregular/post_process.cpp +++ b/src/irregular/post_process.cpp @@ -123,7 +123,7 @@ AnchorOutput packingsolver::irregular::anchor( return output; } -Solution packingsolver::irregular::reoptimize_bin( +Solution packingsolver::irregular::reoptimize_bin_leftovers( const Solution& solution, BinPos bin_pos, const OptimizeParameters& parameters) @@ -137,6 +137,10 @@ Solution packingsolver::irregular::reoptimize_bin( // Add bin types. const SolutionBin& bin = solution.bin(bin_pos); + // An empty bin has nothing to re-optimize: its sub-instance would have no + // items, and its solution no bin. + if (bin.items.empty()) + return solution; BinTypeId bin_type_id = bin_instance_builder.add_bin_type_from_original_instance(bin.bin_type_id); bin_instance_builder.set_bin_type_copies(bin_type_id, 1); bin_instance_builder.set_bin_type_copies_min(bin_type_id, 0); @@ -196,24 +200,56 @@ Solution packingsolver::irregular::reoptimize_bin( return new_solution; } -Solution packingsolver::irregular::reoptimize_non_last_bins( +namespace +{ + +/** + * Re-optimize each of the first 'number_of_bins' bins of a solution, and keep + * a re-optimized bin if its leftovers are better. + */ +Solution reoptimize_bins_leftovers( const Solution& solution, + BinPos number_of_bins, const OptimizeParameters& parameters) { Solution new_solution = solution; for (BinPos bin_pos = 0; - bin_pos < solution.number_of_different_bins() - 1; + bin_pos < number_of_bins; ++bin_pos) { if (parameters.timer.needs_to_end()) break; - Solution bin_solution = reoptimize_bin( + Solution bin_solution = irregular::reoptimize_bin_leftovers( new_solution, bin_pos, parameters); // Only the bin at 'bin_pos' differs: the comparison is on its leftover // value, then on its staircase leftover value. - if (new_solution < bin_solution) + if (!(bin_solution < new_solution) + && bin_solution.leftovers_better(new_solution)) { new_solution = bin_solution; + } } return new_solution; } + +} + +Solution packingsolver::irregular::reoptimize_non_last_bins_leftovers( + const Solution& solution, + const OptimizeParameters& parameters) +{ + return reoptimize_bins_leftovers( + solution, + solution.number_of_different_bins() - 1, + parameters); +} + +Solution packingsolver::irregular::reoptimize_all_bins_leftovers( + const Solution& solution, + const OptimizeParameters& parameters) +{ + return reoptimize_bins_leftovers( + solution, + solution.number_of_different_bins(), + parameters); +} diff --git a/src/irregular/solution.cpp b/src/irregular/solution.cpp index 4fc454131..66f0fc3e9 100644 --- a/src/irregular/solution.cpp +++ b/src/irregular/solution.cpp @@ -459,6 +459,27 @@ AreaDbl Solution::total_staircase_leftover_value() const return area - staircase_dominated_area_; } +bool Solution::leftovers_better(const Solution& solution) const +{ + // The total leftover values of all the bins of the instance are compared + // through the value lost in the used bins, with the tolerances of + // 'Objective::BinPackingWithLeftovers'. + LeftoverValueType leftover_value_type = instance().parameters().leftover_value_type; + if (shape::strictly_lesser( + leftover_value_length_2d(leftover_value_type, leftover_value_loss_), + leftover_value_length_2d(leftover_value_type, solution.leftover_value_loss_))) { + return true; + } + if (shape::strictly_lesser( + leftover_value_length_2d(leftover_value_type, solution.leftover_value_loss_), + leftover_value_length_2d(leftover_value_type, leftover_value_loss_))) { + return false; + } + return shape::strictly_lesser( + std::sqrt(staircase_dominated_area_), + std::sqrt(solution.staircase_dominated_area_)); +} + bool Solution::operator<(const Solution& solution) const { // Check feasibility. diff --git a/src/rectangle/algorithm_formatter.cpp b/src/rectangle/algorithm_formatter.cpp index 0b1e6c797..7832a24ea 100644 --- a/src/rectangle/algorithm_formatter.cpp +++ b/src/rectangle/algorithm_formatter.cpp @@ -224,6 +224,32 @@ void AlgorithmFormatter::update_solution( mutex_.unlock(); } +void AlgorithmFormatter::update_solution_leftovers( + const Solution& solution, + const std::string& s) +{ + mutex_.lock(); + output_.time = parameters_.timer.elapsed_time(); + const Solution& solution_best = output_.solution_pool.best(); + bool updated = false; + if (solution_best < solution) { + updated = (output_.solution_pool.add(solution, s) == 1); + } else if (!(solution < solution_best) + && solution.leftovers_better(solution_best)) { + updated = output_.solution_pool.replace_best(solution, s); + } + if (updated) { + print(s); + if (parameters_.write_json_output) + output_.json["IntermediaryOutputs"].push_back(output_.to_json()); + parameters_.new_solution_callback(output_); + + if (output_.is_proven_optimal()) + end_ = true; + } + mutex_.unlock(); +} + void AlgorithmFormatter::update_is_proven_infeasible() { mutex_.lock(); diff --git a/src/rectangle/benders_decomposition.cpp b/src/rectangle/benders_decomposition.cpp index c99163e0d..b60ec9751 100644 --- a/src/rectangle/benders_decomposition.cpp +++ b/src/rectangle/benders_decomposition.cpp @@ -104,6 +104,7 @@ OptimizeParameters subproblem_parameters( const BendersDecompositionParameters& parameters) { OptimizeParameters sub_parameters; + sub_parameters.post_process = false; sub_parameters.verbosity_level = 0; sub_parameters.timer = parameters.timer; sub_parameters.optimization_mode diff --git a/src/rectangle/optimize.cpp b/src/rectangle/optimize.cpp index 143c685b3..ae8902a2e 100644 --- a/src/rectangle/optimize.cpp +++ b/src/rectangle/optimize.cpp @@ -414,6 +414,8 @@ void optimize_sequential_single_knapsack( = [&algorithm_formatter, ¶meters, &queue_size, &queue_size_ms](const Instance& kp_instance) { OptimizeParameters kp_parameters; + // The leftovers of the sub-instance don't matter. + kp_parameters.post_process = false; kp_parameters.verbosity_level = 0; kp_parameters.timer = parameters.timer; kp_parameters.timer.add_end_boolean(&algorithm_formatter.end_boolean()); @@ -478,6 +480,8 @@ void optimize_sequential_value_correction( = [&algorithm_formatter, ¶meters](const Instance& kp_instance) { OptimizeParameters kp_parameters; + // The leftovers of the sub-instance don't matter. + kp_parameters.post_process = false; kp_parameters.verbosity_level = 0; kp_parameters.timer = parameters.timer; kp_parameters.timer.add_end_boolean(&algorithm_formatter.end_boolean()); @@ -533,6 +537,8 @@ void optimize_dichotomic_search( = [&algorithm_formatter, ¶meters, &queue_size](const Instance& bpp_instance) { OptimizeParameters bpp_parameters; + // The leftovers of the sub-instance don't matter. + bpp_parameters.post_process = false; bpp_parameters.verbosity_level = 0; bpp_parameters.timer = parameters.timer; bpp_parameters.timer.add_end_boolean(&algorithm_formatter.end_boolean()); @@ -592,6 +598,8 @@ void optimize_column_generation( = [¶meters](const Instance& kp_instance, PricingType pricing_type) { OptimizeParameters kp_parameters; + // The leftovers of the sub-instance don't matter. + kp_parameters.post_process = false; kp_parameters.verbosity_level = 0; kp_parameters.timer = parameters.timer; kp_parameters.optimization_mode @@ -740,48 +748,75 @@ void optimize_tree_search_packing_classes( } /** - * Post-processes for BinPackingWithLeftovers in a not-anytime mode. + * Leftover post-processes in a not-anytime mode, for the objectives of + * 'leftover_post_process_objective'. * - * Last bin post-process: re-optimize just the last bin of 'solution_best' - * with the tree search with guides {2, 3} (see 'reoptimize_bin'), in case they - * pack it into less leftover space: the used area of the last bin doesn't - * have to span the whole width or the whole height of the bin, whichever - * algorithm found the solution. It is only applied if the last bin contains - * fewer items than 'post_process_last_bin_maximum_number_of_items': the tree - * search may take much longer than the algorithm which found the solution. + * Last bin post-process, for 'BinPackingWithLeftovers' only: re-optimize + * just the last bin of 'solution_best' with the tree search with guides + * {2, 3} (see 'reoptimize_bin_leftovers'), in case they pack it into less + * leftover space: the used area of the last bin doesn't have to span the + * whole width or the whole height of the bin, whichever algorithm found the + * solution. It is only applied if the last bin contains fewer items than + * 'post_process_last_bin_maximum_number_of_items': the tree search may take + * much longer than the algorithm which found the solution. * * It is applied to the best solution of each algorithm, not only to the * best solution overall: the re-optimized last bin of a solution which isn't * the best one may make it better than the best one. * * Other bins post-process: the leftover values of the other bins only come - * after the ones of the last bin in the objective, so the algorithms hardly - * try to keep the free space of the other bins in a single region. - * Re-optimize each other bin of the best solution, with the algorithms - * selected as for any instance (see 'reoptimize_non_last_bins'). It is only + * after the ones of the last bin in the 'BinPackingWithLeftovers' objective, + * and the other objectives don't consider the leftovers at all, so the + * algorithms hardly try to keep the free space of the bins in a single + * region. Re-optimize each bin of the best solution (each bin but the last + * one for 'BinPackingWithLeftovers'), with the algorithms selected as for any + * instance (see 'reoptimize_non_last_bins_leftovers' and + * 'reoptimize_all_bins_leftovers'). For 'BinPackingWithLeftovers', it is only * applied if 'leftover_all_bins' is enabled, since it can only improve the * leftover values of the other bins. + * + * For the objectives other than 'BinPackingWithLeftovers' and + * 'VariableSizedBinPackingWithLeftovers', the post-processed solution has + * the same value as the best solution: it replaces it if its leftovers are + * better (see 'AlgorithmFormatter::update_solution_leftovers'). */ bool post_process_applies( const Instance& instance, const OptimizeParameters& parameters) { - return instance.objective() == Objective::BinPackingWithLeftovers + return leftover_post_process_objective(instance.objective()) && parameters.optimization_mode != OptimizationMode::Anytime && parameters.optimization_mode != OptimizationMode::Exact && parameters.post_process; } +/** Report a solution of the leftover post-process. */ +void update_post_process_solution( + const Instance& instance, + const Solution& solution, + AlgorithmFormatter& algorithm_formatter) +{ + if (instance.objective() == Objective::BinPackingWithLeftovers) { + algorithm_formatter.update_solution(solution, "post-process"); + } else { + algorithm_formatter.update_solution_leftovers(solution, "post-process"); + } +} + void optimize_last_bin_post_process( const Instance& instance, const OptimizeParameters& parameters, const Solution& solution_best, AlgorithmFormatter& algorithm_formatter) { - if (post_process_applies(instance, parameters) - && solution_best.number_of_bins() > 0 - && solution_best.bin(solution_best.number_of_different_bins() - 1).copies == 1 - && (ItemPos)solution_best.bin(solution_best.number_of_different_bins() - 1).items.size() + if (!post_process_applies(instance, parameters) + || instance.objective() != Objective::BinPackingWithLeftovers + || solution_best.number_of_bins() == 0) { + return; + } + BinPos bin_pos = solution_best.number_of_different_bins() - 1; + if (solution_best.bin(bin_pos).copies == 1 + && (ItemPos)solution_best.bin(bin_pos).items.size() < parameters.post_process_last_bin_maximum_number_of_items) { // Only the tree search is run: the guides {2, 3} are specific to it. OptimizeParameters last_bin_parameters; @@ -791,11 +826,11 @@ void optimize_last_bin_post_process( last_bin_parameters.linear_programming_solver_name = parameters.linear_programming_solver_name; last_bin_parameters.tree_search_guides = {2, 3}; last_bin_parameters.use_tree_search = true; - Solution solution = reoptimize_bin( + Solution solution = reoptimize_bin_leftovers( solution_best, - solution_best.number_of_different_bins() - 1, + bin_pos, last_bin_parameters); - algorithm_formatter.update_solution(solution, "post-process"); + update_post_process_solution(instance, solution, algorithm_formatter); } } @@ -805,16 +840,24 @@ void optimize_other_bins_post_process( const Solution& solution_best, AlgorithmFormatter& algorithm_formatter) { - if (!post_process_applies(instance, parameters) - || !instance.parameters().leftover_all_bins) { + if (!post_process_applies(instance, parameters)) + return; + bool bin_packing_with_leftovers = (instance.objective() == Objective::BinPackingWithLeftovers); + if (bin_packing_with_leftovers + && !instance.parameters().leftover_all_bins) { return; } OptimizeParameters post_process_parameters = parameters; - post_process_parameters.timer.add_end_boolean(&algorithm_formatter.end_boolean()); - Solution solution = reoptimize_non_last_bins( - solution_best, - post_process_parameters); - algorithm_formatter.update_solution(solution, "post-process"); + // For the objectives which don't value the leftovers, the algorithm + // formatter ends as soon as the best solution is proven optimal, which + // doesn't mean that its leftovers can't be improved: only the time limit + // stops the post-process. + if (bin_packing_with_leftovers) + post_process_parameters.timer.add_end_boolean(&algorithm_formatter.end_boolean()); + Solution solution = (bin_packing_with_leftovers)? + reoptimize_non_last_bins_leftovers(solution_best, post_process_parameters): + reoptimize_all_bins_leftovers(solution_best, post_process_parameters); + update_post_process_solution(instance, solution, algorithm_formatter); } /** @@ -1298,7 +1341,9 @@ packingsolver::rectangle::Output packingsolver::rectangle::optimize( // 'NotAnytimeDeterministic' mode, these local formatters forward every // solution and bound to the shared 'algorithm_formatter' as soon as they // get it, instead of only at the end. - bool local = deterministic || post_process_applies(instance, parameters); + bool local = deterministic + || (post_process_applies(instance, parameters) + && instance.objective() == Objective::BinPackingWithLeftovers); if (local && !deterministic) { local_parameters.new_solution_callback = [&algorithm_formatter](const rectangle::Output& local_output) { @@ -1510,11 +1555,12 @@ packingsolver::rectangle::Output packingsolver::rectangle::optimize( } } - // Last bin post-process, on the best solution of each algorithm, and on - // the best solution overall if it doesn't come from one of them (e.g. - // found before running them). The best solution is copied since the - // post-process may replace it. - if (post_process_applies(instance, parameters)) { + // Last bin post-process, for 'BinPackingWithLeftovers', on the best + // solution of each algorithm, and on the best solution overall if it + // doesn't come from one of them (e.g. found before running them). The + // best solution is copied since the post-process may replace it. + if (post_process_applies(instance, parameters) + && instance.objective() == Objective::BinPackingWithLeftovers) { Solution solution_best = output.solution_pool.best(); bool solution_best_found_by_an_algorithm = false; for (const auto& local_output: local_outputs) { diff --git a/src/rectangle/post_process.cpp b/src/rectangle/post_process.cpp index f77deff7b..a039f2eb2 100644 --- a/src/rectangle/post_process.cpp +++ b/src/rectangle/post_process.cpp @@ -14,7 +14,7 @@ GroupIdenticalBinsOutput packingsolver::rectangle::group_identical_bins( return output; } -Solution packingsolver::rectangle::reoptimize_bin( +Solution packingsolver::rectangle::reoptimize_bin_leftovers( const Solution& solution, BinPos bin_pos, const OptimizeParameters& parameters) @@ -28,6 +28,10 @@ Solution packingsolver::rectangle::reoptimize_bin( // Add bin types. const SolutionBin& bin = solution.bin(bin_pos); + // An empty bin has nothing to re-optimize: its sub-instance would have no + // items, and its solution no bin. + if (bin.items.empty()) + return solution; BinTypeId bin_type_id = bin_instance_builder.add_bin_type_from_original_instance(bin.bin_type_id); bin_instance_builder.set_bin_type_copies(bin_type_id, 1); bin_instance_builder.set_bin_type_copies_min(bin_type_id, 0); @@ -87,24 +91,56 @@ Solution packingsolver::rectangle::reoptimize_bin( return new_solution; } -Solution packingsolver::rectangle::reoptimize_non_last_bins( +namespace +{ + +/** + * Re-optimize each of the first 'number_of_bins' bins of a solution, and keep + * a re-optimized bin if its leftovers are better. + */ +Solution reoptimize_bins_leftovers( const Solution& solution, + BinPos number_of_bins, const OptimizeParameters& parameters) { Solution new_solution = solution; for (BinPos bin_pos = 0; - bin_pos < solution.number_of_different_bins() - 1; + bin_pos < number_of_bins; ++bin_pos) { if (parameters.timer.needs_to_end()) break; - Solution bin_solution = reoptimize_bin( + Solution bin_solution = rectangle::reoptimize_bin_leftovers( new_solution, bin_pos, parameters); // Only the bin at 'bin_pos' differs: the comparison is on its leftover // value, then on its staircase leftover value. - if (new_solution < bin_solution) + if (!(bin_solution < new_solution) + && bin_solution.leftovers_better(new_solution)) { new_solution = bin_solution; + } } return new_solution; } + +} + +Solution packingsolver::rectangle::reoptimize_non_last_bins_leftovers( + const Solution& solution, + const OptimizeParameters& parameters) +{ + return reoptimize_bins_leftovers( + solution, + solution.number_of_different_bins() - 1, + parameters); +} + +Solution packingsolver::rectangle::reoptimize_all_bins_leftovers( + const Solution& solution, + const OptimizeParameters& parameters) +{ + return reoptimize_bins_leftovers( + solution, + solution.number_of_different_bins(), + parameters); +} diff --git a/src/rectangle/solution.cpp b/src/rectangle/solution.cpp index 3ef947262..55e8f0a07 100644 --- a/src/rectangle/solution.cpp +++ b/src/rectangle/solution.cpp @@ -273,6 +273,18 @@ double Solution::total_leftover_value() const return value - leftover_value_loss_; } +bool Solution::leftovers_better(const Solution& solution) const +{ + // The total leftover values of all the bins of the instance are compared + // through the value lost in the used bins. + LeftoverValueType leftover_value_type = instance().parameters().leftover_value_type; + if (strictly_greater_leftover_value(leftover_value_type, solution.leftover_value_loss_, leftover_value_loss_)) + return true; + if (strictly_greater_leftover_value(leftover_value_type, leftover_value_loss_, solution.leftover_value_loss_)) + return false; + return staircase_dominated_area_ < solution.staircase_dominated_area_; +} + bool Solution::operator<(const Solution& solution) const { // Check feasibility. diff --git a/test/box/box_test.cpp b/test/box/box_test.cpp index e691756a3..2f538fe54 100644 --- a/test/box/box_test.cpp +++ b/test/box/box_test.cpp @@ -214,7 +214,7 @@ TEST(Box, LeftoverModeStaircase) } } -TEST(Box, ReoptimizeNonLastBins) +TEST(Box, ReoptimizeNonLastBinsLeftovers) { // Two 10 x 10 x 10 bins and three 5 x 5 x 5 items. In the first bin of // the solution, the two items are in opposite corners: the leftover value @@ -245,7 +245,7 @@ TEST(Box, ReoptimizeNonLastBins) parameters.optimization_mode = packingsolver::OptimizationMode::NotAnytimeSequential; parameters.use_tree_search = !use_tree_search_maximal_spaces; parameters.use_tree_search_maximal_spaces = use_tree_search_maximal_spaces; - const Solution new_solution = reoptimize_non_last_bins(solution, parameters); + const Solution new_solution = reoptimize_non_last_bins_leftovers(solution, parameters); ASSERT_TRUE(new_solution.feasible()); EXPECT_EQ(new_solution.number_of_bins(), 2); EXPECT_EQ(new_solution.bin(0).leftover_value, (1000.0 - 250) * (1000.0 - 250)) diff --git a/test/boxstacks/leftover_volume_test.cpp b/test/boxstacks/leftover_volume_test.cpp index 83ce2206d..099bffcca 100644 --- a/test/boxstacks/leftover_volume_test.cpp +++ b/test/boxstacks/leftover_volume_test.cpp @@ -171,7 +171,7 @@ TEST(BoxStacks, LeftoverModeRanksSolutions) EXPECT_TRUE(along_y < along_x); // operator< means the right solution is better. } -TEST(BoxStacks, ReoptimizeNonLastBins) +TEST(BoxStacks, ReoptimizeNonLastBinsLeftovers) { // Two 10 x 10 x 10 bins and three 5 x 5 x 10 items. In the first bin of // the solution, the two stacks are in opposite corners: the leftover @@ -200,7 +200,7 @@ TEST(BoxStacks, ReoptimizeNonLastBins) OptimizeParameters parameters; parameters.optimization_mode = OptimizationMode::NotAnytimeSequential; - const Solution new_solution = reoptimize_non_last_bins(solution, parameters); + const Solution new_solution = reoptimize_non_last_bins_leftovers(solution, parameters); ASSERT_TRUE(new_solution.feasible()); EXPECT_EQ(new_solution.number_of_bins(), 2); EXPECT_EQ(new_solution.bin(0).leftover_value, ((100.0 - 50) * 10) * ((100.0 - 50) * 10)); diff --git a/test/rectangle/rectangle_test.cpp b/test/rectangle/rectangle_test.cpp index 9cdc7e346..ba8b81963 100644 --- a/test/rectangle/rectangle_test.cpp +++ b/test/rectangle/rectangle_test.cpp @@ -1,3 +1,4 @@ +#include "packingsolver/rectangle/algorithm_formatter.hpp" #include "packingsolver/rectangle/instance_builder.hpp" #include "packingsolver/rectangle/optimize.hpp" #include "packingsolver/rectangle/post_process.hpp" @@ -11,7 +12,7 @@ using namespace packingsolver::rectangle; namespace fs = boost::filesystem; -TEST(Rectangle, ReoptimizeNonLastBins) +TEST(Rectangle, ReoptimizeNonLastBinsLeftovers) { // Two 10 x 10 bins and three 5 x 5 items. In the first bin of the // solution, the two items are in opposite corners: the free space is @@ -38,7 +39,7 @@ TEST(Rectangle, ReoptimizeNonLastBins) OptimizeParameters parameters; parameters.optimization_mode = packingsolver::OptimizationMode::NotAnytimeSequential; - const Solution new_solution = reoptimize_non_last_bins(solution, parameters); + const Solution new_solution = reoptimize_non_last_bins_leftovers(solution, parameters); ASSERT_TRUE(new_solution.feasible()); EXPECT_EQ(new_solution.number_of_bins(), 2); EXPECT_EQ(new_solution.bin(0).leftover_value, 50 * 50); @@ -47,6 +48,115 @@ TEST(Rectangle, ReoptimizeNonLastBins) EXPECT_TRUE(solution < new_solution); } +namespace +{ + +/** + * Two 10 x 10 bins and three 5 x 5 items, for the 'BinPacking' objective: in + * each bin of the solution, the items are in opposite corners, so the free + * space is split into two regions. + */ +Solution scattered_solution(const Instance& instance) +{ + SolutionBuilder solution_builder(instance); + solution_builder.add_bin(0, 1); + solution_builder.add_item(0, 0, {0, 0}, false); + solution_builder.add_item(0, 0, {5, 5}, false); + solution_builder.add_bin(0, 1); + solution_builder.add_item(1, 0, {5, 5}, false); + return solution_builder.build(); +} + +Instance scattered_instance() +{ + InstanceBuilder instance_builder; + instance_builder.set_objective(packingsolver::Objective::BinPacking); + packingsolver::BinTypeId bin_type_id = instance_builder.add_bin_type(10, 10); + instance_builder.set_bin_type_copies(bin_type_id, 2); + packingsolver::ItemTypeId item_type_id = instance_builder.add_item_type(5, 5); + instance_builder.set_item_type_copies(item_type_id, 3); + return instance_builder.build(); +} + +} + +TEST(Rectangle, ReoptimizeAllBinsLeftovers) +{ + // Re-optimized, the items of the first bin are next to each other, and + // the item of the last bin is in the corner. The number of bins, i.e. the + // value of the solution, is the same, but the leftovers are better. + const Instance instance = scattered_instance(); + const Solution solution = scattered_solution(instance); + ASSERT_TRUE(solution.feasible()); + EXPECT_EQ(solution.bin(0).leftover_value, 0); + EXPECT_EQ(solution.bin(1).leftover_value, 0); + + OptimizeParameters parameters; + parameters.optimization_mode = packingsolver::OptimizationMode::NotAnytimeSequential; + const Solution new_solution = reoptimize_all_bins_leftovers(solution, parameters); + ASSERT_TRUE(new_solution.feasible()); + EXPECT_EQ(new_solution.number_of_bins(), 2); + EXPECT_EQ(new_solution.bin(0).leftover_value, 50 * 50); + EXPECT_EQ(new_solution.bin(1).leftover_value, 75 * 75); + EXPECT_FALSE(solution < new_solution); + EXPECT_FALSE(new_solution < solution); + EXPECT_TRUE(new_solution.leftovers_better(solution)); + EXPECT_FALSE(solution.leftovers_better(new_solution)); +} + +TEST(Rectangle, UpdateSolutionLeftovers) +{ + // A solution with the same value as the best one but better leftovers + // replaces it; a solution with worse leftovers doesn't. + const Instance instance = scattered_instance(); + const Solution solution = scattered_solution(instance); + OptimizeParameters parameters; + parameters.optimization_mode = packingsolver::OptimizationMode::NotAnytimeSequential; + parameters.verbosity_level = 0; + const Solution new_solution = reoptimize_all_bins_leftovers(solution, parameters); + ASSERT_TRUE(new_solution.leftovers_better(solution)); + + Output output(instance); + AlgorithmFormatter algorithm_formatter(instance, parameters, output); + algorithm_formatter.start(); + algorithm_formatter.update_solution(solution, "scattered"); + EXPECT_EQ(output.solution_pool.best_label(), "scattered"); + + // 'update_solution' only keeps strictly better solutions. + algorithm_formatter.update_solution(new_solution, "compact"); + EXPECT_EQ(output.solution_pool.best_label(), "scattered"); + + algorithm_formatter.update_solution_leftovers(new_solution, "compact"); + EXPECT_EQ(output.solution_pool.best_label(), "compact"); + EXPECT_EQ(output.solution_pool.best().total_leftover_value(), new_solution.total_leftover_value()); + + algorithm_formatter.update_solution_leftovers(solution, "scattered"); + EXPECT_EQ(output.solution_pool.best_label(), "compact"); +} + +TEST(Rectangle, LeftoverPostProcessBinPacking) +{ + // With the 'BinPacking' objective, the leftover post-process re-packs the + // bins of the best solution to concentrate their free space: two 5 x 5 + // items in a 10 x 10 bin, next to each other, leave a 10 x 5 leftover. + InstanceBuilder instance_builder; + instance_builder.set_objective(packingsolver::Objective::BinPacking); + packingsolver::BinTypeId bin_type_id = instance_builder.add_bin_type(10, 10); + instance_builder.set_bin_type_copies(bin_type_id, 2); + packingsolver::ItemTypeId item_type_id = instance_builder.add_item_type(5, 5); + instance_builder.set_item_type_copies(item_type_id, 2); + const Instance instance = instance_builder.build(); + OptimizeParameters parameters; + parameters.optimization_mode = packingsolver::OptimizationMode::NotAnytimeSequential; + parameters.verbosity_level = 0; + const Output output = optimize(instance, parameters); + const Solution& solution = output.solution_pool.best(); + ASSERT_TRUE(solution.feasible()); + EXPECT_EQ(solution.number_of_bins(), 1); + EXPECT_EQ(solution.bin(0).leftover_value, 50.0 * 50); + EXPECT_EQ(solution.total_leftover_value(), 100.0 * 100 + 50.0 * 50); +} + TEST(Rectangle, LeftoverModeStaircase) { // A 12 x 10 bin with a 6 x 4 item at the origin and a 3 x 4 item above