MiniMax and Alpha Beta Pruning - GitLab · Alpha-Beta Pruning Alpha-beta pruningallows to avoid...

Post on 22-Jun-2020

25 views 0 download

Transcript of MiniMax and Alpha Beta Pruning - GitLab · Alpha-Beta Pruning Alpha-beta pruningallows to avoid...

MiniMax andAlpha Beta Pruning

Minimax

• Algorithm that finds an optimal move for you (maximizer) in a two player game

• A game can be seen as a tree where nodes are possible game states.

• One of components of a game state is whose move it is:

• your (maximizer’s)

• your opponent’s (minimizer’s)

• The initial state of the game is the root of the game tree.

• Maximizer starts the game.

• Children of the root tree represent all possible game states resulting from possible moves that maximizer could make.

• Those children nodes have children representing the game states after every possible minimizer’s moves.

• These nodes have children corresponding to the possible second moves of the maximizer.

• ...

• The leaves of the tree are final states of the game.

• Every game state has a value associated with it

• The value of leaf nodes is obtained by some heuristic

• The heuristic value measures the favorability of the node for the maximizing player

• Internal (non leaf) nodes inherit their value from a child leaf node• At a node maximizer chooses the child with the greatest value, this value

becomes the value of the node• At a node minimizer chooses the child with the smallest value, this value becomes

the value of the node

• The full minimax search explores some parts of the tree it does not have to.

• Expanding the entire game tree may be even infeasible.

• One of the solutions is to only search the tree to a specified depth. When the depth limit of the search is exceeded, apply evaluation function to the node as if it were a leaf

• Another solution is to prune some of the branches (using, for example, alpha-beta pruning technique)

Alpha-Beta Pruning

Alpha-beta pruning allows to avoid searching subtrees of moves which do not lead to the optimal minimax solution.

5 2 7 6 1 3 11622 74 9 2

path we are currently exploring

path to currently best-known solution

explored but not the best path

pruned (not explored) branches

5 2 7 6 1 3 11622 74 9 2

5 2 7 6 1 3 11622 74 9 2

≥ "

5 2 7 6 1 3 11622 74 9 2

= "

5 2 7 6 1 3 11622 74 9 2

= "

≤ "

5 2 7 6 1 3 11622 74 9 2

= "

≤ "

5 2 7 6 1 3 11622 74 9 2

= "

≤ "

≥ %

5 2 7 6 1 3 11622 74 9 2

= "

≤ "

≥ %

5 2 7 6 1 3 11622 74 9 2

= "

= "

≥ $

5 2 7 1 3 11622 74 9 2

= "

= "

≥ $

6

5 2 7 1 3 11622 74 9 2

= "

= "

≥ $

≥ "

6

5 2 7 1 3 11622 74 9 2

= "

= "

≥ $

≥ "

6

5 2 7 1 3 11622 74 9 2

= "

= "

≥ $

≥ "

≥ %

6

5 2 7 1 3 11622 74 9 2

= "

= "

≥ $

≥ "

= %

6

5 2 7 1 3 11622 74 9 2

= "

= "

≥ $

≥ "

= %

≤ %

6

5 2 7 1 3 11622 74 9 2

= "

= "

≥ $

≥ "

= %

≤ %

6

5 2 7 1 3 11622 74 9 2

= "

= "

≥ $

≥ "

= %

≤ %

6

5 2 7 1 3 11622 74 9 2

= "

= "

≥ $

≥ "

= %

≤ %

6

5 2 7 1 3 11622 74 9 2

= "

= "

≥ $

≥ "

= %

≤ %

6

≥4

5 2 7 1 3 11622 74 9 2

= "

= "

≥ $

≥ "

= %

≤ %

6

=7

5 2 7 1 3 11622 74 9 2

= "

= "

≥ $

≥ "

= %

≤ %

6

=7

≤ 7

5 2 7 1 3 11622 74 9 2

= "

= "

≥ $

≥ "

= %

≤ %

6

=7

≤ 7

5 2 7 1 3 11622 74 9 2

= "

= "

≥ $

≥ "

= %

≤ %

6

=7

≤ 7

≥ '

5 2 7 1 3 11622 74 9 2

= "

= "

≥ $

≥ "

= %

≤ %

6

=7

≤ 7

≥ '

5 2 7 1 3 11622 74 9 2

= "

= "

≥ $

≥ "

= %

≤ %

6

=7

= 7

≥ '

5 2 7 1 3 11622 74 9 2

= "

= "

≥ $

=7

= %

≤ %

6

=7

= 7

≥ '

Why “Alpha-beta” ?

Any new node is considered as a game-state of a poten4al solu4on only if the following equa4on holds

α ≤ # ≤ $where # is the current (es4mate of the) value of the node.

Therefore α ≤ $

! = ∞$ = -∞

≥ ∞≤ -∞

! = ∞$ = -∞

! = ∞$ = -∞

! = ∞$ = -∞

! = ∞$ = -∞

! = ∞$ = -∞

! = ∞$ = -∞

! = ∞$ = -∞

! = ∞$ = -∞

5

! = ∞$ = -∞

! = ∞$ = -∞

! = ∞$ = 5

5

! = ∞$ = -∞

! = ∞$ = -∞

! = ∞$ = 5

5 2

! = ∞$ = -∞

! = 5$ = -∞

! = ∞$ = 5

5 2

5

! = ∞$ = -∞

! = 5$ = -∞

! = ∞$ = 5

5 2

! = 5$ = -∞5

! = ∞$ = -∞

! = 5$ = -∞

! = ∞$ = 5

5 2

! = 5$ = -∞

7

5

! = ∞$ = -∞

! = 5$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

5

! = ∞$ = -∞

! = 5$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

5 7

! = ∞$ = -∞

! = 5$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

5 7

5

! = ∞$ = 5

! = 5$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

5 7

5

! = ∞$ = 5

! = 5$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !! = ∞$ = 5

5 7

5

! = ∞$ = 5

! = 5$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !! = ∞$ = 5

5 7

5

! = ∞$ = 5

! = ∞$ = 5

! = 5$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

1

! = ∞$ = 5! = ∞

$ = 5

5

75

! = ∞$ = 5

! = 5$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

1

! = ∞$ = 5! = ∞

$ = 5

5

75

3

! = ∞$ = 5

! = 5$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

1

! = ∞$ = 5! = ∞

$ = 5

5

75

3

3

! = ∞$ = 5

! = 5$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

1

! = 3$ = 5! = ∞

$ = 5

5

75

3

3

! = ∞$ = 5

! = 5$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

1

! = 3$ = 5! = ∞

$ = 5

5

75

3

3

α ≰ !3

! = ∞$ = 5! = 5

$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

1

! = 3$ = 5! = ∞

$ = 5

5

75

3

3

α ≰ !3

! = ∞$ = 5

! = ∞$ = 5! = 5

$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

1

! = 3$ = 5! = ∞

$ = 5

5

75

3

3

α ≰ !3

! = ∞$ = 5

! = ∞$ = 5

! = ∞$ = 5! = 5

$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

1

! = 3$ = 5! = ∞

$ = 5

5

75

3

3

α ≰ !3

! = ∞$ = 5

! = ∞$ = 5

4

! = ∞$ = 5! = 5

$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

1

! = 3$ = 5! = ∞

$ = 5

5

75

3

3

α ≰ !3

! = ∞$ = 5

! = ∞$ = 5

4 7

! = ∞$ = 5! = 5

$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

1

! = 3$ = 5! = ∞

$ = 5

5

75

3

3

α ≰ !3

! = ∞$ = 5

! = ∞$ = 7

4 7

! = 7$ = 5! = 5

$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

1

! = 3$ = 5! = ∞

$ = 5

5

75

3

3

α ≰ !3

! = ∞$ = 5

! = ∞$ = 7

4 7

7

! = 7$ = 5! = 5

$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

1

! = 3$ = 5! = ∞

$ = 5

5

75

3

3

α ≰ !3

! = ∞$ = 5

! = ∞$ = 7

4 7

7

! = 7$ = 5

! = 7$ = 5! = 5

$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

1

! = 3$ = 5! = ∞

$ = 5

5

75

3

3

α ≰ !3

! = ∞$ = 5

! = ∞$ = 7

4 7

7

! = 7$ = 9

9

! = 7$ = 5! = 5

$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

1

! = 3$ = 5! = ∞

$ = 5

5

75

3

3

α ≰ !3

! = ∞$ = 5

! = ∞$ = 7

4 7

7

! = 7$ = 9

9

α ≰ !

! = 7$ = 5! = 5

$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

1

! = 3$ = 5! = ∞

$ = 5

5

75

3

3

α ≰ !3

! = ∞$ = 5

! = ∞$ = 7

4 7

7

! = 7$ = 9

9

α ≰ !9

! = 7$ = 5! = 5

$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

1

! = 3$ = 5! = ∞

$ = 5

5

75

3

3

α ≰ !3

! = ∞$ = 5

! = ∞$ = 7

4 7

7

! = 7$ = 9

9

α ≰ !9

7

! = 7$ = 5! = 5

$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

1

! = 3$ = 5! = ∞

$ = 5

5

75

3

3

α ≰ !3

! = ∞$ = 5

! = ∞$ = 7

4 7

7

! = 7$ = 9

9

α ≰ !9

7

7

! = 7$ = 5! = 5

$ = -∞

! = ∞$ = 5

5 2

! = 5$ = 7

7

α ≰ !

1

! = 3$ = 5! = ∞

$ = 5

5

75

3

3

α ≰ !3

! = ∞$ = 5

! = ∞$ = 7

4 7

7

! = 7$ = 9

9

α ≰ !9

7

7

Alpha-Beta Pruning Prac2ce

h"p://inst.eecs.berkeley.edu/~cs61b/fa14/ta-materials/apps/ab_tree_prac=ce/

Optimizing even more...

• Order in which children are visited is relevant• Some branches will never be played by ra6onal players