function mcts(root: Node, iterations: number) { for (let i = 0; i < iterations; i++) {let node = root;
while (node.isExpanded && !node.isLeaf) node = selectChild(node); // UCB1: mean + c · sqrt(ln(parentVisits) / visits)
if (!node.isLeaf) {node = node.expandOneChild();
}
const reward = rollout(node); // random moves to the end of the game
for (let current: Node | null = node; current; current = current.parent) {current.visits++; current.total += reward;
}
}
return root.children.reduce((a, b) => (b.visits > a.visits ? b : a)); // the most visited move
}
An unknown game tree of depth 3: MCTS never scores every leaf, it samples. Rewards are the leaf scores scaled to 0–1 for MAX.
Understanding Monte Carlo tree search
The idea
Monte Carlo tree search never looks at the whole tree and never needs an evaluation function. Each iteration selects a path from the root by a bandit rule, UCB1, that balances the children's average reward against how rarely they were tried; expands one new node at the end of the path; plays random moves from there to the end of the game; and backs the result up along the path, updating visit counts and mean rewards.
Promising moves are visited more and so searched deeper, bad ones are abandoned after a few playouts, and the estimates sharpen with every iteration. The move to play is the most visited child of the root. With a learned policy to guide selection and a value network instead of random playouts, this is the search inside AlphaGo.
The four stages
Complexity
Versus its siblings
Pseudocode
MCTS(root, iterations)
repeat iterations times
select: from the root, follow the child with the best UCB1 until a node with an untried child or a leaf
expand: add one untried child of that node
simulate: play random moves from it to the end; reward ← the result
back up: for every node on the path, visits += 1, total += reward
return the most visited child of the root
When to use
Pitfalls
History
Rémi Coulom named Monte Carlo tree search in 2006 for his go program Crazy Stone, and Levente Kocsis and Csaba Szepesvári gave the UCT selection rule the same year. It lifted computer go from amateur to strong-amateur level within a decade, and with deep networks became AlphaGo in 2016.