Ch.28: Algorithms: How Minimax and Alpha-Beta Choose a Move

Outline

Transcript

0:00 Minimax helps a game-playing program choose a move against an opponent trying to beat it. We'll run it, then use alpha-beta pruning to skip calculations without changing the choice. 20 looks tempting. My instinct is to take its route and let the opponent sort out the details. Where does that go wrong? At the opponent's choice. They don't have to help you. Take an invented token game. You choose a route, they pick a pair, then you take one token for its points. Three moves: your route, their pair, your token.

0:31 Every number measures your score, even during their turn. They want it low; you want it high. There are no dice or hidden moves. B's 20 sits beside seven. Could I make them offer that pair so I could take 20 at the end? No. They can send you to one and two instead. You don't control that middle choice. Ah, so circling the biggest token skips somebody else's turn. The opponent has declined to participate in my very attractive plan. Yeah, they have their own plan. A offers 3 and 5, or 6 and 9. We'll work backward through those choices instead of chasing the largest number anywhere on the board.

1:08 The branching diagram is a game tree. Each line leads to an allowed move. Nodes mark positions; the topmost node is called the root. At your nodes, take the largest available result: MAX. At theirs, take the smallest: MIN. Those labels say whose choice we're predicting. We're still measuring everything in your points. Taking a minimum models their opposition without changing the meaning of the numbers. Now run those choices backward. Consider A's final choice between 3 and 5. You take five, so that pair's MAX result is 5.

1:40 The other pair offers 6 and 9, so you'd choose nine. We can replace each completed pair with what your final move would obtain. Now the opponent faces five or nine. They select five. Route A therefore gives you five against their best reply. Which makes five a guarantee, not a prediction of every play. For example, if they hand you 6 and 9, you can take nine instead. So can I take three and still claim that guarantee? No. You still have to choose correctly. Five survives their strongest reply; it doesn't rescue you from throwing points away.

2:14 Minimax assumes sensible choices on both sides. Let's give B the same treatment. Your last choices yield two from 1 and 2, and 20 from 7 and 20. Their MIN result is 2. So at the start, compare A worth five with B worth two. Your MAX decision selects A. That's the minimax rule in Berkeley's notes; sources are in the description. We're backing results upward because earlier choices depend on later replies. We evaluated all eight final tokens to find that answer. In a bigger game, each additional move can introduce another layer of choices.

2:50 I'd keep this full search as a correctness check for a small game. But must a playing program examine every outcome to choose A? It can stop work on a branch once a known alternative proves that branch can't affect the decision. Let's find the actual proof before adding code. But those eight reads include avoidable work. Alpha-beta adds stopping rules to minimax. Watch this same tree evaluated left to right, with unread values covered. The board hasn't changed. We've covered values to track what the algorithm has evaluated; in a real game, evaluating future positions takes work.

3:25 Read A's first pair, labeled A 1: 3, then 5. Your final choice returns five. The opponent now has an available reply giving you five. So whatever A's other pair contains, that opponent never needs to let me score above 5 through this route. They can choose A one. Exactly. Carry that available five into the search of A's second pair. It's called beta, the opponent's current upper bound on your score. The first token there is six. I can already get at least six from this pair, even though its other token remains covered.

3:59 And the minimizing opponent prefers their existing five. They won't choose this pair once it offers you six or more. Skip the other token in this pair. Wait, then A two returns six in the search. Are we claiming its true value is 6? No. Six is a lower bound: that pair is worth at least six. The hidden token could be better; we haven't measured it. Oh, I didn't realize a call could return a bound instead of a finished score. Here we only need enough information for the opponent to reject that pair.

4:31 Their decision can still finish exactly. They compare a known five against something at least six, and choose five. A's result is settled. Three final tokens read, one skipped inside A. We saved a calculation because the opponent already had a better reply, not because six looked impressive. Now use A's completed result to inspect B. The root already has a move worth five. Call that available lower limit alpha. Alpha is five at the root, but A's beta also ended at five. Which bounds enter B? B receives the root's alpha of five.

5:04 A's beta belonged to its own opponent decision. B begins without that local ceiling; the bounds follow this chain of decisions. Take the one and two pair under B, labeled B one. You pick two. B's opponent can therefore keep your score to at most two. That's already worse for you than A's five. We can leave the other whole pair unread and reject B at the root. Even with 20 in the unread pair, why can't it rescue B? Because the opponent chooses the pair. However generous that unread option becomes, they can refuse it and send you to B one for two.

5:40 The hope of 20 depended on cooperation. B returns two as an upper bound during this search. We know its minimizing choice is at most two; the remaining pair wasn't evaluated. Add up the actual reads: 3, 5, 6 on A; one and two on B. Five final token values evaluated instead of eight. That's a neat saving. A still guarantees 5, and 3 tokens stayed unread. Both pruning decisions had an already available alternative behind them. The first cutoff happened during your final MAX choice; this one happened at their MIN choice.

6:13 We can express both with the same comparison. Let's put those two stopping decisions into code. A recursive search is a function that calls itself for each legal next position, until it reaches a final score. Each call receives a position, whose turn it is, alpha and beta. At the root, use negative infinity for alpha and positive infinity for beta. What do those infinities mean before we've read a token? Neither player has fixed a useful limit yet. Alpha can start as low as possible; beta as high as possible.

6:43 The available moves will tighten them. At a final position, return its points for you. That's a terminal node, also called a leaf: there are no further moves. For MAX, the code starts best at negative infinity. The legal next positions are called children. Follow A two's call to the final token six. That terminal call returns six to A two, before A two updates its bounds. Back in this MAX call, keep the larger returned value. I can see six coming back, but the opponent's five is still there. Which variable changes?

7:14 Best becomes six, so alpha rises to six. Beta stays five, the opponent's available reply. Alpha has crossed beta; the loop stops and returns six as a bound. That settles the MAX cutoff. MIN begins with best at positive infinity, so its first score can replace it. At B's MIN decision, the first pair returns two against alpha five. Does that two replace alpha or beta? Beta falls to two. The MIN code keeps the smaller returned result, so best becomes two and beta follows. Alpha is still five; the comparison stops before B two.

7:50 So both loops stop when alpha is at least beta. Rewind to B's call, before it reads a token. Which beta would you pass in: A's five, or the root's positive infinity? Positive infinity, from the root. A's five belonged to A's call; B lowers its own beta to two. If a call returns only a limit, how can the root's answer still be exact? One player can already refuse that continuation and take the previously established option instead. Its precise value cannot change that choice. Our root compares settled A five against B at most two.

8:26 Can a different visit order increase the work? Yes. Pruning's savings depend on the order in which those alternatives appear. A useful early move tightens a bound before we spend time elsewhere. So finding it last doesn't refund the calculations we've already done. Alpha-beta doesn't promise fewer evaluations on every ordering. Now start with B instead, without rearranging either pair. Before revealing the trace, would that swap force the search to read all eight tokens? No. Inside B, one and two give the opponent a ceiling of 2.

8:57 Read seven next; this pair now guarantees at least seven. The opponent prefers two, so 20 stays covered. Then under A, three and five establish its opponent's five. Reading six again rules out the other token. The swapped route order reads one, 2, 7, 3, 5, 6. Six reads instead of five. We still select A for 5, and 9 and 20 stay unread. Changing when we found useful alternatives changed the work. We tested every arrangement of these eight token values. Pruning matched full minimax each time, using five to eight final-score reads.

9:33 Those counts describe this small tree, not how fast a chess program runs. A chess search may stop before the game ends because there are too many continuations. It then estimates unfinished positions. How can exact pruning still lead us to a bad move? An exact search can use a bad estimate. In a different invented game, stop after choosing X or Y. Their estimates are 5 and 2, so both algorithms pick X. But each position has one forced move left. X actually ends at 0; Y ends at two. One more step of search would select Y instead.

10:07 So pruning preserved the early search's answer, but that answer used a poor estimate. Saving work doesn't certify the model of the game. Better evaluation or deeper search could address that. Minimax chooses against the opponent's best response. Alpha-beta reaches that decision while leaving irrelevant work undone, with savings that depend on the search order. Thanks for listening to Learning Podcasts.