Expand description
§Search core
Small shared traits for planning and search crates.
The crate holds only what at least two independent search crates need, so that a heuristic, goal test or budget written once works with every algorithm of the ecosystem:
Cost: a non-negative, totally ordered scalar path cost. Implemented for the unsigned integers (the ecosystem default isu32), the fixed-pointDecimalwith scale10^K, the tie-breaking pairLexicographic, and, behind thefloatfeature, the checked floatNonNegF64.CostExt: saturating subtraction and a (possibly rounding) conversion tof64, for priorities that subtract or divide costs; also separate fromCost.WideMul: the exact product of two costs in a wider type, for comparing cost ratios by cross-multiplication. Separate fromCost, so cost types without it still work.ExactArith(featurealloc): costs into an exact number type (BigUint) for sums of products and longer products; also separate fromCost.BigIntholds their differences, which may be negative.Heuristic: a cost-to-go estimate with a mutable workspace, plusheuristic_fnfor closures and the blind heuristicZero.Goal: a goal test, implemented for closures.Budget: early stopping over a crate’s own statistics type, implemented for closures.
There is no single “search problem” trait. Each algorithm asks for the capabilities it needs, and successor generation stays in the algorithm crates, whose graph shapes differ.
graph LR
algo[search algorithm] -->|estimate| Heuristic
algo -->|is_goal| Goal
algo -->|exhausted| Budget
algo -->|sums, compares| Cost
Heuristic -.->|"C: Cost (for Zero)"| Cost§Features
alloc(default):HeuristicforBox<H>.float: theNonNegF64cost.
The crate is no_std in every configuration.
§Example
use search_core::{Budget, Cost, Goal, Heuristic, heuristic_fn};
/// Greedy descent on a number line: a toy algorithm using all four traits.
fn descend<C: Cost>(
mut state: u32,
goal: &impl Goal<u32>,
mut h: impl Heuristic<u32, C>,
mut budget: impl Budget<u64>,
) -> Option<u32> {
let mut steps = 0_u64;
while !goal.is_goal(&state) {
if budget.exhausted(&steps) {
return None;
}
h.estimate(&state)?;
state -= 1;
steps += 1;
}
Some(state)
}
let h = heuristic_fn(|s: &u32| Some(*s));
assert_eq!(descend(5, &|s: &u32| *s == 2, h, |n: &u64| *n >= 10), Some(2));
assert_eq!(descend(5, &|s: &u32| *s == 2, h, |n: &u64| *n >= 1), None);§Status
Pre-release; the API may still change before 0.1. See MIGRATION.md for how the sibling
crates would adopt it.
Repository: https://github.com/sunsided/search-core
Structs§
- BigInt
- An arbitrary-precision signed integer: a sign and a
BigUintmagnitude. - BigUint
- An arbitrary-precision unsigned integer: 32-bit limbs, least significant first, without most significant zero limbs.
- Decimal
- A non-negative fixed-point decimal cost with
Kdecimal places. - Heuristic
Fn - A closure used as a
Heuristic; created byheuristic_fn. - Lexicographic
- A path cost ordered by
primaryfirst, with ties broken bysecondary. Both components add up along a path. - NonNeg
F64 - A non-negative, non-NaN
f64cost;+∞is allowed and acts as the largest cost. - Zero
- The blind heuristic: estimates
Cost::ZEROeverywhere. Turns A* into Dijkstra’s algorithm and is admissible and consistent for every non-negative cost.
Enums§
- Parse
Decimal Error - Why a string is not a valid
Decimal<K>.
Traits§
- Budget
- Decides when a search must stop before it finishes, given its counters so far.
- Cost
- A non-negative scalar edge or path cost.
- CostExt
- Priority arithmetic that
Costlacks: subtraction and conversion tof64. - Exact
Arith - A cost that converts into an
ExactNumberwithout loss. - Exact
Number - A number type closed under exact
+and*, with a zero, a one and a distance, for comparisons thatWideMulcannot express: sums of products, products of three or more costs. - Goal
- A goal test over states of type
S. - Heuristic
- An estimate of the cost from a state of type
Sto the nearest goal, in cost typeC. - WideMul
- The product of two costs, in a type wide enough to hold it.
Functions§
- heuristic_
fn - Turns a closure
FnMut(&S) -> Option<C>into aHeuristic.