Skip to main content

search_core/
lib.rs

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