Search

Search papers and lessons

Monte Carlo Tree Search

Level 3 · 14 min read

Monte Carlo tree search grows a search tree by repeatedly selecting, expanding, simulating and backing up results, using a bandit rule to decide where to look next.

On this page

  • Colour = topic mix
  • Probability
  • Reinforcement learning
  • Algorithms
  • Discrete math
  • Sampling
  • Sign in to see your progress

TL;DR

Monte Carlo tree search (MCTS) decides on a move by building a search tree one iteration at a time. Each iteration walks down the tree by a bandit rule, adds a new node, estimates that node's value by playing random moves to the end, and passes the result back up the path. After many iterations, the child of the root that was visited most is chosen.

Where you'll see it: in game programs such as Go engines and in AlphaZero-style systems, where a learned network guides the same loop, and in planning problems where the full tree is too large to enumerate.

Intuition

Suppose you are choosing a move in a game you cannot fully analyse. Exhaustive search of every line is impossible. A cheaper strategy is to play many quick games from each candidate move, count the wins, and favour the moves that win most often. That is the Monte Carlo part: random play gives a noisy estimate of how good a position is, namely how often it wins under random continuation.

The tree part makes this efficient. Random play from the root wastes effort on moves that are clearly bad. MCTS keeps a record of every position it has visited, and it uses that record to focus later simulations on promising lines. The focus comes from the multi-armed bandit idea of Multi-Armed Bandit: at each decision point, the algorithm balances the best-looking child against the children it knows least about.

The loop is shown below. The four phases repeat until the time or iteration budget runs out.

flowchart LR
  S[Selection: walk down by bandit rule] --> E[Expansion: add one new child]
  E --> R[Simulation: play random moves to the end]
  R --> B[Backpropagation: update counts and wins on the path]
  B --> S

The math

The search tree

Treat the game as a Markov Decision Process or as a two-player game. Each node vv stores a visit count N(v)N(v) and a total value W(v)W(v), the sum of the outcomes from simulations that passed through vv. The mean value is W(v)/N(v)W(v)/N(v). The outcome of a finished game is usually 11 for a win and 00 for a loss, or 11, 12\tfrac{1}{2}, 00 for win, draw and loss.

For a two-player game, the value stored at a node is usually from the point of view of the player who moved into that node. This keeps the backup simple: each node prefers the child that is good for the player to move there.

The four phases

  1. Selection. Start at the root. While the current node has been fully expanded (all its legal children exist in the tree), move to the child with the highest score, given by the rule in the next subsection.
  2. Expansion. At a node with an unexplored legal move, add one child for that move.
  3. Simulation. From the new node, play moves to the end of the game. The simplest policy chooses moves uniformly at random. A learned policy or value network can replace the random play, but the phase is the same: produce an outcome.
  4. Backpropagation. Walk back from the new node to the root. At each node on the path, increase N(v)N(v) by one and add the outcome to W(v)W(v), taking the perspective of the player who moved into that node.

After the iterations, the move is chosen by the most-visited child of the root. Visit counts are preferred to mean values because they are more robust to a child that won only by luck on very few simulations.

The selection rule: UCT

The selection rule is UCB1 from Multi-Armed Bandit applied to each node's children. Let vv be a node with parent visit count NN and let cc be an exploration constant. The UCT score of child jj is

UCTj=W(j)N(j)+cln⁡NN(j).\mathrm{UCT}_j = \frac{W(j)}{N(j)} + c\sqrt{\frac{\ln N}{N(j)}}.

The first term exploits children with good records. The second explores children that have few visits relative to their parent. For outcomes in [0,1][0, 1], the standard choice is c=2c = \sqrt{2}, which is the same constant as in the bandit bound. In practice cc is tuned for the game and the value scale.

Kocsis and Szepesvári showed that applying UCB1 at every node makes the estimated value at the root converge to the true minimax value as the number of simulations grows. This consistency result is the theoretical reason the rule works, though it says nothing about how many simulations a given game needs.

Worked examples

Example
One selection step

The root has two children, A and B. After N=4N = 4 iterations total, A has been visited N(A)=3N(A) = 3 times with total value W(A)=2W(A) = 2, and B has been visited N(B)=1N(B) = 1 time with value W(B)=1W(B) = 1. Use c=2≈1.4142c = \sqrt{2} \approx 1.4142.

  1. ln⁡4≈1.3863\ln 4 \approx 1.3863.
  2. For A: mean 2/3≈0.66672/3 \approx 0.6667. Exploration term cln⁡4/3=1.4142⋅0.4621=1.4142⋅0.6798≈0.9614c\sqrt{\ln 4 / 3} = 1.4142 \cdot \sqrt{0.4621} = 1.4142 \cdot 0.6798 \approx 0.9614. Score ≈1.6281\approx 1.6281.
  3. For B: mean 1.01.0. Exploration term cln⁡4/1=1.4142⋅1.1774≈1.6651c\sqrt{\ln 4 / 1} = 1.4142 \cdot 1.1774 \approx 1.6651. Score ≈2.6651\approx 2.6651.
  4. B has the higher score, so selection goes to B.

B has a perfect record but only one visit, so its exploration term is large. Selection tries it again before committing to A, which is the bandit trade-off in one step. If B's single simulation had been a loss, its mean would be 00 and its score would be 1.66511.6651, still above A's 1.62811.6281. Exploration is strong in the early iterations and fades as visits accumulate.

Example
Choosing the final move

After 100 iterations at the root, three children have these counts and total values:

ChildVisits NNWins WWMean W/NW/N
A60360.60
B30210.70
C1060.60
  1. The most-visited child is A, with 60 visits.
  2. The child with the highest mean is B, at 0.70.
  3. MCTS normally plays A. The mean of B is higher but based on 30 simulations, against 60 for A.
  4. The visit count reflects where the search spent its effort, and the search spends more effort on moves whose bandit score stays high. A move that keeps being explored and still looks good is usually a more reliable choice than one with a high mean from fewer samples.

If the budget were doubled and B's mean held at 0.70 with its visits growing faster, B would overtake A in visits, and the two criteria would agree.

Tradeoffs and failure modes

SituationProblemBetter choice
Branching factor is very largeExpansion is slow and simulations rarely reach the same positions twiceProgressive widening, or a learned policy prior to rank children
Rollouts are random and the game is longRandom playouts are noisy and rarely informativeReplace rollouts with a learned value estimate
Game is deterministic and smallFull minimax with pruning is exact and cheapAlpha-beta search, see Minimax Game
Time limit is tightToo few iterations leave the tree shallowTune cc and the simulation policy; reuse the tree between moves
Opponent behaviour is known and exploitableUniform random rollouts assume a random opponentUse a stronger rollout policy or a learned model
Values are not in [0,1][0, 1]The c=2c = \sqrt{2} constant assumes that scaleNormalise outcomes or retune cc

The main failure is over-trusting random rollouts in games where the outcome depends on a few critical moves. A rollout can miss a tactic that a shallow search would find, and the tree then reinforces the wrong move. Strong systems combine MCTS with a value estimate so that simulations stop early and the estimate carries the rest.

History

The bandit-based selection rule for trees was introduced by Levente Kocsis and Csaba Szepesvári in 2006 as UCT (upper confidence bounds applied to trees). Around the same time, Rémi Coulom developed a tree-search Go program that used random playouts with a selective tree, and he coined the name "Monte Carlo tree search". Go programs such as MoGo and Crazy Stone then improved strongly on earlier methods, and MCTS became the standard approach for Go and for many other games where the branching factor was too large for classical search.

Later systems combined MCTS with deep neural networks. The AlphaGo line and AlphaZero used a learned policy to guide selection and a learned value in place of most random playouts, and MuZero extended the approach to learned models. The core loop of selection, expansion, evaluation and backup survives in all of them.

Check yourself

QuestionIn worked example 1, if B's visit count were 4 with value 3 and A stayed the same, which child would selection pick and why?Show answer

The root now has N=3+4=7N = 3 + 4 = 7 visits, so both exploration terms use ln⁡7≈1.9459\ln 7 \approx 1.9459. For B: mean 0.750.75, exploration term cln⁡7/4=1.4142⋅0.4865=1.4142⋅0.6974≈0.9864c\sqrt{\ln 7 / 4} = 1.4142 \cdot \sqrt{0.4865} = 1.4142 \cdot 0.6974 \approx 0.9864, score ≈1.7364\approx 1.7364. For A: mean 0.66670.6667, exploration term cln⁡7/3=1.4142⋅0.8054≈1.1390c\sqrt{\ln 7 / 3} = 1.4142 \cdot 0.8054 \approx 1.1390, score ≈1.8057\approx 1.8057. A is chosen. Its lower mean is outweighed by its exploration term, which is larger because it has fewer visits relative to the parent. Both terms must be recomputed from current counts at every selection.

QuestionWhy does MCTS back up the outcome to every node on the path, not only to the leaf?Show answer

The value of a leaf is an estimate for that position alone. Backing it up to every ancestor means each node's mean reflects all the simulations that passed through it, which is how the parent's choice among its children gets information. Without backup, the root would have no record of which first moves led to good outcomes, and selection could not use the bandit rule at the upper levels.

QuestionA researcher says that choosing the child with the highest mean value is always better than choosing the most-visited child. Assess this.Show answer

Not always. The mean is noisy when a child has few visits. A child that won the first two simulations has mean 1.0 but almost no evidence. Visit count is a proxy for how much the search trusted that child: the bandit rule keeps visiting a child whose score stays high, so a heavily visited child has survived many comparisons. Some implementations use a combination, such as the most-visited child with a minimum mean, to avoid both failure modes.

QuestionIn a two-player game, why are values stored from the perspective of the player who moved into a node?Show answer

At a node where player X has just moved, the parent node's decision is made by the other player, who wants the children that are good for them. Storing the value from the mover's point of view lets each node pick the child with the highest value for the player choosing among its children, and the sign is consistent through the tree. Storing from a fixed player's view would require flipping signs at alternating levels, which is easy to get wrong.

Sources