0% found this document useful (0 votes)
14 views

2.6-Adversarial Search Algorithms

The document discusses adversarial search algorithms and their application to game playing. It describes how games can be modeled as search problems with alternating moves between two agents, and introduces the minimax algorithm as a way for agents to determine optimal strategies in adversarial settings by recursively considering all possible moves.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

2.6-Adversarial Search Algorithms

The document discusses adversarial search algorithms and their application to game playing. It describes how games can be modeled as search problems with alternating moves between two agents, and introduces the minimax algorithm as a way for agents to determine optimal strategies in adversarial settings by recursively considering all possible moves.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

08-02-2024

Artificial Intelligence
(BITE308L)

Adversarial
Search Algorithms

Dr. S. Hemalatha
School of Computer science Engineering & Information Systems
VIT, Vellore

ADVERSARIAL
SEARCH ALGORITHMS
• Multi-agent environments:
• The unpredictability of these other agents can introduce contingencies into the
agent’s problem-solving process
• Competitive environments:
• the agents’ goals are in conflict
• Giving rise to ADVERSARIAL search problems
• often known as GAMES

• Mathematical game theory (a branch of economics):


• Any multiagent environment viewed as a game
• provided that the impact of each agent on the others is significant
• regardless of whether the agents are cooperative or competitive
Prepared by S.Hemalatha/SCORE

1
08-02-2024

ADVERSARIAL
SEARCH ALGORITHMS

• ZERO-SUM GAMES
• AI: Most common games are of a specialized kind;
• deterministic, turn-taking, two-player,
• zero-sum games of perfect information (such as chess)
• Means: deterministic & fully observable environments:
• two agents act alternately
• the utility values at the end of the game are always equal & opposite
• Eg: if one player wins a game of chess, the other player necessarily loses
• This opposition between the agents’ utility functions that makes the situation
adversarial.

Prepared by S.Hemalatha/SCORE

ADVERSARIAL
SEARCH ALGORITHMS

• AI research:
• Abstract nature of games makes them an appealing subject for study
• state of a game is easy to represent
• agents are usually restricted to a small number of actions whose outcomes are
defined by precise rules
• Physical games, such as croquet and ice hockey, have much more complicated
descriptions, a much larger range of possible actions, and rather imprecise
rules defining the legality of actions
• Thus, physical games have NOT attracted much interest in the AI community
• Exception: robot soccer

Prepared by S.Hemalatha/SCORE

2
08-02-2024

GAMES
AI REPRESENTATION

• Games are interesting because they are too hard to solve


• Eg: Chess
• average branching factor – 35
• games often go to 50 moves by each player
• so the search tree has about 35100 /10154 nodes (although the search graph has
“only” about 1040 distinct nodes)
• Games require: decision making ability
• even when calculating the optimal decision is infeasible

Prepared by S.Hemalatha/SCORE

GAMES
AI REPRESENTATION

• Games also penalize inefficiency severely


(A∗ search)
• Half as efficient will simply take twice as long to run to completion
• a chess program that is half as efficient in using its available time probably will
be beaten into the ground
• other things being equal
• Game-playing research has therefore spawned a number of interesting ideas on
how to make the best possible use of time

Prepared by S.Hemalatha/SCORE

3
08-02-2024

GAMING
REQUIREMENTS

• Algorithm to find Optimal Move


• Techniques for choosing a good move when time is limited
• Pruning:
• Ignore portions of the search tree that make no difference to the final choice
• Heuristic evaluation functions
• Approximate the true utility of a state without doing a complete search

• Final Lookup:
• how state-of-the-art game-playing programs fare against human opposition
• & at directions for future developments.

Prepared by S.Hemalatha/SCORE

MAX
MIN

• Games with 2 players considered


• Players are called MAX & MIN
• MAX moves first
• Then, they take turns moving until the game is over
• At the end of the game:
• points awarded to the winning player
• penalties given to the loser
• A game can be formally defined as a kind of search problem with the following
elements:

Prepared by S.Hemalatha/SCORE

4
08-02-2024

GAME – SEARCH PROBLEM


& ITS ELEMENTS

Game tree for a game


defined by:
• 𝑆 ,
• 𝐴𝐶𝑇𝐼𝑂𝑁𝑆(𝑠) &
• 𝑅𝐸𝑆𝑈𝐿𝑇(𝑠, 𝑎)

Game tree: a tree


Nodes: game states
edges: moves

Prepared by S.Hemalatha/SCORE

GAME TREE
TIC-TAC-TOE

Prepared by S.Hemalatha/SCORE

5
08-02-2024

GAME TREE SIZE


TIC-TAC-TOE:
• Game tree is relatively small—fewer than 9! = 362, 880 terminal nodes
CHESS:
• there are over 1040 nodes
• So, game tree thought of as a theoretical construct
• we cannot realize in the physical world
• But regardless of the size of the game tree, it is MAX’s job to search for a
good move
SEARCH TREE
• search tree- term used for a tree
• that is superimposed on the full game tree
• examines enough nodes to allow a player to determine what move to make.
Prepared by S.Hemalatha/SCORE

OPTIMAL DECISION IN GAMES


• In Normal search, Optimal solution – a sequence of actions leading to a goal state—a
terminal state (WIN)
STRATEGY
• In adversarial search
• MIN has something to say about it
• MAX therefore must find a contingent strategy
• which specifies MAX’s move in the initial state
• then MAX’s moves in the states resulting from every possible response by MIN,
• then MAX’s moves in the states resulting from every possible response by MIN to
those moves, and so on
OPTIMAL STRATEGY
• Leading to outcomes at least as good as any other strategy when one is playing an infallible
(unfailing) opponent
Prepared by S.Hemalatha/SCORE

6
08-02-2024

THE MINIMAX ALGORITHM

• Mini-max algorithm: A recursive / backtracking algorithm used in


• Decision-Making & Game Theory
• Provides an optimal move for the player assuming that opponent is also
playing optimally
• Uses recursion to search through game-tree
• Mostly used for game playing in AI, such as
• Chess
• Checkers
• tic-tac-toe, go
• various tow-players game

Prepared by S.Hemalatha/SCORE

THE MINIMAX ALGORITHM


• Computes the minimax decision for the current state
In this algorithm:
• two players play the game, one is called MAX and other is called MIN
• Both the players fight it as the opponent player gets the minimum benefit while
they get the maximum benefit
• Both Players of the game are opponent of each other, where MAX will select
the maximized value and MIN will select the minimized value.
• Performs a depth-first search algorithm for the exploration of the complete
game tree
• Proceeds all the way down to the terminal node of the tree
• Then backtrack the tree as the recursion

Prepared by S.Hemalatha/SCORE

7
08-02-2024

THE MINIMAX ALGORITHM


PSEUDOCODE
function minimax(node, depth, maximizingPlayer)
if depth ==0 or node is a terminal node then
return static evaluation of node

if MaximizingPlayer then // for Maximizer Player


maxEva= -infinity
for each child of node do
eva= minimax(child, depth-1, false)
maxEva= max(maxEva,eva) //gives Maximum of the values
return maxEva

else // for Minimizer player


minEva= +infinity
for each child of node do
eva= minimax(child, depth-1, true)
minEva= min(minEva, eva) //gives minimum of the values
return minEva
Prepared by S.Hemalatha/SCORE

THE MINIMAX ALGORITHM

Prepared by S.Hemalatha/SCORE

8
08-02-2024

MINIMAX ALGORITHM
EXAMPLE

Step-1:
• entire game-tree generaated
• utility function applied the to get
utility values for terminal states

A initial state of the tree


• Suppose maximizer takes first turn
• worst-case initial value =−∞
• & minimizer will take next turn
• worst-case initial value= +∞.

Prepared by S.Hemalatha/SCORE

MINIMAX ALGORITHM
EXAMPLE

Step 2:
• Find the utility value for the Maximizer
• For node D max(-1,- -∞) => max(-1,4)= 4
• For Node E max(2, -∞) => max(2, 6)= 6
• For Node F max(-3, -∞) => max(-3,-5) = -3
• For node G max(0, -∞) = max(0, 7) = 7

Prepared by S.Hemalatha/SCORE

9
08-02-2024

MINIMAX ALGORITHM
EXAMPLE

Step 3
• It's a turn for minimizer,
• For node B= min(4,6) = 4
• For node C= min (-3, 7) = -3

Prepared by S.Hemalatha/SCORE

MINIMAX ALGORITHM
EXAMPLE

• Step 4:
• Now it's a turn for Maximizer,
• For node A max(4, -3)= 4

Prepared by S.Hemalatha/SCORE

10
08-02-2024

MINIMAX ALGORITHM
PROPERTIES
• Complete:
• Min-Max algorithm is Complete
• It will definitely find a solution (if exist), in the finite search tree
• Optimal:
• Min-Max algorithm is optimal if both opponents are playing optimally
• Time complexity:
• Time complexity: 𝑂 𝑏 (it performs DFS for the game-tree)
• 𝑏: branching factor of the game-tree
• 𝑚: maximum depth of the tree
• Space Complexity:
• Space complexity: 𝑂 𝑏𝑚 (DFS)
Prepared by S.Hemalatha/SCORE

MINIMAX ALGORITHM
LIMITATIONS

• The algorithm gets really slow for complex games such as Chess, go, etc
• This type of games has a huge branching factor,
• the player has lots of choices to decide
• This limitation of the minimax algorithm can be improved from alpha-beta
pruning

Prepared by S.Hemalatha/SCORE

11
08-02-2024

MINIMAX ALGORITHM
EXAMPLE SCENARIO
X O O
O
X X

X O O X O O X O O
X O O O X
X X X X X X X

X O O X O O X O O X O O
X O X O O O O X O X
O X X X X X X X O X X

Prepared by S.Hemalatha/SCORE

ALPHA BETA PRUNING


MINIMAX problem
• Number of game states it has to examine is exponential in the depth of the tree
• The exponent canNOT be eliminated
• But, it turns out it can be effectively in half
• The trick is that it is possible to compute the correct minimax decision without looking
at every node in the game tree
• Idea of pruning applied
• To eliminate large parts of the tree from consideration
ALPHA–BETA PRUNING
• When applied to a standard minimax tree, it returns the same move as minimax
would
• But prunes away branches that cannot possibly influence the final decision
Prepared by S.Hemalatha/SCORE

12
08-02-2024

ALPHA BETA PRUNING


ALPHA–BETA PRUNING
• Modified version of the minimax algorithm
• Optimization technique for the minimax algorithm
• involves 2-threshold parameter Alpha and beta for future expansion
• Called alpha-beta pruning
• Also called as Alpha-Beta Algorithm

• Can be applied at any depth of a tree,


• Sometimes it not only prune the tree leaves
• But also entire sub-tree

Prepared by S.Hemalatha/SCORE

ALPHA BETA PRUNING

The two-parameters:
• Alpha:
• The best (highest-value) choice we have found so far at any point along the path
of Maximizer
• Initial value of alpha is −∞
• Beta:
• The best (lowest-value) choice we have found so far at any point along the path
of Minimizer
• Initial value of beta is +∞

Prepared by S.Hemalatha/SCORE

13
08-02-2024

ALPHA BETA PRUNING


KEY POINTS

• The Max player will only update the value of alpha


• The Min player will only update the value of beta
• While backtracking the tree, the node values will be passed to upper nodes instead
of values of alpha and beta.
• We will only pass the alpha, beta values to the child nodes

Prepared by S.Hemalatha/SCORE

ALPHA BETA PRUNING-PSEUDOCODE


• function minimax(node, depth, alpha, beta, maximizingPlayer) is
• if depth ==0 or node is a terminal node then
• return static evaluation of node

• if MaximizingPlayer then // for Maximizer Player
• maxEva= -infinity
• for each child of node do
• eva= minimax(child, depth-1, alpha, beta, False)
• maxEva= max(maxEva, eva)
• alpha= max(alpha, maxEva)
• if beta<=alpha
• break
• return maxEva

Prepared by S.Hemalatha/SCORE

14
08-02-2024

ALPHA BETA PRUNING-PSEUDOCODE

• else // for Minimizer player


• minEva= +infinity
• for each child of node do
• eva= minimax(child, depth-1, alpha, beta, true)
• minEva= min(minEva, eva)
• beta= min(beta, eva)
• if beta<=alpha
• break
• return minEva

Prepared by S.Hemalatha/SCORE

ALPHA BETA PRUNING


EXAMPLE-1

Prepared by S.Hemalatha/SCORE

15
08-02-2024

ALPHA BETA PRUNING


EXAMPLE-1

Prepared by S.Hemalatha/SCORE

ALPHA BETA PRUNING


EXAMPLE

Step 1:
• Max player will start first move
from node A
• 𝛼 = −∞ and 𝛽 = +∞
• alpha & beta values passed down
to node B
• Again, 𝛼 = −∞ and 𝛽 = +∞
• Node B passes the same value to
its child D

Prepared by S.Hemalatha/SCORE

16
08-02-2024

ALPHA BETA PRUNING


EXAMPLE
Step 2:
• At Node D:
• Value of 𝛼 will be calculated as
its turn for Max
• Value of 𝛼 is compared with
firstly 2
• then 3
• max (2, 3) = 3 will be the value
of 𝛼 at node D
• and node value will also 3

Prepared by S.Hemalatha/SCORE

ALPHA BETA PRUNING


EXAMPLE
Step 3:
• Now backtrack to node B,
• Value of 𝛽 will change as this is
a turn of Min
• Now 𝛽 = +∞
• will compare with the available
subsequent nodes value
• min ∞, 3 = 3
• At node B now,
• 𝛼 = −∞, and 𝛽 = 3

Prepared by S.Hemalatha/SCORE

17
08-02-2024

ALPHA BETA PRUNING


EXAMPLE
Step 4:
• At node E
• Max will take its turn
• value of alpha will change
• current value of alpha will be
compared with 5
• max −∞, 5 = 5,
• At node E,
• 𝛼 = 5 & 𝛽 = 3 (𝛼 ≥ 𝛽)
• So, right successor of E will be
pruned
• Algorithm will not traverse it
• Value at node E: 5

Prepared by S.Hemalatha/SCORE

ALPHA BETA PRUNING


EXAMPLE
Step 5:
• Again backtrack the tree
• (node B to node A)
• At node A:
• 𝛼 = max −∞, 3 = 3
• 𝛽 = +∞
• 𝛼 & 𝛽 now pass to right
successor of A (node C)
• At node C, α=3 and β= +∞, and
the same values will be passed on
to node F.
Prepared by S.Hemalatha/SCORE

18
08-02-2024

ALPHA BETA PRUNING


EXAMPLE

Step 6:
• At node F:
• Right: 𝛼 = max 3,0 = 3
• Leftt: 𝛼 = max 3,1 = 3
• still α remains 3
• but the node value of F will
become 1

Prepared by S.Hemalatha/SCORE

ALPHA BETA PRUNING


EXAMPLE
Step 7:
• Node F returns the node value 1
to node C
• At Node C:
• 𝛼 = 3 & 𝛽 = +∞
• 𝛽 = min +∞, 1 = 1
• Now at C:
• 𝛼 = 3 & 𝛽 = 1 (𝛼 ≥ 𝛽)
• So, the next child of C which is
G will be pruned
• & the algorithm will not
compute the entire sub-tree G.

Prepared by S.Hemalatha/SCORE

19
08-02-2024

ALPHA BETA PRUNING


EXAMPLE
Step 8:
• C now returns the value of 1 to A
• here the best value for A is max
(3, 1) = 3
• Following is the final game tree
which is the showing the nodes
which are computed and nodes
which has never computed
• Hence the optimal value for the
maximizer is 3 for this example

Prepared by S.Hemalatha/SCORE

ALPHA BETA PRUNING


MOVE ORDERING
• The effectiveness of alpha-beta pruning is highly dependent on the order in which
each node is examined. Move order is an important aspect of alpha-beta pruning.
• It can be of two types:
• Worst ordering: In some cases, alpha-beta pruning algorithm does not prune any of
the leaves of the tree, and works exactly as minimax algorithm. In this case, it also
consumes more time because of alpha-beta factors, such a move of pruning is called
worst ordering. In this case, the best move occurs on the right side of the tree. The
time complexity for such an order is O(bm).
• Ideal ordering: The ideal ordering for alpha-beta pruning occurs when lots of
pruning happens in the tree, and best moves occur at the left side of the tree. We
apply DFS hence it first search left of the tree and go deep twice as minimax
algorithm in the same amount of time. Complexity in ideal ordering is O(bm/2).
Prepared by S.Hemalatha/SCORE

20
08-02-2024

ALPHA BETA PRUNING


RULES TO FIND GOOD ORDERING

• Following are some rules to find good ordering in alpha-beta pruning:


• Occur the best move from the shallowest node.
• Order the nodes in the tree such that the best nodes are checked first.
• Use domain knowledge while finding the best move. Ex: for Chess, try order:
captures first, then threats, then forward moves, backward moves.
• We can bookkeep the states, as there is a possibility that states may repeat.

Prepared by S.Hemalatha/SCORE

SUMMARY

In this session,
• Adversarial Search basics
• Mini-max algorithm
• Alpha-beta pruning

Next session,
• Knowledge representation

Prepared by S.Hemalatha/SCORE

21

You might also like