Skip to content

irregular: tree search option to expand only the K biggest item types per node - #829

Closed
HamzaYslmn wants to merge 2 commits into
fontanf:masterfrom
HamzaYslmn:tree-search-maximum-number-of-item-types
Closed

HamzaYslmn wants to merge 2 commits into
fontanf:masterfrom
HamzaYslmn:tree-search-maximum-number-of-item-types

Conversation

@HamzaYslmn

@HamzaYslmn HamzaYslmn commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On irregular knapsack instances with many distinct item types (a few hundred and up), the tree search stops making progress: it places the largest few dozen items, then each further step takes several seconds, and memory grows to several GB. This PR adds an opt-in parameter that bounds the work per node. With it, the same instances reach 74-80% bin utilization in 60 s instead of 35-47% in 300 s, with about 10-20x less memory.

Default behaviour is unchanged.

Cause

In BranchingScheme::insertions, every node tries every remaining item type × rotation × supporting part, and children() builds a node for each resulting insertion before the beam keeps the best ones. Both the time per level and the memory therefore grow with the number of item types. With ~1000 types a single level takes seconds, so within a normal time limit the search only gets through a few dozen items.

Change

New parameter OptimizeParameters::tree_search_maximum_number_of_item_types (CLI --tree-search-maximum-number-of-item-types), default -1 (no limit, original code path).

With a value K >= 0, each node expands item types in decreasing order of area and stops once K of them have produced at least one insertion. Item types that do not fit anywhere are skipped and do not count toward K, so smaller items are considered once the large ones no longer fit, and the search does not dead-end. Work and memory per node become bounded by K × rotations × supports rather than by the number of item types.

Results

Two instances are added in data/irregular/users/ (synthetic, generated to resemble sheet-metal jobs: plates and brackets with bolt holes and cut-outs, irregular blobs, thin letter-like parts). Every item is a distinct type with one copy; rotations 0/90/180/270; item-item spacing 2; profit = area. The solutions found with K = 25 are included.

Instance Run Items placed Bin utilization Wall time Peak memory
1000 types, 2000×1000 master, --time-limit 60 13 44% 77 s 6.2 GB
master, --time-limit 300 22 47% 323 s 6.9 GB
K = 10, --time-limit 60 143 76% 61 s 0.3 GB
K = 25, --time-limit 60 166 80% 61 s 0.7 GB
2000 types, 3000×1500 master, --time-limit 60 8 30% 135 s 10.8 GB
master, --time-limit 300 10 35% 417 s 13.3 GB
K = 10, --time-limit 60 214 73% 62 s 0.5 GB
K = 25, --time-limit 60 178 74% 62 s 0.7 GB

Utilization = placed item area / bin area. Measured on Windows, 4 threads per run, master at d4b0082. Note that master also exceeds its time limit on these instances (77 s for a 60 s limit, 417 s for 300 s), since a single node expansion outlasts it.

1000 item types:

1000 item types

2000 item types:

2000 item types

Reproduce

packingsolver_irregular --input data/irregular/users/2026-11-08_many_item_types_1000.json --time-limit 60 --verbosity-level 1
packingsolver_irregular --input data/irregular/users/2026-11-08_many_item_types_1000.json --time-limit 60 --verbosity-level 1 --tree-search-maximum-number-of-item-types 25

Notes

  • The parameter is opt-in and not used by the automatic algorithm selection. Values between 10 and 25 worked best on these instances; enabling it automatically above some number of item types could be a follow-up if you think it is worthwhile.
  • No unit test is included. I can add one wherever you prefer.
  • While preparing the second instance, one randomly generated shape made both master and this branch fail in shape::trapezoidation ('open_trapezoid_pos' must be != -1). That is unrelated to this change; I can open a separate issue with the shape.

… per node

On a knapsack instance with many distinct item types (~1000), every node tries every remaining item type x rotation x supporting part and children() builds a node for every insertion before the beam keeps the best ones, so time and memory per level grow with the number of types and the search stalls after the biggest few dozen items. tree_search_maximum_number_of_item_types (CLI --tree-search-maximum-number-of-item-types, default -1: unchanged) expands only the K biggest item types that have at least one insertion at each node; an item type without one does not count, so smaller ones are tried instead.
Synthetic sheet-metal parts (plates and brackets with holes, irregular blobs, letter-like strokes), every item a distinct type with one copy: 1000 types on a 2000x1000 bin and 2000 types on a 3000x1500 bin, item-item spacing 2, rotations 0/90/180/270, profit = area. The solutions are the ones found with --tree-search-maximum-number-of-item-types 25 and a 60 s time limit.
fontanf added a commit that referenced this pull request Nov 8, 2026
…orting part in the tree search with maximal spaces

No block can be inserted in an empty space without supporting part touching
its bottom: compute the supporting parts first, and don't compute the
candidate blocks and their score bounds in this case. On the instances of
#829, these empty spaces are most of the empty spaces selected, and the
search is about 4 times faster.
fontanf added a commit that referenced this pull request Nov 8, 2026
fontanf added a commit that referenced this pull request Nov 9, 2026
…orting part in the tree search with maximal spaces

No block can be inserted in an empty space without supporting part touching
its bottom: compute the supporting parts first, and don't compute the
candidate blocks and their score bounds in this case. On the instances of
#829, these empty spaces are most of the empty spaces selected, and the
search is about 4 times faster.
fontanf added a commit that referenced this pull request Nov 9, 2026
fontanf added a commit that referenced this pull request Nov 9, 2026
@fontanf

fontanf commented Nov 9, 2026

Copy link
Copy Markdown
Owner

Thanks a lot for this PR and for the very clear analysis and instances.

I'm closing it in favor of another approach. Limiting the expanded item types doesn't fit the tree search well, since it relies on a skyline-like structure and on dominances between the nodes. These instances, where many items fit in each section of the bin, are a better fit for the tree search with maximal spaces. It had a few issues, which are fixed in:

With the default parameters and a 60 s time limit, the solver now uses 83.4% of the bin (790 items) on the 1000-item-types instance and 85.8% (1501 items) on the 2000-item-types one. The time limit is respected.

I also tried a "K item types per node" limit in the tree search with maximal spaces, with K = 5, 10, 25, 50 and 100. It made no measurable difference there, so I didn't keep it.

Your two instances are added in data/irregular/users/ in #833, with the new solutions.

About the shape::trapezoidation failure ('open_trapezoid_pos' must be != -1), yes, please open an issue in fontanf/shape with the shape.

@fontanf fontanf closed this Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants