Game Playing Part 2 Alpha-Beta Pruning Yingyu Liang - - PowerPoint PPT Presentation

game playing
SMART_READER_LITE
LIVE PREVIEW

Game Playing Part 2 Alpha-Beta Pruning Yingyu Liang - - PowerPoint PPT Presentation

Game Playing Part 2 Alpha-Beta Pruning Yingyu Liang yliang@cs.wisc.edu Computer Sciences Department University of Wisconsin, Madison [based on slides from A. Moore http://www.cs.cmu.edu/~awm/tutorials , C. Dyer, J. Skrentny, Jerry Zhu] slide 1


slide-1
SLIDE 1

slide 1

Game Playing

Part 2 Alpha-Beta Pruning

Yingyu Liang yliang@cs.wisc.edu Computer Sciences Department University of Wisconsin, Madison

[based on slides from A. Moore http://www.cs.cmu.edu/~awm/tutorials , C. Dyer, J. Skrentny, Jerry Zhu]

slide-2
SLIDE 2

slide 2

alpha-beta pruning Gives the same game theoretic values as minimax, but prunes part of the game tree.

"If you have an idea that is surely bad, don't take the time to see how truly awful it is." -- Pat Winston

slide-3
SLIDE 3

slide 3

Alpha-Beta Motivation

S A 100 C 200 D

100

B E

120

F

20

max min

  • Depth-first order
  • After returning from A, Max can get at least 100 at S
  • After returning from F, Max can get at most 20 at B
  • At this point, Max losts interest in B
  • There is no need to explore G. The subtree at G is
  • pruned. Saves time.

G

slide-4
SLIDE 4

slide 4

Alpha-beta pruning

function Max-Value (s,α,β) inputs: s: current state in game, Max about to play α: best score (highest) for Max along path to s β: best score (lowest) for Min along path to s

  • utput: min(β , 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’,α,β)) if ( α ≥ β ) then return β /* alpha pruning */ return α

Starting from the root: Max-Value(root, -, +)

slide-5
SLIDE 5

slide 5

Alpha-beta pruning

function Max-Value (s,α,β) inputs: s: current state in game, Max about to play α: best score (highest) for Max along path to s β: best score (lowest) for Min along path to s

  • utput: min(β , 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’,α,β)) if ( α ≥ β ) then return β /* alpha pruning */ return α function Min-Value(s,α,β)

  • utput: max(α , 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’,α,β)) if (α ≥ β ) then return α /* beta pruning */ return β

Starting from the root: Max-Value(root, -, +)

slide-6
SLIDE 6

slide 6

Alpha-beta pruning example 1

S A 100 C 200 D

100

B E

120

F

20

max min

G α=- β=+

  • Keep two bounds along the path

▪ : the best Max can do ▪ : the best (smallest) Min can do

  • If at anytime  exceeds , the remaining children are

pruned.

slide-7
SLIDE 7

slide 7

Alpha-beta pruning example 1

S A 100 C 200 D

100

B E

120

F

20

max min

G α=- β=+ α=- β=+

slide-8
SLIDE 8

slide 8

Alpha-beta pruning example 1

S A 100 C 200 D

100

B E

120

F

20

max min

G α=- β=+ α=- β=200

slide-9
SLIDE 9

slide 9

Alpha-beta pruning example 1

S A 100 C 200 D

100

B E

120

F

20

max min

G α=- β=+ α=- β=100

slide-10
SLIDE 10

slide 10

Alpha-beta pruning example 1

S A 100 C 200 D

100

B E

120

F

20

max min

G α=100 β=+ α=- β=100

slide-11
SLIDE 11

slide 11

Alpha-beta pruning example 1

S A 100 C 200 D

100

B E

120

F

20

max min

G α=100 β=+ α=- β=100 α=100 β=+

slide-12
SLIDE 12

slide 12

Alpha-beta pruning example 1

S A 100 C 200 D

100

B E

120

F

20

max min

G α=100 β=+ α=- β=100 α=100 β=120

slide-13
SLIDE 13

slide 13

Alpha-beta pruning example 1

S A 100 C 200 D

100

B E

120

F

20

max min

G α=100 β=+ α=- β=100 α=100 β=20

X

slide-14
SLIDE 14

slide 14

Alpha-beta pruning

function Max-Value (s,α,β) inputs: s: current state in game, Max about to play α: best score (highest) for Max along path to s β: best score (lowest) for Min along path to s

  • utput: min(β , 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’,α,β)) if ( α ≥ β ) then return β /* alpha pruning */ return α function Min-Value(s,α,β)

  • utput: max(α , 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’,α,β)) if (α ≥ β ) then return α /* beta pruning */ return β

Starting from the root: Max-Value(root, -, +)

slide-15
SLIDE 15

slide 15

Alpha-beta pruning example 1

A B 20 D 20 E

  • 10

C 25 F

  • 20

G

25

max min max

H

  • Keep two bounds along the path

▪ : the best Max can do ▪ : the best (smallest) Min can do

  • If at anytime  exceeds , the remaining children are

pruned.

S

X What are the alpha and beta values on S?

slide-16
SLIDE 16

slide 16

Alpha-beta pruning example 2

A B 20 D 20 E

  • 10

C 25 F

  • 20

G

25

max min max

H

  • Keep two bounds along the path

▪ : the best Max can do ▪ : the best (smallest) Min can do

  • If at anytime  exceeds , the remaining children are

pruned.

S

X

α=- β=+

slide-17
SLIDE 17

slide 17

Alpha-beta pruning example 2

A B 20 D 20 E

  • 10

C 25 F

  • 20

G

25

max min max

H

  • Keep two bounds along the path

▪ : the best Max can do ▪ : the best (smallest) Min can do

  • If at anytime  exceeds , the remaining children are

pruned.

S

X

α=- β=+

slide-18
SLIDE 18

slide 18

Alpha-beta pruning example 2

A B 20 D 20 E

  • 10

C 25 F

  • 20

G

25

max min max

H

  • Keep two bounds along the path

▪ : the best Max can do ▪ : the best (smallest) Min can do

  • If at anytime  exceeds , the remaining children are

pruned.

S

X

α=- β=+

slide-19
SLIDE 19

slide 19

Alpha-beta pruning example 2

A B 20 D 20 E

  • 10

C 25 F

  • 20

G

25

max min max

H

  • Keep two bounds along the path

▪ : the best Max can do ▪ : the best (smallest) Min can do

  • If at anytime  exceeds , the remaining children are

pruned.

S

X

α=20 β=+

slide-20
SLIDE 20

slide 20

Alpha-beta pruning example 2

A B 20 D 20 E

  • 10

C 25 F

  • 20

G

25

max min max

H

  • Keep two bounds along the path

▪ : the best Max can do ▪ : the best (smallest) Min can do

  • If at anytime  exceeds , the remaining children are

pruned.

S

X

α=20 β=+

slide-21
SLIDE 21

slide 21

Alpha-beta pruning example 2

A B 20 D 20 E

  • 10

C 25 F

  • 20

G

25

max min max

H

  • Keep two bounds along the path

▪ : the best Max can do ▪ : the best (smallest) Min can do

  • If at anytime  exceeds , the remaining children are

pruned.

S

X

α=20 β=+

slide-22
SLIDE 22

slide 22

Alpha-beta pruning example 2

A B 20 D 20 E

  • 10

C 25 F

  • 20

G

25

max min max

H

  • Keep two bounds along the path

▪ : the best Max can do ▪ : the best (smallest) Min can do

  • If at anytime  exceeds , the remaining children are

pruned.

S

X

α=20 β=+

What are the alpha and beta values on A?

slide-23
SLIDE 23

slide 23

Alpha-beta pruning example 2

A B 20 D 20 E

  • 10

C 25 F

  • 20

G

25

max min max

H

  • Keep two bounds along the path

▪ : the best Max can do ▪ : the best (smallest) Min can do

  • If at anytime  exceeds , the remaining children are

pruned.

S

X

α=- β=20

slide-24
SLIDE 24

slide 24

Alpha-beta pruning example 2

A B 20 D 20 E

  • 10

C 25 F

  • 20

G

25

max min max

H

  • Keep two bounds along the path

▪ : the best Max can do ▪ : the best (smallest) Min can do

  • If at anytime  exceeds , the remaining children are

pruned.

S

X

α=- β=20

slide-25
SLIDE 25

slide 25

Alpha-beta pruning example 2

A B 20 D 20 E

  • 10

C 25 F

  • 20

G

25

max min max

H

  • Keep two bounds along the path

▪ : the best Max can do ▪ : the best (smallest) Min can do

  • If at anytime  exceeds , the remaining children are

pruned.

S

X

α=-20 β=20

slide-26
SLIDE 26

slide 26

Alpha-beta pruning example 2

A B 20 D 20 E

  • 10

C 25 F

  • 20

G

25

max min max

H

  • Keep two bounds along the path

▪ : the best Max can do ▪ : the best (smallest) Min can do

  • If at anytime  exceeds , the remaining children are

pruned.

S

X

α=25 β=20

slide-27
SLIDE 27

slide 27

Yet another alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If at anytime  exceeds , the remaining children are

pruned.

A A

α=-

O W

  • 3

B N

4

F G

  • 5

X

  • 5

E D C R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

J L

2

max

– = –  + = + 

[Example from James Skrentny]

slide-28
SLIDE 28

slide 28

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

B B

β = +

O W

  • 3

N

4

F G

  • 5

X

  • 5

E D C R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

J L

2

A

α =-

max min

[Example from James Skrentny]

slide-29
SLIDE 29

slide 29

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

F F

α =-

O W

  • 3

B

β = +

N

4

G

  • 5

X

  • 5

E D C R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

J L

2

A

α =-

max min max

[Example from James Skrentny]

slide-30
SLIDE 30

slide 30

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

O W

  • 3

B

β = +

N

4

F

α =-

G

  • 5

X

  • 5

E D C R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

J L

2

A

α =-

max N

4

min max

slide-31
SLIDE 31

slide 31

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

F

α =-

F

α = 4

O W

  • 3

B

β = +

N

4

G

  • 5

X

  • 5

E D C R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

J L

2

A

α =-

max min max

 updated

slide-32
SLIDE 32

slide 32

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

O O

β = +

W

  • 3

B

β = +

N

4

F

α = 4

G

  • 5

X

  • 5

E D C R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

J L

2

A

α =-

max min max min

slide-33
SLIDE 33

slide 33

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

O

β = +

W

  • 3

B

β = +

N

4

F

α = 4

G

  • 5

X

  • 5

E D C R P

9

Q

  • 6

S

3

T

5

U

  • 7

K M H

3

I

8

J L

2

A

α =-

max min W

  • 3

min V

  • 9
slide-34
SLIDE 34

slide 34

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

O

β = +

O

β =- 3

W

  • 3

B

β = +

N

4

F

α = 4

G

  • 5

X

  • 5

E D C R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

J L

2

A

α =-

max min max min

slide-35
SLIDE 35

slide 35

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

O

β =- 3

W

  • 3

B

β = +

N

4

F

α = 4

G

  • 5

X

  • 5

E D C R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

J L

2

A

α =-

max min max min X

  • 5

Pruned!

slide-36
SLIDE 36

slide 36

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

B

β = +

B

β = 4

O

β =- 3

W

  • 3

N

4

F

α = 4

G

  • 5

X

  • 5

E D C R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

J L

2

A

α =-

max min max min X

  • 5
slide-37
SLIDE 37

slide 37

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

O

β =- 3

W

  • 3

B

β = 4

N

4

F

α = 4

G

  • 5

X

  • 5

E D C R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

J L

2

A

α =-

max min max min X

  • 5

G

  • 5
slide-38
SLIDE 38

slide 38

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

O

β =- 3

W

  • 3

B

β =- 5

N

4

F

α = 4

G

  • 5

X

  • 5

E D C R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

J L

2

A

α =-

max min max min X

  • 5

G

  • 5
slide-39
SLIDE 39

slide 39

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

O

β =- 3

W

  • 3

B

β =- 5

N

4

F

α = 4

X

  • 5

E D C R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

J L

2

max min max min X

  • 5

A

α=-5

G

  • 5
slide-40
SLIDE 40

slide 40

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

C C

β = +

O

β =- 3

W

  • 3

B

β =- 5

N

4

F

α = 4

G

  • 5

X

  • 5

E D R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

J L

2

A

α =

max min max min X

  • 5

A

α =- 5

slide-41
SLIDE 41

slide 41

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

O

β =- 3

W

  • 3

B

β =- 5

N

4

F

α = 4

G

  • 5

X

  • 5

E D C

β = +

R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

J L

2

A

α =

max min max min X

  • 5

A

α =- 5

H

3

slide-42
SLIDE 42

slide 42

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

C

β = +

C

β = 3

O

β =- 3

W

  • 3

B

β =- 5

N

4

F

α = 4

G

  • 5

X

  • 5

E D R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

J L

2

A

α =

max min max min X

  • 5

A

α =- 5

slide-43
SLIDE 43

slide 43

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

O

β =- 3

W

  • 3

B

β =- 5

N

4

F

α = 4

G

  • 5

X

  • 5

E D C

β = 3

R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

J L

2

A

α =

max min max min X

  • 5

A

α =- 5

I

8

slide-44
SLIDE 44

slide 44

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

J J

α =- 5

O

β =- 3

W

  • 3

B

β =- 5

N

4

F

α = 4

G

  • 5

X

  • 5

E D C

β = 3

R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

L

2

A

α =

max min min X

  • 5

A

α =- 5

slide-45
SLIDE 45

slide 45

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

O

β =- 3

W

  • 3

B

β =- 5

N

4

F

α = 4

G

  • 5

X

  • 5

E D C

β = 3

R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

J

α =- 5

L

2

A

α =

max min max min X

  • 5

A

α =- 5

P

9

slide-46
SLIDE 46

slide 46

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

J

α =-

J

α = 9

O

β =- 3

W

  • 3

B

β =- 5

N

4

F

α = 4

G

  • 5

X

  • 5

E D C

β = 3

R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

L

2

A

α =

max min max min X

  • 5

A

α =- 5

Q

  • 6

R

slide-47
SLIDE 47

slide 47

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

J

α =-

J

α = 9

O

β =- 3

W

  • 3

B

β =- 5

N

4

F

α = 4

G

  • 5

X

  • 5

E D C

β = 3

R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

L

2

A

α =

max min max min X

  • 5

A

α = 3

Q

  • 6

R

slide-48
SLIDE 48

slide 48

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

O

β =- 3

W

  • 3

B

β =- 5

N

4

F

α = 4

G

  • 5

X

  • 5

E D C

β = 3

R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K M H

3

I

8

J

α = 9

L

2

A

α =

max min max min X

  • 5

A

α = 3

slide-49
SLIDE 49

slide 49

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

O

β =- 3

W

  • 3

B

β =- 5

N

4

F

α = 4

G

  • 5

X

  • 5

E

β = 2

D C

β = 3

R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K

α = 5

M H

3

I

8

J

α = 9

L

2

A

α =

max min max min X

  • 5

A

α = 3

slide-50
SLIDE 50

slide 50

Alpha-beta pruning example

  • Keep two bounds along the path

▪ : the best Max can do on the path ▪ : the best (smallest) Min can do on the path

  • If a max node exceeds , it is pruned.
  • If a min node goes below , it is pruned.

[Example from James Skrentny]

O

β =- 3

W

  • 3

B

β =- 5

N

4

F

α = 4

G

  • 5

X

  • 5

E

β = 2

D C

β = 3

R P

9

Q

  • 6

S

3

T

5

U

  • 7

V

  • 9

K

α = 5

M H

3

I

8

J

α = 9

L

2

A

α =

max min max min X

  • 5

A

α = 3

slide-51
SLIDE 51

slide 51

How effective is alpha-beta pruning?

  • Depends on the order of successors!
  • In the best case, the number of nodes to search is O(bm/2), the

square root of minimax’s cost.

  • This occurs when each player's best move is the leftmost child.
  • In DeepBlue (IBM Chess), the average branching factor was

about 6 with alpha-beta instead of 35-40 without.

  • The worst case is no pruning at all.

E

β = 2

S

3

T

5

U

4

V

3

K

α = 5

M L

2

E

β = 2

S

3

T

5

U

4

V

3

K

α = 5

M

α = 4

L

2

E

β = 2

S

3

T

5

U

4

V

3

K M L

2

A

α = 3

A

α = 3

A

α = 3