Professional Documents
Culture Documents
Biti1113 Games in Ai
Biti1113 Games in Ai
Game Playing
Prepared By Zahriah Sahri
1
AI and Computer Game Playing
Game playing was the first test application
of AI
2
AI and Computer Game Playing
Game playing was the first test application
of AI
3
AI and Computer Game Playing
Current and popular games in AI
https://aigamedev.com/open/highlights/top-ai-games/
4
Why Do Games Attract Interest of
Computer Scientists?
Seemed to be a good domain for work on
AI, because games were thought to:
provide a source of a good structured task in
which success or failure is easy to measure.
AI Games Characteristics
Chess, Checkers, Go, Mancala, Tic-Tac-Toe,
Othello, Nim, …
Two players alternate moves
Zero-sum: one player’s loss is another’s gain
Perfect Information: each player knows the entire
game state
Deterministic: no element of chance/randomness
Clear set of legal moves
Well-defined outcome (e.g. win, lose, draw)
How Computer Play Games?
Minimax
Alphabeta Pruning
Expectimax
Markov decision processes (MDPs)
The Minimax Algorithm
Invented by Von Neumann &
Morgenstern in 1944 as part of game
theory
Used for two player games.
One player (MAX) is trying to maximize
the value of the evaluation function.
The opponent (MIN) is trying to minimize
its value
The Minimax Algorithm
15
Minimax Pseudocode
function MINIMAX-DECISION(state) returns an action
inputs: state, current state in game
vMAX-VALUE(state)
return the action in SUCCESSORS(state) with value v
function MAX-VALUE(state) returns a utility value
if TERMINAL-TEST(state) then return
UTILITY(state)
v -∞
for a,s in SUCCESSORS(state) do
v MAX(v, MIN-VALUE(s) )
return v
function MIN-VALUE(state) returns a utility value
if TERMINAL-TEST(state) then return
UTILITY(state)
v∞
for a,s in SUCCESSORS(state) do
v MIN(v, MAX-VALUE(s) )
return v
Properties of minimax
• Complete?Yes (if tree is finite)
• Optimal?Yes (against an optimal opponent)
• Time complexity? O(bm, m is depth of tree and b is
legal moves at each node.
• Space complexity?
• O(bm) for an algorithm that generates all successors at once (depth-
first exploration)
• O(m) if it generates successors one at a time.
19
Do We Need to Evaluate Every
Node in Minimax?
No need!!!
How???
Uses Alpha-Beta pruning 20
Alpha-beta Pruning
The idea: “If you have an idea which is surely bad,
don’t take the time to see how truly awful it is” ~ Pat
Winston
Alpha-Beta pruning
prunes away subtress that can’t possibly
influence the final decision
>=2
• We don’t need to compute the
value at this node.
=2 <=1
• No matter what it is it can’t
affect the value of the root
node.
21
2 7 1 ?
Why is it called α-β Pruning?
• Add α,β bounds to each node n
• If is N the possible value for a node n,
then 𝛼 ≤ 𝑁 ≤ 𝛽
• α is the maximum value that MAX can
guarantee at that level or above
• β is the minimum value that MIN can
guarantee at that level or above.
• Not a new algorithm, rather an
optimization technique for Minimax 22
Alpha-Beta Pruning Strategy
Traverse the search tree in depth-first order
For each MAX node n, α(n)=maximum child value
found so far
• Starts with –
For each MIN node n, β(n)=minimum child value
found so far
• Starts with +
Carry α and β values down to child nodes during
search
Updates value of α and β during search
• MAX updates at MAX nodes
• MIN updates at MIN nodes
Prune remaining branches at a node when α ≥ β
When to Prune (Cut-off) ?
Prune whenever ≥ .
MAX cutoff rule: At a MAX node n, cut off
search if (n)>=β(n)
• Max nodes update based on children’s returned
values.
24
Alpha-Beta Pruning Example
• Do DF-search until first leaf
α,β initial values
α=-∞,
β = +∞
α,β passed to kids
α=-∞,
β = +∞
25
Alpha-Beta Pruning Example
α=-∞,
β = +∞
α=-∞,
β=3
26
Alpha-Beta Pruning Example
α=-∞,
β = +∞
α=-∞,
β=3
3 12
α=-∞,
β = +∞
α=-∞,
β=3
3 12 8
28
Alpha-Beta Pruning Example
3 12 8
29
Alpha-Beta Pruning Example
.
α=3,
β = +∞
α, β passed to kids,
α=3,
β = +∞
30
Alpha-Beta Pruning Example
.
α=3,
β = +∞
MIN updates β
based on kids,
α=3,
β=2
31
Alpha-Beta Pruning Example
.
α=3,
β = +∞
32
Alpha-Beta Pruning Example
MAX updates α, based on kids.
No change α=3,
β = +∞
2 is returned as node value
33
Alpha-Beta Pruning Example
α=3,
β = +∞
α, β passed to kids
α=3,
β = +∞
34
Alpha-Beta Pruning Example
α=3,
β = +∞ MIN updates β
based on kids,
α=3,
β = 14
35
Alpha-Beta Pruning Example
α=3,
β = +∞ MIN updates β
based on kids,
α=3,
β=5
36
Alpha-Beta Pruning Example
α=3,
β = +∞ MIN updates β
based on kids,
α=3,
β=2
37
Alpha-Beta Pruning Example
MAX updates α, based on kids.
No change α=3,
β = +∞ 2 is returned as
node value
38
Alpha-Beta Pruning Example
3 MAX selects 3 for this node and
choose the move
39
Alpha-Beta Pruning
Alpha-Beta Pruning (cont.)
Alpha-Beta Pruning (cont.)
Alpha-Beta Pruning (cont.)
Alpha-Beta Pruning (cont.)
Alpha-Beta Pruning (cont.)
Alpha-Beta Pruning (cont.)
Alpha-Beta Pruning (cont.)
Alpha-Beta Pruning (cont.)
Alpha-Beta Pruning (cont.)
Alpha-Beta Pruning (cont.)
Alpha-Beta Pruning (cont.)
Alpha-Beta Pruning (cont.)
Alpha-Beta Pruning (cont.)
Alpha-Beta Pruning (cont.)
Alpha-Beta Pruning (cont.)
Alpha-Beta Pruning (cont.)
Alpha-Beta Pruning (cont.)
Benefits of Alpha-Beta pruning
• reduces the number of nodes explored in
Minimax strategy.
• reduces the time required for the search
and it must be restricted so that no time is
to be wasted searching moves that are
obviously bad for the current player.
• This allows to go into deeper levels in the
game tree
58