Skip to content

test(hexmap): measure A* against the bit-parallel BFS (L2) - #138

Merged
bal7hazar merged 1 commit into
mainfrom
feat/hexmap-astar
Sep 28, 2026
Merged

bal7hazar merged 1 commit into
mainfrom
feat/hexmap-astar

Conversation

@bal7hazar

Copy link
Copy Markdown
Collaborator

Summary

Lot L2: why origami_hexmap has no A*, answered with measurements. Result: no A* formulation beats Bfs::search by more than 5 % on a realistic class of inputs, so nothing is exported. Everything is test-only in crates/hexmap/src/tests/bench_astar.cairo. The numbers are in GAS.md, section "L2 A*" (it replaces the "L2 A* baseline" stub).

  • Astar::search (best formulation, test-only). The hex distance is the heuristic. With unit costs, a neighbour's f is the parent's f plus 0, 1 or 2, so the open list is three bitmap buckets on u128 limbs. Neighbours are split into closer / same-distance / farther classes by 3 field products, chosen by the signs of the cube offset to the target (12 cases, computed with bounded_int). The path is rebuilt from the list of expanded tiles, following chained closer neighbours without a scan. It has the same contract as Bfs::search, edge endpoints and panics included.
  • Other open lists, with the same expansion: a binary heap in a Felt252Dict, a sorted array, and an unsorted array that is scanned.
  • Greedy best-first, and the BFS pruned by the hex distance with iterative deepening on the bound.
  • Oracle. Every search must return the same path length as Bfs::search and a valid path. It is checked on the fixtures, CAVE/MAZE/EMPTY/SERPENTINE samples, random caves and mazes (7x7, 17x14, 19x13), board sizes 3x3 to 83x3 / 3x83, and edge endpoints.
Class Astar::search vs Bfs::search
Open boards (EMPTY near/far, hex distance 2 to 10) +16 % to +24 %
Caves 17x14 +17 % to +297 %
Mazes, serpentines +20 % to +160 %
Unreachable targets 4x to 11x
7x7 boards (BFS single limb) +47 % to +480 %
Adjacent endpoints (hex distance 1) -6.6 % / -7.9 % (prologue only, not a realistic search)

One expansion costs 35.5k gas: about 320 CASM steps for index, coordinates, classes, buckets, log and backtracking. A BFS layer costs 19.3k and advances the whole frontier. The dict heap, sorted array and scanned array run at 1.5x to 228x the BFS, greedy at 1.7x to 15x, and the pruned BFS at 1.7x to 40x. origami_map A* on 18x14 (17-tile path) costs 17.9M for reference.

Only additions: pub mod bench_astar; in lib.cairo, the bench file, and the GAS.md section. No public API change.

Test plan

  • snforge test -p origami_hexmap astar: 201 tests passed (184 budgeted benchmarks, oracle, panics)
  • scarb fmt --check
  • CI Test origami_hexmap

🤖 Generated with Claude Code

A* with the hex distance as heuristic, open list as three bitmap buckets
(f, f + 1, f + 2), neighbour classes from the signs of the cube offset,
chained backtracking. Measured with a Felt252Dict heap, a sorted array and
a scanned array as open lists, a greedy best-first search and the BFS pruned
by the hex distance with iterative deepening, on the L1 fixtures and pairs at
hex distance 1, 2, 5 and 10.

No formulation beats Bfs::search by more than 5 % on a realistic class:
the best A* is +16 % to +24 % on open boards and up to 11x on unreachable
targets; it only wins on adjacent endpoints (-7 %). Everything stays
test-only in tests/bench_astar.cairo; the numbers close the question in
GAS.md, section L2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bal7hazar
bal7hazar merged commit d4c33e7 into main Sep 28, 2026
10 checks passed
@bal7hazar
bal7hazar deleted the feat/hexmap-astar branch September 28, 2026 18:04
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