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

Slides:



Advertisements
Similar presentations
Adversarial Search We have experience in search where we assume that we are the only intelligent being and we have explicit control over the “world”. Lets.
Advertisements

Adversarial Search Reference: “Artificial Intelligence: A Modern Approach, 3 rd ed” (Russell and Norvig)
Games & Adversarial Search
Artificial Intelligence Adversarial search Fall 2008 professor: Luigi Ceccaroni.
ICS-271:Notes 6: 1 Notes 6: Game-Playing ICS 271 Fall 2008.
Game Playing (Tic-Tac-Toe), ANDOR graph By Chinmaya, Hanoosh,Rajkumar.
CS 484 – Artificial Intelligence
Games: Expectimax Andrea Danyluk September 25, 2013.
Lecture 12 Last time: CSPs, backtracking, forward checking Today: Game Playing.
All rights reservedL. Manevitz Lecture 31 Artificial Intelligence A/O* and Minimax L. Manevitz.
This time: Outline Game playing The minimax algorithm
Lecture 02 – Part C Game Playing: Adversarial Search
Using Search in Problem Solving
Game Playing CSC361 AI CSC361: Game Playing.
Min-Max Trees Based on slides by: Rob Powers Ian Gent Yishay Mansour.
1 search CS 331/531 Dr M M Awais A* Examples:. 2 search CS 331/531 Dr M M Awais 8-Puzzle f(N) = g(N) + h(N)
ICS-271:Notes 6: 1 Notes 6: Game-Playing ICS 271 Fall 2006.
Adversarial Search and Game Playing Examples. Game Tree MAX’s play  MIN’s play  Terminal state (win for MAX)  Here, symmetries have been used to reduce.
Game Playing: Adversarial Search Chapter 6. Why study games Fun Clear criteria for success Interesting, hard problems which require minimal “initial structure”
1 Adversary Search Ref: Chapter 5. 2 Games & A.I. Easy to measure success Easy to represent states Small number of operators Comparison against humans.
Game Trees: MiniMax strategy, Tree Evaluation, Pruning, Utility evaluation Adapted from slides of Yoonsuck Choe.
Minimax Trees: Utility Evaluation, Tree Evaluation, Pruning CPSC 315 – Programming Studio Spring 2008 Project 2, Lecture 2 Adapted from slides of Yoonsuck.
Game Playing Chapter 5. Game playing §Search applied to a problem against an adversary l some actions are not under the control of the problem-solver.
Lecture 6: Game Playing Heshaam Faili University of Tehran Two-player games Minmax search algorithm Alpha-Beta pruning Games with chance.
Game Playing.
Game Playing Chapter 5. Game playing §Search applied to a problem against an adversary l some actions are not under the control of the problem-solver.
1 Computer Group Engineering Department University of Science and Culture S. H. Davarpanah
October 3, 2012Introduction to Artificial Intelligence Lecture 9: Two-Player Games 1 Iterative Deepening A* Algorithm A* has memory demands that increase.
Instructor: Vincent Conitzer
Game Playing. Towards Intelligence? Many researchers attacked “intelligent behavior” by looking to strategy games involving deep thought. Many researchers.
Minimax with Alpha Beta Pruning The minimax algorithm is a way of finding an optimal move in a two player game. Alpha-beta pruning is a way of finding.
Games. Adversaries Consider the process of reasoning when an adversary is trying to defeat our efforts In game playing situations one searches down the.
1 Adversarial Search CS 171/271 (Chapter 6) Some text and images in these slides were drawn from Russel & Norvig’s published material.
Game Playing. Introduction One of the earliest areas in artificial intelligence is game playing. Two-person zero-sum game. Games for which the state space.
Quiz 4 : Minimax Minimax is a paranoid algorithm. True
CSCI 4310 Lecture 6: Adversarial Tree Search. Book Winston Chapter 6.
Adversarial Search Chapter Games vs. search problems "Unpredictable" opponent  specifying a move for every possible opponent reply Time limits.
CSE373: Data Structures & Algorithms Lecture 23: Intro to Artificial Intelligence and Game Theory Based on slides adapted Luke Zettlemoyer, Dan Klein,
ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.
Game-playing AIs Part 2 CIS 391 Fall CSE Intro to AI 2 Games: Outline of Unit Part II  The Minimax Rule  Alpha-Beta Pruning  Game-playing.
CMSC 421: Intro to Artificial Intelligence October 6, 2003 Lecture 7: Games Professor: Bonnie J. Dorr TA: Nate Waisbrot.
Game Playing: Adversarial Search chapter 5. Game Playing: Adversarial Search  Introduction  So far, in problem solving, single agent search  The machine.
Adversarial Search 2 (Game Playing)
Adversarial Search and Game Playing Russell and Norvig: Chapter 6 Slides adapted from: robotics.stanford.edu/~latombe/cs121/2004/home.htm Prof: Dekang.
Explorations in Artificial Intelligence Prof. Carla P. Gomes Module 5 Adversarial Search (Thanks Meinolf Sellman!)
Search: Games & Adversarial Search Artificial Intelligence CMSC January 28, 2003.
Announcements Homework 1 Full assignment posted..
Instructor: Vincent Conitzer
By: Casey Savage, Hayley Stueber, and James Olson
Iterative Deepening A*
PENGANTAR INTELIJENSIA BUATAN (64A614)
Adversarial Search and Game Playing (Where making good decisions requires respecting your opponent) R&N: Chap. 6.
Pengantar Kecerdasan Buatan
Adversarial Search.
Artificial Intelligence
Chapter 6 : Game Search 게임 탐색 (Adversarial Search)
Alpha-Beta Search.
Artificial Intelligence
Alpha-Beta Search.
Instructor: Vincent Conitzer
Introduction to Artificial Intelligence Lecture 9: Two-Player Games I
Alpha-Beta Search.
Alpha-Beta Search.
Instructor: Vincent Conitzer
Based on slides by: Rob Powers Ian Gent
Adversarial Search CS 171/271 (Chapter 6)
Alpha-Beta Search.
Minimax strategies, alpha beta pruning
Unit II Game Playing.
Minimax Trees: Utility Evaluation, Tree Evaluation, Pruning
Presentation transcript:

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

Game Class class 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."

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.

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

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)…

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…

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

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

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 GAME OVER

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.

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

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

= call_max = call_min = take maximum = take minimum Initial call to minimax_decision GAME OVER

= call_max = call_min = take maximum = take minimum Initial call to minimax_decision GAME OVER EVALUATION FUNCTION!!!

Alpha Beta Search Doing a little pruning…

Alpha Beta ● Think about this moment of the search process… Imagine we already looked at all the stuff down this part of tree… Do we even need to bother looking at these two last red ones here? Could their blue parent ever get picked up top?

Alpha Beta ● The blue node will return at least a 24. So, it’s branch will never get picked by the top-level node Imagine we already looked at all the stuff down this part of tree… Do we even need to bother looking at these two last red ones here? Could their blue parent ever get picked up top?  24

Alpha Beta ● So, don’t bother looking at the blue node’s other children. They just don’t matter Imagine we already looked at all the stuff down this part of tree…  24

Alpha Beta ● Continue with the rest of the search Imagine we already looked at all the stuff down this part of tree…  24

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.)

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

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

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.

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.