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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/leftovers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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<rectangle>`, :ref:`box<box>`, :ref:`box-stacks<boxstacks>` and :ref:`irregular<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
Expand Down
51 changes: 47 additions & 4 deletions include/packingsolver/algorithms/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -342,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
Expand Down Expand Up @@ -483,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_;
Expand Down
12 changes: 12 additions & 0 deletions include/packingsolver/box/algorithm_formatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
5 changes: 3 additions & 2 deletions include/packingsolver/box/optimize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,9 @@ struct OptimizeParameters: packingsolver::Parameters<Instance, Solution, Output>
std::vector<Direction> 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;

Expand Down
34 changes: 23 additions & 11 deletions include/packingsolver/box/post_process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,25 @@ struct GroupIdenticalBinsOutput: packingsolver::Output<Instance, Solution>
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.
Expand All @@ -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 = {});

Expand Down
12 changes: 12 additions & 0 deletions include/packingsolver/box/solution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions include/packingsolver/boxstacks/algorithm_formatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
5 changes: 3 additions & 2 deletions include/packingsolver/boxstacks/optimize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,9 @@ struct OptimizeParameters: packingsolver::Parameters<Instance, Solution, Output>
std::vector<GuideId> 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;

Expand Down
34 changes: 23 additions & 11 deletions include/packingsolver/boxstacks/post_process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,25 @@ struct GroupIdenticalBinsOutput: packingsolver::Output<Instance, Solution>
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.
Expand All @@ -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 = {});

Expand Down
12 changes: 12 additions & 0 deletions include/packingsolver/boxstacks/solution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions include/packingsolver/irregular/algorithm_formatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions include/packingsolver/irregular/optimize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,9 @@ struct OptimizeParameters: packingsolver::Parameters<Instance, Solution, Output>
std::vector<GuideId> 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;

Expand Down
34 changes: 23 additions & 11 deletions include/packingsolver/irregular/post_process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {});

Expand Down
Loading
Loading