1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//! # 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 is `u32`), the fixed-point [`Decimal`] with scale `10^K`,
//! and, behind the `float` feature, the checked float `NonNegF64`.
//! - [`CostExt`]: saturating subtraction and a (possibly rounding) conversion to `f64`, for
//! priorities that subtract or divide costs; also separate from [`Cost`].
//! - [`WideMul`]: the exact product of two costs in a wider type, for comparing cost ratios
//! by cross-multiplication. Separate from [`Cost`], so cost types without it still work.
//! - [`ExactArith`] (feature `alloc`): costs into an exact number type ([`BigUint`]) for
//! sums of products and longer products; also separate from [`Cost`].
//! - [`Heuristic`]: a cost-to-go estimate with a mutable workspace, plus [`heuristic_fn`] for
//! closures and the blind heuristic [`Zero`].
//! - [`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.
//!
//! ```mermaid
//! 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): `Heuristic` for `Box<H>`.
//! - `float`: the `NonNegF64` cost.
//!
//! 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>
extern crate alloc;
pub use Budget;
pub use NonNegF64;
pub use ;
pub use ;
pub use Goal;
pub use ;
/// Doctests for the code examples in README.md (not part of the API).