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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! # Stochastic search
//!
//! Planning under probabilistic outcomes: value iteration, RTDP, labeled RTDP, BRTDP, FRTDP,
//! VPI-RTDP, LAO*, iLAO* and CG-iLAO* for
//! fully observable problems, MOLAO*, iMOLAO* and MOLRTDP for multi-objective ones; POMCP and DESPOT for partially observable ones.
//!
//! A problem is a stochastic shortest path (SSP) problem: reach a goal state with minimal
//! expected total cost, where every action has a cost and a probability distribution over
//! successor states. It is described by small capability traits instead of one universal
//! problem trait: the MDP implements [`Mdp`], the goal test is a [`Goal`] and the initial
//! value estimate a [`Heuristic`]. Closures work for both (wrap heuristic closures in
//! [`heuristic_fn`]). Every algorithm takes
//! a [`Config`] and returns a [`Solution`]: a value function and a greedy policy, plus a
//! [`Status`] and [`Stats`].
//!
//! Partially observable problems are generative models ([`Pomdp`]): a step samples successor,
//! observation and reward from a `rand_core` generator. The online planners [`pomcp()`] and
//! [`despot()`] take a particle belief and return a [`Plan`] for one decision.
//!
//! The crate is `no_std` and needs only `alloc`.
//!
//! ## Algorithms
//!
//! - [`lrtdp()`]: labeled real-time dynamic programming (Bonet and Geffner 2003). Explores only
//! the states a good policy visits; epsilon-consistent on them when it converges.
//! - [`rtdp()`]: plain RTDP (Barto, Bradtke and Singh 1995), with LRTDP's solved check as
//! its stopping rule. The classic baseline.
//! - [`brtdp()`]: bounded RTDP (McMahan, Likhachev and Gordon 2005). Keeps a lower and an
//! upper bound and samples where they are far apart; anytime, with a known gap.
//! - [`frtdp()`]: focused RTDP (Smith and Simmons 2006). The same bounds; trials follow the
//! successor of highest priority with an adaptive depth limit.
//! - [`vpi_rtdp()`]: VPI-RTDP (Sanner et al. 2009). The same bounds; trials follow the value
//! of perfect information about successors where the bounds are informative.
//! - [`lao_star()`]: classic LAO* (Hansen and Zilberstein 2001), one tip per expansion
//! step with value iteration on its ancestors. The paper's original; slower than iLAO*.
//! - [`cg_ilao_star()`]: CG-iLAO* (Schmalz and Trevizan 2024). iLAO* over a partial SSP that
//! adds an action only when constraint generation finds it necessary.
//! - [`ilao_star()`]: improved LAO* (Hansen and Zilberstein 2001). Grows the best partial
//! solution graph in depth-first passes; no sampling.
//! - [`value_iteration()`]: value iteration over every reachable state. The exact reference.
//! - [`pomcp()`]: partially observable Monte-Carlo planning (Silver and Veness 2010). UCT
//! over action-observation histories with rollouts.
//! - [`despot()`]: anytime regularized DESPOT (Somani et al. 2013, Ye et al. 2017). Bound
//! guided search over a tree of `K` determinized scenarios.
//! - [`molrtdp()`], [`molao_star()`], [`imolao_star()`] and [`mo_value_iteration()`]:
//! multi-objective SSPs (Chen, Trevizan and Thiébaux 2023) over an [`MoMdp`] with cost
//! vectors. They return an [`MoSolution`], a convex coverage set of deterministic
//! policies; heuristics are [`ZeroVector`], [`IdealPoint`] and [`Abstraction`] (also over
//! [`Determinized`]).
//! - [`Pomcp`] and [`Despot`]: the same planners kept between decisions, reusing the subtree
//! after the action taken and the observation received; [`update_belief`] is POMCP's
//! particle filter with reinvigoration.
//!
//! ## The heuristic contract
//!
//! [`Goal`], [`Heuristic`], [`heuristic_fn`] and [`Budget`] are re-exported from
//! `search-core`, shared with the other planning crates. The algorithms here use
//! `Heuristic<S, f64>` (closures infer `f64`) under this contract:
//!
//! - `estimate` is called once per state, when the state is first stored; never for goals.
//! It takes `&mut self`, so a heuristic can keep a workspace; algorithms take it by value,
//! pass `&mut h` to keep `h`.
//! - `None` (or an infinite estimate) means no proper policy exists from the state, a proven
//! dead end that is never expanded. Return it only when that is certain.
//! - NaN and negative infinity are rejected with [`Error::InvalidHeuristic`].
//! - The optimality guarantees need an admissible heuristic (never above the optimal
//! expected cost). Inadmissible ones never panic; they only void the guarantee.
//!
//! [`Zero`], the blind heuristic, is `search-core`'s too; it estimates `0.0` for `f64`.
//! The multi-objective algorithms take cost-vector sets and use [`ZeroVector`] instead.
//!
//! ## Dead ends
//!
//! States from which no policy reaches a goal with probability 1 are valued per
//! [`DeadEnds`]: at infinity (the default, exact SSP semantics), or at a finite give-up
//! penalty that makes every input solvable. The bounded algorithms ([`brtdp()`], [`frtdp()`],
//! [`vpi_rtdp()`]) need finite upper bounds, so under infinite dead ends their lower bound
//! must prove the dead ends they meet.
//!
//! ## Events
//!
//! An [`Observer`] set with [`Config::with_observer`] receives [`Event`]s while a run
//! progresses: progress, proven dead ends, and suspected divergence (a heuristic signal,
//! never a verdict). The default observer `()` costs nothing.
//!
//! ## Example
//!
//! ```
//! use stochastic_search::{Config, Mdp, heuristic_fn, lrtdp};
//!
//! /// A die roll: action `roll` (cost 1) ends on a six; action `pay` (cost 4) ends always.
//! struct Dice;
//!
//! impl Mdp for Dice {
//! type State = bool;
//! type Action = &'static str;
//!
//! fn actions(&self, _done: &bool, mut emit: impl FnMut(&'static str, f64)) {
//! emit("roll", 1.0);
//! emit("pay", 4.0);
//! }
//!
//! fn outcomes(&self, _done: &bool, action: &&'static str, mut emit: impl FnMut(bool, f64)) {
//! match *action {
//! "roll" => {
//! emit(true, 1.0 / 6.0);
//! emit(false, 5.0 / 6.0);
//! }
//! _ => emit(true, 1.0),
//! }
//! }
//! }
//!
//! let heuristic = heuristic_fn(|_: &bool| Some(1.0));
//! let solution = lrtdp(&Dice, false, &|done: &bool| *done, heuristic, Config::default())?;
//!
//! // Rolling takes 6 attempts in expectation, so paying 4 is cheaper.
//! assert_eq!(solution.initial_action(), Some(&"pay"));
//! assert!((solution.initial_value() - 4.0).abs() < 1e-5);
//! # Ok::<(), stochastic_search::Error<bool>>(())
//! ```
//!
//! See `docs/design.md` in the repository for the design.
//!
//! Repository: <https://github.com/sunsided/stochastic-search>
extern crate alloc;
/// Doctests for the code examples in README.md (not part of the API).
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use cg_ilao_star;
pub use ;
pub use ;
pub use ;
pub use ilao_star;
pub use lao_star;
pub use lrtdp;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use rtdp;
pub use ;
pub use value_iteration;