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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//! What a search returns: [`SearchResult`] with an [`Outcome`] and [`Stats`], and the
//! [`Limits`], the counter [`Budget`] that stops it early.
use alloc::vec::Vec;
use search_core::Budget;
/// Counters collected during one search run.
///
/// Non-exhaustive: later algorithms may add counters. Start from [`Stats::default`] where
/// a value is needed.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct Stats {
/// States whose successors were generated. A goal state that ends the search is not
/// counted. A state reopened through a cheaper path counts once per expansion.
pub expanded: u64,
/// Edges reported by [`Successors::successors`](crate::Successors::successors), whether
/// or not their target was new.
pub generated: u64,
/// Closed states reopened because a cheaper path to them turned up. Always `0` for
/// breadth-first and greedy best-first search, and for A* with a consistent heuristic.
pub reopened: u64,
/// True edge costs computed by an expensive edge evaluator. Only
/// [`lazy_sp`](crate::lazy_sp) evaluates edges; `0` everywhere else.
pub evaluations: u64,
}
impl Stats {
/// The counters of `self` and `other` added up, for searches made of several runs.
#[must_use]
pub(crate) fn plus(self, other: Self) -> Self {
Self {
expanded: self.expanded + other.expanded,
generated: self.generated + other.generated,
reopened: self.reopened + other.reopened,
evaluations: self.evaluations + other.evaluations,
}
}
}
/// A path from the start state to a goal state.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Solution<S, A, C> {
/// The states along the path, from the start to the goal. Never empty: a start state that
/// already is a goal yields a single state.
pub states: Vec<S>,
/// The edge labels along the path; `actions[i]` leads from `states[i]` to
/// `states[i + 1]`. Always one shorter than [`Solution::states`].
pub actions: Vec<A>,
/// The sum of the edge costs along the path.
pub cost: C,
}
impl<S, A, C> Solution<S, A, C> {
/// The number of edges on the path.
#[must_use]
pub fn len(&self) -> usize {
self.actions.len()
}
/// Returns `true` if the start state is the goal, so the path has no edges.
#[must_use]
pub fn is_empty(&self) -> bool {
self.actions.is_empty()
}
/// The goal state the path ends in.
///
/// # Panics
///
/// Never panics for solutions returned by a search, whose [`Solution::states`] is never
/// empty.
#[must_use]
pub fn goal(&self) -> &S {
self.states
.last()
.expect("a solution has at least one state")
}
}
/// How a search ended.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Outcome<S, A, C> {
/// A path to a goal was found.
Found(Solution<S, A, C>),
/// The search space was exhausted without reaching a goal, so no goal is reachable
/// (given a heuristic whose dead ends are real).
Unsolvable,
/// The [`Budget`] stopped the search before it could decide.
LimitReached,
/// A search with its own bound cut states off and found no goal within the bound: the
/// depth limit of [`depth_limited_dfs`](crate::depth_limited_dfs), the beam width of
/// [`beam_search`](crate::beam_search), or the memory of
/// [`sma_star`](crate::sma_star). A goal may lie beyond it; a larger bound may find it.
Cutoff,
}
/// The result of one search run.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SearchResult<S, A, C> {
/// How the search ended.
pub outcome: Outcome<S, A, C>,
/// Counters collected until the search ended.
pub stats: Stats,
}
impl<S, A, C> SearchResult<S, A, C> {
/// The solution, if one was found.
#[must_use]
pub fn solution(&self) -> Option<&Solution<S, A, C>> {
match &self.outcome {
Outcome::Found(solution) => Some(solution),
Outcome::Unsolvable | Outcome::LimitReached | Outcome::Cutoff => None,
}
}
/// Consumes the result and returns the solution, if one was found.
#[must_use]
pub fn into_solution(self) -> Option<Solution<S, A, C>> {
match self.outcome {
Outcome::Found(solution) => Some(solution),
Outcome::Unsolvable | Outcome::LimitReached | Outcome::Cutoff => None,
}
}
}
/// Simple counter limits. The default is unlimited.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Limits {
/// Stop once this many states have been expanded. `None` is unbounded.
pub max_expansions: Option<u64>,
}
impl Limits {
/// No limits: the search runs until it finds a goal or exhausts the space.
pub const NONE: Self = Self {
max_expansions: None,
};
/// Stops after `max` expansions.
#[must_use]
pub const fn expansions(max: u64) -> Self {
Self {
max_expansions: Some(max),
}
}
}
/// Asked before every expansion, with the counters so far, so a search stopped by
/// [`Limits::max_expansions`] `= n` has expanded exactly `n` states. Closures
/// `FnMut(&Stats) -> bool` are budgets too, which lets callers with a clock (this crate is
/// `no_std`) add a time limit; annotate the argument (`|stats: &Stats| ...`).
impl Budget<Stats> for Limits {
fn exhausted(&mut self, stats: &Stats) -> bool {
self.max_expansions.is_some_and(|max| stats.expanded >= max)
}
}