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
//! Expectimax: minimax for games with chance positions, such as dice rolls or card draws.
use Vec;
use cratechoose;
use crate;
use crate::;
/// A game with chance positions, where an outcome is drawn instead of a side choosing.
///
/// A chance position lists its outcomes, each a move with its probability; any other
/// position is played by a [`Side`](crate::Side) as in [`Game`]. Scores must be
/// [`Expectation`]s, so a chance position can be valued at the mean of its outcomes.
///
/// Only [`expectimax()`] reads the outcomes. Every other search sees a chance position
/// through [`Game::moves`] and [`Game::side_to_move`] alone: if `moves` lists nothing
/// there, as in the example below, [`alpha_beta()`](crate::alpha_beta()) scores it as a
/// finished game. Search games with chance positions with [`expectimax()`].
/// Searches every line from `position` up to `depth` plies with expectimax.
///
/// Like [`minimax()`](crate::minimax()), except that a chance position is worth the
/// [`expectation`](Expectation::expectation) of its outcomes. An outcome counts as a ply, so
/// a chance position at the horizon is evaluated like any other. Quiescence works as in the
/// other searches, through [`Game::noisy_moves`]; a chance position is never quiesced.
///
/// The result is always [`Bound::Exact`]. At a chance position, the principal variation
/// follows the most likely outcome, so replaying it reaches a position whose evaluation is
/// that line's score, not the expectation; each step carries the expected value of the
/// position it leads to. At a chance root searched to depth 1 or more,
/// [`SearchResult::root_moves`] lists the outcomes with their values; at depth 0 the chance
/// root is only evaluated, so it lists none.
///
/// # Examples
///
/// Taking 1 point for sure, or gambling on 3 points against a loss of 2:
///
/// ```
/// use game_tree_search::{Game, Side, Stochastic, TotalF64, expectimax};
///
/// /// The choice, the gamble, and the end with its points.
/// #[derive(Debug, Clone, Copy, PartialEq)]
/// enum Bet {
/// Choose,
/// Gamble,
/// Done(f64),
/// }
///
/// /// A bet whose gamble wins with probability `odds`.
/// struct Wager {
/// odds: f64,
/// }
///
/// impl Game for Wager {
/// type Position = Bet;
/// type Move = Bet;
/// type Score = TotalF64;
///
/// fn side_to_move(&self, _: &Bet) -> Side {
/// Side::Max
/// }
///
/// fn moves(&self, position: &Bet, moves: &mut Vec<Bet>) {
/// if *position == Bet::Choose {
/// moves.extend([Bet::Done(1.0), Bet::Gamble]);
/// }
/// }
///
/// fn play(&self, _: &Bet, mv: &Bet) -> Bet {
/// *mv
/// }
///
/// fn evaluate(&self, position: &Bet) -> TotalF64 {
/// let points = if let Bet::Done(points) = *position { points } else { 0.0 };
/// TotalF64::new(points).expect("not NaN")
/// }
/// }
///
/// impl Stochastic for Wager {
/// fn outcomes(&self, position: &Bet, outcomes: &mut Vec<(Bet, f64)>) {
/// if *position == Bet::Gamble {
/// outcomes.extend([(Bet::Done(3.0), self.odds), (Bet::Done(-2.0), 1.0 - self.odds)]);
/// }
/// }
/// }
///
/// // Even odds: the gamble is worth 0.5, so the sure point wins.
/// let even = expectimax(&Wager { odds: 0.5 }, &Bet::Choose, 2);
/// assert_eq!((even.score.get(), even.best()), (1.0, Some(&Bet::Done(1.0))));
///
/// // At 75%, the gamble is worth 1.75.
/// let good = expectimax(&Wager { odds: 0.75 }, &Bet::Choose, 2);
/// assert_eq!((good.score.get(), good.best()), (1.75, Some(&Bet::Gamble)));
/// ```
/// The expectimax value of `position`, `ply` moves below the root with `depth` plies left,
/// and its principal variation in reverse (last move first).
///
/// Records every child of the root in `root_moves`, given only at the root.
/// The value of the chance `position`: the expectation over its `outcomes`, with the line of
/// the most likely one.