Ch.29: Algorithms: How Monte Carlo Tree Search Chooses a Move

Outline

Transcript

0:00 Monte Carlo tree search chooses moves when checking every continuation would cost too much. We'll trace one small game through its four steps, from misleading samples to the move it actually plays. Suppose a game-playing program has a small simulation budget. My first thought is to divide it equally between the moves, then trust whichever wins more often. But an unlucky trial can hide a winning choice. This search remembers what happened and keeps testing alternatives. Take four stones. We alternate removing one or two, and whoever takes the last stone wins.

0:32 You're first, so your immediate choices are take one or take two. Already playable. I don't need chess rules to get myself into trouble. What does the program store about those choices? A tree. Each node represents a position, including whose turn it is. An edge represents a legal move connecting that position to the next. So the root is four stones with you to move. Its two possible children leave either three stones or two, both on the opponent's turn. Right. This tiny game is easy to solve exactly; we're using it to watch the bookkeeping.

1:06 Larger games make the limited computation matter. A trial follows remembered choices, adds a new position, plays forward to an outcome, then updates the counts. Those are selection, expansion, simulation and backup. Berkeley's course separates that simulated continuation from the stored tree. We'll follow that distinction, with the sources linked in the description. The simulation is a pretend game, not a move on the real board. The actual decision waits until we've spent our search budget. And an early result can be completely misleading.

1:37 Let's run the first two games before trusting either branch. Let's hear it. Losing still has to update a counter, or the program can't distinguish a bad move from one it never tried. Now run that first trial. For example, expand your take-one move and store its three-stone position. The opponent acts next in the simulation. The rollout, meaning the pretend continuation, chooses legal moves at random here. The opponent removes one, leaving two stones for you. You also take one. A single stone remains, so the opponent takes it and wins.

2:11 The first complete trial is a loss for you. Back up that outcome along the stored path. Your take-one branch has zero wins from one visit. A visit counts even when the trial loses. Does the tree now remember every move we just made, all the way down to the empty board? No. We added one child; the later moves belonged to the rollout. They're on the separate continuation panel, not stored as new tree nodes. Got it. Our first loss stays in the tree's counts. The other opening move hasn't been tested yet.

2:42 Next explore the root's untried take-two alternative. Its child has two stones remaining, and the simulated opponent again chooses just one. That leaves a final stone for you to collect and win. Your take-two child records one win in one visit. Compare the branches: zero out of one versus one out of one. A common mistake is treating those sampled results as solved positions. I'd still be tempted by that perfect record. One game, one victory. A very efficient way to become overconfident. And we've quietly relied on the opponent leaving you the final stone.

3:17 They could have taken both. Our cheerful result includes their mistake. So a winning sample can describe something the opponent would never allow if they spotted the better reply. Exactly. The next trials grow the tree into those replies, where the opponent gets to pursue their own win. Let's follow the promising branch deeper. Consider a 3rd trial: select your take-two edge, whose current result is better, then expand the opponent's take-one reply. One stone remains for you. The rollout takes it, so you win again.

3:49 But the new child represents a move the opponent chose. Whose win should that deeper node record? Here each child counts wins for whoever chose its incoming edge. That deeper child belongs to the opponent's decision, so your win gives it zero credit. Your root choice also gets credit, moving its record to two wins from two trials. One outcome updates each player's record differently. That distinction matters. On your turn, selection favors results good for you; during their turn, it favors what helps them.

4:20 We aren't simulating a helpful opponent. Now trial four expands their other legal reply: remove both remaining stones. The game ends immediately, and the opponent wins. Their new terminal child receives one win from one visit. Your root take-two branch gains a visit but no win, changing to two out of three. So backup isn't copying the same success number into every node. It increments visits and assigns credit using the recorded winner and each edge's player. Yes. In trial five, selection reaches that known winning reply again.

4:51 No extra child or random continuation is needed at an already finished game. The opponent's terminal record becomes two out of two. Your take-two branch has two wins across four visits. Add the first trial through take one, and the root has five visits. I think I'd have missed that mismatch when debugging. A root statistic can look sensible while deeper records accidentally reward the wrong player. Yeah, the overall win count wouldn't expose that mistake. Trace the winner back through those stored positions.

5:21 We've found the opponent's refusal, which means we need another test of take one. But the neglected branch still has no wins. If selection always follows the highest win fraction, how does it ever earn another chance? It gets an exploration bonus. The selection rule adds that bonus to the observed win fraction, rewarding choices with fewer trials as well as choices with better results. Like testing a second implementation before declaring the first one fastest. A weak first measurement shouldn't permanently cancel the comparison.

5:51 Right, except the program has a precise rule for spending that next test. The screen shows UCT, the upper-confidence rule applied to trees. Its bonus uses a square root: the number, zero or greater, that multiplied by itself gives the amount inside. For example, the square root of 4 is two. Inside that root, the natural logarithm is a slowly growing function of the parent's visits. The calculator value for five is about 1.61. Divide by the child's visits before taking the square root. So a smaller child count makes a larger bonus at the same parent.

6:27 With exploration weight one, our five-trial snapshot compares zero wins in one visit against two in four. The substitutions are visible: about 1.27 versus 1.13. Adding a bonus can push the score above 1; it isn't a probability. Hold on. Is that losing move now the better choice, or has it only earned another test? We're still simulating. Trial six follows take one, expands the opponent's one-stone reply, then simulates your one and their final one. You lose again. Another loss goes into the record.

7:01 Take one is now zero out of two. Neighboring take two remains at two out of four. Yeah, exploration bought evidence, however disappointing. Two further trials revisit that stored terminal win under take two. For instance, trial nine returns to take one and expands the opponent's other reply. That reply takes two from three, leaving you one. The rollout collects it, and you win. Its tally becomes one out of three; the other tally is unchanged at two in six. Oh, there's the win our first two attempts missed.

7:33 We actually reached the other opponent reply instead of assuming the losing samples settled the branch. Both branches have now supplied wins, but take two still has more visits. What happens if the simulation budget ends right there? With nine completed trials, we'd play take two, because its six visits exceed take one's three. The final rule chooses the most visited root child, without adding an exploration bonus. Now let this same run reach 32 trials. Take one has been visited 26 times and won 22 games.

8:04 Take two's six trials produced two wins. We choose take one. Could we prove that choice correct here, rather than just feel better about the larger sample? We can, because the example is small. Taking one leaves three stones. Suppose their reply removes 1: two remain, and you collect both to win. But what if their reply is to remove two instead? Their take two leaves your final stone. Either reply lets you finish. By contrast, your initial take-two move leaves two stones, which the opponent can collect immediately.

8:36 Then the choice at nine really was wrong, and the decision at 32 is right. That comparison uses the same visit-based rule at both stops. Yes. After just two trials, visits were tied one each; only the sampled win fractions favored take two. The later nine-trial snapshot gives us an actual losing choice to compare. And we checked correctness separately from the search statistics. I can examine every continuation here, but a large game makes that complete check expensive. 22 out of 26 describes the games this changing search examined, not a guaranteed chance against perfect play.

9:12 More computation helped this run; no particular finite budget guarantees that recovery. The result is a move chosen with limited evidence. To make that decision useful, the code has to preserve what each trial meant. Now connect that decision to its implementation. Provide legal moves, a function that applies them, a terminal outcome rule and a computation budget. The code on screen follows our four phases. Selection returns a path through the stored tree. Expansion appends a new child to that path when possible.

9:43 Simulation supplies a winner, and backup updates each visited node once. Let's replay trial four through those functions. Selection follows your take two to the two-stone position. Expansion appends their two-stone reply and the board becomes empty. Wait, the next line still calls simulate to end. Why call it when the opponent has already won? The function first checks for a finished game and returns its known winner. Consider the terminal case we've reached: it returns Opponent immediately. Only unfinished positions need a continuation.

10:13 So that function can return an outcome without running a rollout. Backup increments visits, then adds a win only when the node's owner matches that winner. Exactly. The opponent-owned child gets its first win and first visit. Your initial two-stone take now has two successes across three visits. That matches our earlier trace. And the root counts your wins, so it also gains no win. Its visits become four. Three stored nodes updated from one outcome, with different credit. That makes the ownership bug visible during a code review.

10:46 What about an untried move, where the selection formula would divide by 0 visits? Expand an untried action before using that formula to compare visited children. After that first simulation, its count is nonzero; no visits doesn't mean a poor measured result. For instance, if an agent's transition function permits an illegal take, would more trials expose that rule error? Not necessarily. The search may keep exploring the invented game. Check the rules independently, and remember that the rollout's move policy also affects what evidence you collect.

11:19 Random legal play made our continuation inspectable; other policies are possible. The original UCT paper's question is where to spend the next simulation, which is exactly what our comparison answered. We can now follow a trial from a stored choice through expansion and an outcome to player-owned counts. The budget boundary turns that remembered evidence into a real move. And that earlier mistaken decision shows why the distinction matters. Follow the trials, check whose win each count represents, then choose within the budget.

11:50 Thanks for listening to Learning Podcasts.