game_tree_search/result.rs
1//! What a search returns: [`SearchResult`] with the score, its [`Bound`], the principal
2//! variation and the root moves tried as [`ScoredMove`]s, and [`Stats`].
3
4use alloc::vec::Vec;
5
6use crate::Bound;
7
8/// Counters collected during one search run.
9///
10/// Non-exhaustive: later algorithms may add counters. Start from [`Stats::default`] where a
11/// value is needed.
12#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
13#[non_exhaustive]
14pub struct Stats {
15 /// Positions visited, the root included: each one was either expanded or evaluated.
16 pub nodes: u64,
17 /// Positions whose remaining moves were skipped, by a window cutoff or because
18 /// [`Game::satisfices`](crate::Game::satisfices) said so. Always `0` for the reference
19 /// searches.
20 pub cuts: u64,
21 /// Positions reached with no plies left, whether or not the game goes on there: scored by
22 /// [`Game::evaluate`](crate::Game::evaluate), or searched on through their
23 /// [`Game::noisy_moves`](crate::Game::noisy_moves), whose positions count here too.
24 pub horizon: u64,
25 /// The deepest ply visited: the most moves played from the root to any position the
26 /// search reached.
27 pub max_ply: u32,
28 /// Positions answered by a transposition table entry instead of being searched.
29 pub table_hits: u64,
30 /// Children searched a second time because a principal variation search scout found
31 /// them better than expected. Always `0` for the other algorithms.
32 pub researches: u64,
33 /// Root searches MTD(f) ran, its final and fallback searches included. Always `0` for
34 /// the other algorithms.
35 pub passes: u64,
36 /// Iterations searched again with a wider window because the score fell outside an
37 /// aspiration window (see [`Deepening::window`](crate::Deepening::window)).
38 pub widenings: u64,
39}
40
41impl Stats {
42 /// All counters at zero.
43 pub(crate) const ZERO: Self = Self {
44 nodes: 0,
45 cuts: 0,
46 horizon: 0,
47 max_ply: 0,
48 table_hits: 0,
49 researches: 0,
50 passes: 0,
51 widenings: 0,
52 };
53
54 /// Counts a visit to a position `ply` moves below the root.
55 pub(crate) fn visit(&mut self, ply: u32) {
56 self.nodes += 1;
57 self.max_ply = self.max_ply.max(ply);
58 }
59}
60
61/// A move with the score of the position it leads to: a step of the principal variation,
62/// or a move tried at the root.
63///
64/// Non-exhaustive: later searches may report more per move.
65#[derive(Debug, Clone, PartialEq, Eq)]
66#[non_exhaustive]
67pub struct ScoredMove<M, S> {
68 /// The move.
69 pub mv: M,
70 /// The score of the position the move leads to, from
71 /// [`Side::Max`](crate::Side::Max)'s point of view.
72 pub score: S,
73 /// How [`ScoredMove::score`] relates to that position's minimax value. Root moves other
74 /// than the best are often only bounded, since the search stops once they are proven
75 /// worse.
76 pub bound: Bound,
77}
78
79/// Where a reference search records the root's moves: given only at the root.
80pub(crate) type RootMoves<'a, G> =
81 Option<&'a mut Vec<ScoredMove<<G as crate::Game>::Move, <G as crate::Game>::Score>>>;
82
83/// A line of play in `G`, one scored move per ply.
84pub(crate) type Line<G> = Vec<ScoredMove<<G as crate::Game>::Move, <G as crate::Game>::Score>>;
85
86impl<M, S> ScoredMove<M, S> {
87 /// `mv`, leading to a position whose minimax value is `score`.
88 pub(crate) const fn exact(mv: M, score: S) -> Self {
89 Self {
90 mv,
91 score,
92 bound: Bound::Exact,
93 }
94 }
95}
96
97/// The result of searching a position.
98///
99/// Non-exhaustive: later algorithms may report more.
100#[derive(Debug, Clone, PartialEq, Eq)]
101#[non_exhaustive]
102pub struct SearchResult<M, S> {
103 /// The score of the root from [`Side::Max`](crate::Side::Max)'s point of view, assuming
104 /// both sides play the principal variation.
105 pub score: S,
106 /// How [`SearchResult::score`] relates to the root's minimax value; always
107 /// [`Bound::Exact`] for the reference searches.
108 pub bound: Bound,
109 /// The principal variation: the best line of play from the root, one move per ply, each
110 /// with the score and bound of the position it leads to.
111 ///
112 /// Ends where the game ends or the depth runs out; empty if the root is evaluated
113 /// directly. Playing it from the root reaches a position whose
114 /// [`evaluate`](crate::Game::evaluate) is [`SearchResult::score`], unless the line ends
115 /// early at a position a transposition table answered, which keeps no line below it.
116 pub pv: Vec<ScoredMove<M, S>>,
117 /// Every root move searched, in the order searched; the best is among them. A root searched
118 /// to depth `0` searches only its [`Game::noisy_moves`](crate::Game::noisy_moves).
119 pub root_moves: Vec<ScoredMove<M, S>>,
120 /// The legal root moves never searched because the search stopped early.
121 pub unexamined: usize,
122 /// The depth searched, in plies. For [`deepen()`](crate::deepen()), the deepest iteration
123 /// that finished.
124 pub depth: u32,
125 /// Whether no line ran into the depth limit, so every line was searched to the end of the
126 /// game: a deeper search would return the same result. A line whose game ends exactly at
127 /// the depth limit still counts as running into it. Only searched lines count, so a search
128 /// that prunes more, for example thanks to a better move order, is proven sooner.
129 pub proven: bool,
130 /// What the search did. For [`deepen()`](crate::deepen()), summed over every iteration,
131 /// including one the budget stopped.
132 pub stats: Stats,
133}
134
135impl<M, S> SearchResult<M, S> {
136 /// The move to play at the root: the first move of the principal variation.
137 ///
138 /// `None` if the root ends the game or was searched to depth `0`.
139 #[must_use]
140 pub fn best(&self) -> Option<&M> {
141 self.pv.first().map(|step| &step.mv)
142 }
143}