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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//! Multi-objective stochastic shortest path problems (MOSSPs) and the heuristic search
//! algorithms of Chen, Trevizan and Thiébaux (2023): MOLAO*, iMOLAO* and MOLRTDP, with
//! multi-objective value iteration (MOVI) as the exact reference.
//!
//! Every action has a cost vector instead of a scalar cost ([`MoMdp`]). No single policy is
//! optimal; the solution is a *convex coverage set* (CCS): one deterministic proper policy
//! per vertex of the lower convex hull of the expected cost vectors of all proper policies.
//! Every policy that minimises some positive weighting `w . V` of the objectives is a
//! vertex policy or a mixture of them; the stochastic mixtures, which fill the hull's
//! faces, are implied by the vertices and never listed.
//!
//! A value is a set of cost vectors. Backups take the bounded set sum of the successors'
//! sets and prune the union over actions to a CCS with a small linear program per vector.
//! The paper's upper bound `b` ([`Objectives::bound`]) caps every value: a sum that exceeds
//! `b` in some objective stands for an improper policy and becomes `b`, which is dropped
//! from the final answer. This makes the algorithms terminate when improper policies have
//! finite cost in some objectives (the paper's weak improper policy assumption).
//!
//! # Heuristics
//!
//! A heuristic is a `Heuristic<S, Vec<[f64; N]>>`: a set of vectors per state, `None` (or
//! an empty set) for a proven dead end. Admissible means: for every weight `w` in the
//! simplex, `min_{u in H(s)} w . u <= min_{v in V*(s)} w . v`. That is the bound the CCS
//! backups preserve, and it is weaker than the paper's set domination (Definition 4.1),
//! which the determinisation heuristic does not satisfy (see `docs/design.md`). Provided:
//!
//! - [`ZeroVector`]: the blind heuristic `{0}`.
//! - [`IdealPoint`]: one single-objective heuristic per objective, combined into the one
//! vector `[h_1(s), ..., h_N(s)]` (the paper's `H_ideal`).
//! - [`Abstraction`]: MOVI on a smaller MOSSP, looked up through a projection (the
//! explicit-state form of the paper's `H_mossp`); over [`Determinized`] it solves the
//! all-outcomes determinisation exactly, which bounds the paper's `H_mo` heuristics.
//!
//! # Example
//!
//! Figure 1 of the paper: from `s0`, action `a1` costs `[1, 0]` and action `a2` costs
//! `[0, 1]`; each reaches its goal with probability 0.5 and stays in `s0` otherwise.
//!
//! ```
//! use stochastic_search::{Config, MoMdp, Objectives, ZeroVector, molrtdp};
//!
//! struct Figure1;
//!
//! impl MoMdp<2> for Figure1 {
//! type State = &'static str;
//! type Action = &'static str;
//!
//! fn actions(&self, _state: &&'static str, mut emit: impl FnMut(&'static str, [f64; 2])) {
//! emit("a1", [1.0, 0.0]);
//! emit("a2", [0.0, 1.0]);
//! }
//!
//! fn outcomes(
//! &self,
//! _state: &&'static str,
//! action: &&'static str,
//! mut emit: impl FnMut(&'static str, f64),
//! ) {
//! emit(if *action == "a1" { "g1" } else { "g2" }, 0.5);
//! emit("s0", 0.5);
//! }
//! }
//!
//! let goal = |state: &&str| state.starts_with('g');
//! let objectives = Objectives::new([100.0, 100.0]);
//! let solution = molrtdp(&Figure1, "s0", &goal, ZeroVector, objectives, Config::default())?;
//!
//! assert!(solution.is_converged());
//! let mut front: Vec<[f64; 2]> = solution.policies().map(|policy| *policy.value()).collect();
//! front.sort_by(|a, b| a[0].total_cmp(&b[0]));
//! assert_eq!(front.len(), 2);
//! assert!((front[0][1] - 2.0).abs() < 1e-4 && (front[1][0] - 2.0).abs() < 1e-4);
//! # Ok::<(), stochastic_search::Error<&str>>(())
//! ```
pub use ;
pub use ;
pub use molrtdp;
pub use mo_value_iteration;
pub use ;
use crateError;
/// A finite MDP whose actions cost a vector of `N` objectives.
///
/// With a [`Goal`](crate::Goal) it defines a multi-objective stochastic shortest path
/// problem. Identical to [`Mdp`](crate::Mdp) except for the cost type, and queried the same
/// way: once per state, never for goals.
///
/// # Example
///
/// A retry loop that trades time (objective 0) against fuel (objective 1):
///
/// ```
/// use stochastic_search::MoMdp;
///
/// struct Retry;
///
/// impl MoMdp<2> for Retry {
/// type State = bool;
/// type Action = &'static str;
///
/// fn actions(&self, _done: &bool, mut emit: impl FnMut(&'static str, [f64; 2])) {
/// emit("walk", [3.0, 0.0]);
/// emit("drive", [1.0, 2.0]);
/// }
///
/// fn outcomes(&self, _done: &bool, _action: &&'static str, mut emit: impl FnMut(bool, f64)) {
/// emit(true, 1.0);
/// }
/// }
/// ```
/// The objective space of a multi-objective run: the upper bound `b` and the coverage set
/// margin.
///
/// The struct is `#[non_exhaustive]`; build it with [`Objectives::new`].
///
/// ```
/// use stochastic_search::Objectives;
///
/// let objectives = Objectives::new([100.0, 100.0]).with_margin(1e-4);
/// # let _ = objectives;
/// ```