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
//! The result of a run: a value function and a greedy policy ([`Solution`]).
use core::fmt;
use core::hash::Hash;
use alloc::vec::Vec;
use crate::budget::Stats;
use crate::error::Error;
use crate::model::Mdp;
use crate::space::{Id, Space};
/// The result of solving the MDP `M`: a [`Solution`] or the [`Error`] that stopped the run.
pub type SolveResult<M> =
Result<Solution<<M as Mdp>::State, <M as Mdp>::Action>, Error<<M as Mdp>::State>>;
/// How a run ended.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Status {
/// The convergence criterion holds: for LRTDP, RTDP, LAO* and iLAO*, the initial state is
/// labeled solved by the solved check; for value iteration, the last sweep's largest
/// residual was below epsilon; for BRTDP, FRTDP and VPI-RTDP, the initial state's bounds are at most
/// epsilon
/// apart.
Converged,
/// The [`Budget`](crate::Budget) stopped the run first. Values and policy are the ones
/// reached so far and carry no guarantee, except that the bounds of BRTDP, FRTDP and VPI-RTDP still bracket the
/// optimal values.
BudgetExhausted,
/// Proven: no policy reaches a goal with probability 1 from the initial state, so under
/// [`DeadEnds::Infinite`](crate::DeadEnds::Infinite) its value is infinite and it has no
/// policy action. Never returned under a finite penalty, where giving up is always
/// possible.
Unsolvable,
}
/// A value function and a greedy policy over the states a run touched.
///
/// See `docs/design.md`, section "Convergence criterion", for what a converged solution
/// guarantees.
pub struct Solution<S, A> {
/// The states, values and policy actions.
space: Space<S, A>,
/// The id of the initial state.
initial: Id,
/// How the run ended.
status: Status,
/// Counters of the run.
stats: Stats,
/// The upper bounds of BRTDP, FRTDP and VPI-RTDP, indexed by [`Id`]; empty for the other
/// algorithms.
upper: Vec<f64>,
}
impl<S, A> Solution<S, A> {
/// Wraps the state space of a finished run.
///
/// A run whose initial value is infinite becomes [`Status::Unsolvable`], even if the
/// budget ran out: infinity only enters values through proven dead ends (states without
/// actions, heuristic dead ends, the dead-end proof) and spreads only through backups,
/// where it means every action risks a proven dead end.
pub(crate) fn new(space: Space<S, A>, initial: Id, status: Status, counters: Stats) -> Self {
let status = if space.node(initial).value.is_infinite() {
Status::Unsolvable
} else {
status
};
Self {
space,
initial,
status,
stats: counters,
upper: Vec::new(),
}
}
/// Attaches the upper bounds of a bounded run, one per stored state, indexed by [`Id`].
pub(crate) fn with_upper(mut self, upper: Vec<f64>) -> Self {
debug_assert_eq!(upper.len(), self.space.len(), "one upper bound per state");
self.upper = upper;
self
}
/// How the run ended.
#[must_use]
pub fn status(&self) -> Status {
self.status
}
/// Returns `true` if the run met its convergence criterion with a finite initial value.
#[must_use]
pub fn is_converged(&self) -> bool {
self.status == Status::Converged
}
/// Counters of the run.
#[must_use]
pub fn stats(&self) -> &Stats {
&self.stats
}
/// The initial state.
#[must_use]
pub fn initial_state(&self) -> &S {
self.space.state(self.initial)
}
/// The value of the initial state: the expected cost to reach a goal.
///
/// Infinite exactly when the status is [`Status::Unsolvable`] (which relies on the
/// heuristic returning `None` only for real dead ends).
#[must_use]
pub fn initial_value(&self) -> f64 {
self.space.node(self.initial).value
}
/// The upper bound on the initial state's optimal value, for [`brtdp`](crate::brtdp());
/// `None` for the other algorithms, which keep no upper bound.
#[must_use]
pub fn initial_upper_bound(&self) -> Option<f64> {
self.upper.get(self.initial as usize).copied()
}
/// The policy's action in the initial state; see [`Solution::action`].
#[must_use]
pub fn initial_action(&self) -> Option<&A> {
self.policy_of(self.initial)
}
/// The policy action stored for `id`.
fn policy_of(&self, id: Id) -> Option<&A> {
let index = self.space.node(id).policy?;
Some(&self.space.action(index).action)
}
}
impl<S: Hash + Eq, A> Solution<S, A> {
/// The value of `state`, or `None` if the run never touched it.
///
/// For LRTDP, RTDP, LAO* and iLAO*, values outside the policy's reachable states are only
/// bounds (lower bounds with an admissible heuristic), not converged values. For the bounded algorithms this is the lower
/// bound; see [`Solution::upper_bound`].
#[must_use]
pub fn value(&self, state: &S) -> Option<f64> {
self.space.find(state).map(|id| self.space.node(id).value)
}
/// The upper bound on the optimal value of `state`, for [`brtdp`](crate::brtdp()).
///
/// `None` if the run never touched `state`, and always for the other algorithms. With
/// [`Solution::value`] as the lower bound, the bounded algorithms bracket the optimal value of every
/// stored state; see [`brtdp`](crate::brtdp()), section "Guarantee".
#[must_use]
pub fn upper_bound(&self, state: &S) -> Option<f64> {
self.space
.find(state)
.and_then(|id| self.upper.get(id as usize).copied())
}
/// The policy's action in `state`.
///
/// `None` at goals, dead ends, states where giving up is cheaper (under
/// [`DeadEnds::Penalty`](crate::DeadEnds::Penalty)) and states the run never backed up.
/// Every state reachable from the initial state under the policy of a converged run has
/// an action unless it is one of the first three. The policy of BRTDP, FRTDP and VPI-RTDP
/// is greedy on the upper bound; their fill pass gives a converged run the same
/// coverage (see [`brtdp`](crate::brtdp())).
#[must_use]
pub fn action(&self, state: &S) -> Option<&A> {
self.space.find(state).and_then(|id| self.policy_of(id))
}
}
impl<S: fmt::Debug, A> fmt::Debug for Solution<S, A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Solution")
.field("initial_state", self.initial_state())
.field("initial_value", &self.initial_value())
.field("status", &self.status)
.field("stats", &self.stats)
.finish_non_exhaustive()
}
}