game-tree-search
Adversarial game-tree search for computer opponents in board, card and turn-based games: minimax, negamax, alpha-beta, principal variation search, MTD(f), iterative deepening with transposition tables, expectimax and Monte Carlo tree search
cargo add game-tree-search Who it is for
- Game developers writing a computer opponent for a two-sided, turn-based game: board games, card games, tactics and combat systems. The side to move belongs to the position, so turn orders by initiative or speed, where one side may act several times in a row, work as they are.
- Opponents that explain themselves or scale in difficulty: a search returns the chosen move with its score, the expected line of play, and the alternatives with their scores or bounds, so a game can show what its opponent considered or pick a weaker move on purpose.
- Games with a frame or time limit: a node or time budget stops iterative deepening with the best move found so far, and a reusable searcher allocates nothing per position once warm. The crate is
no_std, so it also runs on consoles, handhelds and in WebAssembly.
It is not for single-agent problems: for pathfinding, see heuristic-search; for puzzles, including checking that a puzzle has a unique solution and grading it, see constraint-search. Games with hidden information, such as most trick-taking card games, search sampled deals of the hidden cards, one deal at a time; constraint-search draws deals consistent with what a player has seen, and tells which cards are certain or likely.
Algorithms 18
- Alpha-beta pruning Alpha-beta · αβ search
Fail-soft alpha-beta with move ordering, transposition-table cutoffs and an optional satisficing stop; returns the minimax score.
- AlphaZero self-play targets Self-play training targets · Root Dirichlet noise
Dirichlet noise on the root's priors, the visit distribution π and root value as the training target, and move choice by temperature.
- Batched MCTS with virtual loss Virtual loss · Batched leaf evaluation
New leaves wait under virtual loss until an evaluator values them together, as a neural network on a GPU wants to; the batch's values are backed up and its priors set.
- Expectimax Expectiminimax
Minimax with chance positions valued at the probability-weighted mean of their outcomes, searching every line to a fixed depth.
- Gumbel MCTS Gumbel MuZero root selection · Sequential halving with Gumbel
Sequential halving with Gumbel noise at the root and deterministic selection towards the improved policy below it, ported from mctx; returns the improved policy as the training target.
- Iterative deepening alpha-beta Iterative deepening (game-tree search)
Alpha-beta, PVS or MTD(f) one ply deeper at a time under a node or time budget, each iteration trying the last principal variation first.
-
Several threads deepen the same root through one shared transposition table (SharedTable or the lockless PackedTable); helpers keep the game's first move and rotate the rest, and the furthest thread's result wins.
-
Entries packed into one 64-bit word stored next to that word XOR-ed with the key, so threads probe and store without locks and a torn entry reads as a miss.
- Minimax Minimax search
Searches every line to a fixed depth; the reference the faster searches are tested against. The side to move comes from the position, so one side may move several times in a row.
- Monte Carlo tree search MCTS · UCT · Upper confidence bounds applied to trees
MCTS under a simulation or node budget, with replaceable selection (UCT or PUCT), leaf evaluation (random rollout or the static value) and random source; reports visits and values per root move, and keeps the subtree of the moves played for the next search.
- MTD(f) Memory-enhanced Test Driver
Null-window passes of the root that close in on its score from a first guess, then one search around the score for the principal variation.
-
Minimax written for one side, negating a child's score only where the side to move changes.
- Principal variation search PVS · NegaScout
Alpha-beta that scouts every move after the first with a null window and searches it again only when the scout fails high.
- PUCT Predictor-UCT · PUCB
MCTS selection by mean plus c_puct · prior · √(parent visits) / (1 + child visits), with move priors from the evaluator; uniform priors without them.
- Quiescence search Stand pat · Capture search
Past the horizon, every search goes on through the moves a game marks as noisy, and the side to move may stand pat on the evaluation.
-
Independent MCTS trees of one root on std threads, each with its own budget and seeded generator, their root visits and values added up move by move.
- Transposition table Hash table (game-tree search)
Remembers scores, bounds and best moves by position key, so a position reached by another move order is answered instead of searched; SlotTable is fixed-size, MapTable (feature hashbrown) grows.
-
Several threads grow one tree, selecting and backing up under one lock and evaluating outside it, spread over different lines by virtual loss.