19
Minimax procedure • Create start node as a MAX node with current board configuration • Expand nodes down to some depth (a.k.a. ply) of lookahead in the game • Apply the evaluation function at each of the leaf nodes • “Back up” values for each of the non-leaf nodes until a value is computed for the root node – At MIN nodes, the backed-up value is the minimum of the values associated with its children. – At MAX nodes, the backed up value is the maximum of the values associated with its children. • Pick the operator associated with the child node whose backed-up value determined the value at the root

Game AI

Embed Size (px)

DESCRIPTION

Artigicial Inteligent notes

Citation preview

Page 1: Game AI

Minimax procedure• Create start node as a MAX node with current board configuration • Expand nodes down to some depth (a.k.a. ply) of lookahead in the

game• Apply the evaluation function at each of the leaf nodes • “Back up” values for each of the non-leaf nodes until a value is

computed for the root node– At MIN nodes, the backed-up value is the minimum of the values

associated with its children. – At MAX nodes, the backed up value is the maximum of the values

associated with its children. • Pick the operator associated with the child node whose backed-up

value determined the value at the root

Page 2: Game AI

Minimax Algorithm

2 7 1 8

MAX

MIN

2 7 1 8

2 1

2 7 1 8

2 1

2

2 7 1 8

2 1

2This is the moveselected by minimaxStatic evaluator

value

Page 3: Game AI

Minimax Tree

MAX node

MIN node

f valuevalue computed

by minimax

Page 4: Game AI

Minimax Principle• “Assume the worst”

– say we are two plays (in the tree) away from the terminal states– high numbers favor the computer

• so we want to choose moves which maximize utility– low numbers favor the opponent

• so they will choose moves which minimize utility

• Minimax Principle– you (the computer) assume that the opponent will choose the minimizing

move next (after your move)– so you now choose the best move under this assumption

• i.e., the maximum (highest-value) option considering both your move and the opponent’s optimal move.

– we can generalize this argument more than 2 moves ahead: we can search ahead as far as we can afford.

Page 5: Game AI

Minimax Search Example• Explore the tree as far as the terminal states• Calculate utilities for the resulting board configurations• The computer will make the move such that when the opponent makes his best

move, the board configuration will be in the best position for the computer

Page 6: Game AI

Propagating Minimax Values up the Game Tree• Starting from the leaves

– Assign a value to the parent node as follows• Children are Opponent’s moves: Minimum of all immediate children• Children are Computer’s moves: Maximum of all immediate children

Page 7: Game AI

Deeper Game TreesMinimax can be generalized to deeper game trees (than just 2 levels): “propagate” utility values upwards in the tree

Page 8: Game AI

General Minimax Algorithm on a Game Tree

For each move by the computer1. perform depth-first search as far as the terminal state2. assign utilities at each terminal state3. propagate upwards the minimax choices

if the parent is a minimizer (opponent)propagate up the minimum value of the children

if the parent is a maximizer (computer)propagate up the maximum value of the children

4. choose the move (the child of the current node) correspondingto the maximum of the minimax values if the children

Note: - minimax values are gradually propagated upwards as depth-first search proceeds, i.e., minimax values propagate up the tree in a “left-to-right” fashion

Page 9: Game AI

Complexity of Minimax Algorithm• Assume that all terminal states are at depth d• Space complexity

– depth-first search, so O(bd)• Time complexity

– branching factor b, thus, O(bd)

• The O(bd) time complexity is a major problem since the computer typically only has a finite amount of time to make a move,

– e.g., in chess, 2 minute time-limit, but b=35, d=50 !

• So the direct minimax algorithm is impractical in practice– instead what about depth-limited search to depth m?– but utilities are defined only at terminal states– => need to know the “utility” of non-terminal states

• these are called heuristic/static evaluation functions

Page 10: Game AI

Idea of Static Evaluation Functions• An Evaluation Function:

– provide an estimate of how good the current board configuration is for the computer.

– think of it as the expected utility averaged over all possible game outcomes from that position

– it reflects the computer’s chances of winning from that node– but it must be easy to calculate, i.e., cannot involve searching the tree below

the depth-limit node– thus, we use various easily calculated heuristics, e.g.,

• Chess: Value of all white pieces - Value of all black pieces– Typically, one figures how good it is for the computer, and how good it is for

the opponent, and subtracts the opponent’s score from the computer’s

• Must agree with the utility function when calculated at terminal nodes

Page 11: Game AI

Minimax with Evaluation Functions

• The same as general minimax, except– only goes to depth m– uses evaluation functions (estimates of utility)

• How would this algorithm perform at chess?– could look ahead about 4 “ply” (pairs of moves)– would be consistently beaten even by average chess players!

For each move by the computer1. perform depth-first search with depth-limit m2. assign evaluation functions at each state at depth m 3. propagate upwards the minimax choices

if the parent is a minimizer (opponent)propagate up the minimum value of the children

if the parent is a maximizer (computer)propagate up the maximum value of the children

4. choose the move (the child of the current node) correspondingto the maximum of the minimax values if the children

Page 12: Game AI

Alpha-beta pruning• We can improve on the performance of the

minimax algorithm through alpha-beta pruning

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

2 7 1

=2

>=2

<=1

?

• We don’t need to compute the value at this node.

• No matter what it is, it can’t affect the value of the root node.

MAX

MAX

MIN

Page 13: Game AI

The Alpha Beta Principle: Marry Search Tree Generation with Position Evaluations

Computer's

1

0 -81

4 1 2 0 -8

X X

3

X

Opponent’s Moves(min of evaluations)

(X indicates ‘not evaluated’)

Page 14: Game AI

Alpha Beta Procedure• Depth first search of game tree, keeping track of:

– Alpha: Highest value seen so far on maximizing level– Beta: Lowest value seen so far on minimizing level

• Pruning– When Maximizing,

• do not expand any more sibling nodes once a node has been seen whose evaluation is smaller than Alpha

– When Minimizing, • do not expand any sibling nodes once a node has been seen whose

evaluation is greater than Beta

Page 15: Game AI

Alpha-Beta Pruning gets its name from the following two terms:

alpha (α) = the value of the best choice at any node for the MAX algorithm

beta (β) = the value of the best choice at any node for the MIN algorithm

The minimax algorithm is a way of finding an optimal move in a two player game. Alpha-beta pruning is a way of finding the optimal minimax solution while avoiding searching subtrees of moves which won't be selected. In the search tree for a two-player game, there are two kinds of nodes, nodes representing your moves and nodes representing your opponent's moves.

Page 16: Game AI

The Alpha-Beta Procedure

•Alpha-beta pruning is a procedure to reduce the amount of computation and searching during minimax. •Minimax is a two-pass search, one pass is used to assign heuristic values to the nodes at the ply depth and the second is used to propagate the values up the tree.

Page 17: Game AI

• Alpha-beta search proceeds in a depth-first fashion.

• An alpha value is an initial or temporary value associated with a MAX node. Because MAX nodes are given the maximum value among their children, an alpha value can never decrease; it can only go up.

• A beta value is an initial or temporary value associated with a MIN node. Because MIN nodes are given the minimum value among their children, a beta value can never increase; it can only go down.

Page 18: Game AI

• For example, suppose a MAX node's alpha = 6. Then the search needn't consider any branches emanating from a MIN descendant that has a beta value that is less-than-or-equal to 6. So if you know that a MAX node has an alpha of 6, and you know that one of its MIN descendants has a beta that is less than or equal to 6, you needn't search any further below that MIN node. This is called alpha pruning.

Page 19: Game AI

For example, suppose a MAX node's alpha = 6. Then the search needn't consider any branches emanating from a MIN descendant that has a beta value that is less-than-or-equal to 6. So if you know that a MAX node has an alpha of 6, and you know that one of its MIN descendants has a beta that is less than or equal to 6, you needn't search any further below that MIN node. This is called alpha pruning. The reason is that no matter what happens below that MIN node, it cannot take on a value that is greater than 6. So its value cannot be propagated up to its MAX (alpha) parent. Similarly, if a MIN node's beta value = 6, you needn't search any further below a descendant MAX that has acquired an alpha value of 6 or more. This is called beta pruning. The reason again is that no matter what happens below that MAX node, it cannot take on a value that is less than 6. So its value cannot be propagated up to its MIN (beta) parent. Rules for Alpha-beta PruningAlpha Pruning: Search can be stopped below any MIN node having a beta value less than or equal to the alpha value of any of its MAX ancestors. Beta Pruning: Search can be stopped below any MAX node having a alpha value greater than or equal to the beta value of any of its MIN ancestors.