search_core/lib.rs
1//! # Search core
2//!
3//! Shared cost, heuristic, goal and budget traits for search and planning crates, with exact
4//! `no_std` cost types.
5//!
6//! The crate holds only what at least two independent search crates need, so that a
7//! heuristic, goal test or budget written once works with every algorithm of the ecosystem:
8//!
9//! - [`Cost`]: a non-negative, totally ordered scalar path cost. Implemented for the unsigned
10//! integers (the ecosystem default is `u32`), the fixed-point [`Decimal`] with scale `10^K`,
11//! the tie-breaking pair [`Lexicographic`], and, behind the `float` feature, the checked
12//! float `NonNegF64`.
13//! - [`CostExt`]: saturating subtraction and a (possibly rounding) conversion to `f64`, for
14//! priorities that subtract or divide costs; also separate from [`Cost`].
15//! - [`WideMul`]: the exact product of two costs in a wider type, for comparing cost ratios
16//! by cross-multiplication. Separate from [`Cost`], so cost types without it still work.
17//! - [`ExactArith`] (feature `alloc`): costs into an exact number type ([`BigUint`]) for
18//! sums of products and longer products; also separate from [`Cost`]. [`BigInt`] holds
19//! their differences, which may be negative.
20//! - [`Heuristic`]: a cost-to-go estimate with a mutable workspace, plus [`heuristic_fn`] for
21//! closures and the blind heuristic [`Zero`].
22//! - [`Goal`]: a goal test, implemented for closures.
23//! - [`Budget`]: early stopping over a crate's own statistics type, implemented for closures.
24//!
25//! There is no single "search problem" trait. Each algorithm asks for the capabilities it
26//! needs, and successor generation stays in the algorithm crates, whose graph shapes differ.
27//!
28//! ```mermaid
29//! graph LR
30//! algo[search algorithm] -->|estimate| Heuristic
31//! algo -->|is_goal| Goal
32//! algo -->|exhausted| Budget
33//! algo -->|sums, compares| Cost
34//! Heuristic -.->|"C: Cost (for Zero)"| Cost
35//! ```
36//!
37//! ## Features
38//!
39//! - `alloc` (default): `Heuristic` for `Box<H>`; [`ExactArith`] with [`BigUint`] and
40//! [`BigInt`].
41//! - `float`: the `NonNegF64` cost.
42//!
43//! The crate is `no_std` in every configuration.
44//!
45//! ## Example
46//!
47//! ```
48//! use search_core::{Budget, Cost, Goal, Heuristic, heuristic_fn};
49//!
50//! /// Greedy descent on a number line: a toy algorithm using all four traits.
51//! fn descend<C: Cost>(
52//! mut state: u32,
53//! goal: &impl Goal<u32>,
54//! mut h: impl Heuristic<u32, C>,
55//! mut budget: impl Budget<u64>,
56//! ) -> Option<u32> {
57//! let mut steps = 0_u64;
58//! while !goal.is_goal(&state) {
59//! if budget.exhausted(&steps) {
60//! return None;
61//! }
62//!
63//! h.estimate(&state)?;
64//! state -= 1;
65//! steps += 1;
66//! }
67//!
68//! Some(state)
69//! }
70//!
71//! let h = heuristic_fn(|s: &u32| Some(*s));
72//! assert_eq!(descend(5, &|s: &u32| *s == 2, h, |n: &u64| *n >= 10), Some(2));
73//! assert_eq!(descend(5, &|s: &u32| *s == 2, h, |n: &u64| *n >= 1), None);
74//! ```
75//!
76//! Repository: <https://github.com/sunsided/search-core>
77
78#![no_std]
79#![forbid(unsafe_code)]
80
81#[cfg(feature = "alloc")]
82extern crate alloc;
83
84mod budget;
85mod cost;
86mod goal;
87mod heuristic;
88
89pub use budget::Budget;
90#[cfg(feature = "float")]
91pub use cost::NonNegF64;
92#[cfg(feature = "alloc")]
93pub use cost::{BigInt, BigUint, ExactArith, ExactNumber};
94pub use cost::{Cost, CostExt, Decimal, Lexicographic, ParseDecimalError, WideMul};
95pub use goal::Goal;
96pub use heuristic::{Heuristic, HeuristicFn, Zero, heuristic_fn};
97
98/// Doctests for the code examples in README.md (not part of the API).
99#[cfg(doctest)]
100#[doc = include_str!("../README.md")]
101mod readme_doctests {}