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

Chap04 GamePlaying Complete

Uploaded by

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

Chap04 GamePlaying Complete

Uploaded by

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

Chapter 2: Artificial Intelligence

Game Playing (Adversarial Search)

Lalita Randive (MIT, Aurangabad)

1
Game Playing

Why do AI researchers study game playing?

1. It’s a good reasoning problem, formal and nontrivial.

2. Direct comparison with humans and other


computer programs is easy.
What Kinds of Games?
Mainly games of strategy with the following
characteristics:

1. Sequence of moves to play


2. Rules that specify possible moves
3. Rules that specify a payment for each
move
4. Objective is to maximize your payment
Games vs. Search Problems

• Unpredictable opponent  specifying a move


for every possible opponent reply

• Time limits  unlikely to find goal, must


approximate
Two-Player Game
Opponent’s Move

Generate New Position

Game yes
Over?
no
Generate Successors

Evaluate Successors

Move to Highest-Valued Successor

no Game yes
Over?
Games as Adversarial Search
• States:
– board configurations
• Initial state:
– the board position and which player will move
• Successor function:
– returns list of (move, state) pairs, each indicating a legal
move and the resulting state
• Terminal test:
– determines when the game is over
• Utility function:
– gives a numeric value in terminal states (e.g., -
1, 0, +1 for loss, tie, win)
Game Tree (2-player, Deterministic Turns)

computer’s
turn

opponent’s
turn

computer’s The computer is Max.


turn
The opponent is
opponent’s Min.
turn

leaf nodes At the leaf nodes, the


are evaluated utility function
is employed. Big value
means good, small is
Mini-Max Terminology
• move: a move by both players
• ply: a half-move
• utility function: the function applied to leaf nodes
• backed-up value
– of a max-position: the value of its largest successor
– of a min-position: the value of its smallest successor
• minimax procedure: search down several levels; at
the bottom level apply the utility function, back-up
values all the way up to the root node, and that node
selects the move.
Minimax
• Perfect play for deterministic games
• Idea: choose move to position with highest minimax value
= best achievable payoff against best play
• E.g., 2-ply game:
80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
11
80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
12
80

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
13
30

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
14
30

30

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
15
30

30 25

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
16
30

30 25

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
17
30

30

30 25

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
18
30

30

30 25

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
19
30

30 20

30 25 20

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
20
30

30 20

30 25 20 05

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
21
30

30 20

30 25 20 05

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
22
30

30 20

30 25 20 05

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
23
20

20

30 20

30 25 20 05

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
24
20

20 15

30 20 15 60

30 25 20 05 10 15 45 60

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
25
20

20 15

30 20 15 60

30 25 20 05 10 15 45 60

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
26
20

20 15

30 20 15 60

30 25 20 05 10 15 45 60

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
27
Minimax Strategy
• Why do we take the min value every other
level of the tree?

• These nodes represent the opponent’s choice


of move.

• The computer assumes that the human will


choose that move that is of least value to the
computer.
What is a zero-sum game?

Zero-Sum Games General Games


Agents have opposite utilities (values Agents have independent utilities
on outcomes) (values on outcomes)
Lets us think of a single value that Cooperation, indifference,
one maximizes and the other competition, and more are all
minimizes (for two player game possible
) More later on non-zero-sum
games
Adversarial, pure competition
Minimax algorithm
Adversarial analogue of DFS
Properties of Minimax
• Complete?
– Yes (if tree is finite)
• Optimal?
– Yes (against an optimal opponent)
– No (does not exploit opponent weakness against suboptimal
opponent)
• Time complexity?
– O(bm)
• Space complexity?
– O(bm) (depth-first exploration)
Good Enough?
• Chess:
– branching factor b≈35

– game length m≈100

– search space bm ≈ 35100 ≈ 10154

• The Universe:
– number of atoms ≈ 1078

– age ≈ 1018 seconds

– 108 moves/sec x 1078 x 1018 =


10104

• Exact solution completely infeasible


80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
32
30

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
33
30

30 25

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
34
30 Do we need to check
this node?

30 25

80 30 25 ?? 55 20 05 65 40 10 70 15 50 45 60 75
35
30 No - this branch is guaranteed to be
worse than what max already has

30 25

80 30 25
55 20 05 65 40 10 70 15 50 45 60 75
36
??
30

30 20
Do we need to check
this node?

30 25 20 05

80 30 25
55 20 05 ?? 40 10 70 15 50 45 60 75
37
35
30

30 20

30 25 20 05

80 30 25 20 05
55 40 10 70 15 50 45 60 75
38
35 ??
Alpha-Beta
• The alpha-beta procedure can speed up a
depth-first minimax search.
• Alpha: a lower bound on the value that a max
node may ultimately be assigned
v>
• Beta: an upper bound on the value that a
minimizing node may ultimately be assigned
v<
Alpha-Beta
MinVal(state, alpha, beta){
if (terminal(state))
return utility(state);
for (s in children(state)){
child =
MaxVal(s,alpha,beta); beta =
min(beta,child);
if (alpha>=beta)
return child;
}
alpha = the highest value for
return MAX
best along the path
child
beta =(min); } value for MIN along the path
the lowest
Alpha-Beta
MaxVal(state, alpha, beta){
if (terminal(state))
return utility(state);
for (s in children(state)){
child =
MinVal(s,alpha,beta); alpha
= max(alpha,child);
if (alpha>=beta)
return child;
}
return best child
(max);
alpha = the}highest value for MAX along the path
beta = the lowest value for MIN along the path
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the
path
α=-

β=∞

α=-

β=∞

α=-

β=∞

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
42
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the
path
α=-

β=∞

α=-

β=∞

α=-∞ 80
β=80

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
43
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the
path
α=-

β=∞

α=-

β=∞

α=-∞
30
β=30

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
44
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the
path
α=-

β=∞

α=30
β=∞ 30

α=-∞
30
β=30

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
45
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the
path
α=-

β=∞

α=30
β=∞ 30

α=30
β=∞
α=-
∞ 30
β=30

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
46
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the
path
α=-

β=∞

α=30
β=∞ 30

β≤α
α=30
prune!
α=- β=25
∞ 30
β=30 25

80 30 25 55 20 05 65 40 10 70 15 50 45 60 7547
75
35 47
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the
path
α=-∞ 30
β=30

α=30
β=∞ 30

α=30

α=- β=25
∞ 30
β=30 25

80 30 25 55 20 05 65 40 10 70 15 50 45 60 7548
75
35 48
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the
path
α=-∞ 30
β=30

α=30 α=-∞
β=30
β=∞ 30

α=30

α=-
25β=25α=-∞
∞ 30 β=30
β=30

80 30 25 55 20 05 65 40 10 70 15 50 45 60 7549
75
35 49
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the
path
α=-∞ 30
β=30

α=30 α=20
β=∞ 30 20
β=30

α=30 α=20

α=- β=30
25β=25α=-∞ 20
∞ 30 β=20
β=30

80 30 25 55 20 05 65 40 10 70 15 50 45 60 7550
75
35 50
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the
path
α=-∞ 30
β=30

α=30 α=20
β=∞ 30 20
β=30

α=30 α=20

α=- β=05
25β=25α=-∞ 20
∞ 30 β=20
β=30 05

80 30 25 55 20 05 65 40 10 70 15 50 45 60 7551
75
35 51
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the
path
α=-∞ 30
β=30

α=30 α=20
β=∞ 30 20
β=30
β≤α
α=30 α=20
prune!
α=- β=05
25β=25α=-∞ 20
∞ 30 β=20
β=30 05

80 30 25 55 20 05 40 10 70 15 50 45 60 7552
75
35 52
α=-
α - the best value ∞
for max along the path β=∞
β - the best value
for min along the
path
α=-∞ 20
β=20

α=30 α=20
β=∞ 30 20
β=30

α=30 α=20

α=- β=05
25β=25α=-∞ 20
∞ 30 β=20 05
β=30

80 30 25 55 20 05 40 10 70 15 50 45 60 7553
75
35 53
α=20
α - the best value 20 β=∞
for max along the path
β - the best value
for min along the
path
α=-∞ 20
β=20

α=30 α=20
β=∞ 30 20
β=30

α=30 α=20

α=- β=05
25β=25α=-∞ 20
∞ 30 β=20 05
β=30

80 30 25 55 20 05 40 10 70 15 50 45 60 7554
75
35 54
α=20
α - the best value 20 β=∞
for max along the path
β - the best value
for min along the
path
α=20
20
β=∞

α=20
30 20 β=∞

α=20
30 25 20 05 β=∞

80 30 25 55 20 05 40 10 70 15 50 45 60 7555
75
35 55
α=20
α - the best value 20 β=∞
for max along the path
β - the best value
for min along the
path
α=20
20
β=∞

α=20
30 20 β=∞

α=20
30 25 20 05
β=10 10

80 30 25 55 20 05 40 10 70 15 50 45 60 7556
75
35 56
α=20
α - the best value 20 β=∞
for max along the path
β - the best value
for min along the
path
α=20
20
β=∞

α=20
30 20 10 β=∞

α=20
30 25 20 05
β=10 10

80 30 25 55 20 05 40 10 70 15 50 45 60 7557
75
35 57
α=20
α - the best value 20 β=∞
for max along the path
β - the best value
for min along the
path
α=20
20
β=∞

α=20
30 20 10 β=∞

α=20
α=20
30 25 20 05 β=15
β=10 10
15

80 30 25 55 20 05 40 10 70 15 50 45 60 7558
75
35 58
α=20
α - the best value 20 β=∞
for max along the path
β - the best value
for min along the
path
α=20
20
β=∞

α=20
30 20 15 β=∞

α=20
α=20
30 25 20 05 β=15
β=10 10
15

80 30 25 55 20 05 40 10 70 15 50 45 60 7559
75
35 59
α=20
α - the best value 20 β=∞
for max along the path
β - the best value
for min along the
path
α=20
20 15
β=15

α=20
30 20 15 β=∞

α=20
α=20
30 25 20 05 β=15
β=10 10
15

80 30 25 55 20 05 40 10 70 15 50 45 60 7560
75
35 60
α=20
α - the best value 20 β=∞
for max along the path β≤α
β - the best value
for min along the
prune!
path
α=20
20 15
β=15

α=20
30 20 15 β=∞ X
α=20
α=20
30 25 20 05
β=10 10
15
β=15
X X

80 30 25 55 20 05 40 10 70 15 X X X X
61
35
Bad and Good Cases for Alpha-Beta Pruning
• Bad: Worst moves encountered first
4 MAX
+ +
2 3 4
+----+----+
+ +----+----+ MIN
6 7 +----+----+ MAX
+--+
4 +--+ +--+ +-+-+
5 +--+ +--+ +--+ +--+
8 +--+--+ 6
5 4 23 2 1137 4 3 5 2 3 8 62 1 6 1 2
4 4
• Good: Good moves ordered first
4 MAX
+ + +
4 3 2 MIN
+ + +
+ + + MAX
+--+ +--+ +--+ ++--+ + + 4
4 2 6 x 8 x3 2 6
8 3
• If we can order moves, we can get more benefit from alpha-beta pruning
x
Properties of α-β
• Pruning does not affect final result. This means that it gets the
exact same result as does full minimax.

• Good move ordering improves effectiveness of pruning

• With "perfect ordering," time complexity = O(bm/2)


 doubles depth of search

• A simple example of reasoning about ‘which computations are


relevant’ (a form of metareasoning)
Why O(bm/2)?
Let T(m) be time complexity of search for depth m

Normally:
T(m) = b.T(m-1) + c  T(m) = O(bm)

With ideal α-β pruning:


T(m) = T(m-1) + (b-1)T(m-2) + c  T(m) = O(bm/2)
Node Ordering
Iterative deepening search

Use evaluations of the previous search for order

Also helps in returning a move in given time


Good Enough?
• Chess: The universe
– branching factor b≈35 can play chess
- can we?
– game length m≈100
– search space bm/2 ≈ 3550 ≈ 1077
• The Universe:
– number of atoms ≈ 1078
– age ≈ 1018 seconds
– 108 moves/sec x 1078 x 1018 = 10104 66
Cutting off Search
MinimaxCutoff is identical to MinimaxValue except
1. Terminal? is replaced by Cutoff?
2. Utility is replaced by Eval

Does it work in practice?


bm = 106, b=35  m=4

4 ply lookahead is a
hopeless chess player!
– 4-ply ≈ human novice
– 8-ply ≈ typical PC,
human master
– 12-ply ≈ Deep Blue, 67
Cutoff

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
68
0

0 0

0 0 Cutoff 0 0

80 30 25 35 55 20 05 65 40 10 70 15 50 45 60 75
69
Evaluation Functions
Tic Tac Toe
• Let p be a position in the game
• Define the utility function f(p) by
– f(p) =
• largest positive number if p is a win for computer
• smallest negative number if p is a win for opponent
• RCDC – RCDO
– where RCDC is number of rows, columns and diagonals in
which computer could still win
– and RCDO is number of rows, columns and diagonals in
which opponent could still win.
Sample Evaluations
• X = Computer; O = Opponent

O O O X
X X X

X O X

O
rows rows
cols cols
diags diags
Evaluation functions
• For chess/checkers, typically linear weighted sum of features
Eval(s) = w1 f1(s) + w2 f2(s) + … + wm fm(s)

e.g., w1 = 9 with
f1(s) = (number of white queens) – (number of black
queens), etc.
Example: Samuel’s Checker-Playing
Program
• It uses a linear evaluation function
f(n) = w1f1(n) + w2f2(n) + ... + wmfm(n)

For example: f = 6K + 4M + U
– K = King Advantage
– M = Man Advantage
– U = Undenied Mobility Advantage (number of
moves that Max where Min has no jump moves)
Samuel’s Checker Player
• In learning mode

– Computer acts as 2 players: A and B


– A adjusts its coefficients after every move
– B uses the static utility function
– If A wins, its function is given to B
Samuel’s Checker Player
• How does A change its function?
Coefficent replacement
(node) = backed-up value(node) – initial value(node)
if > 0 then terms that
contributed positively are given more weight and
terms that contributed negatively get less weight
if < 0 then terms that
contributed negatively are given more weight and
terms that contributed positively get less weight
Chess: Rich history of cumulative ideas
Minimax search, evaluation function learning (1950).

Alpha-Beta search (1966).

Transposition Tables (1967).

Iterative deepening DFS (1975).

End game data bases ,singular extensions(1977, 1980)

Parallel search and evaluation(1983 ,1985)

Circuitry (1987)
77
Chess game tree

78
Image from Kasparov versus Deep Blue: Computer Chess Comes of Age By Monty Newborn
Problem with fixed depth Searches
if we only search n moves ahead,
it may bepossible that the
hy can be delayed by a
catastrop of moves that do not
progress
sequence
makes in other direction
any ves may not be found)

also work
(good
mo

79
Problems with a fixed ply: The Horizon Effect

Lose queen Lose pawn


The “look ahead horizon”
Lose queen!!!

• Inevitable losses are postponed


• Unachievable goals appear achievable
• Short-term gains mask unavoidable
consequences (traps)
Solutions
• How to counter the horizon effect
– Feedover
• Do not cut off search at non-quiescent board positions
(dynamic positions)
• Example, king in danger
• Keep searching down that path until reach quiescent
(stable) nodes
– Secondary Search
• Search further down selected path to ensure this is the
best move
Quiescence Search

This involves searching past the terminal search nodes


(depth of 0) and testing all the non-quiescent or 'violent'
moves until the situation becomes calm, and only then apply
the evaluator.

Enables programs to detect long capture sequences


and calculate whether or not they are worth initiating.

Expand searches to avoid evaluating a position where


tactical disruption is in progress.
Additional Refinements

• Probabilistic Cut: cut branches probabilistically based


on shallow search and global depth-level statistics
(forward pruning)

• Openings/Endgames: for some parts of the game


(especially initial and end moves), keep a catalog of
best moves to make.

• Singular Extensions: find obviously good moves and


try them at cutoff.
End-Game Databases

• Ken Thompson - all 5 piece end-games


• Lewis Stiller - all 6 piece end-games
– Refuted common chess wisdom: many
positions thought to be ties were really
forced wins -- 90% for white
– Is perfect chess a win for white?
The MONSTER

White wins in 255 moves


(Stiller, 1991)
Deterministic Games in Practice
• Checkers: Chinook ended 40-year-reign of human world champion Marion
Tinsley in 1994. Used a precomputed endgame database defining perfect
play for all positions involving 8 or fewer pieces on the board, a total of
444 billion positions. Checkers is now solved!
• Chess: Deep Blue defeated human world champion Garry Kasparov in a
six-game match in 1997. Deep Blue searches 200 million positions per
second, uses very sophisticated evaluation, and undisclosed methods for
extending some lines of search up to 40 ply. Current programs are even
better, if less historic!
• Othello: human champions refuse to compete against computers, who are
too good.
• Go: until recently, human champions refused to compete against
computers, who were too bad. In Go, b > 300, so most programs use
pattern knowledge bases to suggest plausible moves, along with
aggressive pruning. In 2016, DeepMind’s AlphaGo defeated Lee Sedol 4-1
to end the human reign.
Game of Go
human champions refused to compete
against computers, because software
used to be too bad.
Chess Go
Size of board 8x8 19 x 19
Average no. of 100 300
moves per game
Avg branching 35 235
factor per
turn
Additional Players can
complexity pass
AlphaGo (2016)
• Combination of
– Deep Neural Networks
– Monte Carlo Tree Search

• More details later.


Other Games
deterministic chance

chess,
perfect backgammon,
checkers, go,
information monopoly
othello

imperfect bridge, poker,


stratego
information scrabble
Games of Chance
• What about games that involve chance, such
as
– rolling dice
– picking a card
• Use three kinds of nodes:
– max nodes   min
– min nodes  chance
– chance nodes max
Games of Chance
Expectiminimax
c chance node with
max children

d1 di dk

S(c,di)

expectimax(c) = ∑P(di) max(backed-up-value(s))


i s in S(c,di)

expectimin(c’) = ∑P(di) min(backed-up-value(s))


i s in S(c,di)
Example Tree with Chance
max

chance
.4 .6
min   1.2

chance
.4

.6

leaf .4
3 5 1 4 1 2 4 5
.6
max
Complexity
• Instead of O(bm), it is O(bmnm) where n is the
number of chance outcomes.

• Since the complexity is higher (both time and


space), we cannot search as deeply.

• Pruning algorithms may be applied.


Imperfect Information
• E.g. card games, where
opponents’ initial ar
cards unknown e
• Idea: For all deals consistent with what
you can see
– compute the minimax value of
available actions for each of possible
deals
– compute the expected value over all
deals
Status of AI Game Players
• Tic Tac Toe • Poker
– Tied for best player in world – 2015, Heads-up limit hold'em poker
• Othello is solved
– Computer better than any human • Checkers
– Human champions now refuse to – 1994, Chinook ended 40-year reign
play computer of human champion Marion Tinsley
• Scrabble • Chess
– Maven beat world champions Joel – 1997, Deep Blue beat human
Sherman and Matt Graham champion Gary Kasparov in six-
game match
• Backgammon – Deep Blue searches 200M
– 1992, Tesauro combines 3-ply positions/second, up to 40 ply
search & neural networks (with 160 – Now looking at other applications
hidden units) yielding top-3 player
(molecular dynamics, drug
• Bridge synthesis)
– Gib ranked among top players in the • Go
world – 2016, Deepmind’s AlphaGo
defeated Lee Sedol & 2017 defeated
Ke Jie
Summary
• Games are fun to work on!

• They illustrate several important points about AI.

• Perfection is unattainable  must approximate.

• Game playing programs have shown the world what


AI can do.

97

You might also like