game_tree_search/lib.rs
1//! # Game-tree search
2//!
3//! Adversarial game-tree search for computer opponents in board, card and turn-based games:
4//! choosing a move when an opponent chooses the next one.
5//!
6//! ## Scope
7//!
8//! - Minimax and negamax
9//! - Alpha-beta pruning with move ordering, principal variation search (PVS/NegaScout) and MTD(f)
10//! - Iterative deepening with transposition tables, aspiration windows and quiescence search
11//! - Expectimax for games with chance nodes
12//! - Monte Carlo tree search with UCT
13//!
14//! ## Usage
15//!
16//! A [`Game`] describes positions, their moves, the [`Side`] to move and a [`Score`] from
17//! [`Side::Max`]'s point of view. The side to move belongs to the position, so one side may
18//! move several times in a row.
19//!
20//! [`minimax()`] and [`negamax()`] search every line to a fixed depth and serve as the reference.
21//! [`alpha_beta()`], [`pvs()`] and [`mtdf()`] return the same score while skipping moves that
22//! cannot change the decision, helped by [`Game::order_moves`] and, where a game allows it, by
23//! [`Game::satisfices`], which trades that exactness for speed. Each returns a [`SearchResult`]:
24//! the score and its [`Bound`], the principal variation with a score and bound per step, and every
25//! root move searched. [`deepen()`] runs alpha-beta one ply deeper at a time until a [`Budget`] of
26//! positions or time runs out, trying each iteration's principal variation first in the next; a
27//! [`Deepening`] hook can stop it after any iteration or give it aspiration windows.
28//! [`expectimax()`] searches [`Stochastic`] games, whose chance positions, such as dice rolls, are
29//! worth the [`Expectation`] of their outcomes.
30//!
31//! [`Mcts`] grows a tree by random simulations instead, for games too wide or too hard to
32//! evaluate for a full search; its selection rule ([`Uct`], or [`Puct`] guided by move
33//! priors from the [`Evaluator`]), leaf evaluation and random source are replaceable, and
34//! [`Mcts::search_batched`] hands new leaves to a [`BatchEvaluator`], such as a neural network,
35//! several at a time under virtual loss. For self-play, [`Mcts::with_root_noise`] adds
36//! Dirichlet noise to the root's priors, and an [`MctsResult`] gives the training targets
37//! ([`MctsResult::policy`], [`MctsResult::value`]) and a move chosen by temperature
38//! ([`MctsResult::sample`]).
39//!
40//! [`Game::noisy_moves`] turns on quiescence search: past the horizon, every search goes on
41//! through moves such as captures until the position is quiet.
42//!
43//! A [`Hashed`] game can give its [`Searcher`] a transposition [`Table`], such as
44//! [`SlotTable`], with [`Searcher::with_table`]: positions reached by different move orders
45//! are then answered from the table instead of being searched again. A
46//! [`Searcher`] keeps the working buffers between searches, so repeated searches stop
47//! allocating once warm.
48//!
49//! For many searches at once, such as one per game agent, keep one searcher per thread or
50//! agent and share the game by reference: searches take `&G`, and searchers are `Send`
51//! whenever their moves and scores are.
52//!
53//! Repository: <https://github.com/sunsided/game-tree-search>
54
55#![no_std]
56#![forbid(unsafe_code)]
57
58extern crate alloc;
59#[cfg(feature = "parallel")]
60extern crate std;
61
62mod alpha_beta;
63mod bound;
64mod deepening;
65mod expectimax;
66mod game;
67mod incremental;
68#[cfg(feature = "parallel")]
69mod lazy_smp;
70mod mcts;
71mod minimax;
72mod mtdf;
73mod negamax;
74mod pvs;
75mod result;
76mod score;
77mod searcher;
78mod table;
79
80pub use alpha_beta::{Window, alpha_beta};
81pub use bound::Bound;
82pub use deepening::{Deepening, deepen};
83pub use expectimax::{Stochastic, expectimax};
84pub use game::{Game, Side};
85pub use incremental::Incremental;
86#[cfg(feature = "parallel")]
87pub use lazy_smp::{deepen_shared, deepen_shared_with};
88#[cfg(feature = "rand_core")]
89pub use mcts::RandCore;
90pub use mcts::{
91 BatchEvaluator, BatchLeaf, Evaluation, Evaluator, Gumbel, Mcts, MctsResult, MoveStats,
92 NodeStats, Puct, Random, Rewarded, Rollout, RootNoise, Selection, SplitMix64, StaticValue,
93 TreeStats, Uct, Unbatched,
94};
95pub use minimax::minimax;
96pub use mtdf::mtdf;
97pub use negamax::negamax;
98pub use pvs::pvs;
99pub use result::{ScoredMove, SearchResult, Stats};
100pub use score::{Expectation, Score, TotalF32, TotalF64};
101pub use search_core::Budget;
102pub use searcher::{Algorithm, Searcher};
103#[cfg(feature = "hashbrown")]
104pub use table::MapTable;
105pub use table::{Entry, Hashed, Memory, NoTable, SlotTable, Table, TableKey, Transpositions};
106#[cfg(feature = "parallel")]
107pub use table::{Packed, PackedTable, SharedTable};
108
109/// Doctests for the code examples in README.md (not part of the API).
110#[cfg(doctest)]
111#[doc = include_str!("../README.md")]
112mod readme_doctests {}