aima code: adversarial search cse-391: artificial intelligence university of pennsylvania matt...

24
AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

Upload: amice-marsh

Post on 19-Jan-2018

218 views

Category:

Documents


0 download

DESCRIPTION

MINIMAX ● Search a game tree all the way to the end. – Start at initial game state. – Exhaustively expand children nodes (all the possible moves in the game from each board). – When you hit an “end state,” decide if it was good/win or bad/loss and give it an appropriate value. ● Then pass up the MIN or the MAX of the values of the children at each level (depending on who’s turn it was). ● At the top level, make the best move: follow the child that passes up the best value.

TRANSCRIPT

Page 1: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

AIMA Code: Adversarial Search

CSE-391: Artificial IntelligenceUniversity of Pennsylvania

Matt HuenerfauthFebruary 2005

Page 2: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

Game Classclass Game: . . . def terminal_test(self, state): "Return True if this is a final state for the game.“ def utility(self, state, player): "Return the value of this final state to player." def to_move(self, state): "Return the player whose move it is in this state.“ def successors(self, state): "Return a list of legal (move, state) pairs."

Page 3: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

MINIMAX● Search a game tree all the way to the end.

– Start at initial game state. – Exhaustively expand children nodes (all the possible moves in

the game from each board).– When you hit an “end state,” decide if it was good/win or

bad/loss and give it an appropriate value.● Then pass up the MIN or the MAX of the values of the

children at each level (depending on who’s turn it was). ● At the top level, make the best move:

follow the child that passes up the best value.

Page 4: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

argmax(list,func) – see utils.py

def argmax(list, func):

"Return element of list with highest func(list[i]) score"

best = list[0]; best_score = func(best)

for x in list:

x_score = func(x)

if x_score > best_score:

best, best_score = x, x_score

return best

Page 5: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

We’ll see this line of code on the next slide…state – will be a state of the game…call_min – is a function we’ll define later…

argmax( game.successors(state), lambda ((a, s)): call_min(s) )

● Successors is a list of children of this state: (action, state) pairs.● The lambda function created takes two arguments.

– It ignores the first and passes the second to call_min().– Applied to (action, state) pairs, it calls call_min on each state.

● Argmax returns pair with highest value for call_min(state).

Use of argmax(list,func)…

Page 6: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

At the root of the MINIMAX search at some state in a game.

def minimax_decision(state, game): # In a state in a game. player = game.to_move(state) # Who’s turn is it? action, state = \

argmax(game.successors(state), # What successor returns lambda ((a, s)): call_min(s)) # maximum call_min()?return action# Move to that state.

We first decide whose turn it is.

Then our function will tell us what move to make that will move us to the successor state that:- has the MAXIMUM return value - when we call the CALL_MIN function on it.

We’re going to see a trend here pretty soon…

Page 7: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

Take the MAX of your MINs,and the MIN of your MAXs…

Page 8: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

def call_max(state): if game.terminal_test(state): # Check if game over. return game.utility(state, player) v = -infinity for (a, s) in game.successors(state): # Return the

maximum v = max(v, call_min(s)) # of the call_min’s. return v

def call_min(state): if game.terminal_test(state): # Check if game over. return game.utility(state, player) v = infinity for (a, s) in game.successors(state): # Return the minimum v = min(v, call_max(s)) # of the call_max’s. return v

Page 9: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

def call_max(state): if game.terminal_test(state): return game.utility(state, player) v = -infinity for (a, s) in game.successors(state): v = max(v, call_min(s)) return v

def call_min(state): if game.terminal_test(state): return game.utility(state, player) v = infinity for (a, s) in game.successors(state): v = min(v, call_max(s)) return v

= take maximum= take minimum

Initial call to minimax_decision

GAMEOVER

GAMEOVER

GAMEOVER

GAMEOVER

GAMEOVER

GAMEOVER

GAMEOVER

GAMEOVER

GAMEOVER

Page 10: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

Depth Limited MINIMAX

● Most games are so complex that searching all of the possible moves in the game until you reach “GAME OVER” on every branch takes too long.– So, we set a limit on how far down to look.– Now, we’ll need a function that can look at a game

state that’s not yet finished. It needs to guess how good it thinks that state of the game is.

– As we discussed in class, an “Evaluation Function” looks at non-terminal state, and returns a value of how promising that state looks.

Page 11: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

So, we add a depth limit. Each time we make a call to the next level down, we’ll decrement this depth limit.

def minimax_decision(state, game, d): # We’re in some state during a game. player = game.to_move(state) # Who’s turn is it? action, state = \ # Return the successor of current

argmax(game.successors(state), # board that returns the highest lambda ((a, s)): call_min(s, d-1)) # value when we call the return action # call_min function on it.

Depth Limited MINIMAX

Page 12: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

def call_max(state, d): if d == 0: # Use eval. func. if hit limit. return game.evaluation_function(state, player) if game.terminal_test(state): # Check if game over. return game.utility(state, player) v = -infinity for (a, s) in game.successors(state): # Return the maximum v = max(v, call_min(s, d-1)) # of the call_mins. return v

def call_min(state, d): if d == 0: # Use eval. func. if hit limit. return game.evaluation_function(state, player) if game.terminal_test(state): # Check if game over. return game.utility(state, player) v = infinity for (a, s) in game.successors(state): # Return the minimum v = min(v, call_max(s, d-1)) # of the call_maxs. return v

Page 13: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

= call_max

= call_min

= take maximum= take minimum

Initial call to minimax_decision

GAMEOVER

GAMEOVER

GAMEOVER

GAMEOVER

GAMEOVER

GAMEOVER

GAMEOVER

GAMEOVER

GAMEOVER

Page 14: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

= call_max

= call_min

= take maximum= take minimum

Initial call to minimax_decision

GAMEOVER

GAMEOVER

GAMEOVER

GAMEOVER

GAMEOVER

GAMEOVER

GAMEOVER EVALUATION

FUNCTION!!!

Page 15: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

Alpha Beta Search

Doing a little pruning…

Page 16: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

Alpha Beta

● Think about this momentof the search process…

23

24

16 23 5Imagine we already looked at all the stuff down this part of tree…

Do we even need tobother looking at thesetwo last red ones here?

Could their blue parentever get picked up top?

Page 17: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

Alpha Beta

● The blue node will return at least a 24.So, it’s branch willnever get picked bythe top-level node.

23

24

16 23 5Imagine we already looked at all the stuff down this part of tree…

Do we even need tobother looking at thesetwo last red ones here?

Could their blue parentever get picked up top?

24

Page 18: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

Alpha Beta

● So, don’t bother lookingat the blue node’s otherchildren. They just don’tmatter. 23

24

16 23 5Imagine we already looked at all the stuff down this part of tree…

24

Page 19: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

Alpha Beta

● Continue with the rest of the search.

23

24

16 23 5Imagine we already looked at all the stuff down this part of tree…

24

Page 20: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

Alpha Beta Search

● A version of MINIMAX that incorporates this handy trick. – It lets us “prune” branches from our tree. – This saves us a lot of searching through useless parts

of the tree.

● How do we modify the MINIMAX code to do this? (For now, let’s forget about the depth limit stuff… easy to add later.)

Page 21: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

First Step: Thread alpha and beta values through all the calls…

Alpha: highest value seen in any call_max on the path from root to the current node.

Beta: lowest value seen in any call_min on the path from root to current node.

Implementing Alpha Beta

Page 22: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

def call_max(state, alpha, beta): if game.terminal_test(state): return game.utility(state, player) v = -infinity for (a, s) in game.successors(state): v = max(v, call_min(s, alpha, beta)) if v >= beta: return v # stop checking kids if any beta alpha = max(alpha, v) # alpha= highest value seen in a max return v # no pruning trick if we got to here

def call_min(state, alpha, beta): if game.terminal_test(state): return game.utility(state, player) v = infinity for (a, s) in game.successors(state): v = min(v, call_max(s, alpha, beta)) if v <= alpha: return v # stop checking kids if any alpha beta = min(beta, v) # beta= lowest value seen in a min return v # no pruning trick if we got to here

Page 23: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

At the root…def alpha_beta(state, game): player = game.to_move(state) action, state = \

argmax(game.successors(state), lambda ((a, s)): call_min(s, -infinity, infinity)) return action

Alpha: Starts at –infinity so we know it will get set to new value first time it’s “max”ed inside a call_max function.

Beta: Starts at infinity. Same reason for min.

Page 24: AIMA Code: Adversarial Search CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth February 2005

Wrap Up

● Game Class– Utility(), Evaluation_Function(), Is_Move()

● MINIMAX– Search the whole game tree to the end each time.

● Depth-Limit MINIMAX– Stop before hitting “GAME OVER” in all branches.

● Alpha-Beta– Cool pruning trick to eliminate useless parts of tree.