heuristic-search
Explicit-state heuristic graph search: A*, weighted and anytime, bounded-suboptimal, bidirectional, incremental and real-time search.
Part of the planning and search crates; see also this crate's overview page.
Who this is for
Anyone who has a graph, or a problem that can be phrased as one, and needs a path through
it: a planner that generates its states on the fly, a robot or a game character on a grid.
The graph stays in your code; you describe it through small traits (Successors, and
Predecessors for the bidirectional searches), pass the goal test and the heuristic as
separate values, and get back a path, an outcome and the expansion counters. The crate
knows nothing about PDDL, SAS+ or any other model, so domain-specific representations sit
on top of it rather than in it.
It is also meant for comparing algorithms on the same problem: every search uses the same traits, budgets and statistics, so trying IDA* or Weighted A* instead of A* changes the call, not the problem description. Symbolic (BDD), multi-objective, stochastic and width-based search are out of scope.
What's included
See docs/design.md for the design behind it.
- Foundational: BFS, Dijkstra, A*, greedy best-first search (GBFS); DFS, depth-limited DFS,
iterative deepening DFS, IDA*, IDA* with a bounded transposition table; depth cutoffs
are reported apart from budget stops (
Outcome::Cutoff) - Fringe search (
fringe_search): optimal with an admissible heuristic, threshold iterations over a cache ofgper state, no priority queue - RBFS (
rbfs): recursive best-first search, optimal with an admissible heuristic in memory linear in the depth, iterative so deep paths cannot overflow the stack - SMA* (
sma_star): A* within a node budget, forgetting and regenerating leaves; the cheapest path that fits in memory,Cutoffwhen none does - Beam search (
beam_search, incomplete;Cutoffwhen the width pruned) and beam-stack search (beam_stack_search): complete and anytime, optimal once it completes, with a fixed beam width - Grids: Jump Point Search (
jps) on anyUniformGrid, 8-connected without corner cutting, with the exact octile costOctile(a + b√2, compared without rounding);EightConnectedmakes a grid a graph for the other searches; any-angle Theta* and Lazy Theta* (theta_star,lazy_theta_star, featurefloat) on aLineOfSightgrid - Hierarchical A* (
hierarchical_a_star, HPA*): aHierarchybuilt once over a graph split into regions (rooms and doors, or square clusters of a grid with entrance cells on their borders), A* on the abstract graph, then A* inside each region; refinement across threads withhierarchical_a_star_par(featureparallel) - Every search also from several start states (
*_multi; the incremental planners asLpaStarMulti,DStarLiteMulti,AdStarMultiwith fixed starts) - Many queries per frame: reusable workspaces for the best-first searches (
Workspacewitha_star_in,dijkstra_in, ...), resumable A*, Weighted A*, GBFS and Dijkstra under a per-call budget (AStarSearch), flow fields from a goal set shared by every agent heading there (FlowField, also onEightConnectedgrids), and batches of queries on several threads (solve_batch, featureparallel); graphs, heuristics, planners and results areSend + Syncwhen their parameters are - Weighted and anytime: Weighted A*, ARA*, ANA*
- Bounded-suboptimal: focal search (with a public focal list, tie-breaking and an optional focal value apart from
f), EES, DPS - Bidirectional: bidirectional BFS and Dijkstra, NBS, DIBBS, BAE*, MM (and MM0); meets by a user
relation (
MeetRelation) between states and backward nodes of their own type (such as subgoals), with path replay or a typedReplayError - Multiple heuristics: shared MHA*
- Landmark heuristics: ALT lower bounds (
Landmarks) from forward and backward distance tables, farthest-first or avoidance selection, active landmarks per query; for A*, the anytime planners and (towards the target and from the source) the bidirectional searches - Expensive edge evaluation: LazySP
- Preprocessed queries: contraction hierarchies (
ContractionHierarchy) for many exact queries on a finite graph with static costs; shortcuts unpack into the graph's own actions, and ranks and upward search spaces are readable for visualisation - Dynamic graphs: LPA*, D* Lite, AD* (start moves keyed by D* Lite's
km); Field D* (FieldDStar, featurefloat) over the corners of aCostGridwith per-cell costs, interpolated any-angle paths through cell edges, cell-change repair and a moving start; Real-time D* (RealTimeDStar), D* Lite and LSS-LRTA* under a per-move expansion limit - Real-time search with learned tables that persist across trials (
LearnedHeuristic): LRTA*, RTA*, LSS-LRTA* (A* lookahead, Dijkstra update), RTAA*, and the limited-horizon Bellman update of Hadar et al. 2025 (lhb_updateoffline,lhb_lrta_star,lhb_gbfsandlhb_bwa_starwith A*, GBFS and batch weighted A* search phases inside the LSS-LRTA* agent); learning curves inbenches/learning.rsand the design notes - Parallel (feature
parallel): hash-distributed A* (hda_star), one A* search over several threads that own the states by hash, with the same optimal cost as A* - Naming: every A* variant is
*_star(a_star,weighted_a_star, ...) - Shared traits:
Cost,Heuristic,GoalandBudget<Stats>come fromsearch-coreand are re-exported from the crate root. Costs are the unsigned integers,Decimal<K>and (featurefloat)NonNegF64.
Usage
Add the crate with cargo add heuristic-search, or in Cargo.toml:
[]
= "0.1"
Then describe the graph and run a search:
use ;
/// A directed graph as adjacency lists of `(target, cost)` pairs.
>>);
let graph = Graph;
let result = dijkstra;
let solution = result.solution.expect;
assert_eq!;
assert_eq!;
The crate is no_std and needs only alloc. The feature float adds the float cost
NonNegF64, the any-angle grid searches Theta* and Lazy Theta*, and Field D*. The
feature parallel adds hash-distributed A*, solve_batch and hierarchical_a_star_par
and needs std.
Examples
The examples
directory has runnable scenarios on generated inputs (a city road network, a game map, a
hospital floor, a rover's terrain). Each one compares a search with its obvious baseline,
prints the states it expanded and the time it took, and asserts that the costs agree:
| Example | Scenario | Shows |
|---|---|---|
contraction_hierarchies |
1000 routes in a city of 22 629 intersections | A query settles about 292 states where Dijkstra settles 11 248; how many queries pay for the preprocessing |
alt_landmarks |
The same routes | Landmark bounds cut A*'s expansions about 13-fold over the straight-line heuristic, also inside bae_star |
mm_bidirectional |
The same routes | Neither side of MM expands beyond half the optimal cost; on roads plain A* is still faster |
crowd_pathfinding |
300 units on a 256 x 256 map | One FlowField serves every unit heading to a rally point; solve_batch, reused workspaces and A* paused across frames |
parallel_hda_star |
One long query on a 512 x 512 map | Where hash-distributed A* pays off (expensive successors) and where it does not (a plain grid) |
indoor_navigation |
100 deliveries on a hospital floor | Room regions give exact paths with about 16 times fewer expansions; square clusters need no floor plan and cost a few percent more |
rover_replanning |
A rover that sees obstacles only within its sensor range | Real-time D* never exceeds its per-step limit; D* Lite drives shorter but can stall for thousands of expansions |
learning_heuristics |
Repeated trials past a cul-de-sac | LRTA*, LSS-LRTA* and the LHB phases learn the way out; GBFS and batched BWAS can settle on a detour |
Run one with cargo run --release --example contraction_hierarchies. crowd_pathfinding,
parallel_hda_star and indoor_navigation need --features parallel.
MSRV
Rust 1.85 (rust-version in Cargo.toml). The MSRV is a signal, not a contract: it may
rise in a minor release.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. See CONTRIBUTING.md.