Games - Elearning UPN JATIM

PENGANTAR
INTELIJENSIA BUATAN
(64A614)
Kuliah 5 : Game Playing
Fakultas Teknik Jurusan Informatika
Universitas Surabaya
1
Outline


Game Playing
Game Trees
Minimax
 Alpha-Beta Pruning

2
Games
Some games:





Ball games
Card games
Board games
Computer games
...
3
Human Game Playing
4
Computer Game Playing
Garry Kasparov and Deep Blue, 1997
5
Game-Playing Agent
sensors
?
environment
agent
Environment
actuators
6
Type of games
7
Typical Case




2-person game
Players alternate moves
Zero-sum: one player’s loss is the other’s
gain
Perfect information: both players have
access to complete information about the
state of the game. No information is hidden
from either player.
8
Typical Case



No chance (e.g., using dice) involved
Examples: Tic-Tac-Toe, Checkers, Chess, Go,
Nim, Othello
Not : Bridge, Solitaire, Backgammon, ...
9
Two-player games

A game formulated as a search problem:




Initial state: board position and turn
Successor function(Action) : definition of legal moves
Terminal state:
conditions for when game is over
Utility function:
a numeric value that describes the
outcome of the game. E.g., -1, 0, 1 for loss, draw, win.
(payoff function)
10
The Minimax Algorithm



Basic idea : Choose move with highest minimax
value = Best achievable payoff against best play.
Algorithm :
1. Generate game tree completely.
2. Determine utility of each terminal state.
3. Propagate the utility values upward in the tree by
applying MIN and MAX operators on the nodes
in the current level.
4. At the root node use minimax decision to select
the move with the max (of the min) utility value.
Steps 2 and 3 in the algorithm assume that the
opponent will play perfectly.
11
Tic-Tac-Toe
12
13
Generate Game Tree
14
Generate Game Tree
x
x
x
x
15
Generate Game Tree
x
x o
x
o
x
o
16
Generate Game Tree
x
1 ply
1 move
x o
x
o
x
o
17
A subtree
x o x
ox
o
win
lose
x o x
x ox
o
x o x
x ox
o
o
x o x
x ox
o x o
x o x
ox
x
o
x o x x o x
x ox o ox
oo x
o
x o x
o ox
x xo
x o x
ox
x o
x o x
ox
x oo
x o x
o ox
x o
x o x
o ox
x x o
draw
x o x
ox
o x o
x o x
x ox
o x o
18
What is a good move?
x o x
ox
o
win
lose
x o x
x ox
o
x o x
x ox
o
o
x o x
x ox
o x o
x o x
ox
x
o
x o x x o x
x ox o ox
oo x
o
x o x
o ox
x xo
x o x
ox
x o
x o x
ox
x oo
x o x
o ox
x o
x o x
o ox
x x o
draw
x o x
ox
o x o
x o x
x ox
o x o
19
Minimax
3
12
8 2 4 6
14
5
2
•Minimize opponent’s chance
•Maximize your chance
20
Minimax
3
2
2
MIN
3
12
8 2 4 6
14
5
2
•Minimize opponent’s chance
•Maximize your chance
21
Minimax
3
MAX
3
2
2
MIN
3
12
8 2 4 6
14
5
2
•Minimize opponent’s chance
•Maximize your chance
22
Minimax
3
MAX
3
2
2
MIN
3
12
8 2 4 6
14
5
2
•Minimize opponent’s chance
•Maximize your chance
23
Minimax = Maximum of the minimum
1st ply
2nd ply
24
MAX
1
Select this move
MIN
MAX
4
1
4
B
D
-5
A
-3
1
E
-5
= terminal position
2
1
-7
= agent
C
F
-3
2
G
-3
= opponent
-8
25
MiniMax search on Tic-Tac-Toe

Evaluation function Eval(n) for A



infinity if n is a win state for A (Max)
-- infinity if n is a win state for B (Min)
(# of 3-moves for A) -- (# of 3-moves for
B) a 3-move is an open row, column,
diagonal
26
MiniMax search on Tic-Tac-Toe
A is X
Eval(s) = 6 - 4
27
Tic-Tac-Toe MiniMax search,
d=2
28
Tic-Tac-Toe MiniMax search,
d=4
29
Tic-Tac-Toe MiniMax search,
d=6
30
Partial Game Tree for Tic-Tac-Toe
• f(n) = +1 if the position is a
win for X.
• f(n) = -1 if the position is a
win for O.
• f(n) = 0 if the position is a
31
draw.
Properties of minimax





Complete? Yes (if tree is finite)
Optimal? Yes (against an optimal opponent)
Time complexity? O(bm)
Space complexity? O(bm) (depth-first exploration)
For chess, b ≈ 35, m ≈100 for "reasonable" games
 exact solution completely infeasible
32
Minimax Exercise
0 5 -3 3 3 -3 0 2 -2 3 5 2 5 -5 0 1 5 1 -3 0 -5 5 -3 3 2
33
Alpha-Beta Prunning



A disadvantage of MinMax search is that it
has to expand every node in the subtree to
depth m. Can we make a correct MinMax
decision without looking at every node?
We want to prune the tree: stop exploring
subtrees for which their value will not
influence the final MinMax root decision
Observation: all nodes whose values are
greater (smaller) that the current minimum
(maximum) need not be explored !
34
Do We Have To Do All That Work ?
MAX
MIN
3
12
8
35
Do We Have To Do All That Work ?
3
MAX
3
MIN
3
12
8
36
Do We Have To Do All That Work ?
3
MAX
3
2
MIN
3
12
8 2
Since 2 is smaller than 3, then there is no need for
further search
37
Do We Have To Do All That Work ?
3
MAX
3
X
2
MIN
3
12
8
14
5
2
38
Alpha-Beta Pruning


Generally applied optimization on Mini-max.
Instead of:




first creating the entire tree (up to depth-level)
then doing all propagation
Interleave the generation of the tree and the
propagation of values.
Point:

some of the obtained values in the tree will provide
information that other (non-generated) parts are
redundant and do not need to be generated.
39
Alpha-Beta Pruning
MAX
MIN
MAX
2
2
2
=2
1
5
1
40
- The (temporary) values at MAX-nodes are ALPHA-values
- The (temporary) values at MIN-nodes are BETA-values
MAX
MIN
MAX
Alpha-value
2
2
2
=2
1
5
1
Beta-value
41
Alpha-Beta pruning Example
42
Alpha-Beta pruning Example
43
Alpha-Beta pruning Example
44
Alpha-Beta pruning Example
45
Alpha-Beta pruning Example
46
Properties of Alpha-Beta pruning



Pruning does not affect final result.
Good move ordering improves effectiveness of
pruning.
With "perfect ordering," time complexity = O(bm/2)
 doubles depth of search
47
Alpha-Beta pruning Example
6
MAX
MIN
6
48
Alpha-Beta pruning Example
6
MAX
MIN
2
6
6
12
8
2
49
Alpha-Beta pruning Example
6
MAX
MIN
6
12
5
2
6
8
2
5
50
Alpha-Beta pruning Example
6
MAX
Selected move
MIN
6
12
5
2
6
8
2
5
51
6
A
MAX
6
B
2
C
beta
cutoff
MIN
6
D
>=8
E
alpha
cutoff
2
F
G
MAX
H
I
J
6
5
8
= agent
K
L
M
2
1
= opponent
52
Alpha-Beta pruning Example
 4 16
8 6
 5 23
= 4 15
8 2
=8 5
9 8
= 5 30
 2 10  1 18
 4 12  3 20
= 4 14 = 5 22
8 7 3 9 1 6 2 4 1
1 3 4 7
 5 31 = 5 39
9 11 13
MAX
 3 38
 1 33
 2 35
 3 25
 9 27  6 29 = 3 37
1 3 5 3 9 2 6 5 2
17 19 21 24 26
MIN
28
MAX
1 2 3 9 7 2 8 6 4
32 34 36
11 static evaluations saved !! 53
Example of a perfectly ordered tree
MAX
21
21
21
24
12
27
21 20 19 24 23 22 27 26 25
12
15
3
18
12 11 10 15 14 13 18 17 16
3
6
MIN
9
MAX
3 2 1 6 5 4 9 8 7
54
• Minimax
Exercise
• Alpha Beta Pruning
Max
A
E
Min
C
B
F
D
H
G
I
J
Max
K
Min
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
2
3
8
5
7
6
0
1
5
2
8
4
10
2
55