Foundations to Frontier
Tests
L. Reinforcement Learning and Autonomy › L6 Planning and Control

Monte Carlo tree search

Monte Carlo tree search (MCTS) plans by simulation. It grows a search tree from the current position, choosing which branch to explore with a bandit rule, estimating new positions with random playouts, and backing the results up the tree. With enough simulations its choices converge to those of exact minimax, yet it needs no evaluation function. This page builds UCT and flat Monte Carlo for tic-tac-toe, where exact minimax values are available, and measures how often each picks an optimal move and how each fares against a perfect player.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Thinking ahead by imagining many games

A chess player considering a move imagines how the game might go: a few replies, a few answers to those, focusing on the lines that look most promising or most dangerous. Monte Carlo tree search does the same with a computer: it imagines thousands of quick, random games from the current position, keeps statistics on which first moves tend to win, and spends more of its imagination on the moves that look best so far. The more games it imagines, the better its choice.

Planning and control lab
X
55draw
44loss
52draw
O
40draw
41loss
68draw
X
O to move. Each empty square shows how many of the 300 UCT simulations went through that move, and its exact minimax outcome for the player to move. UCT plays the most-visited square (shaded): 7, which is optimal. Flat Monte Carlo with the same rollouts, spread evenly and scored against random replies, chooses 2 (a mistake). UCT spends its simulations on the promising moves and, deeper in the tree, on the opponent's best replies, so its values approach minimax; flat Monte Carlo assumes a random opponent forever.
Why engineers care

MCTS was the breakthrough behind computer Go, sits at the heart of AlphaGo, AlphaZero and MuZero, and is used for planning in robotics, chemistry and language-model reasoning.

📝 Quick check (Level 0)
0/3 answered
  1. 1. MCTS estimates a move's quality by…
  2. 2. MCTS spends more simulations on…
  3. 3. MCTS was the breakthrough behind computer…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Search tree: nodes are positions (or action sequences) reached from the root; each stores a visit count N and total result W.
  • The four steps of MCTS: selection (descend the tree by a rule), expansion (add one new child), simulation (play to the end, often randomly), backup (add the result to every node on the path).
  • UCT (Kocsis and Szepesvári 2006): select the child maximising W/N + c√(ln N_parent / N), treating each node as a multi-armed bandit (UCB1).
  • Flat Monte Carlo: spread rollouts over the root moves only and pick the best average; it never builds a tree.
  • Final move choice: usually the most visited child, which is more robust than the highest mean.
  • PUCT (AlphaZero): replace random rollouts by a value network and guide selection with a policy network prior: Q + c·P·√N_parent/(1 + N).
  • Anytime property: MCTS can be stopped at any moment and returns its current best move.
📝 Quick check (Level 1)
0/3 answered
  1. 1. The four steps of MCTS are…
  2. 2. UCT treats each node as…
  3. 3. The final move is usually…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

UCT and flat Monte Carlo on tic-tac-toe

Exercise 1.

  • Tic-tac-toe with exact minimax values for every position (4,520 non-terminal positions are reachable from the empty board).
  • UCT with c = √2: tree nodes are move sequences, expansion adds one untried move, rollouts play uniformly random moves, and each node scores 1, ½ or 0 for the player who moved into it.
  • Flat Monte Carlo: the same number of rollouts spread evenly over the legal moves, choosing the best average.
  • A move is correct if its minimax value equals the position’s value.

Practice.

  • Start with plain UCT and random rollouts when you have a simulator and no evaluation function; tune c and the number of simulations.
  • Replace rollouts by a learned value and add a policy prior (PUCT) when you can train networks: this is AlphaZero, and it scales to Go and chess.
  • Learn the model too if no simulator exists: MuZero plans with MCTS inside a learned latent model (topic L5-1).
  • Use progressive widening or sampled actions for large or continuous action spaces, and determinisation or information-set MCTS for hidden information.
  • Tools: OpenSpiel and mctx (DeepMind’s JAX MCTS library).
def mcts(root, n_sims, rng, c=math.sqrt(2)):
    N, W, board = {(): 0}, {(): 0.0}, {(): root}
    for _ in range(n_sims):
        node = (); path = [node]
        while winner(board[node]) is None:
            b = board[node]; kids = [node + (m,) for m in legal(b)]
            untried = [k for k in kids if k not in N]
            if untried:                                    # expansion
                node = untried[rng.integers(len(untried))]
                board[node] = play(b, node[-1]); N[node] = W[node] = 0
                path.append(node); break
            node = max(kids, key=lambda k: W[k] / N[k]      # selection (UCB1)
                       + c * math.sqrt(math.log(N[path[-1]]) / N[k]))
            path.append(node)
        z = rollout(board[node], rng)                      # simulation
        for nd in path:                                    # backup
            N[nd] += 1; W[nd] += score(z, mover_into(nd))
🐍 Exercise 1: UCT for tic-tac-toe
Implement minimax with memoisation and UCT, check that UCT finds an immediate win and avoids a losing move in a tactical position, and look at how it spreads its simulations.
📝 Quick check (Level 2)
0/3 answered
  1. 1. AlphaZero replaces random rollouts with…
  2. 2. MuZero plans inside…
  3. 3. For continuous actions MCTS uses…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

UCT converges to minimax. At each node UCB1 tries every child infinitely often but the best child exponentially more often, with regret growing only logarithmically. Kocsis and Szepesvári (2006) showed by induction from the leaves that the failure probability at the root goes to zero as the number of simulations grows, for finite-horizon games: each node’s mean result converges to its minimax value.

Why flat Monte Carlo can fail forever. Flat Monte Carlo estimates each move’s value against uniformly random play by both sides. That converges, but to the wrong quantity: a move can win against random replies most of the time while losing to the one correct reply. More rollouts only sharpen this wrong estimate. UCT avoids it because, deeper in the tree, it also concentrates the opponent’s moves on their best replies.

Bias and variance of rollouts. Random rollouts give unbiased estimates of the value under random play, which is a biased estimate of the minimax value. A learned value function trades this for function-approximation error, which is the basis of AlphaGo and AlphaZero’s success.

Choosing c. For results in [0, 1], c = √2 matches the UCB1 bound; smaller c exploits more and finds forced lines faster in tactical positions, larger c explores more. AlphaZero tunes the corresponding constant in PUCT.

📝 Quick check (Level 3)
0/3 answered
  1. 1. UCT's choice converges to…
  2. 2. Flat Monte Carlo converges to the value against…
  3. 3. Random rollouts are biased estimates of…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How often do UCT and flat Monte Carlo choose a minimax-optimal move as the number of rollouts grows, and how do they fare against a perfect player?

History.

  • B. Abramson (1990) proposed estimating positions by the outcome of random games; B. Brügmann (1993) applied Monte Carlo to Go.
  • R. Coulom (2006) introduced Monte Carlo tree search in Crazy Stone; L. Kocsis and C. Szepesvári (2006) introduced UCT; S. Gelly and colleagues (2006) built MoGo.
  • D. Silver and colleagues (2016) combined MCTS with policy and value networks in AlphaGo; AlphaZero (2018) and MuZero (2020) followed.
  • C. Browne and colleagues (2012) surveyed MCTS methods.

Exercise 2.

  • 40 positions sampled from the 895 reachable positions with at least 5 empty squares whose moves differ in minimax value.
  • UCT and flat Monte Carlo with 10, 30, 100, 300, 1,000 and 3,000 rollouts per decision; a choice is correct if it is minimax-optimal.
  • Then 20 games of each method (30 or 300 rollouts per move) against a perfect player that picks uniformly among optimal moves, alternating who starts. Against perfect play the best result is a draw.

Seed 0, share of positions where the chosen move was optimal:

rollouts 10 30 100 300 1,000 3,000
UCT 0.550 0.825 0.875 1.000 1.000 1.000
flat Monte Carlo 0.600 0.800 0.900 0.925 0.925 0.925
  • UCT reached perfect play on these positions: 1.000 with 3,000 rollouts on all three seeds (0.95 to 1.000 from 300 on).
  • Flat Monte Carlo stalled: 0.925 to 0.95 with 3,000 rollouts on all seeds, because in some positions the move that is best against random replies is a mistake against the correct one.
  • With very few rollouts the two were similar (0.55 to 0.6 with 10), since UCT has no tree to speak of yet.
  • Against the perfect player, UCT with 300 rollouts per move lost 4, 3 and 4 of 20 games on the three seeds (drawing the rest), against 9, 7 and 6 losses for flat Monte Carlo; with 30 rollouts both lost most games (8 to 14 losses). Opening positions, with more empty squares than the test set, still need more simulations.
  • Exercise 1: UCT with 300 simulations found the immediate win; with 2,000 it chose a safe edge where corners lose, spreading its visits almost evenly over the four safe moves (426 to 447 each) and giving the losing corners 124 and 132.
🐍 Exercise 2 (research): how often do UCT and flat Monte Carlo choose a minimax-optimal move as the number of rollouts grows, and how do they fare against a perfect player?
Measure how often UCT and flat Monte Carlo choose minimax-optimal moves on 40 tactical tic-tac-toe positions with 10 to 3,000 rollouts, and play each against a perfect player.

MCTS in practice

  • Board games: AlphaZero-style PUCT search with networks is the standard for Go, chess, shogi and many other games.
  • Planning with learned models: MuZero and EfficientZero plan in learned latent spaces for Atari and control.
  • Combinatorial problems: MCTS guides chemical retrosynthesis, program synthesis and matrix-multiplication discovery (AlphaTensor).
  • Language models: tree search over reasoning steps with a learned verifier or value model is an active area.

References

  1. L. Kocsis and C. Szepesvári (2006). “Bandit based Monte-Carlo planning.” ECML.
  2. R. Coulom (2006). “Efficient selectivity and backup operators in Monte-Carlo tree search.” Computers and Games.
  3. C. Browne and colleagues (2012). “A survey of Monte Carlo tree search methods.” IEEE Transactions on Computational Intelligence and AI in Games.
  4. D. Silver and colleagues (2018). “A general reinforcement learning algorithm that masters chess, shogi, and Go through self-play.” Science.
  5. J. Schrittwieser and colleagues (2020). “Mastering Atari, Go, chess and shogi by planning with a learned model.” Nature.