You are on page 1of 26

Introduction to Artificial Intelligence

Adversarial Search (Games)

University of Technology and Applied Sciences


Computing and Information Sciences

Outline:

1. Adversarial Search and Games


2. Zero-Sum Games
3. Game Trees
4. Adversarial Search: Minimax
5. Optimizing Minimax: Alpha Beta Pruning

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

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.

CCIS@UTAS CSDS3203 Intro. to AI

Adversarial Search and Games

CCIS@UTAS CSDS3203 Intro. to AI

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.

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

Multi-agent Environments

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

3
Zero-Sum Games

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

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.

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

4
Game Tree

CCIS@UTAS
CCIS@UTAS CSDS3203
CSDS3203 Introduction
Introduction to
to Artificial
Artificial Intelligence
Intelligence

Single Player Game


Single Player Game

• Assume we have a tic-tac-toe with one


player.
• Let’s call him Max and have him play. The
game is won if three of Max’s marks are
placed in horizontal, vertical, or diagonal
row.

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

5
Game Tree: Single Player

• Changes in the states as Max makes moves can


be depicted in a tree.

• Finding the shortest path to that allows Max to win


entails that we can use search algorithms.

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

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

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

6
Game Tree: Single Player

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

Game Tree: Single Player

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

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

Adversarial Search: Minimax

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

Compute each node’s minimax value


• Minimax value is the best achievable utility against an optimal adversary.
• The lowest of a set of maximum values.
• Minimax is a decision rule used in artificial intelligence, decision theory, game theory,
statistics, and philosophy for minimizing the possible loss for a worst-case scenario.

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

Adversarial Search: Minimax

Always Sum = 0

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

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.

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

Minimax: Algorithm Outline


• If the state is terminal node: Value is Utility(state)
• If state is Maxnode: Value is the highest of all successor nodes values (children)
• If state is Minnode: Value is the lowest of all successor nodes values (children)

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

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)

# find highest of all successor nodes values def


MaxValue(s):
m = − ∞ # current max

for a in Actions(s, Player(s)): # list legal moves in this state


v = Value(Result(s, a)) # find the value of the next state
m = max(v, m) # update max value

return m

def MinValue(s):
...

.
CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

Minimax: Example

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

11
Minimax: Example

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

Minimax: Example - Solution

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

12
Minimax: Example - Solution

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

Minimax: Example - Solution

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

13
Minimax: Example - Solution

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

Optimizing Minimax: Alpha Beta Pruning

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

14
α − β Pruning

Simple trick that significantly increases the efficiency of minimax search.


Basic idea: if you know half-way through a calculation that it will succeed or fail, then
there is no point of doing the rest of it.
In programming, given A > 5 or B < 0, if the first condition (A > 5) is true, then there is
no point of evaluating B < 0.

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

α − β Pruning

Minimax (root) = max(min(3, 12, 8), min(2, X , Y ), min(14, 5, 2))

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

15
α − β Pruning

Minimax (root) = max(min(3, 12, 8), min(2, X , Y ), min(14, 5, 2))


= max (3, mi n (2, X , Y )), 2)

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

α − β Pruning

Minimax (root) = max(min(3, 12, 8), min(2, X , Y ), min(14, 5, 2))


= max(3, mi n (2, X , Y )), 2)
= max (3, Z , 2) where Z = mi n (2, X , Y ) ≤ 2

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

16
α − β Pruning

Minimax (root) = max(min(3, 12, 8), min(2, X , Y ), min(14, 5, 2))


= max(3, mi n (2, X , Y )), 2)
= max(3, Z , 2) where Z = mi n (2, X , Y ) ≤ 2
= 3

Minimax decisions are independent of the values of X and Y .

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

α − β Pruning: Algorithm Outline


• Strategy: Just like minimax, it performs a DFS
• Parameters: Keep track of two bounds
◦ α: largest value for Max across seen children (current lower bound on MAX’s
outcome).
◦ β: lowest value for Min across seen children (current upper bound on MIN’s
outcome).
• Initialization: α = −∞, β = +∞
• Propagation: Send α, β values down during the search to be used for pruning.
◦ Update α, β values by propagating upwards values of terminal nodes.
◦ Update α only at Max nodes and update β only at Min nodes.

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

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.

CCIS@UTAS CSDS3203 Intro. to AI

Cutting Off Search: Illustration

After a certain
depth apply a
heuristic
function

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

18
Updated Minimax Algorithm

# start call
Value(s0, 0, −∞, + ∞ )

# find the value of a state

def Value(s, depth, α, β):


if CutOff(depth): return
Eval(s)
elif if Terminal(s): return
Utility(s)
elif Max(state):
return MaxValue(s) # see definition below
elif Min(state):
return MinValue(s)

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

Updated Minimax Algorithm (contd.)

# find highest of all successor nodes values


def MaxValue(s, depth, α, β):

v = −∞ # current max

for a in Actions(s, Player(s)):


v = max(v, Value(Result(s, a), depth+1, α, β))

if v >= β:
return v # prune the tree

α = max(α, v) # update α

return v

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence


CSDS3203

19
Updated Minimax Algorithm (contd.)

# find lowest of all successor nodes values


def MinValue(s, depth, α, β):

v = +∞ # current max

for a in Actions(s, Player(s)):


v = min(v, Value(Result(s, a), depth+1, α, β))

if v <= α:
return v # prune the tree

β = min(β, v) # update β

return v

CCIS@UTAS CSDS3203 Introduction to Artificial Intelligence

α − β Pruning: Easy Example


Run α − β pruning on the following game tree. Show which leaves that are not
evaluated, and which branches are pruned.

α= - ∞ 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

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

α − β Pruning: Easy Example


Step 2: At Node D, the value of α will be calculated as its turn for Max. The value of α is
compared first with 2 and then with 3, and the max (2, 3) = 3 will be the value of α at node
D and node value will also 3.
α=- ∞ MAX
Β=∞
A

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

α − β Pruning: Easy Example


Step 4: At node E, Max will take its turn, and the value of alpha will change. The current
value of alpha will be compared with 5, so max (-∞, 5) = 5, hence at node E, α= 5 and
β= 3, where α>=β, so the right successor of E will be pruned, and algorithm will not
traverse it, and the value at node E will be 5.
α= - ∞ MAX
Β=∞
A
α=- ∞
Β=3

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

α − β Pruning: Easy Example


Step 6: At node F, again the value of α will be compared with left child which is 0, and
max(3,0)= 3, and then compared with right child which is 1, and max(3,1)= 3 still α
remains 3, but the node value of F will become 1.
α=- ∞ MAX
Β=∞ 3
A
α=- ∞
Β=3 3

α=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

α − β Pruning: Easy 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.
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

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

CCIS@UTAS CSDS3203 Intro. to AI

Exercise
Apply α − β pruning to solve the given game tree below. Mark the pruned nodes and
write the final values (α and β) in all applicable nodes.

CCIS@UTAS CSDS3203 Intro. to AI

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)

CCIS@UTAS CSDS3203 Intro. to AI

References

Russell, S., & Norvig, P. (2020). Artificial Intelligence: A Modern Approach (4th Edition).
Pearson. http://aima.cs.berkeley.edu/

CCIS@UTAS CSDS3203 Intro. to AI

26

You might also like