Skip to content

Cache the node hash and allocate children with make_shared in the other tree searches - #881

Merged
fontanf merged 1 commit into
masterfrom
tree-search-node-hash
Nov 19, 2026
Merged

fontanf merged 1 commit into
masterfrom
tree-search-node-hash

Conversation

@fontanf

@fontanf fontanf commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Follow-up of #880, which did this for the rectangleguillotine tree search.

Changes

The history maps of the beam searches hashed a node's whole item_number_of_copies vector on every lookup. The hash is now stored in the node (item_number_of_copies_hash) and updated incrementally when items are packed.

The hash is the sum over the item types of their number of copies times a random 64-bit key per item type, modulo 2^64. The keys come from a fixed seed and live in the new src/algorithms/item_copies_hash.hpp (ItemCopiesHashKeys). Unlike the Zobrist hash of #880, it needs one key per item type, whatever the number of copies. Node equality is unchanged: a collision only puts two nodes in the same bucket, so the searches explore the same nodes.

Schemes updated:

  • tree search: box, boxstacks, onedimensional, rectangle, irregular
  • maximal spaces tree search: box, rectangle, rectangleguillotine, irregular
  • irregular periodic packing tree search

The child() functions that built a node and then copied it into a shared pointer now use std::make_shared (box, boxstacks, onedimensional, rectangle and irregular tree searches).

In box, boxstacks and onedimensional, the hasher still returns 0 for unbounded knapsack instances, as before.

Tests

  • New test NodeHashFollowsItemNumberOfCopies for each scheme, with a shared helper in test/node_hash_test.hpp. Over the nodes of a small expanded tree, it checks that the same numbers of copies give the same hash and that different numbers of copies give different hashes. When an update is deliberately broken, only the test of that scheme fails.
  • test/boxstacks/tree_search_test.cpp was empty. It now holds this test and is registered in CMake.
  • Deterministic mode (not-anytime-sequential): identical solutions on master and this branch on the 476 instances of data/*/tests.

Speed

Not measured meaningfully yet:

  • The onedimensional and rectangle runs I tried (Falkenauer t120, berkey1987 Class_01) take under 0.4 s, too short to separate the difference from noise.
  • The box and irregular runs in deterministic mode didn't finish in a reasonable time, so I stopped them.
  • There was no boxstacks data available locally.

…er tree searches

The history maps of the beam searches hashed a node's whole
item_number_of_copies vector on every lookup. The hash is now stored in the
node and updated incrementally when items are packed: the sum over the item
types of their numbers of copies times a random 64-bit key per item type
(modulo 2^64, fixed seed, see ItemCopiesHashKeys). Node equality is
unchanged, so the searches explore the same nodes.

This applies to the tree searches of box, boxstacks, onedimensional,
rectangle and irregular, and to the maximal spaces tree searches of box,
rectangle, rectangleguillotine and irregular, and to the irregular periodic
packing tree search. The child() functions that allocated a node and then
copied it into a shared pointer now use std::make_shared.

New tests NodeHashFollowsItemNumberOfCopies for each branching scheme: over
the nodes of a small expanded tree, the same numbers of copies give the same
hash, different numbers of copies different hashes. Breaking an update makes
only the test of that scheme fail.

Deterministic mode: identical solutions before and after on the 476
instances of data/*/tests.
@fontanf
fontanf merged commit b2d64b3 into master Nov 19, 2026
8 checks passed
@fontanf
fontanf deleted the tree-search-node-hash branch November 19, 2026 05:18
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.

1 participant