game playing - github pages

42
slide 1 Game Playing Part 1 Minimax Search Yingyu Liang [email protected] Computer Sciences Department University of Wisconsin, Madison [based on slides from A. Moore, C. Dyer, J. Skrentny, Jerry Zhu]

Upload: others

Post on 10-May-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Game Playing - GitHub Pages

slide 1

Game PlayingPart 1 Minimax Search

Yingyu [email protected]

Computer Sciences DepartmentUniversity of Wisconsin, Madison

[based on slides from A. Moore, C. Dyer, J. Skrentny, Jerry Zhu]

Page 2: Game Playing - GitHub Pages

slide 2

Not playing these games (not in this course) …

Page 3: Game Playing - GitHub Pages

slide 3

Instead, learn principles of how machines play

Page 4: Game Playing - GitHub Pages

slide 4

Overview• Important characteristics of games two-player zero-sum discrete finite deterministic

game of perfect information• Minimax search• Alpha-beta pruning

Page 5: Game Playing - GitHub Pages

slide 5

Game Examples

Page 6: Game Playing - GitHub Pages

slide 6

Two-player zero-sum discrete finite deterministic games of perfect information

Definitions:

• Zero-sum: one player’s gain is the other player’s loss. Does not mean fair.

• Discrete: states and decisions have discrete values• Finite: finite number of states and decisions• Deterministic: no coin flips, die rolls – no chance• Perfect information: each player can see the

complete game state. No simultaneous decisions.

Page 7: Game Playing - GitHub Pages

slide 7

Which of these are: Two-player zero-sum discrete finite deterministic games of perfect information?

Page 8: Game Playing - GitHub Pages

slide 8

Which of these are: Two-player zero-sum discrete finite deterministic games of perfect information?

Page 9: Game Playing - GitHub Pages

slide 9

Which of these are: Two-player zero-sum discrete finite deterministic games of perfect information?

Zero-sum: one player’s gain is the other player’s loss. Does not mean fair.

Page 10: Game Playing - GitHub Pages

slide 10

Which of these are: Two-player zero-sum discrete finite deterministic games of perfect information?

Zero-sum: one player’s gain is the other player’s loss. Does not mean fair.

Discrete: states and decisions have discrete values

Page 11: Game Playing - GitHub Pages

slide 11

Which of these are: Two-player zero-sum discrete finite deterministic games of perfect information?

Zero-sum: one player’s gain is the other player’s loss. Does not mean fair.

Discrete: states and decisions have discrete values

Finite: finite number of states and decisions

Page 12: Game Playing - GitHub Pages

slide 12

Which of these are: Two-player zero-sum discrete finite deterministic games of perfect information?

Zero-sum: one player’s gain is the other player’s loss. Does not mean fair.

Discrete: states and decisions have discrete values

Finite: finite number of states and decisions

Deterministic: no coin flips, die rolls – no chance

Page 13: Game Playing - GitHub Pages

slide 13

Which of these are: Two-player zero-sum discrete finite deterministic games of perfect information?

Zero-sum: one player’s gain is the other player’s loss. Does not mean fair.

Discrete: states and decisions have discrete values

Finite: finite number of states and decisions

Deterministic: no coin flips, die rolls – no chance

Perfect information: each player can see the complete game state. No simultaneous decisions.

Page 14: Game Playing - GitHub Pages

slide 14

Which of these are: Two-player zero-sum discrete finite deterministic games of perfect information?

Zero-sum: one player’s gain is the other player’s loss. Does not mean fair.

Discrete: states and decisions have discrete values

Finite: finite number of states and decisions

Deterministic: no coin flips, die rolls – no chance

Perfect information: each player can see the complete game state. No simultaneous decisions.

Page 15: Game Playing - GitHub Pages

slide 16

II-Nim: Max simple game

• There are 2 piles of sticks. Each pile has 2 sticks. • Each player takes one or more sticks from one pile.• The player who takes the last stick loses.

(ii, ii)

Page 16: Game Playing - GitHub Pages

slide 17

II-Nim: Max simple game

• There are 2 piles of sticks. Each pile has 2 sticks. • Each player takes one or more sticks from one pile.• The player who takes the last stick loses.

(ii, ii)

• Two players: Max and Min• If Max wins, the score is +1; otherwise -1 • Min’s score is –Max’s• Use Max’s as the score of the game

Page 17: Game Playing - GitHub Pages

slide 18

II-Nim: one trajectory of the game(ii, ii)

Page 18: Game Playing - GitHub Pages

slide 19

II-Nim: one trajectory of the game(ii, ii)

Max takes one stick from one pile

(i, ii)

Page 19: Game Playing - GitHub Pages

slide 20

II-Nim: one trajectory of the game(ii, ii)

Max takes one stick from one pile

(i, ii)Min takes two sticks from the other pile

(i,-)

Page 20: Game Playing - GitHub Pages

slide 21

II-Nim: one trajectory of the game(ii, ii)

Max takes one stick from one pile

(i, ii)Min takes two sticks from the other pile

(i,-)Max takes the last stick

(-,-)Max gets score -1

Page 21: Game Playing - GitHub Pages

slide 22

The game tree for II-Nim

(ii ii) Max

Convention: score is w.r.t. the first player Max. Min’s score = – Max

who is to move at this state

Two players: Max and Min

Max wants the largest scoreMin wants the smallest score

Page 22: Game Playing - GitHub Pages

slide 23

The game tree for II-Nim

(ii ii) Max

(i ii) Min (- ii) MinSymmetry(i ii) = (ii i)

Two players: Max and Min

Max wants the largest scoreMin wants the smallest score

Page 23: Game Playing - GitHub Pages

slide 24

The game tree for II-Nim

(ii ii) Max

(i ii) Min (- ii) Min

(i i) Max(- ii) Max (- i) Max

Two players: Max and Min

Max wants the largest scoreMin wants the smallest score

Page 24: Game Playing - GitHub Pages

slide 25

The game tree for II-Nim

(ii ii) Max

(i ii) Min (- ii) Min

(i i) Max(- ii) Max (- i) Max (- i) Max (- -) Max+1

Two players: Max and Min

Max wants the largest scoreMin wants the smallest score

Page 25: Game Playing - GitHub Pages

slide 26

The game tree for II-Nim

(ii ii) Max

(i ii) Min (- ii) Min

(i i) Max(- ii) Max (- i) Max (- i) Max (- -) Max+1

(- i) Min (- -) Min-1

Two players: Max and Min

Max wants the largest scoreMin wants the smallest score

Page 26: Game Playing - GitHub Pages

slide 27

The game tree for II-Nim

(ii ii) Max

(i ii) Min (- ii) Min

(i i) Max(- ii) Max (- i) Max (- i) Max (- -) Max+1

(- i) Min (- -) Min-1

(- i) Min

Two players: Max and Min

Max wants the largest scoreMin wants the smallest score

Page 27: Game Playing - GitHub Pages

slide 28

The game tree for II-Nim

(ii ii) Max

(i ii) Min (- ii) Min

(i i) Max(- ii) Max (- i) Max (- i) Max (- -) Max+1

(- i) Min (- -) Min-1

(- i) Min (- -) Min-1

Two players: Max and Min

Max wants the largest scoreMin wants the smallest score

Page 28: Game Playing - GitHub Pages

slide 29

The game tree for II-Nim

(ii ii) Max

(i ii) Min (- ii) Min

(i i) Max(- ii) Max (- i) Max (- i) Max (- -) Max+1

(- i) Min (- -) Min-1

(- i) Min (- -) Min-1

(- -) Min-1

Two players: Max and Min

Max wants the largest scoreMin wants the smallest score

Page 29: Game Playing - GitHub Pages

slide 30

The game tree for II-Nim

(ii ii) Max

(i ii) Min (- ii) Min

(i i) Max(- ii) Max (- i) Max (- i) Max (- -) Max+1

(- i) Min (- -) Min-1

(- i) Min (- -) Min-1

(- -) Min-1

(- -) Max+1

Two players: Max and Min

Max wants the largest scoreMin wants the smallest score

Page 30: Game Playing - GitHub Pages

slide 31

The game tree for II-Nim

(ii ii) Max

(i ii) Min (- ii) Min

(i i) Max(- ii) Max (- i) Max (- i) Max (- -) Max+1

(- i) Min (- -) Min-1

(- i) Min (- -) Min-1

(- -) Min-1

(- -) Max+1

(- -) Max+1

Two players: Max and Min

Max wants the largest scoreMin wants the smallest score

Page 31: Game Playing - GitHub Pages

slide 32

Game theoretic value• Game theoretic value (a.k.a. minimax value) of a

node = the score of the terminal node that will be reached if both players play optimally.

Page 32: Game Playing - GitHub Pages

slide 33

The game tree for II-Nim

(ii ii) Max

(i ii) Min (- ii) Min

(i i) Max(- ii) Max (- i) Max (- i) Max (- -) Max+1

(- i) Min (- -) Min-1

(- i) Min (- -) Min-1

(- -) Min-1

(- -) Max+1

(- -) Max+1

Two players: Max and Min

Max wants the largest scoreMin wants the smallest score

Page 33: Game Playing - GitHub Pages

slide 34

The game tree for II-Nim

(ii ii) Max

(i ii) Min (- ii) Min

(i i) Max(- ii) Max (- i) Max (- i) Max (- -) Max+1

(- i) Min +1

(- -) Min-1

(- -) Min-1

(- -) Max+1

(- -) Max+1

Two players: Max and Min

Max wants the largest scoreMin wants the smallest score

(- -) Min-1

(- i) Min+1

Page 34: Game Playing - GitHub Pages

slide 35

The game tree for II-Nim

(ii ii) Max

(i ii) Min (- ii) Min

(i i) Max+1

(- ii) Max +1

(- i) Max -1

(- i) Max -1

(- -) Max+1

(- i) Min +1

(- i) Min+1

(- -) Min-1

(- -) Max+1

(- -) Max+1

Two players: Max and Min

Max wants the largest scoreMin wants the smallest score

(- -) Min-1

(- -) Min-1

Page 35: Game Playing - GitHub Pages

slide 36

The game tree for II-Nim

(ii ii) Max

(i i) Max+1

(- ii) Max +1

(- i) Max -1

(- i) Max -1

(- -) Max+1

(- i) Min +1

(- i) Min+1

(- -) Min-1

(- -) Max+1

(- -) Max+1

Two players: Max and Min

Max wants the largest scoreMin wants the smallest score

(- -) Min-1

(- -) Min-1

(- ii) Min -1

(i ii) Min -1

Page 36: Game Playing - GitHub Pages

slide 37

The game tree for II-Nim

(i i) Max+1

(- ii) Max +1

(- i) Max -1

(- i) Max -1

(- -) Max+1

(- i) Min +1

(- i) Min+1

(- -) Min-1

(- -) Max+1

(- -) Max+1

Two players: Max and Min

Max wants the largest scoreMin wants the smallest score

(- -) Min-1

(- -) Min-1

(- ii) Min -1

(i ii) Min -1

(ii ii) Max -1

Page 37: Game Playing - GitHub Pages

slide 38

The game tree for II-Nim

(i i) Max+1

(- ii) Max +1

(- i) Max -1

(- i) Max -1

(- -) Max+1

(- i) Min +1

(- i) Min+1

(- -) Min-1

(- -) Max+1

(- -) Max+1

Two players: Max and Min

Max wants the largest scoreMin wants the smallest score

(- -) Min-1

(- -) Min-1

(- ii) Min -1

(i ii) Min -1

(ii ii) Max -1

Page 38: Game Playing - GitHub Pages

slide 39

The game tree for II-Nim

(ii ii) Max -1

(i ii) Min-1

(- ii) Min -1

(i i) Max+1

(- ii) Max +1

(- i) Max -1

(- i) Max -1

(- -) Max+1

(- i) Min +1

(- -) Min-1

(- i) Min+1

(- -) Min-1

(- -) Min-1

(- -) Max+1

(- -) Max+1

Two players: Max and Min

Max wants the largest scoreMin wants the smallest score

The first player always loses, if the second player plays optimally

Page 39: Game Playing - GitHub Pages

slide 40

The game tree for II-Nim

(ii ii) Max -1

(i ii) Min-1

(- ii) Min -1

(i i) Max+1

(- ii) Max +1

(- i) Max -1

(- i) Max -1

(- -) Max+1

(- i) Min +1

(- -) Min -1

(- i) Min+1

(- -) Min-1

(- -) Min -1

(- -) Max+1

(- -) Max+1

Two players: Max and Min

Max wants the largest scoreMin wants the smallest score

The first player always loses, if the second player plays optimally

Page 40: Game Playing - GitHub Pages

slide 42

Game theoretic value• Game theoretic value (a.k.a. minimax value) of a

node = the score of the terminal node that will be reached if both players play optimally.

• = The numbers we filled in.• Computed bottom up In Max’s turn, take the max of the children (Max

will pick that maximizing action) In Min’s turn, take the min of the children (Min will

pick that minimizing action)• Implemented as a modified version of DFS: minimax

algorithm

Page 41: Game Playing - GitHub Pages

slide 43

Minimax algorithmfunction Max-Value(s)inputs:

s: current state in game, Max about to playoutput: best-score (for Max) available from s

if ( s is a terminal state )then return ( terminal value of s )else

α := – ∞for each s’ in Succ(s)

α := max( α , Min-value(s’))return α

function Min-Value(s)output: best-score (for Min) available from s

if ( s is a terminal state )then return ( terminal value of s)else

β := ∞for each s’ in Succs(s)

β := min( β , Max-value(s’))return β

• Time complexity?• Space complexity?

Page 42: Game Playing - GitHub Pages

slide 44

Minimax algorithmfunction Max-Value(s)inputs:

s: current state in game, Max about to playoutput: best-score (for Max) available from s

if ( s is a terminal state )then return ( terminal value of s )else

α := – ∞for each s’ in Succ(s)

α := max( α , Min-value(s’))return α

function Min-Value(s)output: best-score (for Min) available from s

if ( s is a terminal state )then return ( terminal value of s)else

β := ∞for each s’ in Succs(s)

β := min( β , Max-value(s’))return β

• Time complexity?O(bm) bad

• Space complexity? O(bm)