Expand description
§Game-tree search
Adversarial game-tree search for computer opponents in board, card and turn-based games: choosing a move when an opponent chooses the next one.
§Scope
- Minimax and negamax
- Alpha-beta pruning with move ordering, principal variation search (PVS/NegaScout) and MTD(f)
- Iterative deepening with transposition tables, aspiration windows and quiescence search
- Expectimax for games with chance nodes
- Monte Carlo tree search with UCT
§Usage
A Game describes positions, their moves, the Side to move and a Score from
Side::Max’s point of view. The side to move belongs to the position, so one side may
move several times in a row.
minimax() and negamax() search every line to a fixed depth and serve as the reference.
alpha_beta(), pvs() and mtdf() return the same score while skipping moves that
cannot change the decision, helped by Game::order_moves and, where a game allows it, by
Game::satisfices, which trades that exactness for speed. Each returns a SearchResult:
the score and its Bound, the principal variation with a score and bound per step, and every
root move searched. deepen() runs alpha-beta one ply deeper at a time until a Budget of
positions or time runs out, trying each iteration’s principal variation first in the next; a
Deepening hook can stop it after any iteration or give it aspiration windows.
expectimax() searches Stochastic games, whose chance positions, such as dice rolls, are
worth the Expectation of their outcomes.
Mcts grows a tree by random simulations instead, for games too wide or too hard to
evaluate for a full search; its selection rule (Uct, or Puct guided by move
priors from the Evaluator), leaf evaluation and random source are replaceable, and
Mcts::search_batched hands new leaves to a BatchEvaluator, such as a neural network,
several at a time under virtual loss. For self-play, Mcts::with_root_noise adds
Dirichlet noise to the root’s priors, and an MctsResult gives the training targets
(MctsResult::policy, MctsResult::value) and a move chosen by temperature
(MctsResult::sample).
Game::noisy_moves turns on quiescence search: past the horizon, every search goes on
through moves such as captures until the position is quiet.
A Hashed game can give its Searcher a transposition Table, such as
SlotTable, with Searcher::with_table: positions reached by different move orders
are then answered from the table instead of being searched again. A
Searcher keeps the working buffers between searches, so repeated searches stop
allocating once warm.
For many searches at once, such as one per game agent, keep one searcher per thread or
agent and share the game by reference: searches take &G, and searchers are Send
whenever their moves and scores are.
Repository: https://github.com/sunsided/game-tree-search
Structs§
- Batch
Leaf - A new leaf of a batch, as a
BatchEvaluatorsees it. - Entry
- What a search learned about a position.
- Evaluation
- What an
Evaluatorfound out about a new leaf. - Gumbel
- The settings of
Mcts::search_gumbel; the defaults are those of mctx. - MapTable
- A transposition table backed by a hash map, holding up to a fixed number of entries.
- Mcts
- Monte Carlo tree search with replaceable selection, leaf evaluation and random source.
- Mcts
Result - The result of a Monte Carlo tree search.
- Move
Stats - A root move with the statistics of the simulations through it.
- NoTable
- No transposition table: every position is searched, nothing is remembered.
- Node
Stats - What a search knows about one node of its tree, as selection sees it.
- Packed
Table - A transposition table several threads search with at once, without locks: each entry is packed into one 64-bit word, stored next to that word XOR-ed with the key (Hyatt and Mann’s lockless hashing).
- Puct
- Predictor-UCT (PUCT, Rosin 2011; as in
AlphaGoZero andAlphaZero): the child’s mean for the side to move plusc_puct * prior * sqrt(parent visits) / (1 + child visits). - Rand
Core - Any
rand_core::Rngas aRandomsource, for a game that shares one generator with the rest of a program (featurerand_core). - Rollout
- Random playout: random moves until the game ends or
max_plieswere played, then therewardof the position reached. - Root
Noise - Dirichlet noise mixed into the root’s move priors, as
AlphaZeroexplores in self-play. - Scored
Move - A move with the score of the position it leads to: a step of the principal variation, or a move tried at the root.
- Search
Result - The result of searching a position.
- Searcher
- Working memory for repeated searches: move lists and principal variations per ply, and
optionally a transposition table
T. - Shared
Table - A transposition table several threads search with at once:
SlotTableshards, each behind its own lock. - Slot
Table - A transposition table with a fixed number of slots, each holding at most one entry.
- Split
Mix64 - The
SplitMix64generator (Steele, Lea and Flood 2014): fast, with a full2^64period. - Static
Value - The game’s own evaluation as the leaf value, without playing on: the
rewardofevaluate. - Stats
- Counters collected during one search run.
- Total
F32 - An
f32score that is never NaN, ordered byf32::total_cmp. - Total
F64 - An
f64score that is never NaN, ordered byf64::total_cmp. - Transpositions
- A
Tableserving as a searcher’sMemory; built bySearcher::with_table. - Tree
Stats - Counters of one Monte Carlo tree search, which its
Budgetsees. - Uct
- Upper confidence bounds applied to trees (UCT, Kocsis and Szepesvári 2006): the child’s
mean for the side to move plus
exploration * sqrt(ln(parent visits) / child visits). - Unbatched
- An
Evaluatorused one leaf at a time as aBatchEvaluator, with its own generator: for testing a batched setup, or for evaluators that gain nothing from batches. - Window
- The scores still worth telling apart at a position, from
Side::Max’s point of view.
Enums§
- Algorithm
- The search a
Searcherruns at each depth, for one search and for every iteration of deepening. - Bound
- How a reported score relates to the true minimax value.
- Side
- The player who chooses the move in a position.
Traits§
- Batch
Evaluator - Values several new leaves in one call, as a neural network on a GPU wants to; see
Mcts::search_batched. - Budget
- Decides when a search must stop before it finishes, given its counters so far.
- Deepening
- Decides after each finished iteration whether deepening goes on.
- Evaluator
- Values a leaf the first time a simulation reaches it.
- Expectation
- A
Scorethat can be averaged, as chance positions need. - Game
- A two-player, zero-sum game as seen by the searches.
- Hashed
- A game whose positions have a hash key, so a transposition table can recognize them.
- Incremental
- A game whose positions can be changed in place and changed back, so the alpha-beta
family can search with one position instead of one per move
(
Searcher::search_in_place,Searcher::deepen_in_place). - Memory
- How a
Searcherremembers positions of a gameG. - Packed
- A move or score that fits into a few bits, so a
PackedTablecan store it in an atomic word. - Random
- A source of random bits for rollouts.
- Rewarded
- A game whose scores map to rewards, as Monte Carlo tree search averages them.
- Score
- A position’s value, from the point of view of
Side::Max. - Selection
- Chooses which visited child a simulation descends into.
- Stochastic
- A game with chance positions, where an outcome is drawn instead of a side choosing.
- Table
- Storage for
Entryvalues by position key. - Table
Key - A position key that a fixed-size table can place in a slot.
Functions§
- alpha_
beta - Searches
positionup todepthplies with fail-soft alpha-beta pruning. - deepen
- Searches
positionwith alpha-beta at depth 1, 2, … up tomax_depth, untilbudgetruns out. - deepen_
shared - Deepens
positionwiththreadsthreads that sharetable(Lazy SMP) and returns the result of the thread that got furthest. - deepen_
shared_ with - Deepens
positionlikedeepen_shared, with the main thread askingdeepeningafter each finished iteration whether to go on, and for each iteration’s aspiration window, asSearcher::deepen_withdoes. - expectimax
- Searches every line from
positionup todepthplies with expectimax. - minimax
- Searches every line from
positionup todepthplies and returns the minimax score. - mtdf
- Searches
positionup todepthplies with MTD(f). - negamax
- Searches every line from
positionup todepthplies, in negamax form. - pvs
- Searches
positionup todepthplies with principal variation search.