Planning & Search in Rust
Esc

Loading…

↑↓ move ↵ open Esc close
Colour theme
Game-tree search

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.

    alpha_beta

  • 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.

    RootNoisepolicy

  • 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.

    search_batchedBatchEvaluator

  • Expectimax Expectiminimax

    Minimax with chance positions valued at the probability-weighted mean of their outcomes, searching every line to a fixed depth.

    expectimaxStochastic

  • 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.

    search_gumbelGumbel

  • 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.

    deepen

  • Lazy SMP Shared hash table parallel search feature parallel

    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.

    deepen_shareddeepen_shared_withSharedTable

  • Lockless transposition table Lockless hashing · XOR-keyed transposition table feature parallel

    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.

    PackedTablePacked

  • 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.

    minimax

  • 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.

    mtdf

  • Minimax written for one side, negating a child's score only where the side to move changes.

    negamax

  • 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.

    Puct

  • Root-parallel MCTS Root parallelization · Root parallelism feature parallel

    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.

    search_parallel

  • 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.

    HashedSlotTable

  • Tree-parallel MCTS Tree parallelization · Global-mutex tree parallelism feature parallel

    Several threads grow one tree, selecting and backing up under one lock and evaluating outside it, spread over different lines by virtual loss.

    search_shared