Short answer: Monte Carlo tree search grows a game tree by repeating selection, expansion, rollout, and backup. Selection walks from the root by a bandit rule, usually UCT: choose the child with the best win rate plus c · sqrt(ln N / n), where N is the parent’s visit count and n is the child’s. A leaf is expanded, a random or heuristic rollout finishes the game, and the result is added to every node on the path. The next move is the most visited root child. The search is an anytime approximation, not an exact minimax.
What the problem actually asks
Minimax needs a reliable evaluation at the leaves. In Go-like games the horizon evaluation is weak and the tree is enormous. A random playout from a position is a noisy estimate of its value. UCT spends more playouts on children that look good and still visits children that look bad, so a lucky early win does not freeze the search. The logarithmic bonus goes to infinity as a child is ignored, which is what forces another visit.
The constant c trades exploitation against exploration. The theoretical UCT value is often sqrt(2) for rewards in [0, 1]. Practical programs tune c. The rule is not a proof that the chosen move is optimal after a finite budget.
What the algorithm guarantees
With infinite samples and a suitable exploration constant, UCT converges to the minimax value in a finite tree of bounded rewards. A finite budget returns the empirical best. Time is proportional to the number of iterations times the cost of a rollout. Extra memory is the tree that was actually expanded, which is a small fraction of the legal tree. The rollout policy affects the quality of the estimates long before the asymptotic regime.
The most visited child, not the child with the highest raw win rate, is the usual move to play. A rarely visited child can have a perfect win rate from one lucky rollout. The visit count is the confidence.
The working steps
- Start with the root. Each iteration begins there.
- While the current node has been fully expanded and has children, descend to the child that maximizes win rate plus c · sqrt(ln N / n).
- If the node is not terminal and has an unexpanded legal move, add one child.
- Roll out from that child to a terminal position with a fast policy. Score the result for the player who just moved at each ancestor, or use a fixed score for the root player and negate as needed.
- Add the score to every node on the path and increment its visit count. After the budget, play the root child with the largest visit count.
Asymptotic convergence is not a finite-budget guarantee. Alpha-beta with a good evaluation can beat a weak rollout in a tactical game. MCTS is strongest when rollouts or a learned prior correlate with the truth. The exploration term must grow, however slowly, or a child can be starved forever.
Approaches that look simpler and fail
| Approach | Why it fails when the most winning child is played after one visit |
|---|---|
| Choose the highest win rate regardless of visits | One lucky rollout gives a win rate of 1. The visit count is what makes that rate meaningful. Play the most visited child. |
| Set c = 0 | Selection becomes greedy on the empirical mean. An early loss can hide a good move for the rest of the budget. |
| Treat a finite search as exact minimax | The backed-up values are sample averages. They match minimax only in the limit. |
| Skip the rollout and back up a random number | The estimate then has nothing to do with the game. The rollout, or a learned value head in a later variant, is the signal. |
Exact backup of a small tree is minimax, pruned by alpha-beta. MCTS is the sampled tree with a bandit selection rule and rollouts.
A working example
This Python fragment selects by win rate plus an exploration term, expands one move, and rolls out at random. The position has 4 stones, and a move takes 1 or 2; taking the last stone wins. The child with the most visits is the move that takes 1.
import math, random
rng = random.Random(0)
class Node:
def __init__(self, stones, parent=None, move=None):
self.stones = stones
self.parent = parent
self.move = move
self.children = []
self.untried = [1, 2] if stones >= 2 else ([1] if stones == 1 else [])
self.visits = 0
self.wins = 0
def rollout(stones):
winner_is_to_move = True
while stones:
stones -= 1 if stones == 1 else rng.choice((1, 2))
winner_is_to_move = not winner_is_to_move
return not winner_is_to_move
root = Node(4)
for _ in range(300):
node = root
while not node.untried and node.children:
node = max(node.children, key=lambda c: c.wins / c.visits + 1.4 * math.sqrt(math.log(node.visits) / c.visits))
if node.untried:
take = node.untried.pop()
child = Node(node.stones - take, node, take)
node.children.append(child)
node = child
mover_wins = True if node.stones == 0 else (not rollout(node.stones))
while node is not None:
node.visits += 1
if mover_wins:
node.wins += 1
mover_wins = not mover_wins
node = node.parent
choice = max(root.children, key=lambda c: c.visits)
assert choice.move == 1
print(choice.move)
Where it is used
AlphaGo and later board-game engines select moves with Monte Carlo tree search: many random playouts, biased toward moves that have won before. Classical chess engines use alpha-beta instead. The tree is grown where the playouts are promising, not uniformly.
When you can drop this algorithm
Drop MCTS when the game is small enough for alpha-beta with a real evaluation to finish. Keep it when the branching factor defeats minimax and a rollout carries information, and play the most visited move.
Why this stays published
We publish this so a search for Monte Carlo tree search returns the UCT selection rule and the reason the played move is the most visited child. Monte Carlo tree search grows a game tree by UCT selection, rollout, and backup, and after a finite budget it plays the most visited move rather than a proven minimax move.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
