Lecture 06.3: Monte Carlo Tree Search (MCTS)
Scaling adversarial search to massive state spaces using randomness and the UCB1 algorithm in Monte Carlo Tree Search.
While Alpha-Beta pruning is highly effective for games like Chess, it still relies on exploring an exponentially growing tree. For incredibly complex games like Go, brute-force search—even with pruning—is computationally infeasible.
Monte Carlo Tree Search (MCTS) is a best-first search algorithm that uses random simulations to make decisions in massive state spaces. It prioritizes nodes that are more likely to lead to an optimal solution, drastically reducing unnecessary exploration without needing complex domain-specific heuristic knowledge.
The Four Steps of MCTS
MCTS figures out the best move by repeating a four-step process until computation time runs out. It learns the policy of the game iteratively.
1. Selection
Starting at the root, the algorithm traverses down the tree by selecting child nodes that maximize a specific policy formula (usually UCB1). It continues until it reaches a leaf node (a node that hasn't been fully expanded yet).
2. Expansion
If the selected leaf node does not represent a terminal state (game over), the algorithm expands the tree by adding one (or more) valid child nodes corresponding to available actions.
3. Simulation (Rollout)
From the newly expanded node, the algorithm plays out a completely random game (a "rollout") all the way to a terminal state to evaluate the outcome.
4. Backpropagation
The result of the random simulation (e.g., Win = 1, Loss = 0) is backpropagated up the exact path taken during Selection. Every node on that path updates its statistics:
- : Total value/reward of the node increases.
- : Number of visits to the node increases by 1.
The UCB1 Formula: Balancing Exploration and Exploitation
During the Selection phase, MCTS must decide which node to traverse. It uses the Upper Confidence Bound applied to Trees (UCB1) formula to strike a balance between exploiting known winning paths and exploring unvisited nodes:
Where:
- : Total reward from child node .
- : Number of times child node has been visited.
- : Total number of visits to the parent node.
- : Exploration constant (frequently set to ).
Breaking Down the Formula
-
Exploitation Term:
This represents the average reward of choosing node . It guides the algorithm to prefer actions that have performed well in past simulations. -
Exploration Term:
This acts as an uncertainty bonus for less-visited nodes:- Because is in the denominator, nodes visited infrequently receive a large score boost, encouraging exploration.
- Because is in the numerator, the exploration pressure gradually grows as the parent node receives more visits.
[!NOTE] If a node has never been visited (), its UCB1 value is considered , guaranteeing that MCTS evaluates unvisited children before exploiting existing high-performing branches.
Step-by-Step Example of MCTS
Let's walk through a concrete execution trace of MCTS, demonstrating how the tree expands and how UCB1 values are calculated.
Initial State
We start with a root node and two child nodes and :
- Root : visited times, total reward .
- Child : visited time, total reward .
- Child : visited time, total reward .
Step 1: Rollout 1 (Playout Outcome = 40)
1. Selection (at Root )
To choose between and , we calculate their UCB1 scores (using exploration constant and parent visits ):
Since , we select and traverse to .
2. Expansion (at )
- Because has been visited () and is a leaf, we expand it.
- We expand by adding three children: (left), (middle), and (right). All are initialized to .
3. Tie-Breaking
- Since all three children have , their UCB1 scores are infinite.
- By tie-breaking rule (prefer rightmost node), we select .
4. Rollout & Backpropagation
- A random playout from yields outcome 40.
- We backpropagate the reward of 40 up the path :
Step 2: Rollout 2 (Playout Outcome = 20)
1. Selection (at Root )
We evaluate UCB1 with parent visits :
Since , we traverse to .
2. Selection (at )
For 's children ():
- ()
- ()
Breaking the tie between and by preferring the right node selects .
3. Rollout & Backpropagation
- A random playout from yields outcome 20.
- Backpropagating 20 up :
Lecture 06.2: Minimax and Alpha-Beta Pruning
Understanding sequential games, the Minimax algorithm for zero-sum games, and optimizing search trees with Alpha-Beta Pruning.
Lecture 07.1: Constraint Satisfaction Problems and Consistency
An in-depth introduction to Constraint Processing, CSP formalisms, varieties of constraints, constraint graphs, bounds consistency, and arc consistency algorithms like AC-3.