diff --git a/data/box/tests/bin_packing_full_bin_items/bins.csv b/data/box/tests/bin_packing_full_bin_items/bins.csv new file mode 100644 index 000000000..e93203ffa --- /dev/null +++ b/data/box/tests/bin_packing_full_bin_items/bins.csv @@ -0,0 +1,2 @@ +X,Y,Z,COST,COPIES,COPIES_MIN,MAXIMUM_WEIGHT +10,10,10,1000,5,0,inf diff --git a/data/box/tests/bin_packing_full_bin_items/items.csv b/data/box/tests/bin_packing_full_bin_items/items.csv new file mode 100644 index 000000000..f51c9118b --- /dev/null +++ b/data/box/tests/bin_packing_full_bin_items/items.csv @@ -0,0 +1,3 @@ +X,Y,Z,PROFIT,COPIES,COPIES_MIN,WEIGHT,ROTATION_XYZ +10,10,10,1000,2,2,0,1 +5,5,5,125,3,3,0,1 diff --git a/data/box/tests/bin_packing_full_bin_items/parameters.csv b/data/box/tests/bin_packing_full_bin_items/parameters.csv new file mode 100644 index 000000000..e72e86bc5 --- /dev/null +++ b/data/box/tests/bin_packing_full_bin_items/parameters.csv @@ -0,0 +1,2 @@ +NAME,VALUE +objective,bin-packing diff --git a/data/box/tests/bin_packing_full_bin_items/reduced_bins.csv b/data/box/tests/bin_packing_full_bin_items/reduced_bins.csv new file mode 100644 index 000000000..d7cd8ed85 --- /dev/null +++ b/data/box/tests/bin_packing_full_bin_items/reduced_bins.csv @@ -0,0 +1,2 @@ +X,Y,Z,COST,COPIES,COPIES_MIN,MAXIMUM_WEIGHT +10,10,10,1000,3,0,inf diff --git a/data/box/tests/bin_packing_full_bin_items/reduced_items.csv b/data/box/tests/bin_packing_full_bin_items/reduced_items.csv new file mode 100644 index 000000000..b80f1a43d --- /dev/null +++ b/data/box/tests/bin_packing_full_bin_items/reduced_items.csv @@ -0,0 +1,2 @@ +X,Y,Z,PROFIT,COPIES,COPIES_MIN,WEIGHT,ROTATION_XYZ +5,5,5,125,3,3,0,1 diff --git a/data/box/tests/bin_packing_full_bin_items/solution.csv b/data/box/tests/bin_packing_full_bin_items/solution.csv new file mode 100644 index 000000000..a8820f5cd --- /dev/null +++ b/data/box/tests/bin_packing_full_bin_items/solution.csv @@ -0,0 +1,7 @@ +TYPE,ID,COPIES,BIN,X,Y,Z,LX,LY,LZ,ROTATION +BIN,0,2,0,0,0,0,10,10,10, +ITEM,0,1,0,0,0,0,10,10,10,XYZ +BIN,0,1,1,0,0,0,10,10,10, +ITEM,1,1,1,0,0,0,5,5,5,XYZ +ITEM,1,1,1,5,0,0,5,5,5,XYZ +ITEM,1,1,1,0,5,0,5,5,5,XYZ diff --git a/data/box/tests/bin_packing_full_bin_items_not_reserved_with_weight/bins.csv b/data/box/tests/bin_packing_full_bin_items_not_reserved_with_weight/bins.csv new file mode 100644 index 000000000..daf753c70 --- /dev/null +++ b/data/box/tests/bin_packing_full_bin_items_not_reserved_with_weight/bins.csv @@ -0,0 +1,2 @@ +X,Y,Z,COST,COPIES,COPIES_MIN,MAXIMUM_WEIGHT +10,10,10,1000,5,0,100 diff --git a/data/box/tests/bin_packing_full_bin_items_not_reserved_with_weight/items.csv b/data/box/tests/bin_packing_full_bin_items_not_reserved_with_weight/items.csv new file mode 100644 index 000000000..0981e3675 --- /dev/null +++ b/data/box/tests/bin_packing_full_bin_items_not_reserved_with_weight/items.csv @@ -0,0 +1,3 @@ +X,Y,Z,PROFIT,COPIES,COPIES_MIN,WEIGHT,ROTATION_XYZ +10,10,10,1000,2,2,1,1 +5,5,5,125,3,3,1,1 diff --git a/data/box/tests/bin_packing_full_bin_items_not_reserved_with_weight/parameters.csv b/data/box/tests/bin_packing_full_bin_items_not_reserved_with_weight/parameters.csv new file mode 100644 index 000000000..e72e86bc5 --- /dev/null +++ b/data/box/tests/bin_packing_full_bin_items_not_reserved_with_weight/parameters.csv @@ -0,0 +1,2 @@ +NAME,VALUE +objective,bin-packing diff --git a/data/box/tests/bin_packing_full_bin_items_not_reserved_with_weight/reduced_bins.csv b/data/box/tests/bin_packing_full_bin_items_not_reserved_with_weight/reduced_bins.csv new file mode 100644 index 000000000..daf753c70 --- /dev/null +++ b/data/box/tests/bin_packing_full_bin_items_not_reserved_with_weight/reduced_bins.csv @@ -0,0 +1,2 @@ +X,Y,Z,COST,COPIES,COPIES_MIN,MAXIMUM_WEIGHT +10,10,10,1000,5,0,100 diff --git a/data/box/tests/bin_packing_full_bin_items_not_reserved_with_weight/reduced_items.csv b/data/box/tests/bin_packing_full_bin_items_not_reserved_with_weight/reduced_items.csv new file mode 100644 index 000000000..0981e3675 --- /dev/null +++ b/data/box/tests/bin_packing_full_bin_items_not_reserved_with_weight/reduced_items.csv @@ -0,0 +1,3 @@ +X,Y,Z,PROFIT,COPIES,COPIES_MIN,WEIGHT,ROTATION_XYZ +10,10,10,1000,2,2,1,1 +5,5,5,125,3,3,1,1 diff --git a/data/box/tests/bin_packing_full_bin_items_not_reserved_with_weight/solution.csv b/data/box/tests/bin_packing_full_bin_items_not_reserved_with_weight/solution.csv new file mode 100644 index 000000000..a8820f5cd --- /dev/null +++ b/data/box/tests/bin_packing_full_bin_items_not_reserved_with_weight/solution.csv @@ -0,0 +1,7 @@ +TYPE,ID,COPIES,BIN,X,Y,Z,LX,LY,LZ,ROTATION +BIN,0,2,0,0,0,0,10,10,10, +ITEM,0,1,0,0,0,0,10,10,10,XYZ +BIN,0,1,1,0,0,0,10,10,10, +ITEM,1,1,1,0,0,0,5,5,5,XYZ +ITEM,1,1,1,5,0,0,5,5,5,XYZ +ITEM,1,1,1,0,5,0,5,5,5,XYZ diff --git a/data/box/tests/bin_packing_perfect_pairs/bins.csv b/data/box/tests/bin_packing_perfect_pairs/bins.csv new file mode 100644 index 000000000..e93203ffa --- /dev/null +++ b/data/box/tests/bin_packing_perfect_pairs/bins.csv @@ -0,0 +1,2 @@ +X,Y,Z,COST,COPIES,COPIES_MIN,MAXIMUM_WEIGHT +10,10,10,1000,5,0,inf diff --git a/data/box/tests/bin_packing_perfect_pairs/items.csv b/data/box/tests/bin_packing_perfect_pairs/items.csv new file mode 100644 index 000000000..b45f70cf9 --- /dev/null +++ b/data/box/tests/bin_packing_perfect_pairs/items.csv @@ -0,0 +1,4 @@ +X,Y,Z,PROFIT,COPIES,COPIES_MIN,WEIGHT,ROTATION_XYZ +6,10,10,600,2,2,0,1 +4,10,10,400,2,2,0,1 +3,3,3,27,2,2,0,1 diff --git a/data/box/tests/bin_packing_perfect_pairs/parameters.csv b/data/box/tests/bin_packing_perfect_pairs/parameters.csv new file mode 100644 index 000000000..e72e86bc5 --- /dev/null +++ b/data/box/tests/bin_packing_perfect_pairs/parameters.csv @@ -0,0 +1,2 @@ +NAME,VALUE +objective,bin-packing diff --git a/data/box/tests/bin_packing_perfect_pairs/reduced_bins.csv b/data/box/tests/bin_packing_perfect_pairs/reduced_bins.csv new file mode 100644 index 000000000..d7cd8ed85 --- /dev/null +++ b/data/box/tests/bin_packing_perfect_pairs/reduced_bins.csv @@ -0,0 +1,2 @@ +X,Y,Z,COST,COPIES,COPIES_MIN,MAXIMUM_WEIGHT +10,10,10,1000,3,0,inf diff --git a/data/box/tests/bin_packing_perfect_pairs/reduced_items.csv b/data/box/tests/bin_packing_perfect_pairs/reduced_items.csv new file mode 100644 index 000000000..6558304de --- /dev/null +++ b/data/box/tests/bin_packing_perfect_pairs/reduced_items.csv @@ -0,0 +1,2 @@ +X,Y,Z,PROFIT,COPIES,COPIES_MIN,WEIGHT,ROTATION_XYZ +3,3,3,27,2,2,0,1 diff --git a/data/box/tests/bin_packing_perfect_pairs/solution.csv b/data/box/tests/bin_packing_perfect_pairs/solution.csv new file mode 100644 index 000000000..fb32d7cd4 --- /dev/null +++ b/data/box/tests/bin_packing_perfect_pairs/solution.csv @@ -0,0 +1,7 @@ +TYPE,ID,COPIES,BIN,X,Y,Z,LX,LY,LZ,ROTATION +BIN,0,2,0,0,0,0,10,10,10, +ITEM,0,1,0,0,0,0,6,10,10,XYZ +ITEM,1,1,0,6,0,0,4,10,10,XYZ +BIN,0,1,1,0,0,0,10,10,10, +ITEM,2,1,1,0,0,0,3,3,3,XYZ +ITEM,2,1,1,3,0,0,3,3,3,XYZ diff --git a/data/rectangleguillotine/tests/bin_packing_full_bin_items/bins.csv b/data/rectangleguillotine/tests/bin_packing_full_bin_items/bins.csv new file mode 100644 index 000000000..c03ef00fd --- /dev/null +++ b/data/rectangleguillotine/tests/bin_packing_full_bin_items/bins.csv @@ -0,0 +1,2 @@ +WIDTH,HEIGHT,COPIES +10,10,5 diff --git a/data/rectangleguillotine/tests/bin_packing_full_bin_items/items.csv b/data/rectangleguillotine/tests/bin_packing_full_bin_items/items.csv new file mode 100644 index 000000000..9810d46fd --- /dev/null +++ b/data/rectangleguillotine/tests/bin_packing_full_bin_items/items.csv @@ -0,0 +1,3 @@ +WIDTH,HEIGHT,PROFIT,ORIENTED,COPIES +10,10,100,1,2 +5,5,25,1,3 diff --git a/data/rectangleguillotine/tests/bin_packing_full_bin_items/parameters.csv b/data/rectangleguillotine/tests/bin_packing_full_bin_items/parameters.csv new file mode 100644 index 000000000..15bc6d121 --- /dev/null +++ b/data/rectangleguillotine/tests/bin_packing_full_bin_items/parameters.csv @@ -0,0 +1,5 @@ +NAME,VALUE +objective,bin-packing +number_of_stages,3 +cut_type,non-exact +first_stage_orientation,vertical diff --git a/data/rectangleguillotine/tests/bin_packing_full_bin_items/reduced_bins.csv b/data/rectangleguillotine/tests/bin_packing_full_bin_items/reduced_bins.csv new file mode 100644 index 000000000..b7b9b7b58 --- /dev/null +++ b/data/rectangleguillotine/tests/bin_packing_full_bin_items/reduced_bins.csv @@ -0,0 +1,2 @@ +WIDTH,HEIGHT,COPIES +10,10,3 diff --git a/data/rectangleguillotine/tests/bin_packing_full_bin_items/reduced_items.csv b/data/rectangleguillotine/tests/bin_packing_full_bin_items/reduced_items.csv new file mode 100644 index 000000000..fb5f72400 --- /dev/null +++ b/data/rectangleguillotine/tests/bin_packing_full_bin_items/reduced_items.csv @@ -0,0 +1,2 @@ +WIDTH,HEIGHT,PROFIT,ORIENTED,COPIES +5,5,25,1,3 diff --git a/data/rectangleguillotine/tests/bin_packing_full_bin_items/solution.csv b/data/rectangleguillotine/tests/bin_packing_full_bin_items/solution.csv new file mode 100644 index 000000000..ec13dec0c --- /dev/null +++ b/data/rectangleguillotine/tests/bin_packing_full_bin_items/solution.csv @@ -0,0 +1,10 @@ +PLATE_ID,COPIES,NODE_ID,X,Y,WIDTH,HEIGHT,TYPE,CUT,PARENT +0,2,0,0,0,10,10,0,0, +0,2,1,0,0,10,10,0,1,0 +1,1,2,0,0,10,10,0,0, +1,1,3,0,0,5,10,-2,1,2 +1,1,4,0,0,5,5,1,2,3 +1,1,5,0,5,5,5,1,2,3 +1,1,6,5,0,5,10,-2,1,2 +1,1,7,5,0,5,5,1,2,6 +1,1,8,5,5,5,5,-1,2,6 diff --git a/data/rectangleguillotine/tests/bin_packing_perfect_pairs/bins.csv b/data/rectangleguillotine/tests/bin_packing_perfect_pairs/bins.csv new file mode 100644 index 000000000..c03ef00fd --- /dev/null +++ b/data/rectangleguillotine/tests/bin_packing_perfect_pairs/bins.csv @@ -0,0 +1,2 @@ +WIDTH,HEIGHT,COPIES +10,10,5 diff --git a/data/rectangleguillotine/tests/bin_packing_perfect_pairs/items.csv b/data/rectangleguillotine/tests/bin_packing_perfect_pairs/items.csv new file mode 100644 index 000000000..c0bca4434 --- /dev/null +++ b/data/rectangleguillotine/tests/bin_packing_perfect_pairs/items.csv @@ -0,0 +1,4 @@ +WIDTH,HEIGHT,PROFIT,ORIENTED,COPIES +6,10,60,1,2 +4,10,40,1,2 +3,3,9,1,2 diff --git a/data/rectangleguillotine/tests/bin_packing_perfect_pairs/parameters.csv b/data/rectangleguillotine/tests/bin_packing_perfect_pairs/parameters.csv new file mode 100644 index 000000000..15bc6d121 --- /dev/null +++ b/data/rectangleguillotine/tests/bin_packing_perfect_pairs/parameters.csv @@ -0,0 +1,5 @@ +NAME,VALUE +objective,bin-packing +number_of_stages,3 +cut_type,non-exact +first_stage_orientation,vertical diff --git a/data/rectangleguillotine/tests/bin_packing_perfect_pairs/reduced_bins.csv b/data/rectangleguillotine/tests/bin_packing_perfect_pairs/reduced_bins.csv new file mode 100644 index 000000000..b7b9b7b58 --- /dev/null +++ b/data/rectangleguillotine/tests/bin_packing_perfect_pairs/reduced_bins.csv @@ -0,0 +1,2 @@ +WIDTH,HEIGHT,COPIES +10,10,3 diff --git a/data/rectangleguillotine/tests/bin_packing_perfect_pairs/reduced_items.csv b/data/rectangleguillotine/tests/bin_packing_perfect_pairs/reduced_items.csv new file mode 100644 index 000000000..267ef9258 --- /dev/null +++ b/data/rectangleguillotine/tests/bin_packing_perfect_pairs/reduced_items.csv @@ -0,0 +1,2 @@ +WIDTH,HEIGHT,PROFIT,ORIENTED,COPIES +3,3,9,1,2 diff --git a/data/rectangleguillotine/tests/bin_packing_perfect_pairs/solution.csv b/data/rectangleguillotine/tests/bin_packing_perfect_pairs/solution.csv new file mode 100644 index 000000000..fd49e2f8b --- /dev/null +++ b/data/rectangleguillotine/tests/bin_packing_perfect_pairs/solution.csv @@ -0,0 +1,10 @@ +PLATE_ID,COPIES,NODE_ID,X,Y,WIDTH,HEIGHT,TYPE,CUT,PARENT +0,2,0,0,0,10,10,0,0, +0,2,1,0,0,6,10,0,1,0 +0,2,2,6,0,4,10,1,1,0 +1,1,3,0,0,10,10,0,0, +1,1,4,0,0,3,10,-2,1,3 +1,1,5,0,0,3,3,2,2,4 +1,1,6,0,3,3,3,2,2,4 +1,1,7,0,6,3,4,-1,2,4 +1,1,8,3,0,7,10,-3,1,3 diff --git a/data/rectangleguillotine/tests/bin_packing_perfect_pairs_cut_thickness/bins.csv b/data/rectangleguillotine/tests/bin_packing_perfect_pairs_cut_thickness/bins.csv new file mode 100644 index 000000000..c03ef00fd --- /dev/null +++ b/data/rectangleguillotine/tests/bin_packing_perfect_pairs_cut_thickness/bins.csv @@ -0,0 +1,2 @@ +WIDTH,HEIGHT,COPIES +10,10,5 diff --git a/data/rectangleguillotine/tests/bin_packing_perfect_pairs_cut_thickness/items.csv b/data/rectangleguillotine/tests/bin_packing_perfect_pairs_cut_thickness/items.csv new file mode 100644 index 000000000..957e45a13 --- /dev/null +++ b/data/rectangleguillotine/tests/bin_packing_perfect_pairs_cut_thickness/items.csv @@ -0,0 +1,4 @@ +WIDTH,HEIGHT,PROFIT,ORIENTED,COPIES +6,10,60,1,2 +3,10,30,1,2 +3,3,9,1,2 diff --git a/data/rectangleguillotine/tests/bin_packing_perfect_pairs_cut_thickness/parameters.csv b/data/rectangleguillotine/tests/bin_packing_perfect_pairs_cut_thickness/parameters.csv new file mode 100644 index 000000000..6a9515e59 --- /dev/null +++ b/data/rectangleguillotine/tests/bin_packing_perfect_pairs_cut_thickness/parameters.csv @@ -0,0 +1,6 @@ +NAME,VALUE +objective,bin-packing +number_of_stages,3 +cut_type,non-exact +first_stage_orientation,vertical +cut_thickness,1 diff --git a/data/rectangleguillotine/tests/bin_packing_perfect_pairs_cut_thickness/reduced_bins.csv b/data/rectangleguillotine/tests/bin_packing_perfect_pairs_cut_thickness/reduced_bins.csv new file mode 100644 index 000000000..b7b9b7b58 --- /dev/null +++ b/data/rectangleguillotine/tests/bin_packing_perfect_pairs_cut_thickness/reduced_bins.csv @@ -0,0 +1,2 @@ +WIDTH,HEIGHT,COPIES +10,10,3 diff --git a/data/rectangleguillotine/tests/bin_packing_perfect_pairs_cut_thickness/reduced_items.csv b/data/rectangleguillotine/tests/bin_packing_perfect_pairs_cut_thickness/reduced_items.csv new file mode 100644 index 000000000..267ef9258 --- /dev/null +++ b/data/rectangleguillotine/tests/bin_packing_perfect_pairs_cut_thickness/reduced_items.csv @@ -0,0 +1,2 @@ +WIDTH,HEIGHT,PROFIT,ORIENTED,COPIES +3,3,9,1,2 diff --git a/data/rectangleguillotine/tests/bin_packing_perfect_pairs_cut_thickness/solution.csv b/data/rectangleguillotine/tests/bin_packing_perfect_pairs_cut_thickness/solution.csv new file mode 100644 index 000000000..207cd65bf --- /dev/null +++ b/data/rectangleguillotine/tests/bin_packing_perfect_pairs_cut_thickness/solution.csv @@ -0,0 +1,10 @@ +PLATE_ID,COPIES,NODE_ID,X,Y,WIDTH,HEIGHT,TYPE,CUT,PARENT +0,2,0,0,0,10,10,0,0, +0,2,1,0,0,6,10,0,1,0 +0,2,2,7,0,3,10,1,1,0 +1,1,3,0,0,10,10,0,0, +1,1,4,0,0,3,10,-2,1,3 +1,1,5,0,0,3,3,2,2,4 +1,1,6,0,4,3,3,2,2,4 +1,1,7,0,8,3,2,-1,2,4 +1,1,8,4,0,6,10,-3,1,3 diff --git a/include/packingsolver/box/reduction.hpp b/include/packingsolver/box/reduction.hpp index 0af46cb18..6fd8a581f 100644 --- a/include/packingsolver/box/reduction.hpp +++ b/include/packingsolver/box/reduction.hpp @@ -72,6 +72,22 @@ struct ReductionParameters: optimizationtools::Parameters * flag. */ bool remove_dominated_bin_types = true; + + /** + * Enable/disable reserving a dedicated bin for each copy of an item + * type filling the bin (gates 'Reduction::reduce_full_bin_items'). + * Still subject to 'Reduction::full_bin_reduction_applies' regardless + * of this flag. + */ + bool reduce_full_bin_items = true; + + /** + * Enable/disable reserving a dedicated bin for pairs of item types + * filling the bin together (gates 'Reduction::reduce_perfect_pairs'). + * Still subject to 'Reduction::full_bin_reduction_applies' regardless + * of this flag. + */ + bool reduce_perfect_pairs = true; }; /** @@ -174,6 +190,37 @@ struct ReductionParameters: optimizationtools::Parameters * the reduced instance back to the original ones. See * 'remove_dominated_bin_types_applies()'. * + * - Reserving dedicated bins (see 'reduce_full_bin_items' and + * 'reduce_perfect_pairs'): rectangle's and onedimensional's own + * operations. Compared against the bin shrunk along each axis to the + * largest total length of the items not exceeding it (see + * 'shrunk_bin_length'), using the current (possibly lifted) boxes. An + * item whose box equals the shrunk bin can't share its bin with any + * other item: along each axis, any other item beside it would make a + * total length beyond the shrunk bin. Such an item gets a dedicated bin. + * An allowed rotation of the item giving the shrunk bin is enough: the + * shrunk length along each axis is at least the length of the item along + * it in any rotation fitting the bin, so every such rotation gives the + * same box. Two items spanning the shrunk bin along two axes, whose + * lengths along the third axis sum to the shrunk bin's, also get a + * dedicated bin together: in any solution, the other items of the bin of + * each of them are beside it along the third axis, in two slabs spanning + * the cross-section of the bin, whose total length is at most the length + * of the other item of the pair (shrunk bin); so the items of the two + * bins other than the pair fit in a single bin, next to the bin holding + * the pair. This requires both items to have a single allowed rotation + * (otherwise they could be placed without spanning the two axes). Only + * for 'BinPacking' with a single bin type, without fixed items, and + * without a weight or resource constraint a hidden reservation could + * violate (see 'full_bin_reduction_applies()'). Never reserves more + * dedicated bins than the bin type has copies; if they take every copy + * while items remain, the instance is infeasible (see + * 'proven_infeasible()'). The dedicated items are left out of the + * reduced instance, the bin type has its 'copies' reduced accordingly, + * and 'unreduce_solution' adds the dedicated bins back. Runs in the same + * loop as 'lift_item_dimensions', since lifting can make an item fill + * the shrunk bin, and a reservation can shrink the bin further. + * * - Merging identical item types (see 'merge_identical_items'): a merged * item type's copies stay fully visible and independently placed in the * reduced instance (nothing is hidden from the downstream solve), so this @@ -211,6 +258,21 @@ class Reduction /** Get the reduced instance. */ const Instance& instance() const { return instance_; } + /** + * Number of bins reserved as dedicated bins (see + * 'reduce_full_bin_items'/'reduce_perfect_pairs'), entirely absent from + * 'instance()' - a caller solving 'instance()' needs to add this back + * onto any bin-count bound it finds. + */ + BinPos number_of_dedicated_bins() const; + + /** + * 'true' iff the reduction alone proves the original instance + * infeasible: the dedicated bins take every copy of the bin type while + * items remain. 'instance()' is then not meaningful to solve. + */ + bool proven_infeasible() const { return proven_infeasible_; } + /** Unreduce a solution of the reduced instance. */ Solution unreduce_solution( const Solution& solution) const; @@ -258,6 +320,13 @@ class Reduction */ static bool remove_dominated_bin_types_applies(const Instance& instance); + /** + * 'true' iff reserving dedicated bins (see 'reduce_full_bin_items' and + * 'reduce_perfect_pairs') is meaningful for 'instance' - see the + * class-level doc comment. + */ + static bool full_bin_reduction_applies(const Instance& instance); + /* * Private types */ @@ -498,6 +567,63 @@ class Reduction */ std::vector reduced_to_original_bin_type_id_; + /** A dedicated bin for each copy of an item type filling the bin. */ + struct FullBinItem + { + /** Item type id, original instance's id space. */ + ItemTypeId item_type_id; + + /** Rotation giving the box of the bin. */ + Rotation rotation; + + /** Number of dedicated bins. */ + ItemPos copies; + }; + + /** Dedicated bins for pairs of item types filling the bin together. */ + struct PerfectPair + { + /** Item type ids, original instance's id space. */ + ItemTypeId item_type_id_1; + ItemTypeId item_type_id_2; + + /** Rotations of the two items. */ + Rotation rotation_1; + Rotation rotation_2; + + /** Axis along which the two items are side by side. */ + int axis; + + /** Number of dedicated bins. */ + ItemPos copies; + }; + + /** See 'reduce_full_bin_items'. */ + std::vector full_bin_items_; + + /** See 'reduce_perfect_pairs'. */ + std::vector perfect_pairs_; + + /** See 'proven_infeasible()'. */ + bool proven_infeasible_ = false; + + /** + * Reserves a dedicated bin for every copy of every item type whose box, + * in one of its allowed rotations, equals the shrunk bin. Returns + * 'true' iff at least one item type was reserved. + */ + bool reduce_full_bin_items( + std::vector& reduction_item_types); + + /** + * Reserves a dedicated bin for 'min(copies_1, copies_2)' copies of every + * pair of item types filling the shrunk bin together (including two + * copies of the same item type). Returns 'true' iff at least one pair + * was reserved. + */ + bool reduce_perfect_pairs( + std::vector& reduction_item_types); + /** * Consumes one copy from the end of 'runs' (a working copy of one * reduced item type's own entry in 'reduced_item_origin_runs_'), diff --git a/include/packingsolver/rectangleguillotine/reduction.hpp b/include/packingsolver/rectangleguillotine/reduction.hpp index 51e874db6..57f82fc32 100644 --- a/include/packingsolver/rectangleguillotine/reduction.hpp +++ b/include/packingsolver/rectangleguillotine/reduction.hpp @@ -73,6 +73,22 @@ struct ReductionParameters: optimizationtools::Parameters * flag. */ bool remove_dominated_bin_types = true; + + /** + * Enable/disable reserving a dedicated bin for each copy of an item + * type filling the bin (gates 'Reduction::reduce_full_bin_items'). + * Still subject to 'Reduction::full_bin_reduction_applies' regardless + * of this flag. + */ + bool reduce_full_bin_items = true; + + /** + * Enable/disable reserving a dedicated bin for pairs of item types + * filling the bin together (gates 'Reduction::reduce_perfect_pairs'). + * Still subject to 'Reduction::full_bin_reduction_applies' regardless + * of this flag. + */ + bool reduce_perfect_pairs = true; }; /** @@ -183,6 +199,33 @@ struct ReductionParameters: optimizationtools::Parameters * 'lift_item_dimensions_applies()' and * 'lift_item_dimensions_applies_axis()'. * + * Reserving dedicated bins (see 'reduce_full_bin_items' and + * 'reduce_perfect_pairs'): rectangle's own operations, compared against + * the bin shrunk along each axis to the largest total length of the items + * not exceeding it, every length (bin included) being increased by the cut + * thickness, as for lifting. An item filling the shrunk bin can't share its + * bin with any other item: it gets a dedicated bin. Two oriented items + * spanning the shrunk bin along one axis, filling it together along the + * other one, also get a dedicated bin together: in a solution, compacted + * along the axis of the pair (see the lifting paragraph above), the other + * items of the bin of each of them are beside it, in subtrees spanning the + * bin along the other axis, whose total length is at most the length of + * the other item of the pair; the subtrees of the two bins fit in a single + * bin, as first-stage nodes if the first stage is along the axis of the + * pair, as second-stage nodes of a single first-stage node otherwise + * (never deeper than they were). Only for 'BinPacking' with a single bin + * type without trims, defects, fixed items or resources, with the cut + * constraints of 'items_can_shrink' (the item of a dedicated bin can be + * smaller than the bin, leaving a waste), and for items alone in their + * stacks (see 'full_bin_reduction_applies()'). Never reserves more + * dedicated bins than the bin type has copies; if they take every copy + * while items remain, the instance is infeasible (see + * 'proven_infeasible()'). The dedicated items are left out of the reduced + * instance, the bin type has its 'copies' reduced accordingly, and + * 'unreduce_solution' adds the dedicated bins back, with the true + * dimensions of the items. Run after lifting, which can make an item fill + * the shrunk bin. + * * Only ever needs one pass (unlike 'rectangle::Reduction''s fixpoint * loop): trimming a negative-profit item type down to its 'copies_min' * can only ever narrow a later merge candidate's mandatory-vs-optional @@ -219,6 +262,21 @@ class Reduction /** Get the reduced instance. */ const Instance& instance() const { return instance_; } + /** + * Number of bins reserved as dedicated bins (see + * 'reduce_full_bin_items'/'reduce_perfect_pairs'), entirely absent from + * 'instance()' - a caller solving 'instance()' needs to add this back + * onto any bin-count bound it finds. + */ + BinPos number_of_dedicated_bins() const; + + /** + * 'true' iff the reduction alone proves the original instance + * infeasible: the dedicated bins take every copy of the bin type while + * items remain. 'instance()' is then not meaningful to solve. + */ + bool proven_infeasible() const { return proven_infeasible_; } + /** Unreduce a solution of the reduced instance. */ Solution unreduce_solution( const Solution& solution) const; @@ -304,6 +362,13 @@ class Reduction */ static bool remove_dominated_bin_types_applies(const Instance& instance); + /** + * 'true' iff reserving dedicated bins (see 'reduce_full_bin_items' and + * 'reduce_perfect_pairs') is meaningful for 'instance' - see the + * class-level doc comment. + */ + static bool full_bin_reduction_applies(const Instance& instance); + /** * Working representation of an item type during the reduction * process. Unlike 'rectangle::Reduction::ReductionItemType', carries @@ -551,6 +616,69 @@ class Reduction */ std::vector reduced_to_original_bin_type_id_; + /** A dedicated bin for each copy of an item type filling the bin. */ + struct FullBinItem + { + /** Item type id, original instance's id space. */ + ItemTypeId item_type_id; + + /** Number of dedicated bins. */ + ItemPos copies; + }; + + /** Dedicated bins for pairs of item types filling the bin together. */ + struct PerfectPair + { + /** Item type ids, original instance's id space. */ + ItemTypeId item_type_id_1; + ItemTypeId item_type_id_2; + + /** Axis along which the two items are side by side ('0' for 'x'). */ + int axis; + + /** Number of dedicated bins. */ + ItemPos copies; + }; + + /** See 'reduce_full_bin_items'. */ + std::vector full_bin_items_; + + /** See 'reduce_perfect_pairs'. */ + std::vector perfect_pairs_; + + /** See 'proven_infeasible()'. */ + bool proven_infeasible_ = false; + + /** + * The bin shrunk along each axis (see the class-level doc comment), + * each length being increased by the cut thickness. + */ + Rectangle shrunk_bin( + const std::vector& reduction_item_types) const; + + /** + * Reserves a dedicated bin for every copy of every item type filling + * the shrunk bin. Returns 'true' iff at least one item type was + * reserved. + */ + bool reduce_full_bin_items( + std::vector& reduction_item_types); + + /** + * Reserves a dedicated bin for 'min(copies_1, copies_2)' copies of every + * pair of oriented item types filling the shrunk bin together + * (including two copies of the same item type). Returns 'true' iff at + * least one pair was reserved. + */ + bool reduce_perfect_pairs( + std::vector& reduction_item_types); + + /** + * True dimensions of the original item type 'item_type_id', in the + * orientation of the reduced item type (see 'swapped_item_types_'). + */ + Rectangle true_rect(ItemTypeId item_type_id) const; + /** * One contiguous run of a reduced item type's copies that all came * from the same original item type - the survivor's own copies form diff --git a/python/src/box.cpp b/python/src/box.cpp index 73f3e5ebb..092f1ca29 100644 --- a/python/src/box.cpp +++ b/python/src/box.cpp @@ -337,7 +337,9 @@ void bind_box(nb::module_& m) .def_rw("merge_identical_items", &ReductionParameters::merge_identical_items) .def_rw("lift_item_dimensions", &ReductionParameters::lift_item_dimensions) .def_rw("remove_dominated_items", &ReductionParameters::remove_dominated_items) - .def_rw("remove_dominated_bin_types", &ReductionParameters::remove_dominated_bin_types); + .def_rw("remove_dominated_bin_types", &ReductionParameters::remove_dominated_bin_types) + .def_rw("reduce_full_bin_items", &ReductionParameters::reduce_full_bin_items) + .def_rw("reduce_perfect_pairs", &ReductionParameters::reduce_perfect_pairs); nb::class_ parameters = bind_parameters_base(m); parameters diff --git a/python/src/rectangleguillotine.cpp b/python/src/rectangleguillotine.cpp index d442f7dab..0d77c2191 100644 --- a/python/src/rectangleguillotine.cpp +++ b/python/src/rectangleguillotine.cpp @@ -471,7 +471,9 @@ void bind_rectangleguillotine(nb::module_& m) .def_rw("merge_identical_items", &ReductionParameters::merge_identical_items) .def_rw("lift_item_dimensions", &ReductionParameters::lift_item_dimensions) .def_rw("remove_dominated_items", &ReductionParameters::remove_dominated_items) - .def_rw("remove_dominated_bin_types", &ReductionParameters::remove_dominated_bin_types); + .def_rw("remove_dominated_bin_types", &ReductionParameters::remove_dominated_bin_types) + .def_rw("reduce_full_bin_items", &ReductionParameters::reduce_full_bin_items) + .def_rw("reduce_perfect_pairs", &ReductionParameters::reduce_perfect_pairs); nb::class_ parameters = bind_parameters_base(m); parameters diff --git a/src/box/optimize.cpp b/src/box/optimize.cpp index 96a6a77b0..36c7ebf51 100644 --- a/src/box/optimize.cpp +++ b/src/box/optimize.cpp @@ -426,18 +426,31 @@ packingsolver::box::Output packingsolver::box::optimize( reduction_parameters.timer = parameters.timer; Reduction reduction(instance, reduction_parameters); + if (reduction.proven_infeasible()) { + // The dedicated bins (see 'Reduction::reduce_full_bin_items'/ + // 'reduce_perfect_pairs') take every copy of the bin type while + // items remain: 'reduction.instance()' is not meaningful. + algorithm_formatter.update_is_proven_infeasible(); + algorithm_formatter.end(); + return output; + } + // Forwards a solution/bound found for the reduced instance to the // original 'algorithm_formatter', in original-instance - // coordinates. Every bound needs no reduction-specific - // translation: removing/merging items via this reduction never - // changes them (cost, feasibility, ...). + // coordinates. Removing/merging items via this reduction never + // changes the bounds (cost, feasibility, ...), except the dedicated + // bins (see 'Reduction::number_of_dedicated_bins', 'BinPacking' + // only), absent from the reduced instance: they are added back onto + // the bin packing bound. auto report_reduced_output = [&reduction, &algorithm_formatter]( const box::Output& reduced_output) { algorithm_formatter.update_solution( reduction.unreduce_solution(reduced_output.solution_pool.best()), reduced_output.solution_pool.best_label()); - algorithm_formatter.update_bounds(reduced_output); + box::Output translated_output(reduced_output); + translated_output.bin_packing_bound += reduction.number_of_dedicated_bins(); + algorithm_formatter.update_bounds(translated_output); }; OptimizeParameters reduced_parameters = parameters; diff --git a/src/box/reduction.cpp b/src/box/reduction.cpp index 723883cee..4731139ad 100644 --- a/src/box/reduction.cpp +++ b/src/box/reduction.cpp @@ -112,19 +112,29 @@ Reduction::Reduction( // Lifting can make more rotations redundant (two rotations giving the // same enlarged box, or a rotation of the enlarged box fitting in no // bin type), and removing rotations can enable lifting (it requires - // every allowed rotation to put the same dimension along the axis). - if (parameters.lift_item_dimensions - && lift_item_dimensions_applies(instance)) { - for (;;) { - if (parameters.timer.needs_to_end()) - break; - if (!lift_item_dimensions(reduction_item_types)) - break; - if (!parameters.remove_redundant_rotations - || !remove_redundant_rotations(reduction_item_types)) { - break; - } - } + // every allowed rotation to put the same dimension along the axis) and + // perfect pairs (they require a single allowed rotation). Lifting can + // make an item fill the shrunk bin, and a dedicated bin can shrink the + // bin further. + bool run_lift = parameters.lift_item_dimensions + && lift_item_dimensions_applies(instance); + bool full_bin_reduction = full_bin_reduction_applies(instance); + bool run_full_bin_items = full_bin_reduction && parameters.reduce_full_bin_items; + bool run_perfect_pairs = full_bin_reduction && parameters.reduce_perfect_pairs; + while (run_lift || run_full_bin_items || run_perfect_pairs) { + if (parameters.timer.needs_to_end()) + break; + bool found = false; + if (run_lift) + found |= lift_item_dimensions(reduction_item_types); + if (found && parameters.remove_redundant_rotations) + remove_redundant_rotations(reduction_item_types); + if (run_full_bin_items) + found |= reduce_full_bin_items(reduction_item_types); + if (run_perfect_pairs) + found |= reduce_perfect_pairs(reduction_item_types); + if (!found) + break; } if (parameters.merge_identical_items) @@ -576,6 +586,167 @@ std::vector Reduction::compute_dominated_bin_types() const return dominated; } +bool Reduction::full_bin_reduction_applies(const Instance& instance) +{ + if (instance.objective() != Objective::BinPacking) + return false; + // A single bin type, so that the bin of the reservations is + // unambiguous, without fixed items (they would share the dedicated + // bins). + if (instance.number_of_bin_types() != 1) + return false; + const BinType& bin_type = instance.bin_type(0); + if (!bin_type.fixed_items.empty()) + return false; + // The exchange arguments move items between bins: a weight or resource + // constraint, a whole-bin aggregate, could be violated. + if (instance.resources_matter()) + return false; + if (bin_type.maximum_weight != std::numeric_limits::infinity()) { + for (ItemTypeId item_type_id = 0; + item_type_id < instance.number_of_item_types(); + ++item_type_id) { + if (instance.item_type(item_type_id).weight != 0) + return false; + } + } + return true; +} + +BinPos Reduction::number_of_dedicated_bins() const +{ + BinPos total = 0; + for (const FullBinItem& full_bin_item: full_bin_items_) + total += full_bin_item.copies; + for (const PerfectPair& pair: perfect_pairs_) + total += pair.copies; + return total; +} + +bool Reduction::reduce_full_bin_items( + std::vector& reduction_item_types) +{ + const BinType& bin_type = original_instance_->bin_type(0); + bool found = false; + for (ItemTypeId item_type_id = 0; + item_type_id < (ItemTypeId)reduction_item_types.size(); + ++item_type_id) { + ReductionItemType& item = reduction_item_types[item_type_id]; + if (item.removed || item.copies == 0) + continue; + if (original_instance_->item_type(item_type_id).copies_fixed > 0) + continue; + // Recomputed for each item type, since each reservation removes + // items and can shrink the bin. + Box shrunk_bin = { + shrunk_bin_length(reduction_item_types, 0), + shrunk_bin_length(reduction_item_types, 1), + shrunk_bin_length(reduction_item_types, 2)}; + bool matches = false; + Rotation rotation = Rotation::XYZ; + for (Rotation r: item.rotations) { + if (item.box.rotate(r) == shrunk_bin) { + matches = true; + rotation = r; + break; + } + } + if (!matches) + continue; + // Never more dedicated bins than the bin type has copies. + if (number_of_dedicated_bins() + item.copies > bin_type.copies) + continue; + full_bin_items_.push_back({item_type_id, rotation, item.copies}); + item.copies = 0; + item.removed = true; + found = true; + } + return found; +} + +bool Reduction::reduce_perfect_pairs( + std::vector& reduction_item_types) +{ + const BinType& bin_type = original_instance_->bin_type(0); + bool found = false; + for (ItemTypeId item_type_id_1 = 0; + item_type_id_1 < (ItemTypeId)reduction_item_types.size(); + ++item_type_id_1) { + ReductionItemType& item_1 = reduction_item_types[item_type_id_1]; + if (item_1.removed || item_1.copies == 0 || item_1.rotations.size() != 1) + continue; + if (original_instance_->item_type(item_type_id_1).copies_fixed > 0) + continue; + for (ItemTypeId item_type_id_2 = item_type_id_1; + item_type_id_2 < (ItemTypeId)reduction_item_types.size(); + ++item_type_id_2) { + ReductionItemType& item_2 = reduction_item_types[item_type_id_2]; + if (item_2.removed || item_2.copies == 0 || item_2.rotations.size() != 1) + continue; + if (original_instance_->item_type(item_type_id_2).copies_fixed > 0) + continue; + // Two copies of the same item type. + if (item_type_id_2 == item_type_id_1 && item_1.copies < 2) + continue; + + Box shrunk_bin = { + shrunk_bin_length(reduction_item_types, 0), + shrunk_bin_length(reduction_item_types, 1), + shrunk_bin_length(reduction_item_types, 2)}; + Box box_1 = item_1.box.rotate(item_1.rotations.front()); + Box box_2 = item_2.box.rotate(item_2.rotations.front()); + int pair_axis = -1; + for (int axis = 0; axis < 3; ++axis) { + bool other_axes_full = true; + for (int other_axis = 0; other_axis < 3; ++other_axis) { + if (other_axis == axis) + continue; + if (box_length(box_1, other_axis) != box_length(shrunk_bin, other_axis) + || box_length(box_2, other_axis) != box_length(shrunk_bin, other_axis)) { + other_axes_full = false; + } + } + if (other_axes_full + && box_length(box_1, axis) + box_length(box_2, axis) + == box_length(shrunk_bin, axis)) { + pair_axis = axis; + break; + } + } + if (pair_axis == -1) + continue; + + ItemPos copies = (item_type_id_2 == item_type_id_1)? + item_1.copies / 2: + (std::min)(item_1.copies, item_2.copies); + // Never more dedicated bins than the bin type has copies. + if (number_of_dedicated_bins() + copies > bin_type.copies) + continue; + perfect_pairs_.push_back({ + item_type_id_1, + item_type_id_2, + item_1.rotations.front(), + item_2.rotations.front(), + pair_axis, + copies}); + if (item_type_id_2 == item_type_id_1) { + item_1.copies -= 2 * copies; + } else { + item_1.copies -= copies; + item_2.copies -= copies; + if (item_2.copies == 0) + item_2.removed = true; + } + if (item_1.copies == 0) + item_1.removed = true; + found = true; + if (item_1.removed) + break; + } + } + return found; +} + bool Reduction::lift_item_dimensions_applies(const Instance& instance) { // A single bin type, so that the bin dimensions are unambiguous, and @@ -827,8 +998,29 @@ Instance Reduction::reduction_to_instance( && bin_type_removed[bin_type_id]) { continue; } - instance_builder.add_bin_type(*original_instance_, bin_type_id); + BinTypeId new_bin_type_id = instance_builder.add_bin_type(*original_instance_, bin_type_id); reduced_to_original_bin_type_id_.push_back(bin_type_id); + // The dedicated bins (see 'reduce_full_bin_items'/ + // 'reduce_perfect_pairs', single bin type) are left out of the + // reduced instance. They never take more than the bin type's + // copies; if they take all of them while items remain, the + // instance is infeasible. + BinPos number_of_dedicated_bins = this->number_of_dedicated_bins(); + if (number_of_dedicated_bins > 0) { + BinPos copies = original_instance_->bin_type(bin_type_id).copies - number_of_dedicated_bins; + if (copies > 0) { + instance_builder.set_bin_type_copies(new_bin_type_id, copies); + } else { + bool remaining_items = false; + for (const ReductionItemType& item: reduction_item_types) + if (item.copies > 0) + remaining_items = true; + // Without remaining items, the bin type keeps its copies, + // never used. + if (remaining_items) + proven_infeasible_ = true; + } + } } reduced_item_origin_runs_.clear(); @@ -998,5 +1190,39 @@ Solution Reduction::unreduce_solution( slot_count[reduced_item_type_id] = 0; } + // The dedicated bins, left out of 'instance_' (single bin type, see + // 'full_bin_reduction_applies'). A pair is placed side by side along + // its axis, with the true boxes of the items. + for (const FullBinItem& full_bin_item: full_bin_items_) { + BinPos new_bin_pos = solution_builder.add_bin(0, full_bin_item.copies); + solution_builder.add_item( + new_bin_pos, + full_bin_item.item_type_id, + {0, 0, 0}, + full_bin_item.rotation); + } + for (const PerfectPair& pair: perfect_pairs_) { + BinPos new_bin_pos = solution_builder.add_bin(0, pair.copies); + solution_builder.add_item( + new_bin_pos, + pair.item_type_id_1, + {0, 0, 0}, + pair.rotation_1); + Box box_1 = original_instance_->item_type(pair.item_type_id_1).box.rotate(pair.rotation_1); + Point position = {0, 0, 0}; + if (pair.axis == 0) { + position.x = box_1.x; + } else if (pair.axis == 1) { + position.y = box_1.y; + } else { + position.z = box_1.z; + } + solution_builder.add_item( + new_bin_pos, + pair.item_type_id_2, + position, + pair.rotation_2); + } + return solution_builder.build(); } diff --git a/src/rectangleguillotine/optimize.cpp b/src/rectangleguillotine/optimize.cpp index ebac2a426..a17d693b9 100644 --- a/src/rectangleguillotine/optimize.cpp +++ b/src/rectangleguillotine/optimize.cpp @@ -700,13 +700,22 @@ packingsolver::rectangleguillotine::Output packingsolver::rectangleguillotine::o reduction_parameters.timer = parameters.timer; Reduction reduction(instance, reduction_parameters); + if (reduction.proven_infeasible()) { + // The dedicated bins (see 'Reduction::reduce_full_bin_items'/ + // 'reduce_perfect_pairs') take every copy of the bin type while + // items remain: 'reduction.instance()' is not meaningful. + algorithm_formatter.update_is_proven_infeasible(); + algorithm_formatter.end(); + return output; + } + // Forwards a solution/bound found for the reduced instance to the // original 'algorithm_formatter', in original-instance - // coordinates. Unlike 'rectangle::Reduction', this class never - // sets aside dedicated bins or proves infeasibility on its own, - // so every bound of 'reduced_output' already is the original - // instance's own bound, in exactly 'Output's own field layout - - // no translation needed. + // coordinates. The bounds of 'reduced_output' are the original + // instance's own bounds, except the dedicated bins (see + // 'Reduction::number_of_dedicated_bins', 'BinPacking' only), + // absent from the reduced instance: they are added back onto the + // bin packing bound. auto report_reduced_output = [&reduction, &algorithm_formatter]( const rectangleguillotine::Output& reduced_output) { @@ -735,7 +744,9 @@ packingsolver::rectangleguillotine::Output packingsolver::rectangleguillotine::o reduction.unreduce_solution(reduced_solution), reduced_output.solution_pool.best_label()); } - algorithm_formatter.update_bounds(reduced_output); + rectangleguillotine::Output translated_output(reduced_output); + translated_output.bin_packing_bound += reduction.number_of_dedicated_bins(); + algorithm_formatter.update_bounds(translated_output); }; OptimizeParameters reduced_parameters = parameters; diff --git a/src/rectangleguillotine/reduction.cpp b/src/rectangleguillotine/reduction.cpp index 487b78c88..2568871d5 100644 --- a/src/rectangleguillotine/reduction.cpp +++ b/src/rectangleguillotine/reduction.cpp @@ -33,6 +33,20 @@ const std::vector* find_item_type_schedule( return nullptr; } +/** + * 'true' iff the nodes at depth 'depth' are delimited along 'x', i.e. are + * the result of cuts along 'x' ('l' and 'r' are cut positions), for a + * first cut orientation 'first_cut_orientation' ('Vertical' or + * 'Horizontal'). Mirrors 'SolutionBuilder::add_node'. + */ +bool delimited_along_x( + CutOrientation first_cut_orientation, + Depth depth) +{ + return (first_cut_orientation == CutOrientation::Vertical && depth % 2 == 1) + || (first_cut_orientation == CutOrientation::Horizontal && depth % 2 == 0); +} + } bool Reduction::remove_negative_profit_items_applies(const Instance& instance) @@ -161,6 +175,214 @@ void Reduction::remove_negative_profit_items( } } +bool Reduction::full_bin_reduction_applies(const Instance& instance) +{ + if (instance.objective() != Objective::BinPacking) + return false; + // A single bin type, so that the bin of the reservations is + // unambiguous, without trims (they would complicate the cut trees of + // the dedicated bins), defects, fixed items (they would share the + // dedicated bins) or resources (a whole-bin aggregate the exchange + // arguments could violate). + if (instance.number_of_bin_types() != 1) + return false; + const BinType& bin_type = instance.bin_type(0); + if (bin_type.left_trim != 0 + || bin_type.right_trim != 0 + || bin_type.bottom_trim != 0 + || bin_type.top_trim != 0) { + return false; + } + if (!bin_type.fixed_items.empty() || bin_type.number_of_resources() > 0) + return false; + // The item of a dedicated bin can be smaller than the bin, leaving a + // waste, and the exchange arguments compact the solutions (defects are + // checked there too). + if (!items_can_shrink(instance)) + return false; + // The node of the item of a dedicated bin can be a plate. + const Parameters& parameters = instance.parameters(); + if (parameters.maximum_distance_1_cuts >= 0 + || parameters.maximum_distance_2_cuts >= 0) { + return false; + } + return true; +} + +BinPos Reduction::number_of_dedicated_bins() const +{ + BinPos total = 0; + for (const FullBinItem& full_bin_item: full_bin_items_) + total += full_bin_item.copies; + for (const PerfectPair& pair: perfect_pairs_) + total += pair.copies; + return total; +} + +Rectangle Reduction::shrunk_bin( + const std::vector& reduction_item_types) const +{ + const BinType& bin_type = original_instance_->bin_type(0); + Length cut_thickness = original_instance_->parameters().cut_thickness; + Rectangle rect; + rect.w = max_achievable_dimension_sum( + reduction_item_types, + bin_type.rect.w + cut_thickness, + 0, + cut_thickness, + -1); + rect.h = max_achievable_dimension_sum( + reduction_item_types, + bin_type.rect.h + cut_thickness, + 1, + cut_thickness, + -1); + return rect; +} + +Rectangle Reduction::true_rect(ItemTypeId item_type_id) const +{ + Rectangle rect = original_instance_->item_type(item_type_id).rect; + if (swapped_item_types_[item_type_id]) + std::swap(rect.w, rect.h); + return rect; +} + +bool Reduction::reduce_full_bin_items( + std::vector& reduction_item_types) +{ + const BinType& bin_type = original_instance_->bin_type(0); + Length cut_thickness = original_instance_->parameters().cut_thickness; + bool found = false; + for (ItemTypeId item_type_id = 0; + item_type_id < (ItemTypeId)reduction_item_types.size(); + ++item_type_id) { + ReductionItemType& item = reduction_item_types[item_type_id]; + if (item.removed || item.copies == 0) + continue; + const ItemType& item_type = original_instance_->item_type(item_type_id); + if (item_type.copies_fixed > 0) + continue; + if (original_instance_->stack_size(item_type.stack_id) != item_type.copies) + continue; + // Recomputed for each item type, since each reservation removes + // items and can shrink the bin. If the item fills it in one of its + // orientations, it fills it in any orientation fitting the bin. + Rectangle bin = shrunk_bin(reduction_item_types); + bool matches = (item.rect.w + cut_thickness == bin.w + && item.rect.h + cut_thickness == bin.h) + || (!item.oriented + && item.rect.h + cut_thickness == bin.w + && item.rect.w + cut_thickness == bin.h); + if (!matches) + continue; + // Never more dedicated bins than the bin type has copies. + if (number_of_dedicated_bins() + item.copies > bin_type.copies) + continue; + full_bin_items_.push_back({item_type_id, item.copies}); + item.copies = 0; + item.removed = true; + found = true; + } + return found; +} + +bool Reduction::reduce_perfect_pairs( + std::vector& reduction_item_types) +{ + const BinType& bin_type = original_instance_->bin_type(0); + const Parameters& parameters = original_instance_->parameters(); + Length cut_thickness = parameters.cut_thickness; + CutOrientation first_cut_orientation = (parameters.first_stage_orientation == CutOrientation::Any)? + CutOrientation::Vertical: + parameters.first_stage_orientation; + // Deepest level of a node of the cut tree of a dedicated bin. + Depth maximum_depth = (parameters.cut_type == CutType::Exact)? + parameters.number_of_stages: + parameters.number_of_stages + 1; + auto item_ok = [this, &reduction_item_types](ItemTypeId item_type_id) + { + const ReductionItemType& item = reduction_item_types[item_type_id]; + const ItemType& item_type = original_instance_->item_type(item_type_id); + return !item.removed + && item.copies > 0 + && item.oriented + && item_type.copies_fixed == 0 + && original_instance_->stack_size(item_type.stack_id) == item_type.copies; + }; + + bool found = false; + for (ItemTypeId item_type_id_1 = 0; + item_type_id_1 < (ItemTypeId)reduction_item_types.size(); + ++item_type_id_1) { + if (!item_ok(item_type_id_1)) + continue; + ReductionItemType& item_1 = reduction_item_types[item_type_id_1]; + for (ItemTypeId item_type_id_2 = item_type_id_1; + item_type_id_2 < (ItemTypeId)reduction_item_types.size(); + ++item_type_id_2) { + if (!item_ok(item_type_id_2)) + continue; + ReductionItemType& item_2 = reduction_item_types[item_type_id_2]; + // Two copies of the same item type. + if (item_type_id_2 == item_type_id_1 && item_1.copies < 2) + continue; + + Rectangle bin = shrunk_bin(reduction_item_types); + int pair_axis = -1; + if (item_1.rect.h + cut_thickness == bin.h + && item_2.rect.h + cut_thickness == bin.h + && item_1.rect.w + cut_thickness + item_2.rect.w + cut_thickness == bin.w) { + pair_axis = 0; + } else if (item_1.rect.w + cut_thickness == bin.w + && item_2.rect.w + cut_thickness == bin.w + && item_1.rect.h + cut_thickness + item_2.rect.h + cut_thickness == bin.h) { + pair_axis = 1; + } + if (pair_axis == -1) + continue; + + // If the first stage is not along the axis of the pair, the + // items are second-stage nodes, which may need a trimming cut + // (see 'unreduce_solution'). + if (delimited_along_x(first_cut_orientation, 1) != (pair_axis == 0)) { + Rectangle rect_1 = original_instance_->item_type(item_type_id_1).rect; + Rectangle rect_2 = original_instance_->item_type(item_type_id_2).rect; + if (item_1.swapped) + std::swap(rect_1.w, rect_1.h); + if (item_2.swapped) + std::swap(rect_2.w, rect_2.h); + Length length_1 = (pair_axis == 0)? rect_1.h: rect_1.w; + Length length_2 = (pair_axis == 0)? rect_2.h: rect_2.w; + if (length_1 != length_2 && maximum_depth < 3) + continue; + } + + ItemPos copies = (item_type_id_2 == item_type_id_1)? + item_1.copies / 2: + (std::min)(item_1.copies, item_2.copies); + // Never more dedicated bins than the bin type has copies. + if (number_of_dedicated_bins() + copies > bin_type.copies) + continue; + perfect_pairs_.push_back({item_type_id_1, item_type_id_2, pair_axis, copies}); + if (item_type_id_2 == item_type_id_1) { + item_1.copies -= 2 * copies; + } else { + item_1.copies -= copies; + item_2.copies -= copies; + if (item_2.copies == 0) + item_2.removed = true; + } + if (item_1.copies == 0) + item_1.removed = true; + found = true; + if (item_1.removed) + break; + } + } + return found; +} + bool Reduction::lift_item_dimensions_applies(const Instance& instance) { // Objectives only depending on which items and bins are used (see the @@ -197,25 +419,6 @@ bool Reduction::lift_item_dimensions_applies(const Instance& instance) return true; } -namespace -{ - -/** - * 'true' iff the nodes at depth 'depth' are delimited along 'x', i.e. are - * the result of cuts along 'x' ('l' and 'r' are cut positions), for a - * first cut orientation 'first_cut_orientation' ('Vertical' or - * 'Horizontal'). Mirrors 'SolutionBuilder::add_node'. - */ -bool delimited_along_x( - CutOrientation first_cut_orientation, - Depth depth) -{ - return (first_cut_orientation == CutOrientation::Vertical && depth % 2 == 1) - || (first_cut_orientation == CutOrientation::Horizontal && depth % 2 == 0); -} - -} - bool Reduction::lift_item_dimensions_applies_axis( const Instance& instance, int axis) @@ -897,6 +1100,29 @@ Instance Reduction::reduction_to_instance( original_to_reduced_bin_type_id[bin_type_id] = instance_builder.add_bin_type(*original_instance_, bin_type_id); reduced_to_original_bin_type_id_.push_back(bin_type_id); + // The dedicated bins (see 'reduce_full_bin_items'/ + // 'reduce_perfect_pairs', single bin type) are left out of the + // reduced instance. They never take more than the bin type's + // copies; if they take all of them while items remain, the + // instance is infeasible. + BinPos number_of_dedicated_bins = this->number_of_dedicated_bins(); + if (number_of_dedicated_bins > 0) { + BinPos copies = original_instance_->bin_type(bin_type_id).copies - number_of_dedicated_bins; + if (copies > 0) { + instance_builder.set_bin_type_copies( + original_to_reduced_bin_type_id[bin_type_id], + copies); + } else { + bool remaining_items = false; + for (const ReductionItemType& item: reduction_item_types) + if (item.copies > 0) + remaining_items = true; + // Without remaining items, the bin type keeps its copies, + // never used. + if (remaining_items) + proven_infeasible_ = true; + } + } } // Renumber the stacks of the remaining item types consecutively. The @@ -1023,6 +1249,21 @@ Reduction::Reduction( && !parameters.timer.needs_to_end()) { lift_item_dimensions(reduction_item_types); } + // After lifting, which can make an item fill the shrunk bin. + // Reserving a dedicated bin can shrink the bin further. + if (full_bin_reduction_applies(instance)) { + for (;;) { + if (parameters.timer.needs_to_end()) + break; + bool found = false; + if (parameters.reduce_full_bin_items) + found |= reduce_full_bin_items(reduction_item_types); + if (parameters.reduce_perfect_pairs) + found |= reduce_perfect_pairs(reduction_item_types); + if (!found) + break; + } + } if (parameters.merge_identical_items) merge_identical_items(reduction_item_types); } @@ -1397,5 +1638,58 @@ Solution Reduction::unreduce_solution( slot_count[reduced_item_type_id] = 0; } + // The dedicated bins, left out of 'instance_' (single bin type without + // trims, see 'full_bin_reduction_applies'), with the true dimensions of + // the items. Items are placed side by side along an axis from the + // origin; a node larger than its item gets a trimming child. + { + const BinType& bin_type = original_instance_->bin_type(0); + Length cut_thickness = parameters.cut_thickness; + CutOrientation first_cut_orientation = (parameters.first_stage_orientation == CutOrientation::Any)? + CutOrientation::Vertical: + parameters.first_stage_orientation; + auto add_dedicated_bin = [&]( + BinPos copies, + const std::vector& item_type_ids, + int axis) + { + bool along_x = (axis == 0); + solution_builder.add_bin(0, copies, first_cut_orientation); + // Length of the bin along the other axis, and of the items. + Length bin_other = (along_x)? bin_type.rect.h: bin_type.rect.w; + Length items_other = 0; + for (ItemTypeId item_type_id: item_type_ids) { + Rectangle rect = true_rect(item_type_id); + items_other = (std::max)(items_other, (along_x)? rect.h: rect.w); + } + // First-stage nodes along the axis, or a single first-stage + // node holding second-stage nodes along it. + bool first_stage = (delimited_along_x(first_cut_orientation, 1) == along_x); + Depth depth = (first_stage)? 1: 2; + Length node_other = (first_stage)? bin_other: items_other; + if (!first_stage) + solution_builder.add_node(1, items_other); + Length start = 0; + for (ItemTypeId item_type_id: item_type_ids) { + Rectangle rect = true_rect(item_type_id); + Length length = (along_x)? rect.w: rect.h; + Length other = (along_x)? rect.h: rect.w; + solution_builder.add_node(depth, start + length); + if (other < node_other) + solution_builder.add_node(depth + 1, other); + solution_builder.set_last_node_item(item_type_id); + start += length + cut_thickness; + } + }; + for (const FullBinItem& full_bin_item: full_bin_items_) + add_dedicated_bin(full_bin_item.copies, {full_bin_item.item_type_id}, 0); + for (const PerfectPair& pair: perfect_pairs_) { + add_dedicated_bin( + pair.copies, + {pair.item_type_id_1, pair.item_type_id_2}, + pair.axis); + } + } + return solution_builder.build(); } diff --git a/test/box/reduction_test.cpp b/test/box/reduction_test.cpp index 7ea1b9d54..ce8ab1325 100644 --- a/test/box/reduction_test.cpp +++ b/test/box/reduction_test.cpp @@ -61,6 +61,19 @@ void expect_instances_equal(const Instance& actual, const Instance& expected) } } +/** + * The default parameters, without the dedicated-bin reservations, for the + * scenarios testing other operations on instances they would reduce + * entirely. + */ +ReductionParameters parameters_without_dedicated_bins() +{ + ReductionParameters parameters; + parameters.reduce_full_bin_items = false; + parameters.reduce_perfect_pairs = false; + return parameters; +} + } /** @@ -300,7 +313,7 @@ INSTANTIATE_TEST_SUITE_P( // merged pattern was reused across more than one physical // bin. fs::path("data") / "box" / "tests" / "bin_packing_merge_identical_items_unequal_copies_produces_compact_groups", - ReductionParameters(), + parameters_without_dedicated_bins(), false, false, false, @@ -334,7 +347,7 @@ INSTANTIATE_TEST_SUITE_P( // lifted to 7x10x10 (nothing else fits beside it). Both // item types are then identical and merge. fs::path("data") / "box" / "tests" / "bin_packing_lift_item_dimensions", - ReductionParameters(), + parameters_without_dedicated_bins(), }, { // Bin 10x10x10. Item type 7x10x10 with one copy, item type // 4x10x10 with two copies, oriented. Along 'x', the bin is @@ -343,7 +356,7 @@ INSTANTIATE_TEST_SUITE_P( // it). The 4x10x10 item type has two copies, so it is not // lifted. fs::path("data") / "box" / "tests" / "bin_packing_lift_item_dimensions_requires_single_copy", - ReductionParameters(), + parameters_without_dedicated_bins(), }, { // Bin 10x10x10. Item type 7x8x4 allowing 'XYZ' and 'YXZ', // item type 4x4x7 oriented. The first item type is only @@ -351,7 +364,7 @@ INSTANTIATE_TEST_SUITE_P( // the same dimension along, to 7. The second item type is // lifted along 'x' and 'y' to 8 (the shrunk bin). fs::path("data") / "box" / "tests" / "bin_packing_lift_item_dimensions_rotations", - ReductionParameters(), + parameters_without_dedicated_bins(), }, { // 'OpenDimensionX', bin 12x10x10. Item types 7x10x10, // 3x10x10 and 5x10x10, oriented, one copy each. Along 'x', @@ -385,5 +398,25 @@ INSTANTIATE_TEST_SUITE_P( // instance, and 'unreduce_solution' maps the bin type back. fs::path("data") / "box" / "tests" / "variable_sized_bin_packing_remove_dominated_bin_types", ReductionParameters(), + }, { + // 'BinPacking', bin 10x10x10, 5 copies. Item type 0 + // (10x10x10, 2 copies) fills the bin: 2 dedicated bins, the + // reduced instance keeps 3 bins for item type 1 (5x5x5, 3 + // copies). + fs::path("data") / "box" / "tests" / "bin_packing_full_bin_items", + ReductionParameters(), + }, { + // 'BinPacking', bin 10x10x10, 5 copies. Item types 0 + // (6x10x10) and 1 (4x10x10), oriented, 2 copies each, fill + // the bin together along 'x': 2 dedicated bins, the reduced + // instance keeps 3 bins for item type 2 (3x3x3, 2 copies). + fs::path("data") / "box" / "tests" / "bin_packing_perfect_pairs", + ReductionParameters(), + }, { + // Same as 'bin_packing_full_bin_items', with a maximum + // weight and weighted items: moving items between bins could + // break the weight constraint, nothing is reserved. + fs::path("data") / "box" / "tests" / "bin_packing_full_bin_items_not_reserved_with_weight", + ReductionParameters(), }, })); diff --git a/test/rectangleguillotine/reduction_test.cpp b/test/rectangleguillotine/reduction_test.cpp index 24d24f13c..0c1faf10e 100644 --- a/test/rectangleguillotine/reduction_test.cpp +++ b/test/rectangleguillotine/reduction_test.cpp @@ -49,6 +49,18 @@ void expect_instances_equal(const Instance& actual, const Instance& expected) EXPECT_EQ(actual.number_of_stacks(), expected.number_of_stacks()); } +/** + * The default parameters, without the dedicated-bin reservations, for the + * scenarios testing other operations on instances they would reduce. + */ +ReductionParameters parameters_without_dedicated_bins() +{ + ReductionParameters parameters; + parameters.reduce_full_bin_items = false; + parameters.reduce_perfect_pairs = false; + return parameters; +} + } /** @@ -171,7 +183,9 @@ TEST(RectangleGuillotineReduction, MergeUnequalCopiesProducesCompactGroups) instance_builder.set_first_stage_orientation(CutOrientation::Vertical); Instance instance = instance_builder.build(); - Reduction reduction(instance); + // Each item fills the bin: without the dedicated-bin reservations, which + // would take them all. + Reduction reduction(instance, parameters_without_dedicated_bins()); const Instance& reduced_instance = reduction.instance(); ASSERT_EQ(reduced_instance.number_of_item_types(), 1); ASSERT_EQ(reduced_instance.item_type(0).copies, 50); @@ -294,7 +308,7 @@ INSTANTIATE_TEST_SUITE_P( // exercises that replay logic directly, with a bin that // places more than one item. fs::path("data") / "rectangleguillotine" / "tests" / "bin_packing_merge_identical_items_unreduce_solution_round_trip", - ReductionParameters(), + parameters_without_dedicated_bins(), }, { // 'rectangleguillotine'-specific: no 'rectangle' equivalent. // Bin 20x10. Two item types, both 10x10, oriented, @@ -350,7 +364,7 @@ INSTANTIATE_TEST_SUITE_P( // item type's own copies instead. They still merge into a // single reduced item type with the combined 5 copies. fs::path("data") / "rectangleguillotine" / "tests" / "bin_packing_merge_identical_items_alone_in_stack_with_multiple_copies", - ReductionParameters(), + parameters_without_dedicated_bins(), true, }, { // Bin 100x100. Item type 0 (10x10) is a square: its @@ -366,21 +380,21 @@ INSTANTIATE_TEST_SUITE_P( // the solution of the reduced instance accept the original // item type rotated. fs::path("data") / "rectangleguillotine" / "tests" / "remove_redundant_rotations_rotation_fits_no_bin", - ReductionParameters(), + parameters_without_dedicated_bins(), }, { // Bin 100x40. Item type 0 (30x45) only fits rotated: it // becomes an oriented 45x30 item type. Item type 1 (45x30) // only fits unrotated: it becomes oriented. Both are then // identical and merge. fs::path("data") / "rectangleguillotine" / "tests" / "bin_packing_merge_identical_items_after_removing_redundant_rotations", - ReductionParameters(), + parameters_without_dedicated_bins(), }, { // Bin 10x10, 'NonExact'. Item types 7x10 and 4x10, oriented, // one copy each: they can't share a bin, so the bin is shrunk // along 'x' to 7, and the 4x10 item type is lifted to 7x10. // Both item types are then identical and merge. fs::path("data") / "rectangleguillotine" / "tests" / "bin_packing_lift_item_dimensions", - ReductionParameters(), + parameters_without_dedicated_bins(), }, { // Bin 10x10, 'NonExact'. Item types 7x6 and 4x5, oriented, // one copy each: the 4x5 item type is lifted to 7 along 'x' @@ -388,20 +402,20 @@ INSTANTIATE_TEST_SUITE_P( // solution of the reduced instance needs its cut tree to be // rewritten along both axes. fs::path("data") / "rectangleguillotine" / "tests" / "bin_packing_lift_item_dimensions_both_axes", - ReductionParameters(), + parameters_without_dedicated_bins(), }, { // Same items as above, 2 stages, 'Exact', vertical first // stage. Only the axis of the 2-cuts ('y') can be lifted: // the 4x5 item type is lifted to 4x6. fs::path("data") / "rectangleguillotine" / "tests" / "bin_packing_lift_item_dimensions_exact", - ReductionParameters(), + parameters_without_dedicated_bins(), }, { // Bin 10x10, cut thickness 1. Item types 6x10 and 4x10, // oriented, one copy each: with the cut thickness, they // can't share a bin (6 + 1 + 4 > 10), so the 4x10 item // type is lifted to 6x10, and merges with the other one. fs::path("data") / "rectangleguillotine" / "tests" / "bin_packing_lift_item_dimensions_cut_thickness", - ReductionParameters(), + parameters_without_dedicated_bins(), }, { // Same as 'bin_packing_lift_item_dimensions', with a // minimum waste length: nothing is lifted. @@ -439,5 +453,23 @@ INSTANTIATE_TEST_SUITE_P( // and 'unreduce_solution' maps the bin type back. fs::path("data") / "rectangleguillotine" / "tests" / "variable_sized_bin_packing_remove_dominated_bin_types", ReductionParameters(), + }, { + // 'BinPacking', bin 10x10, 5 copies. Item type 0 (10x10, 2 + // copies) fills the bin: 2 dedicated bins, the reduced + // instance keeps 3 bins for item type 1 (5x5, 3 copies). + fs::path("data") / "rectangleguillotine" / "tests" / "bin_packing_full_bin_items", + ReductionParameters(), + }, { + // 'BinPacking', bin 10x10, 5 copies. Item types 0 (6x10) + // and 1 (4x10), oriented, 2 copies each, fill the bin + // together: 2 dedicated bins, the reduced instance keeps 3 + // bins for item type 2 (3x3, 2 copies). + fs::path("data") / "rectangleguillotine" / "tests" / "bin_packing_perfect_pairs", + ReductionParameters(), + }, { + // Same, with a cut thickness of 1 and item type 1 of size + // 3x10: 6 + 1 + 3 fills the bin. + fs::path("data") / "rectangleguillotine" / "tests" / "bin_packing_perfect_pairs_cut_thickness", + ReductionParameters(), }, }));