Professional Documents
Culture Documents
Lecture 4 - Adversarial Search
Lecture 4 - Adversarial Search
Outline:
1
• Search problem:
◦ State: configurations of the world
◦ Start state and goal test
◦ Successor function: next actions and costs
◦ A solution: a sequence of actions
◦ Actions have associated costs
• Uninformed search approaches will examine the entire state-space graph in order to
find a path to the goal.
• Informed search approaches will use heuristics to reduce the search space.
• In this lecture we study, yet, another type of search, namely, adversarial search.
2
Adversarial Search
Search techniques where two or more players with different goals are trying to explore
the same search space for the solution are called Adversarial search, or in common
terms Games.
Adversarial Search is the type of search where we examine the problem that comes up
when we try to plan ahead of time and simultaneously, we have opponent who is
planning against our moves.
Often, games occur in a multiagent environment.
There is an opponent trying to foil your plan, i.e., trying to defeat you.
Unlike search, the optimal solution is not a sequence of actions The solution is a
strategy.
• If opponent does a, agent does b, else if opponent does y, agent does x.
• Very hard to hard-code and fragile. What if the opponent action is not coded?
Luckily, we can model games as search problems and use heuristics functions.
Multi-agent Environments
3
Zero-Sum Games
Zero-Sum Games
• Zero-sum games are adversarial search which involves pure competition.
• In Zero-sum game each agent's gain or loss of utility is exactly balanced by the losses
or gains of utility of another agent.
• One player of the game tries to maximize one single value, while another player tries to
minimize it. Each of the players is trying to find out the response of his opponent to
their actions. This requires embedded thinking or backward reasoning to solve the
game problems in AI.
• Each move by one player in the game is called as ply.
• Chess and tic-tac-toe are examples of a Zero-sum game.
• Our focus will be on deterministic, fully observable, zero-sum games, where two
agents act alternately.
4
Game Tree
CCIS@UTAS
CCIS@UTAS CSDS3203
CSDS3203 Introduction
Introduction to
to Artificial
Artificial Intelligence
Intelligence
5
Game Tree: Single Player
Formalization
A game can be formally defined as kind of search problem with the following elements:
S 0 : The initial state, which specifies how the game is set up at the start.
Player(s): Defines which player has the move in the state s.
Actions(s, p): Returns a set of legal moves (actions) in state s given player p.
R esult(s, a): The transition model, which returns new state s ′ based on the action a
on state s.
Terminal(s): A terminal test, which returns true if the game is over and false
otherwise. States where the game has ended are called terminal states.
Utility(s, p): A utility function or objective function for a game that ends in terminal
state s for player p. In Chess the outcome is a win, loss, or draw with values +1, 0,
1/2. For tic-tac-toe we can use a utility of +1,−1, 0
6
Game Tree: Single Player
7
Game Tree: Single Player
In the case of one player, nothing will prevent Max from winning (i.e., choose the path that leads to
the desired utility), unless there is another player who will do everything to make Max lost, let’s call
Min.
CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence
8
Adversarial Search: Minimax
Two players: Min and Max Players alternate turns,
Max moves first.
Max maximizes the result (utility)
Min minimizes the result
Always Sum = 0
9
Minimax: Basic Idea
• Goal: find the optimal strategy for Max.
• Depth-first search of the game tree
• An optimal leaf node could appear at any depth of the tree
• Minimax principle: compute the utility of being in a state assuming both player play
optimally from there until the end of the game
• Propagate minimax values up the tree once terminal nodes are discovered.
10
Minimax Algorithm
# find the value of a state
def Value(s):
if Terminal(s):
return Utility(s) elif
Max(state):
return MaxValue(s) # see definition below
elif Min(state):
return MinValue(s)
return m
def MinValue(s):
...
.
CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence
Minimax: Example
11
Minimax: Example
12
Minimax: Example - Solution
13
Minimax: Example - Solution
14
α − β Pruning
α − β Pruning
15
α − β Pruning
α − β Pruning
16
α − β Pruning
17
Cutting Off the Search
• Purpose: To manage the impracticality of searching through the entire game tree by
stopping at a certain depth.
• Depth Limit: Search is limited to ensure computation finishes in reasonable time.
• Evaluation Function: Applies heuristic at cutoff depth to estimate state desirability.
• Trade-off: Balances between decision accuracy and computation time/resource
constraints.
After a certain
depth apply a
heuristic
function
18
Updated Minimax Algorithm
# start call
Value(s0, 0, −∞, + ∞ )
v = −∞ # current max
if v >= β:
return v # prune the tree
α = max(α, v) # update α
return v
19
Updated Minimax Algorithm (contd.)
v = +∞ # current max
if v <= α:
return v # prune the tree
β = min(β, v) # update β
return v
α= - ∞ MAX
Β=+∞
A
MIN
B C
D E F G MAX
0 1 Terminal
2 3 5 9 7 5
Node
20
α − β Pruning: Easy Example
Step 1: At the first step the, Max player will start first move from node A where α= -∞ and
β= +∞, these value of alpha and beta passed down to node B where again α= -∞ and β=
+∞, and Node B passes the same value to its child D.
α= - ∞ MAX
Β=∞
A
MIN
B C
D E F G MAX
0 1 Terminal
2 3 5 9 7 5
Node
CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence
MIN
B C
α=3
Β=∞
D E F G MAX
0 1 Terminal
2 3 5 9 7 5
Node
CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence
21
α − β Pruning: Easy Example
Step 3: Now algorithm will backtrack to node B, where the value of β will change as this
is the turn of Min, Now β= +∞, will compare with the available subsequent nodes value,
i.e. min (∞, 3) = 3, hence at node B now α= -∞, and β= 3.
α= - ∞ MAX
Β=∞
A
α=- ∞
Β=3
3 MIN
B C
α=3
Β=∞
D E F G MAX
3
0 1 Terminal
2 3 5 9 7 5
Node
CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence
MIN
B 3 C
α=5
α=3 Β=3
Β=∞
D E 5 F G MAX
3
0 1 Terminal
2 3 5 9 7 5
Node
CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence
22
α − β Pruning: Easy Example
Step 5: In next step, algorithm will backtrack the tree again, from node B to node A. At
node A, the value of alpha will be changed the maximum available value is 3 as
max (-∞, 3)= 3, and β= +∞, these two values now passes to right successor of A which is
Node C. At node C, α=3 and β= +∞, and the same values will be passed on to node F.
α=- ∞ MAX
3
Β=∞ A
α=- ∞
Β=3 3
α=3 MIN
B 3 C
Β=∞
α=5 α=3
α=3 Β=3 Β=∞
Β=∞
D 3 MAX
E F G
0 1 Terminal
2 3 5 9 7 5
Node
CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence
α=3 MIN
B 3 C
Β=∞
α=5 α=3
α=3 Β=3 Β=∞
Β=∞
D 3 1 MAX
E F G
0 1 Terminal
2 3 5 9 7 5
Node
CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence
23
α − β Pruning: Easy Example
Step 7: Node F returns the node value 1 to node C, at C α= 3 and β= +∞, here the value
of beta will be changed, it will compare with 1 so min (∞, 1) = 1. Now at C, α=3 and β= 1,
and again it satisfies the condition α>=β, so the next child of C which is G will be pruned,
and the algorithm will not compute the entire sub-tree G.
MAX
α=- ∞ 3
Β=∞ A
α=- ∞
Β=3 1
α=3 MIN
B 3 C
Β=1
α=5 α=3
α=3 Β=3 Β=∞
Β=∞
D 3 1 MAX
E F G
0 1 Terminal
2 3 5 9 7 5
Node
CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence
α=3 MIN
B 3 C
Β=1
α=5 α=3
α=3 Β=3 Β=∞
Β=∞
D 3 1 MAX
E F G
0 1 Terminal
2 3 5 9 7 5
Node
CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence
24
Exercise
•Apply Minimax Algorithm on the following tree considering first node as MAX.
•Apply α−β pruning on this tree.
7 9 8 9 5 6 4 7 0 6 7
Exercise
Apply α − β pruning to solve the given game tree below. Mark the pruned nodes and
write the final values (α and β) in all applicable nodes.
25
Recommended Reading
For more on the topics covered in this lecture please refer to the following sources:
• Russell-Norvig Book (Russell & Norvig, 2020): Sections 6.1 – 6.3.
• Elements of AI: Chapter 2, Section III. Search and Games
(https://course.elementsofai.com/2/3)
References
Russell, S., & Norvig, P. (2020). Artificial Intelligence: A Modern Approach (4th Edition).
Pearson. http://aima.cs.berkeley.edu/
26