42
Artificial Intelligence Minimax and alpha-beta pruning

Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

  • Upload
    others

  • View
    19

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

Artificial Intelligence

Minimax and alpha-beta pruning

Page 2: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

2

In which we examine the problems that

arise when we try to plan ahead to get

the best result in a world that includes

a hostile agent (other agent planning

against us).

Page 3: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

3

Games

• Adversarial search problems

– Competitive environments in which goals of multiple agents are in conflict (often known as games)

• Game theory

– Views any multi-agent environment as game

– Provided the impact of each agent on the others is “significant”

• Game playing is idealization of worlds in which hostile agents act so as to diminish one’s well-being!

– Games problems are like real world problems

• Classic AI games

– Deterministic, turn-taking, two-player, perfect information

Page 4: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

4

Classic AI Games

• State of game easy to represent

• Agents usually restricted to fairly small number of well-defined actions

• Opponent introduces uncertainty

• Games usually much too hard to solve

– Chess

• Branching factor 35

• Often go to 50 moves by each player

• About 35100 nodes!

• Good domain to study

Page 5: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

5

AI Game Play

• Define optimal move and algorithm for

finding it

• Ignore portions of search tree that make no

difference to final choice

– Pruning

Page 6: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

6

A Game Defined

as Search Problem• Initial state

– Board position

– Whose move it is

• Operators (successor function)

– Defines legal moves and resulting states

• Terminal (goal) test

– Determines when game is over (terminal states)

• Utility (objective, payoff) function

– Gives numeric value for the game outcome at terminal states

– e.g., {win = +1, loss = -1, draw = 0}

Page 7: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

7

X X X

X X X

X X X

X O X O X

O

X O X X O

X

X O

X

X O X

O X

O

X O X

O O X

X X O

X O X

X

X O O

-1 0 +1

… … … …

O

X

O

X

Utility

Terminal

Partial search tree for

game Tic-Tac-Toe

(you are ‘X’)

Page 8: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

8

Optimal Strategies:

Perfect Decisions in

Two-Person Games• Two players

– MAX

– MIN

• (Assume) MAX moves first, then they take turns

moving until game over

• At end, points awarded to winning player

– Or penalties given to loser

• Can formulate this gaming structure into a search

problem

Page 9: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

9

An Opponent

• If were normal search problem, then MAX

(you/agent) need only search for sequence of

moves leading to winning state

• But, MIN (the opponent) has input

• MAX must use a “strategy” that will lead to

a winning state regardless of what MIN does

– Strategy picks best move for MAX for each

possible move by MIN

Page 10: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

10

X X X

X X X

X X X

X O X O X

O

X O X X O

X

X O

X

X O X

O X

O

X O X

O O X

X X O

X O X

X

X O O

-1 0 +1

… … … …

MIN

(O)

MAX

(X)

MIN

(O)

MAX

(X)

Utility

Terminal

Partial search tree for

game Tic-Tac-Toe

Page 11: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

11

Techniques

• “Minimax”– Determines the best moves for MAX, assuming

that MAX and opponent (MIN) play perfectly

• MAX attempts to maximize its score

• MIN attempts to minimize MAX’s score

– Decides best first move for MAX

– Serves as basis for analysis of games and algorithms

• Alpha-beta pruning

– Ignore portions of search tree that make no difference to final choice

Page 12: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

Playing Perfectly?

12

Futurama, episode “Mars University”

[The game hasn't begun.]

FRATBOT #2 “Mate in 143

moves.”

FRATBOT #3 “Oh, poo, you

win again! “

Page 13: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

13

Minimax

• Perfect play for deterministic, perfect-information games

• Two players: MAX, MIN

– MAX moves first, then take turns until game is over

– Points are awarded to winner

• Sometimes penalties may be given to loser

• Choose move to position with highest minimaxvalue

– Best achievable payoff against best play

– Maximizes the worst-case outcome for MAX

Page 14: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

14

Minimax Algorithm• Generate whole game tree (or from current state

downward – depth-first process online)

– Initial state(s) to terminal states

• Apply utility function to terminal states

– Get payoff for MAX’s final move

• Use utilities at terminal states to determine utility of nodes one level higher in tree

– Find MIN’s best attempt to minimize high payoff for MAX at terminal level

• Continue backing up the values to the root

– One layer at a time

• Value at root is determines the best payoff and opening move for MAX (minimax decision)

Page 15: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

15

2-Ply Minimax Game(one move for each player)

MAX

MIN

3

2 23

3 12 8 2 4 6 14 5 2

A1 A2 A3

A11 A12 A13 A21 A22 A23 A31 A32 A33

Action for MAX

Action for MIN

Terminal

MAX’s final score

Page 16: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

16

2-Ply Minimax Game(one move for each player)

MAX

MIN

3

2 23

3 12 8 2 4 6 14 5 2

A1 A2 A3

A11 A12 A13 A21 A22 A23 A31 A32 A33

Terminal

Page 17: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

17

Properties of Minimax

• Complete

– If tree is finite

• Time

– Depth-first exploration

– O(bm), max depth of m with b legal moves at each point (impractical for real games)

• Space

– Depth-first exploration

– O(bm)

• Optimality

– Yes against an optimal opponent

– Does even better when MIN not play optimally

Page 18: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

18

Inappropriate Game for Minimax

MAX

MIN 99 100

99 1000 1000 1000 100 101 102 100

Minimax suggest taking right-hand branch (100 better than 99).

The 99 most likely an “error in payoff estimation”.

Use probability distribution over nodes.

Terminal

Page 19: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

19

Pruning

• Minimax search has to search large number of states

• But possible to compute correct minimax decision without looking at every node in search tree

• Eliminating a branch of search tree from consideration (without looking at it) is called pruning

• Alpha-beta pruning

– Prunes away branches that cannot possibly influencefinal minimax decision

– Returns same move as general minimax

Page 20: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

20

Alpha-Beta Pruning

• Can be applied to trees of any depth

• Often possible to prune entire subtrees rather than just

leaves

• Alpha-beta name

– Alpha = value of best (highest-value) choice found so far at any

choice point along path for MAX

• In other words, the worst score (lowest) MAX could possibly get

• Update alpha only during MAX’s turn/ply

– Beta = value of best (lowest-value) choice found so far at any

choice point along path for MIN

• In other words, the worst score (highest) MIN could possibly get

• Update beta only during MIN’s turn/ply

Page 21: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

21

Alpha-Beta Pruning

m

MAX

MIN

MAX

mAlpha

Page 22: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

22

Alpha-Beta Pruning

m

MAX

MIN

MAX n

n

Beta m

Page 23: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

23

Alpha-Beta Pruning

m

MAX

MIN

MAX n

m is best value (to MAX) so

far on current path. If n is

worse than m, MAX will

never choose this path. MIN

should give up (prune).

n

If m > n

Equiv: Alpha > Beta

m

Page 24: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

24

Alpha-Beta Pruning

MAX

MIN

Terminal

Page 25: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

25

Alpha-Beta Pruning

MAX

MIN

Terminal

≤ 3

3

Beta

Page 26: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

26

Alpha-Beta Pruning

MAX

MIN

Terminal

3

3

3 12 8

Final

score

≤ 3

Alpha

Page 27: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

27

Alpha-Beta Pruning

MAX

MIN

Terminal

3

2

3 12 8 2

3 > 2 !!!

No “final” score

But fine to tell MAX “2”

3 2

Page 28: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

28

Alpha-Beta Pruning

MAX

MIN

Terminal

3

3 12 8 2

3 2

14

14

3 > 14 ?

Page 29: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

29

Alpha-Beta Pruning

MAX

MIN

Terminal

3

3 12 8 2

3 2

14

14

5

5

3 > 5 ?

Page 30: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

30

Alpha-Beta Pruning

MAX

MIN

Terminal

3

3 12 8 2

3 2

14

14

5

5

2

22

3

Note: Only showed MIN pruning here

In general, both MIN and MAX check Alpha > Beta, prune

Page 31: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

31

In-Class Exercise

Terminal

Page 32: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

32

Properties of Alpha-Beta

• Pruning does not affect final result

• With “perfect ordering”:

– Time complexity O(bm/2)

• A simple example of the value of “reasoning about which computations are relevant”– Meta-reasoning (reasoning about

reasoning)

Page 33: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

33

Node Ordering

• Good move ordering would improve effectiveness of pruning– Try to first examine successors that are likely to be best

– Prunes faster• e.g., want to see children with values ordered as 1, 10, 100 (not

100, 10, 1)

100 10 1 1 10 100

vs.

Have better chance of being pruned

Page 34: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

34

Tie Breaking

• What if MAX ends up with multiple choices with the same (maximum) score?– According to basic MiniMax, doesn’t matter which– But may have outside preferences to inform choice

• Tie Breaking Strategies– Earliest Move– Latest Move– Random – Make algorithm less predictable

• Also, consider adjusting utility function so less ties are possible

• Alpha ≥ Beta ?– Some sources (i.e., the textbook) recommend this– BUT correctness depends on tie breaking strategy– Only safe for Earliest Move strategy

Page 35: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

Games with Chance

• Many games have a random element

– e.g., throwing dice to determine next move

• Cannot construct standard game tree as before

– As in Tic-Tac-Toe

• Need to include “CHANCE nodes”

• Branches leading from chance node represent

the possible chance-outcomes and probability

– e.g., die rolls: each branch has the roll value (1-6)

and its chance of occurring (1/6th)

35

Page 36: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

ExpectiMiniMax

• TERMINAL, MAX, MIN nodes work same

way as before

• CHANCE nodes are evaluated by taking

weighted average of values (expected value)

resulting from all possible chance outcomes

(e.g., die rolls)

• Process is backed-up recursively all the way

to root (as before)

36

Page 37: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

Simple Example

37

MAX

CHANCE

MIN

TERMINAL

2 3 1 4

A1 A2

.9 (H) .1 (T)

“loaded” (trick)

coin flip for MIN

2 possible start moves for MAX

.9 (H) .1 (T)

2 2 3 3 1 1 4 4

a11 a12

.9x2 + .1x3=2.1 .9x1 + .1x4=1.3

Move A1 is “expected” to be best for MAX

Page 38: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

Alpha-Beta with Chance?

• Analysis for MAX and MIN nodes are same

• But can also prune CHANCE nodes

• Concept is to use “upper bound” on value of

CHANCE nodes

– Example:

• If put bounds on the possible utility values (-3 to +3)

underneath, this can be used to put upper bound on

the expected value at CHANCE nodes above

38

Page 39: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

39

Game Programs

• Chess

– Most attention

– In 1957, predicted computer would beat world champion in 10 years (off by 40 years)

– Deep Blue defeated Garry Kasparov (6 game match)

“The decisive game of the match was Game 2, which left a scar in my memory … we saw something that went well beyond our wildest expectations of how well a computer would be able to foresee the long-term positional consequences of its decisions. The machine refused to move to a position that had a decisive short-term advantage – showing a very human sense of danger”

– (Kasparov, 1997)

Page 40: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

40

Game Programs

• Chess (con’t)

– Searched 126 million nodes per second on average

• Peak speed of 330 million nodes per second

– Generated up to 30 billion positions per move

• Reaching depth 14 routinely

– Heart of machine was iterative-deepening alpha-beta search

• Also generated extensions beyond depth limit for sufficiently interesting lines of moves

– Later Deep Fritz ended in draw in 2002 against world champion Vladimir Kramnik

• Ran on ordinary PC (not a supercomputer)

Page 41: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

41

Game Programs

• Others

– Checkers

– Othello (Reversi)

• Smaller search space than chess (5-15 legal moves)

– Backgammon

• Neural network system was ranked #3 in world (1992)

– Bridge

– Go

• Branching factor of 361 (chess is 35)

• Regular search methods no good

• Programs now exist!

Page 42: Minimax and alpha-beta pruningweb.cse.ohio-state.edu/~barker.348/cse3521_su19/minimaxAB.pdf · Alpha-Beta Pruning • Can be applied to trees of any depth • Often possible to prune

42

Summary

• Games can be defined as search problems

– With complexity of real world problems

• Minimax algorithm determines the best move for a player

– Assuming the opponent plays perfectly

– Enumerates entire game tree

• Alpha-beta algorithm similar to minimax, but prunes away branches that are irrelevant to the final outcome

– May need to cut off search at some point if too deep

• Can incorporate “chance”