0% found this document useful (0 votes)
3 views79 pages

cs188-su24-lec06

Uploaded by

Parv Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views79 pages

cs188-su24-lec06

Uploaded by

Parv Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 79

CS 188: Artificial Intelligence

Search with Other Agents

Instructor: Evgeny Pobachienko


University of California, Berkeley

[These slides adapted from Dan Klein, Pieter Abbeel, Anca Dragan, Stuart Russell, and many others]
Behavior from Computation

[Demo: mystery pacman (L6D1)]


Types of Games
o Many different kinds of games!

o Axes:
o Deterministic or stochastic?
o One, two, or more players?
o Zero sum?
o Perfect information (can you see the state)?
Types of Games

o General Games o Zero-Sum Games


o Agents have independent utilities (values on o Agents have opposite utilities (values on
outcomes) outcomes)
o Cooperation, indifference, competition, and o Lets us think of a single value that one
more are all possible maximizes and the other minimizes
o We don’t make AI to act in isolation, it should a) work o Adversarial, pure competition
around people and b) help people
o That means that every AI agent needs to solve a game
Zero-Sum Games ☺
o Checkers
o (1950): First computer player.
o (1994): First computer champion: Chinook ended 40-
year-reign of human champion Marion Tinsley using
complete 8-piece endgame.
o (2007): Checkers solved!

o Chess
o (1997): Deep Blue defeats human champion Gary
Kasparov in a six-game match. Current programs are
even better, if less historic.

o Go
o (2016): AlphaGo defeats human champion Lee Sedol.
Uses Monte Carlo Tree Search, learned evaluation
function.
Deterministic Games with Terminal Utilities
o 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 → {t, f}
o Terminal Utilities: S x P → R

o Solution for a player is a policy: S → A


Adversarial Games
Adversarial Search
Single-Agent Trees

2 0 … 2 6 … 4 6
Value of a State
Value of a state: Non-Terminal States:
The best achievable
outcome (utility)
from that state

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 -5 -10 +8

Terminal States:
Tic-Tac-Toe Game Tree
Adversarial Search (Minimax)
o Deterministic, zero-sum games: Minimax values:
computed recursively
o Tic-tac-toe, chess, checkers
o One player maximizes result 5 max
o The other minimizes result

2 5 min
o Minimax search:
o A state-space search tree
o Players alternate turns
8 2 5 6
o Compute each node’s minimax
value: the best achievable utility
Terminal values:
against a rational (optimal) part of the game
adversary
Minimax Implementation (Dispatch)
def value(state):
if the state is terminal: 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 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))
return v return v
Minimax Example

3 2 2

3 12 8 2 4 6 14 5 2
Minimax Properties

max

min

10 10 9 100

Optimal against a perfect player. Otherwise?


Minimax Efficiency

o How efficient is minimax?


o Just like (exhaustive) DFS
o Time: O(bm)
o Space: O(bm)

o Example: For chess, b  35, m  100


o Exact solution is completely infeasible
o But, do we need to explore the whole
tree?
Game Tree Pruning
Minimax Example: Metareasoning

3 >=3

3 <=2 2

3 12 8 2 14 5 2
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
Why on Path?

MAX

MIN a

MAX

MIN n

24
Alpha-Beta Pruning Properties
o This pruning has no effect on minimax value computed for the root!

o Values of intermediate nodes might be wrong


o Important: children of the root may have the wrong value max
o So the most naïve version won’t let you do action selection
min
o Good child ordering improves effectiveness of pruning

o With “perfect ordering”:


o Time complexity drops to O(bm/2) 10 10 0
o Doubles solvable depth!
o Full search of, e.g. chess, is still hopeless…

o This is a simple example of metareasoning (computing about what to compute)


Alpha-Beta Quiz
Alpha-Beta Quiz 2

2
Alpha-Beta Quiz 2

2
Alpha-Beta Quiz 2

10
<=2

>=100 2
10
Resource Limits
Resource Limits
o Problem: In realistic games, cannot search to leaves! 4 max

o Solution: Depth-limited search -2 4 min


o Instead, search only to a limited depth in the tree
-1 -2 4 9
o Replace terminal utilities with an evaluation function for non-
terminal positions

o 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

o Guarantee of optimal play is gone


o More plies makes a BIG difference
? ? ? ?
o Use iterative deepening for an anytime algorithm
Depth Matters
o Evaluation functions are
always imperfect
o The deeper in the tree the
evaluation function is buried,
the less the quality of the
evaluation function matters
o An important example of the
tradeoff between complexity of
features and complexity of
computation

[Demo: depth limited (L6D4, L6D5)]


Video of Demo Limited Depth (2)
Video of Demo Limited Depth (10)
Evaluation Functions
Evaluation Functions
o Evaluation functions score non-terminals in depth-limited search

o Ideal function: returns the actual minimax value of the position


o In practice: typically weighted linear sum of features:

o e.g. f1(s) = (num white queens – num black queens), etc.


Evaluation for Pacman

[Demo: thrashing d=2, thrashing d=2 (fixed evaluation function), smart ghosts coordinate (L6D6,7,8,10)]
Video of Demo Thrashing (d=2)
Why Pacman Starves

o 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)
Video of Demo Smart Ghosts (Coordination)
Video of Demo Smart Ghosts (Coordination) –
Zoomed In
Other Game Types
Multi-Agent Utilities
o What if the game is not zero-sum, or has multiple players?

o Generalization of minimax:
o Terminals have utility tuples
o Node values are also utility tuples
o Each player maximizes its own component
o Can give rise to cooperation and
competition dynamically…
1,6,6

1,6,6 7,1,2 6,1,2 7,2,1 5,1,7 1,5,2 7,7,1 5,2,5


Uncertain Outcomes
Worst-Case vs. Average Case

max

min

10 10 9 100

Idea: Uncertain outcomes controlled by chance, not an adversary!


Why not minimax?
o Worst case reasoning is too conservative
o Need average case reasoning
Expectimax Search
o Why wouldn’t we know what the result of an action will be?
o Explicit randomness: rolling dice max
o Unpredictable opponents: the ghosts respond randomly
o Unpredictable humans: humans are not perfect
o Actions can fail: when moving a robot, wheels might slip
chance
o Values should now reflect average-case (expectimax)
outcomes, not worst-case (minimax) outcomes

o Expectimax search: compute the average score under


optimal play 10 10
4 5
9 100
7
o Max nodes as in minimax search
o Chance nodes are like min nodes but the outcome is uncertain
o Calculate their expected utilities
o I.e. take weighted average (expectation) of children

o Later, we’ll learn how to formalize the underlying


uncertain-result problems as Markov Decision Processes
Video of Demo Minimax vs Expectimax (Min)
Video of Demo Minimax vs Expectimax (Exp)
Expectimax Pseudocode

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 EXP: return exp-value(state)

def max-value(state): def exp-value(state):


initialize v = -∞ initialize v = 0
for each successor of state: for each successor of state:
v = max(v, value(successor)) p = probability(successor)
return v v += p * value(successor)
return v
Expectimax Pseudocode

def exp-value(state):
initialize v = 0
for each successor of state: 1/2 1/6
p = probability(successor) 1/3
v += p * value(successor)
return v 5
8 24
7 -12

v = (1/2) (8) + (1/3) (24) + (1/6) (-12) = 10


Expectimax Example

3 12 9 2 4 6 15 6 0
Expectimax Pruning?

3 12 9 2
Depth-Limited Expectimax

Estimate of true
400 300 …expectimax value
(which would
require a lot of
… work to compute)

492 362 …
What Probabilities to Use?

o In expectimax search, we have a


probabilistic model of how the opponent (or
environment) will behave in any state
o Model could be a simple uniform distribution
(roll a die)
o Model could be sophisticated and require a great
deal of computation
o We have a chance node for any outcome out of
our control: opponent or environment
o The model might say that adversarial actions are
likely!
o For now, assume each chance node
magically comes along with probabilities
that specify the distribution over its
outcomes Having a probabilistic belief about
another agent’s action does not mean
that the agent is flipping any coins!
Quiz: Informed Probabilities
o Let’s say you know that your opponent is actually running a depth 2 minimax,
using the result 80% of the time, and moving randomly otherwise
o Question: What tree search should you use?
▪ Answer: Expectimax!
▪ To figure out EACH chance node’s probabilities,
you have to run a simulation of your opponent
▪ This kind of thing gets very slow very quickly
0.1 0.9
▪ Even worse if you have to simulate your
opponent simulating you…
▪ … except for minimax and maximax, which
have the nice property that it all collapses into
one game tree
This is basically how you would model a human, except for their utility: their utility might be the same as yours (i.e. you try
to help them, but they are depth 2 and noisy), or they might have a slightly different utility (like another person navigating
in the office)
Modeling Assumptions
The Dangers of Optimism and Pessimism
Dangerous Optimism Dangerous Pessimism
Assuming chance when the world is adversarial Assuming the worst case when it’s not likely
Assumptions vs. Reality

Adversarial Ghost Random Ghost

Minimax Won 5/5 Won 5/5


Pacman Avg. Score: 483 Avg. Score: 493

Expectimax Won 1/5 Won 5/5


Pacman Avg. Score: -303 Avg. Score: 503

Results from playing 5 games

Pacman used depth 4 search with an eval function that avoids trouble
Ghost used depth 2 search with an eval function that seeks Pacman
[Demos: world assumptions (L7D3,4,5,6)]
Assumptions vs. Reality

Adversarial Ghost Random Ghost

Minimax Won 5/5 Won 5/5


Pacman Avg. Score: 483 Avg. Score: 493

Expectimax Won 1/5 Won 5/5


Pacman Avg. Score: -303 Avg. Score: 503

Results from playing 5 games

Pacman used depth 4 search with an eval function that avoids trouble
Ghost used depth 2 search with an eval function that seeks Pacman
[Demos: world assumptions (L7D3,4,5,6)]
Video of Demo World Assumptions
Random Ghost – Expectimax Pacman
Video of Demo World Assumptions
Adversarial Ghost – Minimax Pacman
Video of Demo World Assumptions
Adversarial Ghost – Expectimax Pacman
Video of Demo World Assumptions
Random Ghost – Minimax Pacman
Mixed Layer Types
o E.g. Backgammon
o Expectiminimax
o Environment is an
extra “random
agent” player that
moves after each
min/max agent
o Each node
computes the
appropriate
combination of its
children
Example: Backgammon
o Dice rolls increase b: 21 possible rolls with 2 dice
o Backgammon  20 legal moves
o Depth 2 = 20 x (21 x 20)3 = 1.2 x 109

o As depth increases, probability of reaching a


given search node shrinks
o So usefulness of search is diminished
o So limiting depth is less damaging
o But pruning is trickier…

o Historic AI: TDGammon uses depth-2 search +


very good evaluation function + reinforcement
learning:
world-champion level play

o 1st AI world champion in any game!


What Utility Values to Use?

0 40 20 30 x2 0 1600 400 900

x>y => f(x)>f(y) f(x) = Ax+B where A>0

o For worst-case minimax reasoning, evaluation function scale doesn’t matter


o We just want better states to have higher evaluations (get the ordering right)
o Minimax decisions are invariant with respect to monotonic transformations on values
o Expectiminimax decisions are invariant with respect to positive affine transformations
o Expectiminimax evaluation functions have to be aligned with actual win probabilities!
72
Monte Carlo Tree Search
o Methods based on alpha-beta search assume a fixed horizon
o Pretty hopeless for Go, with b > 300
o MCTS combines two important ideas:
o Evaluation by rollouts – play multiple games to termination from
a state s (using a simple, fast rollout policy) and count wins and
losses
o Selective search – explore parts of the tree that will help improve
the decision at the root, regardless of depth
Rollouts
“Move 37”

o For each rollout:


o Repeat until terminal:
o Play a move according to a
fixed, fast rollout policy
o Record the result
o Fraction of wins correlates
with the true value of the
position!
o Having a “better” rollout
policy helps
MCTS Version 0
o Do N rollouts from each child of the root, record fraction of
wins
o Pick the move that gives the best outcome by this metric

57/100 39/100 65/100


MCTS Simple Version
o Do N rollouts from each child of the root, record fraction of
wins
o Pick the move that gives the best outcome by this metric

57/100 0/100 59/100


MCTS
o Allocate rollouts to more promising nodes

77/140 0/10 90/150


MCTS
o Allocate rollouts to more promising nodes

61/100 6/10 48/100


MCTS Version 1
o Allocate rollouts to more promising nodes
o Allocate rollouts to more uncertain nodes

61/100 6/10 48/100


UCB heuristics
o UCB1 formula combines “promising” and “uncertain”:

o N(n) = number of rollouts from node n


o U(n) = total utility of rollouts (e.g., # wins) for Player(Parent(n))

80
MCTS Version 2: UCT
o Repeat until out of time:
o Given the current search tree, recursively apply UCB to choose a
path down to a leaf (not fully expanded) node n
o Add a new child c to n and run a rollout from c
o Update the win counts from c back up to the root
o Choose the action leading to the child with highest N

81
UCT Example
5/10 4/9

4/7 1/2 0/1 0/1

2/3 0/2 2/2 1/2

82
Why is there no min or max?????
o “Value” of a node, U(n)/N(n), is a weighted sum of child
values!
o Idea: as N →  , the vast majority of rollouts are
concentrated in the best children, so weighted average →
max/min
o Theorem: as N →  UCT selects the minimax move
o (but N never approaches infinity!)

83
Summary
o Games require decisions when optimality is impossible
o Bounded-depth search and approximate evaluation functions
o Games force efficient use of computation
o Alpha-beta pruning, MCTS
o Game playing has produced important research ideas
o Reinforcement learning (checkers)
o Iterative deepening (chess)
o Rational metareasoning (Othello)
o Monte Carlo tree search (chess, Go)
o Solution methods for partial-information games in economics (poker)
o Video games present much greater challenges – lots to do!
o b = 10500, |S| = 104000, m = 10,000, partially observable, often > 2 players

You might also like