data structures and algorithmsgalles/cs245s15/lecture/twoplayer.pdf · p4-1: two player games...

46
Data Structures and Algorithms CS245-2015S-P4 Two Player Games David Galles Department of Computer Science University of San Francisco

Upload: others

Post on 26-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

Data Structures and AlgorithmsCS245-2015S-P4

Two Player Games

David Galles

Department of Computer Science

University of San Francisco

Page 2: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-0: Overview

Example games (board splitting, chess, Network)

Min/Max trees

Alpha-Beta Pruning

Evaluation Functions

Page 3: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-1: Two player games

Board-Splitting Game

Two players, V & H

V splits the board vertically, selects one half

H splits the board horizontally, selects one half

V tries to maximize the final value, H tries tominimize the final value

14 5 11 4

12 13 9 7

15 13 10 8

16 1 6 2

Page 4: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-2: Two player games

Board-Splitting Game

We assume that both players are rational (makethe best possible move)

How can we determine who will win the game?

Page 5: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-3: Two player games

Board-Splitting Game

We assume that both players are rational (makethe best possible move)

How can we determine who will win the game?Examine all possible games!

Page 6: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-4: Two player games

14 5 11 412 13 19 715 3 10 816 1 6 2

Vertical

Horizontal Horizontal

14 5 11 4

12 13 19 7

15 3 10 8

16 1 6 2

14 5 11 4

12 13 19 7

15 3 10 8

16 1 6 2

14 5 11 4

12 13 19 7

15 3 10 8

16 1 6 2

14 5 11 4

12 13 19 7

15 3 10 8

16 1 6 2

14 5 11 4

12 13 19 7

15 3 10 8

16 1 6 2

14 5 11 4

12 13 19 7

15 3 10 8

16 1 6 2

14 5 11 4

12 13 19 7

15 3 10 8

16 1 6 2

14 5 11 4

12 13 19 7

15 3 10 8

16 1 6 2

14 5 11 4

12 13 19 7

15 3 10 8

16 1 6 2

14 5 11 4

12 13 19 7

15 3 10 8

16 1 6 2

14 5 11 4

12 13 19 7

15 3 10 8

16 1 6 2

14 5 11 4

12 13 19 7

15 3 10 8

16 1 6 2

14 5 11 4

12 13 19 7

15 3 10 8

16 1 6 2

14 5 11 4

12 13 19 7

15 3 10 8

16 1 6 2

14 5 11 4

12 13 19 7

15 3 10 8

16 1 6 2

Vertical Vertical Vertical Vertical

Page 7: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-5: Two player games

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MaxMin

Max

Page 8: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-6: Two player games

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MinMin

Max

12 5 15 1 11 4 6 2

12 15 11 6

612

12

Page 9: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-7: Two player games

Game playing agent can do this to figure out whichmove to make

Examine all possible moves

Examine all possible responses to each move

... all the way to the last move

Caclulate the value of each move (assumingopponent plays perfectly)

Page 10: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-8: Minimax Algorithm

Max(node)

if terminal(node)

return utility(node)

maxVal = MIN_VALUE

children = successors(node)

for child in children

maxVal = max(maxVal, Min(child))

return maxVal

Min(node)

if terminal(node)

return utility(node)

minVal = MAX_VALUE

children = successors(node)

for child in children

minVal = min(minVal, Max(child))

return minVal

Page 11: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-9: Minimax Algorithm

Branching factor of b, game length of d moves,what are the time and space requirements forMinimax?

Page 12: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-10: Minimax Algorithm

Branching factor of b, game length of d moves,what are the time and space requirements forMinimax?

Time: O(bd)

Space: O(d)

Not managable for any real games – chess has anaverage b of 35, can’t search the entire tree

Need to make this more managable

Page 13: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-11: Alpha-Beta Pruning

Does it matter what value is in the yellow circle?

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MinMin

Max

12 5 15 1 11 4 6 2

12 15 11 6

612

12

Page 14: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-12: Alpha-Beta Pruning

If the yellow leaf has a value > 5, parent won’t pickit

If the yellow leaf has a value < 12, grandparentwon’t pick it

To affect the root, value must be < 5 and > 12

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MinMin

Max

12 5 15 1 11 4 6 2

12 15 11 6

612

12

Page 15: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-13: Alpha-Beta Pruning

Value of nodes in neither yellow circle matter. Arethere more?

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MinMin

Max

12 5 15 1 11 4 6 2

12 15 11 6

612

12

Page 16: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-14: Alpha-Beta Pruning

Value of nodes in none of the yellow circles matter.

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MinMin

Max

12 5 15 1 11 4 6 2

12 15 11 6

612

12

Page 17: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-15: Alpha-Beta Pruning

Player

Opponent

Player

Opponent

m

n

… … …

If m is better than n for Player, we will never reachn

(player would pick m instead)

Page 18: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-16: Alpha-Beta Pruning

Maintain two bounds, lower bound α, and an upperbound β

Bounds represent the values the node musthave to possibly affect the root

As you search the tree, update the bounds

Max nodes increase α, min nodes decrease β

If the bounds ever cross, this branch cannot affectthe root, we can prune it.

Page 19: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-17: Alpha-Beta Pruning

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MinMin

Max

α = -inf, β = inf

Page 20: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-18: Alpha-Beta Pruning

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MinMin

Max

α = -inf, β = inf

α = -inf, β = inf

α = -inf, β = inf

α = -inf, β = inf

α = -inf, β = inf

Page 21: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-19: Alpha-Beta Pruning

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MinMin

Max

α = -inf, β = inf

α = -inf, β = inf

α = -inf, β = inf

α = -inf, β = 14

α = -inf, β = 14

14

Page 22: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-20: Alpha-Beta Pruning

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MinMin

Max

α = -inf, β = inf

α = -inf, β = inf

α = 12, β = inf

α = 12, β = inf

12

12

α = 12, β = inf

Page 23: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-21: Alpha-Beta Pruning

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MinMin

Max

α = -inf, β = inf

α = -inf, β = inf

α = 12, β = inf

α = 12, β = -5

12

12

5

Page 24: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-22: Alpha-Beta Pruning

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MinMin

Max

α = -inf, β = inf

α = -inf, β = 12

12

12

5

12

Page 25: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-23: Alpha-Beta Pruning

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MinMin

Max

α = -inf, β = inf

α = -inf, β = 12

12

12

5

12

α = -inf, β = 12

α = -inf, β = 12

α = -inf, β = 12

Page 26: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-24: Alpha-Beta Pruning

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MinMin

Max

α = -inf, β = inf

α = -inf, β = 12

12

12

5

12

α = -inf, β = 12

α = -inf, β = 12

α = -inf, β = 12

15

Page 27: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-25: Alpha-Beta Pruning

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MinMin

Max

α = -inf, β = inf

α = -inf, β = 12

12

12

5

12

α = 15, β = 12

15

15

Page 28: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-26: Alpha-Beta Pruning

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MinMin

Max

α = 12, β = inf

12

12

5

12

15

15

12

Page 29: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-27: Alpha-Beta Pruning

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MinMin

Max

α = 12, β = inf

12

12

5

12

15

15

12

α = 12, β = inf

α = 12, β = inf

α = 12, β = inf

α = 12, β = inf

Page 30: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-28: Alpha-Beta Pruning

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MinMin

Max

α = 12, β = inf

12

12

5

12

15

15

12

α = 12, β = inf

α = 12, β = inf

α = 12, β = 11

11

Page 31: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-29: Alpha-Beta Pruning

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MinMin

Max

α = 12, β = inf

12

12

5

12

15

15

12

α = 12, β = inf

α = 12, β = inf

11

11α = 12, β = inf

α = 12, β = inf

Page 32: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-30: Alpha-Beta Pruning

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MinMin

Max

α = 12, β = inf

12

12

5

12

15

15

12

α = 12, β = inf

α = 12, β = inf

11

11α = 12, β = 4

4

Page 33: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-31: Alpha-Beta Pruning

14 5 11 412 13 19 715 3 10 816 1 6 2

Min Min Min Min Min Min Min Min

Max Max Max Max

MinMin

Max

α = 12, β = inf

12

12

5

12

15

15

12

α = 12, β = 11

11

11

4

11

Page 34: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-32: Alpha-Beta Pruning

We can cut large branches from the search tree

In the previous example, what would happenwith similar values and a deeper tree?

If we choose the order that we evaluate nodes(more on this in a minute ...), we can dramaticallycut down on how much we need to search

Page 35: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-33: Evaluation Functions

We can’t search all the way to the bottom of thesearch tree

Trees are just too big

Search a few levels down, use an evaluationfunction to see how good the board looks at themoment

Back up the result of the evaluation function, as if itwas the utility function for the end of the game

Page 36: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-34: Evaluation Functions

Chess:

Material - value for each piece (pawn = 1,bishop = 3, etc)

Sum of my material - sum of your material

Positional advantagesKing protectedPawn structure

Network:

Partial Networks

Number and length of partial networks

Page 37: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-35: Evaluation Functions

If we have an evaluation function that tells us howgood a move is, why do we need to search at all?

Could just use the evaluation function

If we are only using the evalution function, doessearch do us any good?

Page 38: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-36: Evaluation Functions & α-β

How can we use the evaluation function tomaximize the pruning in alpha-beta pruning?

Page 39: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-37: Evaluation Functions & α-β

How can we use the evaluation function tomaximize the pruning in alpha-beta pruning?

Order children of max nodes, from highest tolowest

Order children of min node, from lowest tohighest

(Other than for ordering, eval function is notused for interior nodes)

With perfect ordering, we need to search only bd/2

(instead of bd) to find the optimal move – cansearch up to twice as far

Page 40: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-38: Stopping the Search

We can’t search all the way to the endgame

Not enough time

Search a set number of moves ahead

Search Depth

Page 41: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-39: Stopping the Search

We can’t search all the way to the endgame

Not enough time

Search a set number of moves ahead

What if we are in the middle of a piece trade?

In general, what if our opponent is about tocapture one of our pieces

Page 42: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-40: Stopping the Search

(b) White to move(a) White to move

Page 43: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-41: Stopping the Search

Quiescence Search

Only apply the evaluation function to nodes thatdo not swing wildly in value

If the next move makes a large change to theevaluation function, look ahead a few moremoves

Not increasing the search depth for the entiretree, just around where the action is

To prevent the search from going too deep, mayrestrict the kinds of moves (captures only, forinstance)

Page 44: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-42: Stopping the Search

Horizon Problem

Sometimes, we can push a bad move past thehorizon of our search

Not preventing the bad move, just delaying it

A position will look good, even though it isutlimately bad

Page 45: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-43: Horizon Problem

Black to move

Page 46: Data Structures and Algorithmsgalles/cs245S15/lecture/twoplayer.pdf · P4-1: Two player games Board-Splitting Game Two players, V & H V splits the board vertically, selects one half

P4-44: Horizon Problem

Singular Extensions

When we are going to stop, see if there is onemove that is clearly better than all of the others.

If so, do a quick “search”, looking only at thebest move for each player

Stop when there is no “clearly better” move

Helps with the horizon problem, for a series offorced moves

Similar to quiescence search