Adversarial Search And
Games
Chapter 06
(adapted from https://inst.eecs.berkeley.edu/~cs188/fa18)
Objectives
After the lesson, student will understand the followings:
• A game can be defined by the initial state (how the board is set up),
the legal actions in each state, the result of each action, a terminal
test (which says when the game is over), and a utility function that
applies to terminal states to say who won and what the final score is.
• In two-player, discrete, deterministic, turn-taking zero-sum games
with perfect information, the minimax algorithm can select optimal
moves by a depth-first enumeration of the game tree.
2
Objectives
• The alpha–beta search algorithm computes the same optimal move
as minimax, but achieves much greater efficiency by eliminating
subtrees that are provably irrelevant.
• Usually, it is not feasible to consider the whole game tree (even with
alpha–beta), so we need to cut the search off at some point and apply
a heuristic evaluation function that estimates the utility of a state.
3
Contents
1. History / Overview
2. Zero-Sum Games
3. Minimax for Zero-Sum Games
4. Minimax Properties & Efficiency
5. α-β Pruning
6. Finite lookahead and evaluation
1. History / Overview
Game Playing
State-of-the-Art
• Checkers: 1950: First computer player. 1994:
First computer champion: Chinook ended 40-
year-reign of human champion Marion Tinsley
using complete 8-piece endgame. 2007:
Checkers solved!
• Chess: 1997: Deep Blue defeats human
champion Gary Kasparov in a six-game match.
Deep Blue examined 200M positions per
second, used very sophisticated evaluation and
undisclosed methods for extending some lines
of search up to 40 ply. Current programs are
even better, if less historic.
• Go: Human champions are now starting to be
challenged by machines. In go, branching >
300! Classic programs use pattern knowledge
bases, but big recent advances use Monte Carlo
(randomized) expansion methods.
Game Playing
State-of-the-Art
• Checkers: 1950: First computer player. 1994:
First computer champion: Chinook ended 40-
year-reign of human champion Marion Tinsley
using complete 8-piece endgame. 2007:
Checkers solved!
• Chess: 1997: Deep Blue defeats human
champion Gary Kasparov in a six-game match.
Deep Blue examined 200M positions per
second, used very sophisticated evaluation and
undisclosed methods for extending some lines
of search up to 40 ply. Current programs are
even better, if less historic.
• Go: 2016: Alpha GO defeats human
champion. Uses Monte Carlo Tree Search,
learned evaluation function.
• Pacman
Video of Demo Mystery Pacman
• Watch video “MysteryPacman.mp4”
8
2. Zero-Sum Games
Types of Games
• Many different kinds of games!
• Axes:
o Deterministic or stochastic?
o Two, three, or more players?
o Zero sum?
o Perfect information (fully observable)?
• Want algorithms for calculating a strategy (policy) which
recommends a move from every possible state
10
Deterministic Games
• Many possible formalizations, one is:
o States: S (start at s0)
o Players: P={1…N} (usually take turns)
o Actions: A (may depend on player/state)
o Transition function: S x A → S
o Terminal test: S → {true, false}
o Terminal utilities: S x P → R
• Solution for a player is a policy: S → A
11
Zero-Sum Games
• Zero-Sum Games • General Games
• Agents have opposite utilities (values on • Agents have independent utilities (values
outcomes) on outcomes)
• Lets us think of a single value that one • Cooperation, indifference, competition,
maximizes and the other minimizes and more are all possible
• Adversarial, pure competition • More later on non-zero-sum games
3. Minimax for Zero-Sum
Games
Adversarial Search
14
Single-Agent Trees
2 0 … 2 6 … 4 6
Value of a state:
The best achievable
Value of a State
outcome (utility) Non-Terminal States:
from that state
8
6 8
2 0 … 2 6 … 4 6
Terminal States:
Adversarial Game Trees
-20 -8 … -18 -5 … -10 +4 -20 +8
Minimax Values
States Under Agent’s Control:
States Under Opponent’s Control:
-8
-8 -10
-8 -5 -10 +8
Terminal States:
Tic-Tac-Toe Game Tree
-1, 0 , +1
Adversarial Search (Minimax)
Minimax values:
• Deterministic, zero-sum games: computed recursively
o Tic-tac-toe, chess, checkers
o One player maximizes result 5 max
o The other minimizes result
2 5 min
• Minimax search:
o A state-space search tree
o Players alternate turns
o Compute each node’s minimax value: 8 2 5 6
the best achievable utility against a
rational (optimal) adversary Terminal values:
part of the game
Minimax Implementation
def max-value(state): def min-value(state):
initialize v = -∞ initialize v = +∞
for each successor of state: for each successor of state:
v = max(v, min-value(successor)) v = min(v, max-value(successor))
return v return v
21
Minimax Implementation (Dispatch)
def value(state):
if the state is a terminal state: return the state’s utility
if the next agent is MAX: return max-value(state)
if the next agent is MIN: return min-value(state)
def min-value(state):
def max-value(state): initialize v = +∞
initialize v = -∞ for each successor of state:
for each successor of state: v = min(v, value(successor))
v = max(v, value(successor)) return v
return v
22
Minimax Example
V=3 3
V=3 3 V=2 2 V=2
14
5 2
3 12 8 2 4 6 14 5 2
4. Minimax Properties &
Efficiency
Minimax Properties
max
min
10 10 9 100
Optimal against a perfect player.
Otherwise?
[Demo: min vs exp (L6D2, L6D3)]
Video of Demo Min vs. Exp (Min)
• Watch video “Min vs Exp (min).mp4”
26
Video of Demo Min vs. Exp (Exp)
• Watch video “Min vs Exp (exp).mp4”
27
Minimax Efficiency
• How efficient is minimax?
o Just like (exhaustive) DFS
o Time: O(bm)
o Space: O(bm)
• Example: For chess, b 35, m 100
o Exact solution is completely infeasible
o But, do we need to explore the whole tree?
28
5. Alpha-Beta Pruning
Game Tree Pruning
30
Minimax Example
V=3 3
V=3 3 V=2 2 V=2
14
5 2
3 12 8 2 4 6 14 5 2
Minimax Pruning
⩾3 3
V=3 3 ⩽2 2 ⩽ 14 2 ⩽5
X X
3 12 8 2 14 5 2
Alpha-Beta Pruning
• General configuration (MIN version) MAX
o We’re computing the MIN-VALUE at some node n
a
X
o We’re looping over n’s children MIN
o n’s estimate of the childrens’ min is dropping
o Who cares about n’s value? MAX
o Let a be the best value that MAX can get at any choice MAX
point along the current path from the root
MIN n
o If n becomes worse than a, MAX will avoid it, so we can V(n) ⩽ x
stop considering n’s other children (it’s already bad x<a
enough that it won’t be played)
• MAX version is symmetric
33
Alpha-Beta Implementation
α: MAX’s best option on path to root
β: MIN’s best option on path to root
def max-value(state, α, β): def min-value(state , α, β):
initialize v = -∞ initialize v = +∞
for each successor of state: for each successor of state:
v = max(v, value(successor, α, β)) v = min(v, value(successor, α, β))
if v ≥ β return v if v ≤ α return v
α = max(α, v) β = min(β, v)
return v return v
34
Alpha-Beta Pruning Properties
• This pruning has no effect on minimax value computed for the root!
• Values of intermediate nodes might be wrong 10 10 max
o Important: children of the root may have the wrong value
o So the most naïve version won’t let you do action selection
10 ⩽ 1010 min
• Good child ordering improves effectiveness of pruning
• With “perfect ordering”: X
o Time complexity drops to O(bm/2)
o Doubles solvable depth! 10 10 0
o Full search of, e.g. chess, is still hopeless…
• This is a simple example of metareasoning (computing about what to compute)
35
Alpha-Beta Quiz 1
36
Alpha-Beta Quiz 2
⩾ 10
10
10
⩽ 10 ⩽2
X
10 ⩾ 100 2
37
6. Finite lookahead and evaluation
Resource Limits 4 max
• Problem: In realistic games, cannot search to leaves! -2 4 min
• Solution: Depth-limited search
-1 -2 4 9
o Instead, search only to a limited depth in the tree
o Replace terminal utilities with an evaluation function for
non-terminal positions
• Example:
o Suppose we have 100 seconds, can explore 10K nodes / sec
o So can check 1M nodes per move
o - reaches about depth 8 – decent chess program
• Guarantee of optimal play is gone
• More plies makes a BIG difference ? ? ? ?
• Use iterative deepening for an anytime algorithm
39
Video of Demo Thrashing (d=2)
• Watch video “Thrashing.mp4”
40
Why Pacman Starves
8 8
8 -2 8
• A danger of replanning agents!
o He knows his score will go up by eating the dot now (west, east)
o He knows his score will go up just as much by eating the dot
later (east, west)
o There are no point-scoring opportunities after eating the dot
(within the horizon, two here)
o Therefore, waiting seems just as good as eating: he may go east,
then back west in the next round of replanning!
Video of Demo Thrashing -- Fixed (d=2)
• Watch video “Thrashing-FixedEvaluationFunction.mp4”
42
Evaluation Functions
43
Evaluation Functions
• Evaluation functions score non-terminals in depth-limited search
• Ideal function: returns the actual minimax value of the position
• In practice: typically weighted linear sum of features:
• e.g. f1(s) = (num white queens – num black queens), etc.
Evaluation for Pacman
45
Video of Demo Smart Ghosts
(Coordination)
• Watch video “SmartGhostsCoordinating.mp4”
46
Video of Demo Smart Ghosts
(Coordination) – Zoomed In
• Watch video “SmartGhostsCoordinating-ZoomedIn.mp4”
47
Depth Matters
• Evaluation functions are always imperfect
• The deeper in the tree the evaluation
function is buried, the less the quality of the
evaluation function matters
• An important example of the tradeoff
between complexity of features and
complexity of computation
48
Video of Demo Limited Depth (2)
• Watch video “DepthLimited-depth2.mp4”
49
Video of Demo Limited Depth (10)
• Watch video “DepthLimited-depth10.mp4”
50
Synergies between Evaluation Function and Alpha-Beta?
• Alpha-Beta: amount of pruning depends on expansion ordering
o Evaluation function can provide guidance to expand most promising nodes first (which
later makes it more likely there is already a good alternative on the path to the root)
- (somewhat similar to role of A* heuristic)
• Alpha-Beta: (similar for roles of min-max swapped)
o Value at a min-node will only keep going down
o Once value of min-node lower than better option for max along path to root, can prune
o Hence: IF evaluation function provides upper-bound on value at min-node, and upper-
bound already lower than better option for max along path to root
THEN can prune
51