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
//! Plain real-time dynamic programming (RTDP, Barto, Bradtke and Singh 1995).
use Hash;
use crate;
use crateConfig;
use crateRun;
use cratefinish;
use crate;
use crateObserver;
use crateSolveResult;
/// Solves an SSP from `initial` with plain RTDP: greedy trials without solved labels.
///
/// Each trial starts at `initial`, backs up the current state, takes its greedy action and
/// samples a successor with a generator seeded from [`Config::seed`], until it reaches a
/// goal, a dead end or [`Config::max_trial_length`] steps. RTDP itself has no stopping
/// rule; this one checks between trials whether every state reachable from `initial` under
/// the greedy policy has a Bellman residual of at most [`Config::epsilon`] (the solved check
/// of [`lrtdp`](crate::lrtdp()), run only on `initial` and without backups). A check costs
/// about one evaluation per envelope state, so it runs only once as many backups happened
/// since the last check as that check took. When it passes, the policy is frozen and the
/// status is [`Status::Converged`](crate::Status::Converged).
///
/// Prefer [`lrtdp`](crate::lrtdp()): its labels stop trials at converged states, which makes
/// it much faster on states that trials rarely reach. Plain RTDP is here as the classic
/// baseline and for comparisons.
///
/// # Guarantee
///
/// A converged run has exactly the guarantee of [`lrtdp`](crate::lrtdp()) (section
/// "Guarantee" there): the passing check is the same solved check, on `initial`.
///
/// # Termination
///
/// As for [`lrtdp`](crate::lrtdp()), including the dead-end proofs under
/// [`DeadEnds::Infinite`](crate::DeadEnds::Infinite). Without labels, a run may take many
/// trials to push a rarely sampled outcome below epsilon; the default
/// [`Limits`](crate::Limits) budget still bounds it.
///
/// # Errors
///
/// Returns an [`Error`](crate::Error) for an invalid [`Config`], or for invalid MDP output or
/// heuristic estimates in the states the run touches.
///
/// # Panics
///
/// Panics if more than `u32::MAX` states, actions or outcomes are stored.
///
/// # Example
///
/// A sure route for cost 3, or a coin flip for cost 1 that retries on failure (expected
/// cost 2):
///
/// ```
/// use stochastic_search::{Config, Mdp, Zero, rtdp};
///
/// struct Routes;
///
/// impl Mdp for Routes {
/// type State = bool;
/// type Action = &'static str;
///
/// fn actions(&self, _done: &bool, mut emit: impl FnMut(&'static str, f64)) {
/// emit("sure", 3.0);
/// emit("flip", 1.0);
/// }
///
/// fn outcomes(&self, _done: &bool, action: &&'static str, mut emit: impl FnMut(bool, f64)) {
/// if *action == "sure" {
/// emit(true, 1.0);
/// } else {
/// emit(true, 0.5);
/// emit(false, 0.5);
/// }
/// }
/// }
///
/// let solution = rtdp(&Routes, false, &|done: &bool| *done, Zero, Config::default())?;
///
/// assert!(solution.is_converged());
/// assert!((solution.initial_value() - 2.0).abs() < 1e-5);
/// assert_eq!(solution.initial_action(), Some(&"flip"));
/// # Ok::<(), stochastic_search::Error<bool>>(())
/// ```