Algorithms for kids
Monte Carlo: how a computer plays by pretending to play thousands of games
In the alpha-beta article, the robot checked moves and replies one after another. But in some games there are so many moves that you can't check them all. So the computer does something surprising: it plays lots of quick, random games in its head and looks at which move wins most often.
First, read how alpha-beta worksToo many moves
In chess you usually have about 35 moves to choose from. In Go, an old game from Asia, a big board can have as many as 250! And after each of your moves, your opponent has almost as many replies. Look how many positions you'd have to check just to see four moves ahead:
1.5M
positions after 4 moves in chess (35 moves per turn)
3.9B
positions after 4 moves in Go (250 moves per turn)
There's a second problem too. Alpha-beta needs a judge who looks at the board and says who's winning. In chess that's fairly easy: you count the pieces. In Go, even masters often can't tell right away.
The trick: pretend games
The robot takes each move it could make and, starting from it, plays a quick game in its head all the way to the end. It picks every later move, its own and the opponent's, completely at random, like rolling a die. At the end it checks just one thing: did I win or lose?
One random game doesn't tell you much, just like one roll of a die. But after hundreds of games it's easy to see that the robot wins most often after move B. No judge needed, you just count the wins!
Where should you play more games?
Pretend games take time. Should every move get the same number? That would be wasteful: if move A clearly loses, why spend time on it? But you also can't give up on a move after two unlucky games. A smart robot plays more where things are going well, but every now and then it peeks at moves it hasn't checked much. It's greedy and curious at the same time.
Try it yourself! Each move has a hidden chance of winning. Press the buttons and watch the robot learn which move is best. Compare “Equally” with “Smart”.
Games: 0, wins: 0 (–)
In “Smart” mode the robot quickly starts playing mostly move B, but every so often it also checks A and C, in case it was just unlucky before. It spends the same number of games more wisely, so it wins more often.
A tree that grows by itself
A real robot doesn't stop at the first move. It builds a tree. Next to each move it writes two numbers: how many games it won after that move, and how many it played in total. 7/12 means 7 wins out of 12 games. Every new pretend game takes four steps:
1. Pick a path
Start at the top of the tree and go down. Each time, pick the move that looks best or hasn't been checked much.
2. Add a new move
When you reach the end of the tree, add one new move that wasn't there yet. For now it has 0/0.
3. Play pretend
From the new move, play a quick random game all the way to the end. This time the robot won!
4. Update the numbers
Go back up the same path. At each move add one game, and since it was a win, one win too.
The robot repeats these four steps thousands of times a second. The tree grows most where the good moves are. When time runs out, the robot picks the move it played most often, because that's the one it trusts the most.
How Monte Carlo beat a champion
For many years computers played Go badly, like beginners. That changed around 2006, when scientists came up with exactly this kind of tree of pretend games. In March 2016 a program called AlphaGo beat Lee Sedol, one of the best players in the world, 4 games to 1. AlphaGo added neural networks to Monte Carlo: they suggested which moves were worth checking and helped judge positions.
On this site, Go and Hex use Monte Carlo. When you play against the computer, the “Engine stats” panel shows how many pretend games it has already played.
Where does the name Monte Carlo come from?
Monte Carlo is a part of Monaco, a tiny country by the sea, famous for its casino, where people spin roulette wheels and roll dice. In the 1940s, scientists who worked out hard problems by drawing random numbers named their method after that casino. Our robot rolls dice too, but instead of money it wins knowledge about moves.
Remember
- When there are too many moves, the robot plays thousands of quick, random pretend games and counts which move wins most often.
- A smart robot is greedy and curious at once: it plays good moves more often but sometimes checks the less-known ones too.
- The tree grows in four steps: pick a path, add a move, play pretend, update the numbers.