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 see who wins.
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 (usually set to ).
Breaking Down the Formula
1. Exploitation Term: This is the average reward of choosing the node. It guides the algorithm to prefer actions that have performed well in past simulations.
2. Exploration Term: This acts as a bonus for unvisited nodes.
- Because (visits to the specific child) is in the denominator, nodes that haven't been visited often get a massive score boost, forcing the algorithm to explore them.
- Because (total parent visits) is in the numerator, the pressure to explore slowly increases over time as the parent is visited more frequently.
If a node has never been visited (), its UCB1 value is considered infinite, guaranteeing that MCTS will expand unexplored nodes before heavily exploiting successful ones.
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 . A snapshot of the statistics is:
- 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 already been visited () and is a leaf, we must expand it.
- Rule: The branching factor increases by 1 for each level of depth. Since depth 0 has 2 options, depth 1 has options.
- 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 (always prefer the right node), we choose the rightmost child: .
4. Rollout & Backpropagation
- A random playout from yields outcome 40.
- We backpropagate the reward of 40 up the path :
The tree state after Rollout 1:
Step 2: Rollout 2 (Playout Outcome = 20)
1. Selection (at Root )
We evaluate UCB1 with parent visits :
Since , we traverse to .
2. Selection (at )
Since is not a leaf node, we evaluate UCB1 for its children ():
- (since )
- (since )
and are tied with infinite UCB1. We break the tie by preferring the right node (between left and middle , the right one is ). We traverse to .
3. Rollout & Backpropagation
- A random playout from yields outcome 20.
- We backpropagate the reward of 20 up the path :
The final tree state after Rollout 2:
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 introduction to CSPs, variables, domains, constraints, and enforcing arc consistency using the AC-3 algorithm.