Download as pdf or txt
Download as pdf or txt
You are on page 1of 58

AI And Computer

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/

 Watch Google's AI master the infamously difficult Atari game


Montezuma's Revenge
(http://www.theverge.com/2016/6/9/11893002/google-ai-
deepmind-atari-montezumas-revenge)

 Top 10 Video Games with the Best AI


(https://www.youtube.com/watch?v=1OXb8mg9IVw)

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?

Computational thinking suggests


transforming (reducing) it into something that
computers do well: search
Search is performed to determine the best
next move in the game
The best move depends on what the opponent
might do. As a result, this is sometimes called
“adversary search”
How Computer Play Games?
Consider games with two players (MAX, MIN)
Initial State
 Initial board position and identifies the player to move
Successor Function
 Returns a list of (move, state) pairs; each a legal move
and resulting state
Terminal Test
 Determines if the game is over (at terminal states)
Utility Function
 Objective function, payoff function, a numeric value
for the terminal states (+1, -1) or (+192, -192)
Game tree
 represents all possible game scenarios
Partial Game Tree for TIC-TAC-TOE
What Searches to Use in 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

Generate the game tree


Apply the utility function to each terminal
state
“Back-up” values through game tree:
– at each MAX node, take max of successors
– at each MIN node, take min of successors
Select best move for MAX node at root
The Minimax Algorithm

The minimax algorithm

 The algorithm first recurses down to the tree


bottom-left nodes
• and uses the Utility function on them to discover that
their values are 3, 12 and 8.
13
The Minimax Algorithm
A

• Then it takes the minimum of these values, 3, and


returns it as the backed-up value of node B.
• Similar process for the other nodes. 14
The Minimax Algorithm

• The minimax algorithm performs a


complete depth-first exploration of
the game tree.
• In minimax, at each point in the
process, only the nodes along a path
of the tree are considered and kept in
memory.

15
Minimax Pseudocode
function MINIMAX-DECISION(state) returns an action
inputs: state, current state in game
vMAX-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.

For chess, b ≈ 35, m ≈100 for "reasonable" games


 exact solution completely infeasible
Comments on Minimax Search

• Performance will depend on


• the quality of the evaluation function (expert
knowledge)
• depth of search (computing power and search
algorithm)
• Differences from normal state space search
• Looking for one move only
• No cost on arcs
• MAX can’t be sure how MIN will respond to his moves
Comments on Minimax Search
• For real games the time cost of minimax is
totally impractical, but this algorithm serves as
the basis:
• for the mathematical analysis of games and
• for more practical algorithms
• Problem with minimax search:
• The number of game states it has to examine
is exponential in the number of moves.

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.

 MIN cutoff rule: At a MIN node n, cut off


search if β(n)<= (n).
• Min 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

• Do DF-search until first leaf

α=-∞,
β = +∞

α=-∞,
β=3

MIN updates β based on kids

26
Alpha-Beta Pruning Example

α=-∞,
β = +∞

α=-∞,
β=3

MIN updates β, based on kids.


No change

3 12

Chapter 3 - Searching Methodology 27


Alpha-Beta Pruning Example

α=-∞,
β = +∞

α=-∞,
β=3

MIN updates β, based on kids.


No change

3 12 8

28
Alpha-Beta Pruning Example

MAX updates α, based on kids.


α=3,
β = +∞

3 is returned as node value


3

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

You might also like