Algorithms / Monte Carlo tree search

Monte Carlo tree search

How a program chooses a move by playing random games and keeping score in a tree. One full round of selection, expansion, simulation and backpropagation, with every number worked out.

Advanced7 steps~7 min
Position — a node, labelled wins / games
Selected — the path this round follows
New position — added to the tree this round
Playout — a random game to the very end
// THE SHORT VERSION

In 30 seconds

Monte Carlo tree search decides what to do next by trying things out. It builds a tree of positions and, for each one, records how many of the random games played through it were won.

Every round has four phases: select a promising path, expand it by one new position, simulate a random game from there, and pass the result back up.

A score called UCB balances moves that have done well against moves barely tried. Because positions are judged by playing them out, no hand-written scoring formula is needed, which is what made the method work for Go.

Monte Carlo tree search, step by step

STEPS01 / 07

For simplicity every node counts wins for the player choosing at the root. Real programs count each node’s wins for the player who moved into it, so the opponent’s replies are selected by what is good for the opponent.

STEP 01 / 07 A tree that keeps score
THE MATH
node = wins / games root: 7/12 A: 6/8 B: 1/2 C: 0/2
  1. Step 1: A tree that keeps score. Monte Carlo tree search (MCTS) chooses a move by playing many random games and keeping score in a tree. Each node is a position, labelled with how many of the games played through it were won. So far 12 games have been tried from the current position: 8 began with move A, and 6 of those were won.
  2. Step 2: Phase 1: selection. Each round starts at the root and picks the move with the highest UCB (upper confidence bound) score: its win rate plus a bonus that is large for moves tried rarely. A has the best win rate, but B has been tried only twice and its bonus tips the balance. This weighs exploiting what looks good against exploring the unknown.
  3. Step 3: Phase 2: expansion. The walk down the tree stops at a node that still has a move nobody has tried. One new child is added for that move: a position the search has never visited. It starts with no games and no wins.
  4. Step 4: Phase 3: simulation. From the new position the game is played out to the end with random moves. Only the rules and the final result are needed. No formula for judging a half-finished position is needed, which is why the method suits games like Go, where a good one is very hard to write. This playout ends in a win.
  5. Step 5: Phase 4: backpropagation. The result travels back up the path. Every node on it counts one more game and, because this one was won, one more win. The new node reads 1/1, B rises from 1/2 to 2/3, and the root from 7/12 to 8/13. The next round will select using these updated numbers.
  6. Step 6: Repeat, then choose. The four phases repeat thousands of times, for as long as the clock allows. Promising moves keep being selected, so the tree grows deep under them and stays shallow elsewhere. When time is up, the move with the most visits is played. The counts here are an example of how they might look after 1,000 rounds.
  7. Step 7: Inside AlphaGo. AlphaGo, which beat Go champion Lee Sedol 4 games to 1 in 2016, is this search guided by two neural networks. A policy network suggests which moves deserve attention, and a value network estimates who is ahead at a new position, used alongside fast playouts. Its successor AlphaGo Zero dropped random playouts altogether.
VALUES SO FAR
root7/12wins/n
A6/8wins/n
B1/2wins/n
C0/2wins/n
UCB of B····highest
playout····result
new····updated
B····updated
root····updated

Spotted a mistake on this page?