5 Game

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 23

Games Playing

as Search Problem
Tic-Tac-Toe Problem
• Given n x n matrix, the object of the game is to
make three of your symbol in a row, column or
diagonal
– This is a win state for the game.
• One player is designated as player X and makes
the first play by marking an X into any of the n x n
open squares of the board.
– Think a 3 x 3 square board where the player can put an
X in any of the 9 open places
• The second player, "O", then follows suit by
marking an O into any of the other open squares
that remain.
• This continues back-and-forth until one player wins
the game or players fill all squares on the board
without establishing a winner. This is a draw.
Partial Game Tree for Tic-Tac-Toe Problem
Applying MiniMax to tic-tac-toe
• The static evaluation function heuristic
How to play a game
•A way to play a game is to:
–Consider all the legal moves you can make
–Compute the new position resulting from each move
–Evaluate each resulting position and determine which is best
–Make that move
–Wait for your opponent to move and repeat
•Key problems are:
–Representing the “board”
–Generating all legal next boards
–Evaluating a position
•For real problems, the search tree is too big to make it
possible to reach the terminal states
–Example: Chess: 10120 nodes; 8 Puzzle: 105 . How many
nodes to consider in a Tic-Tac-Toe game with 3 x 3 board?
Adversarial Search
• It is used in game playing since one player's attempts
to maximize their fitness (win) is opposed by another
player.
• The search tree in adversarial games such as tic-tac-
toe consist of alternating levels where the moving
(MAX) player tries to maximize fitness and then the
opposing (MIN) player tries to minimize it.
• To find the best move
– The system first generates all possible legal moves, and
applies them to the current board.
– Evaluate each of the positions/states and determine the best
one to move.
– In a game like tic-tac-toe this process is repeated for each
possible move until the game is won, lost, or drawn.
Typical case
• 2-person game: two players alternate moves
e.g. chess playing
• Zero-sum game: one player’s loss is the other’s gain
– The zero-sum assumption allows us to use a single evaluation function to
describe the goodness of a board with respect to both players.
– f(n) > 0: position n good for A and bad for B
– f(n) < 0: position n bad for A and good for B
– f(n) near 0: position n is a neutral position
– f(n) = +infinity: win for A
– f(n) = -infinity: win for B
• Perfect information: both players have access to complete
information about the state of the game. No information is
hidden from either player.
– Board configuration is known completely to both players at all times.
• Any examples of perfect information game:
– Tic-Tac-Toe & Chess.
• Playing cards is not. Cards held by one are not known to others.
Game Tree Search
•Problem spaces for typical games are represented as
trees
•Game tree represents possible moves by both players
given an initial configuration.
– Each node represents a (board) configuration. Ex. Each
node marked by a letter (A, B etc.)
– Root node represents initial configuration
– Children of a node n indicate possible configurations after
the player makes a move from node n
• Ex. B, C, D are children of A. They are possible configurations after
player makes a move from configuration A.
– In case of alternating moves between the players,
alternating levels in a tree have alternating moves.
• Each level of the tree has nodes that are all MAX or all MIN; nodes at
level i are of the opposite kind from those at level i+1.
Example Game tree
•Game between players X and Y. Let us analyze from X's
perspective by looking ahead of two moves.
• H is a winning state for X (marked by plus infinity). If H reached, X wins.
No further moves.
• E, M are losing states for X (marked by minus infinity). If E or M reached,
X loses. No further moves.

Question: What should X move from A? Should X move to B, C, or D?


How to go searching?
•For many games, we can look the progress of the
game based on the player’s position on the board.
–We can use heuristics to evaluate these board positions and
judge how good (a chance of winning) any one of the next
moves can be.
–Evaluation is done on leaf nodes
• +inf stands for sure win,
• -inf for sure loss.
• Other numbers stand for intermediate values

•To obtain values of non-leaf nodes, analyze alternate


levels in game tree as maximizing and minimizing
levels.
•Two techniques:
– Min-max algorithm
–Alpha and Beta pruning
Min-max Algorithm
• It is a method in decision theory for minimizing the
maximum possible loss.
– Alternatively, it can be thought of as maximizing the
minimum gain (max-min).
– It started from two player zero-sum game theory,
considering the case where players take alternate
moves.
• The Min-max algorithm helps find the best move,
by working backwards from the end of the game.
– At each step it assumes that player A is trying to
maximize the chances of A winning,
– On the next turn player B is trying to minimize the
chances of A winning (i.e., to maximize B's own chances
of winning).
Example
• Lets consider perfect two players game
•Two players MAX and MIN take turn (with MAX playing first)
• Score tells whether a terminal state is a win, loss, or draw (for MAX)
•Perfect knowledge of states, no uncertainty in successor
function

This is the optimal


play
Min-max procedure
• Create start node as a MAX node with current board
configuration
• Expand nodes down to some depth of look ahead in
the game
–The min-max algorithm proceeds with depth-first search
down to the terminal states at the bottom of the tree.
• 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.
Min-max algorithm
function min-max(node, depth)
if node is a root node or depth = MaxDepth then
return the heuristic value of node
if MIN turn then
minimum = +∞
for each child of node do
minimum = min(minimum, min-max(child, depth+1))
return minimum
else
maximum = -∞
for each child of node do
maximum = max(maximum, min-max(child, depth+1))
return maximum
end min-max
Use Min-max to find optimal play
Comments
• A complete depth-first search exploration of the
game tree is made by min-max algorithm.
– In a tree of maximum depth M with b legal moves at
any point results in complexity on the order of bM.
– This exponential complexity means that min-max by
itself is not practical for real games.

• Is there a way to improve the performance of


Min-Max algorithm without affecting the result?
Exercise: Identify the optimal play path?
MAX

MIN

MAX

MIN

MAX

MIN
Alpha-Beta Pruning
• We can improve on the performance of the min-
max 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.”

• Alpha Beta pruning reduces the complexity by


pruning parts of the tree that won't influence
decision making
– With this we don't have to search the complete tree.
Alpha-Beta Prunning

• 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
Alpha-Beta Procedure
• Two values are used by this procedure.
–Alpha - the highest-value found so far at any choice-point along
the path for Max
–Beta - the lowest-value found so far at any choice-point along the
path for Min
• Traverse the search tree in depth-first order and update
alpha and beta values for each search node as the
search progresses
–At each MAX node n, alpha(n) = maximum value found so far
–At each MIN node n, beta(n) = minimum value found so far
–The alpha values start at -infinity and only increase, while beta
values start at +infinity and only decrease.

• Prune branches that can not change the final decision


–Stop searching when
alpha(i) ≥ beta(n) for some MAX node ancestor i of n.
Example
Effectiveness of alpha-beta
• Alpha-beta is guaranteed to compute the same
value for the root node as computed by min-max,
with less or equal computation
• Worst case: no pruning, examining bM leaf nodes,
where each node has b children and a M-depth
search is performed
• Best case is when each player’s best move is the
first alternative generated
– Best case: examine only bM/2 leaf nodes.
– The result shows that you can search twice as
deep as min-max.
• In Deep Blue (chess program), they found
empirically that alpha-beta pruning meant that the
average branching factor at each node was about 6
instead of about 35!
Exercise: mark pruned paths with ‘X’
MAX

MIN

MAX

MIN

MAX

MIN

You might also like