Skip to main content

Crate game_tree_search

Crate game_tree_search 

Source
Expand description

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§

BatchLeaf
A new leaf of a batch, as a BatchEvaluator sees it.
Entry
What a search learned about a position.
Evaluation
What an Evaluator found 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.
MctsResult
The result of a Monte Carlo tree search.
MoveStats
A root move with the statistics of the simulations through it.
NoTable
No transposition table: every position is searched, nothing is remembered.
NodeStats
What a search knows about one node of its tree, as selection sees it.
PackedTable
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 AlphaGo Zero and AlphaZero): the child’s mean for the side to move plus c_puct * prior * sqrt(parent visits) / (1 + child visits).
RandCore
Any rand_core::Rng as a Random source, for a game that shares one generator with the rest of a program (feature rand_core).
Rollout
Random playout: random moves until the game ends or max_plies were played, then the reward of the position reached.
RootNoise
Dirichlet noise mixed into the root’s move priors, as AlphaZero explores in self-play.
ScoredMove
A move with the score of the position it leads to: a step of the principal variation, or a move tried at the root.
SearchResult
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.
SharedTable
A transposition table several threads search with at once: SlotTable shards, each behind its own lock.
SlotTable
A transposition table with a fixed number of slots, each holding at most one entry.
SplitMix64
The SplitMix64 generator (Steele, Lea and Flood 2014): fast, with a full 2^64 period.
StaticValue
The game’s own evaluation as the leaf value, without playing on: the reward of evaluate.
Stats
Counters collected during one search run.
TotalF32
An f32 score that is never NaN, ordered by f32::total_cmp.
TotalF64
An f64 score that is never NaN, ordered by f64::total_cmp.
Transpositions
A Table serving as a searcher’s Memory; built by Searcher::with_table.
TreeStats
Counters of one Monte Carlo tree search, which its Budget sees.
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 Evaluator used one leaf at a time as a BatchEvaluator, 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 Searcher runs 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§

BatchEvaluator
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 Score that 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 Searcher remembers positions of a game G.
Packed
A move or score that fits into a few bits, so a PackedTable can 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 Entry values by position key.
TableKey
A position key that a fixed-size table can place in a slot.

Functions§

alpha_beta
Searches position up to depth plies with fail-soft alpha-beta pruning.
deepen
Searches position with alpha-beta at depth 1, 2, … up to max_depth, until budget runs out.
deepen_shared
Deepens position with threads threads that share table (Lazy SMP) and returns the result of the thread that got furthest.
deepen_shared_with
Deepens position like deepen_shared, with the main thread asking deepening after each finished iteration whether to go on, and for each iteration’s aspiration window, as Searcher::deepen_with does.
expectimax
Searches every line from position up to depth plies with expectimax.
minimax
Searches every line from position up to depth plies and returns the minimax score.
mtdf
Searches position up to depth plies with MTD(f).
negamax
Searches every line from position up to depth plies, in negamax form.
pvs
Searches position up to depth plies with principal variation search.