Lesson 3 Gaming
Lesson 3 Gaming
GAME PLAYING
Terminology
• A game is a type of conflict in which n individuals or groups known as
players participates
• Game theory is a set of ideas and techniques for analyzing conflict
situations between two or more parties. The outcomes are determined by
their decisions
• Rules: conditions for playing the game
• Perfect information: all moves are known to each players involved
• Strategy: a plan of action for whatever situation might arise.
• Move: choice made by a player
Terminology used
• Utility : a number indicating motivation of players. It is also known as
pay off.
• Outcome: A set of moves or strategies taken by the players, or it is
their utility resulting from the actions or strategies taken by
all players.
• Utility function: a function for a given player that assigns a number for
every possible outcome of game. Where a higher number indicates
the outcome is more preferred.
Benefits of games
Evaluation function estimates cost (path cost) from start to goal through
given node
Searching techniques aim at finding optimal solution (cheapest path)
Games
There is existence of adversary
Solution is a set of moves leading to a certain outcome.
Utility (Evaluation) function evaluate utility (“goodness”) of game
position
The objective is find an approximate solution
Types of board Games
There are various types of board games
1. Perfect information games
2. Imperfect information games
3. Un-deterministic
4. Deterministic
3. Undeterministic
Games with an element of chance
The game moves have some stochastic (random) element
Unpredictable
For example: throwing a coin, Backgammon, Die rolls.
Types of Games
4. Deterministic Game
• Games with no element of chance
• The outcome can be predicted
• For example, Chess, draughts/scramble
• Most searches will involve building the tree to some preset depth of the
tree, and then using a utility evaluation function to guess the value of
game positions at that depth.
• Values at the leaves indicate final values to be gained for each Path at the
end of the Game
Game search vs search problem
• Search problem: the aim is to reach a goal state in a graph, and
finding the path from start state to goal state,
• So she wants to take the option (A,B,C,) that will maximize her
score.
• But she knows in the next level down Stavros will try to minimize
the score, etc.
• For instance, Helen is doing the best she can given that Stavros
is doing the best he can.
1 B -3 C
MIN
4 D 1 E 2 F -3 G
MAX
4 -5 -5 1 -7 2 -3 -8
MAX
MIN
MAX
MIN
GAMETRE
E
MAX
MIN
MAX
MIN
GAMETRE
E
MAX
MIN
MAX
MIN
GAMETRE
E
MAX
MIN
MAX
MIN
GAMETRE
E
MAX
MIN
MAX
MIN
GAMETRE
E
MAX
MIN
MAX
MIN
GAMETRE
E
MAX
MIN
MAX
MIN
GAME TRE
E
MAX
MIN
MAX
MIN
GAME TRE
E
MAX
MIN
MAX
MIN
MINIMAX ALGORIT
HM
max_value(node):
1: if end_state(node): return
value(node)
2: v = –Inf
3: for each child in node.children():
4: v = max(v, min_value(child))
5: return v
min_value(node):
1: if end_state(node): return
value(node)
2: v = +Inf
3: for each child in node.children():
4: v = min(v, max_value(child))
5: return v
MINIMAX ALGORITHM
MAX
Player 2 Player 2
0 1 1
0
0 1 0 1 0 1 0 1
-1 -1 -1 1 -1 1 1 -1
Player 1’s utility is in the leaves; player 2’s utility is the negative of this
Backward induction (aka. minimax)
• From leaves upward, analyze best decision for player at node, give
node a value
• Once we know values, easy to find optimal action (choose best value)
1 Player 1
0 1
Player 2 Player 2
-1 1
0 1 1
0
0 1 0 1 0 1 0 1
-1 -1 -1 1 -1 1 1 -1
Modified game
• From leaves upward, analyze best decision for player at node, give node a
value
6 Player 1
0 1
Player 2 Player 2
-1 6
0 1 1
0
0 1 0 1 0 1 0 1
-1 -2 -3 4 -5 6 7 -8
A recursive implementation
• Value(state)
• If state is terminal, return its value
• If (player(state) = player 1)
• v := -infinity
• For each action
• v := max(v, Value(successor(state, action)))
• Return v
• Else
• v := infinity Space? Time?
• For each action
• v := min(v, Value(successor(state, action)))
• Return v
Do we need to see all the leaves?
• Do we need to see the value of the question mark here?
Player 1
0 1
Player 2 Player 2
0 1 1
0
0 1 0 1 0 1 0 1
-1 -2 ? 4
Do we need to see all the leaves?
• Do we need to see the values of the question marks here?
Player 1
0 1
Player 2 Player 2
0 1 1
0
0 1 0 1 0 1 0 1
-1 -2 ? ? -5 6 7 -8
ALPHA-BETA PRUNING
Alpha-Beta Pruning
• mini-max algorithm involves a lot of computation if the size of
tree is large.
MAX Alpha-value
2
MIN 2 =2 1 Beta-value
MAX
2 5 1
The Alpha-Beta principles (1):
- If an ALPHA-value is larger than or equal the Beta-value
of a descendant node:
stop generation of the children of the descendant (prune)
MAX 2 Alpha-value
MIN 2 =2 1 Beta-value
MAX
2 5 1
The Alpha-Beta principles (2):
- If a Beta-value is smaller or equal than the Alpha-value
of a descendant node:
stop generation of the children of the descendant (prune)
MAX
MIN
MAX 4
2 5 3
Alpha-Beta Pruning example
MAX A
MIN <=6 B C
MAX 6 D >=8 E
H I J K
6 5 8
= opponent
= agent
Alpha-Beta Pruning example
MAX >=6 A
MIN 6 B <=2 C
MAX 6 D >=8 E 2 F G
H I J K L M
6 5 8 2 1
= agent = opponent
Alpha-Beta Pruning example
>=6 A
MAX
6 B 2 C
MIN
6 D >=8 E 2 F G
MAX
H I J K L M
6 5 8 2 1
= agent = opponent
Alpha-Beta Pruning example
MAX 6 A
MIN 6 B 2 C beta
cutoff
H I J K L M
6 5 8 2 1
= agent = opponent
Mini-Max with at work:
4 16 5 31 = 5 39
MAX
8 6 5 23
= 4 15 = 5 30 3 38
MIN
8 2 2 10 1 18 1 33
4 12 3 20 3 25 2 35
=8 5 9 8 9 27 6 29 = 3 37
= 4 14 = 5 22
MAX
8 7 3 9 1 6 2 4 1 1 3 5 3 9 2 6 5 2 1 2 3 9 7 2 8 6 4
1 3 4 7 9 11 13 17 19 21 24 26 28 32 34 36
11 static evaluations saved !!
A L P H A - B E TA P R U N I
NG
MAX
MIN
MAX
MIN
A L P H A - B E TA P R U N I
NG
MAX
MIN
MIN-VALUE ≤ 1
MAX
MIN
A L P H A - B E TA P R U N I
NG
MAX
MIN
MIN-VALUE ≤ 1
‹ MAX-VALUE = 3
MAX
MIN
A L P H A - B E TA P R U N I
NG
MAX
MIN
MIN-VALUE ≤ 1
‹ MAX-VALUE = 3
MAX
MIN
A L P H A - B E TA P R U N I
NG
max_value(node, alpha, beta):
• 1: if end_state(node): return value(node)
• 2: v = –Inf
• 3: for each child in node.children():
)
• 4: v = max(v, min_value(child, alpha, beta)
UPPER-BOUND
• 5: alpha = max(alpha, v)
OF
• 6: if alpha >= beta: return v THE VALUE OF
• 7: return v
min_value(node, alpha, beta): LOWER-BOUND OF
1: if end_state(node): return va lu e(n od e )
v = +Inf T H E V A LUE
for each child in node.children(): GAME
OF THEv = min(v, max_value(child, alpha, beta))
2: beta = min(beta, v)
3: if alpha >= beta: return v return v
4:
5:
A L P H A - B E TA P R U N I
NG
MAX
MIN
MAX α = 0
MIN
A L P H A - B E TA P R U N I
NG
MAX
MIN
MAX α = 1
MIN
A L P H A - B E TA P R U N I
NG
MAX
MIN
MAX α = 6
MIN
ALPHA-BE
Pmin_value(node,
R U N I Nalpha, beta):
TA
G1:2: ifv =end_state(node):
+Inf
return value(node)
MAX α=3
MIN α=3
ALPHA-BE
Pmin_value(node,
R U N I Nalpha, beta):
TA
G1:value(node)
if end_state(node): return
2: v = +Inf
3: for each max_value(child,
min(v, child in node.children():
alpha, beta))
min(beta, v)
MAX 5: 4: beta = 1v =1
3 >= beta: return v
if alpha
6: 1
7: return v
MIN β=6
MAX α=3
MIN α=3
ALPHA-BE
Pmin_value(node,
R U N I Nalpha, beta):
TA
G1:value(node)
if end_state(node): return
2: v = +Inf
3: for each max_value(child,
min(v, child in node.children():
alpha, beta))
min(beta, v)
MAX 5: 4: beta = 1v =1
3 >= beta: return v
if alpha
6: 1
7: return v
MIN β=6
MAX α=3
MIN α=3
A L P H A - B E TA P R U N I
NG
MAX
MIN β=3
MAX
MIN
A L P H A - B E TA P R U N I
NG
MAX α=3
MIN α=3
MAX
MIN
A L P H A - B E TA P R U N I
NG
MAX α=3
MIN α=3
MAX
min_value(node, alpha, beta):
MIN
1: if end_state(node): return value(node)
2: v = +Inf
3: for each child in node.children():
4: v = min(v, max_value(child, alpha,
beta)) 5: beta = min(beta, v)
6: if alpha >= beta: return v 7:
return v
A L P H A - B E TA P R U N I
NG
MAX α=3
MIN α=3
MAX
min_value(node, alpha, beta):
MIN
1: if end_state(node): return
value(node) 2: v = +Inf
3: for each child in node.children():
min(v, max_value(child, alpha, beta))
4: v =2
min(beta, v)
5: beta 3= 2 >= beta:
if alpha 2 return v
6: return v
A L P H A - B E TA P R U N I
NG
MAX α=3
MIN α=3
MAX
MIN
Summary
• alpha-beta algorithm does the same calculation
as minimax, and is more efficient since it prunes
irrelevant branches.
• To perform good and efficient search do the
following:
• select good search method/technique
• provide info/heuristic if possible
• apply prune irrelevant branches
Exercise
• Consider the following tree representing playing
• between hellen and stavros
Exercise
i) Apply the alpha-beta pruning algorithm to it and show the
search tree that would be built by this algorithm.
ii) How many nodes did you not have to visit with alpha-beta
pruning when compared to the full MIN-MAX search?