search-core
Common traits for heuristic search and planning: path costs, heuristics, goal tests and search budgets.
Search algorithm crates use these traits as bounds, and users implement them once for their
problem: a heuristic, goal test or budget written against search-core works with every
algorithm that accepts it, whichever crate provides the algorithm. The crate is no_std,
has no dependencies, and keeps to small, stable abstractions that are useful across many
search algorithms.
Installation
Or in Cargo.toml:
[]
= "0.1"
Overview
Search interface
| Item | Purpose |
|---|---|
Heuristic<S, C = u32> |
Cost-to-go estimate; None marks a dead end. Takes &mut self, so a heuristic can keep a workspace. Implemented for &mut H, Box<H> (feature alloc) and closures via heuristic_fn. |
Zero |
The blind heuristic, for every Cost and for f64. Turns A* into Dijkstra's algorithm. |
Goal<S> |
Goal test, implemented for Fn(&S) -> bool. |
Budget<Stats> |
Early stopping over a search's own statistics type, implemented for FnMut(&Stats) -> bool. |
Costs
| Item | Purpose |
|---|---|
Cost |
Non-negative, totally ordered scalar path cost with ZERO, saturating_add and saturating_times. Implemented for u8 to u128 and usize; u32 is the default cost type of Heuristic. |
Decimal<K> |
Exact fixed-point cost with K decimal places (K <= 19, checked at compile time), stored as u64. FromStr parses text such as "3.25" exactly and rejects anything that would need rounding (ParseDecimalError). |
Lexicographic<P, S> |
A pair cost: primary first, secondary only to break ties, for example the fewest steps and then the fewest conflicts. Any search over C: Cost accepts it. |
NonNegF64 |
Feature float: an f64 cost that rejects NaN and negative values, so it can be Ord. Rounds like f64; prefer integers or Decimal for exact results. |
CostExt |
saturating_sub and a possibly rounding to_f64, for priorities that subtract or divide costs, as in anytime and bidirectional searches. |
WideMul |
Exact product of two costs in a wider type, and the distance abs_diff, for comparing cost ratios by cross-multiplication. |
ExactArith |
Feature alloc: converts a cost into an exact number type (BigUint for integers and Decimal) for sums of products and longer products. |
BigInt |
Feature alloc: an exact signed integer for differences of such expressions, which may be negative, such as Dinkelbach weights den · c₁ - num · c₂. |
CostExt, WideMul and ExactArith are separate from Cost, so a custom cost type only
implements the capabilities the algorithms it is used with ask for. Implement Cost for
your own type to use it with any algorithm generic over C: Cost.
Out of scope
The crate defines no search algorithms and no problem representation. Successor generation (graphs, MDPs), search statistics and limit types, and Pareto dominance differ too much between algorithms to share, so they stay in the crates that implement the algorithms. MIGRATION.md shows how existing search crates map their own traits onto this one.
Example
An algorithm states the capabilities it needs as trait bounds; callers pass any implementation:
use ;
/// Picks the candidate with the lowest `g + h` that is not a dead end.
let candidates = ;
// Distance to 10; states past 10 are dead ends.
let mut to_ten = heuristic_fn;
assert_eq!;
assert_eq!;
let at_ten = ;
assert!;
// Exact decimal costs, parsed without rounding.
let cost: = "3.25".parse.unwrap;
assert_eq!;
assert!;
Features
| Feature | Default | Enables |
|---|---|---|
alloc |
yes | Heuristic for Box<H>; ExactArith, ExactNumber, BigUint and BigInt |
float |
no | The checked float cost NonNegF64 |
With default-features = false, the crate needs neither std nor alloc.
Minimum supported Rust version
Rust 1.85 (edition 2024). The MSRV applies to the library; tests and benchmarks may need a newer toolchain. It may be raised in a minor release when a dependency or a language feature calls for it.
Contributing
See CONTRIBUTING.md.
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.