Ai Games and Searching: CHEN, Yuanyuan Chenyuanyuan@scu - Edu.cn
Ai Games and Searching: CHEN, Yuanyuan Chenyuanyuan@scu - Edu.cn
CHEN, Yuanyuan
[email protected]
OUTLINE
AI GAMES
Brute searching
Herustic searching
Alpha-beta pruning
ARE YOU GOOD AT PLAYING GAMES?
WHAT IS GAME?
Deterministic Chance
Perfect information Chess, Checkers, Go, Backgammon, Monopoly
Othello
Imperfect information ??? Bridge, Poker, Scrabble,
Nuclear war
GAMES AS SEARCH PROBLEM
S
A SIMPLE EXAMPLE:
THE 8-PUZZLE
Start Goal
2 5 1 1 2 3
4 8 4 5 6
7 3 6 HOW 7 8
?
8 1 3
2 6 4 1 3 5 1 2 3
7 5 4 2 8 4
8 7 6 7 6 5
Searching
Goal
1 2 1 5 2 1 2
4 5 3 4 3 4 5 3
7 8 6 7 8 6 7 8 6
1 2 4 1 2 1 5 2 1 5 2 1 5 2 1 2 1 2 1 2 3
4 5 3 5 3 4 3 4 8 3 4 3 4 5 3 4 5 3 4 5
7 8 6 7 8 6 7 8 6 7 6 7 8 6 7 8 6 7 8 6 7 8 6
……
Goal
1 2 1 5 2 1 2
4 5 3 4 3 4 5 3
7 8 6 7 8 6 7 8 6
4 1 2 1 5 2 1 5 2 1 5 2 1 2 3
5 3 4 3 4 8 3 4 3 4 5
7 8 6 7 8 6 7 6 7 8 6 7 8 6
……
DEFINITIONS:
State: Description of the state of the world in which the search agent find itself.
Starting state: The initial state in which the search agent is started.
Goal state: If the agent reaches a goal state, then it terminates and outputs a sol
ution (if desired).
Actions: All of the agents allowed actions.
Solution: The path in the search tree from the starting state to the goal state.
Cost function: Assigns a cost value to every action. Necessary for finding a cos
t-optimal solution.
State space: Set of all states.
APPLIED TO THE 8-PUZZLE
State: 3×3 matrix S with the values 1, 2, 3, 4, 5, 6, 7, 8 (once each) and one
empty square.
Starting state: An arbitrary state.
Goal state: An arbitrary state.
Actions: Movement of the empty square Sij to the left, right, up, down.
Cost function: The constant function 1, since all actions have equal cost.
State space: The state space is degenerate in domains that are mutually unr
eachable.
SEARCH STRATEGIES
B C D
E F G H I J
I J K L M N O P Q R S T
UNINFORMED: BREADTH FIR
ST SEARCH
A
B C D
E F G H I J
I J K L M N O P Q R S T
UNINFORMED: BREADTH FIR
ST SEARCH
A
B C D
E F G H I J
I J K L M N O P Q R S T
UNINFORMED: BREADTH FIR
ST SEARCH
A
B C D
E F G H I J
I J K L M N O P Q R S T
UNINFORMED: BREADTH FIR
ST SEARCH
A
B C D
E F G H I J
I J K L M N O P Q R S T
UNINFORMED: BREADTH FIR
ST SEARCH
1
A
2 3 4
B C D
5 6 7 8 9 10
E F G H I J
11 12 13 14 15 16 17 18 19 20 21 22
I J K L M N O P Q R S T
Goal
2 8 3 2 3 2 8 3 2 8 3
1 4 1 8 4 1 4 1 6 4
7 6 5 7 6 5
7 6 5 7 5
8 3 2 8 3 2 3 2 3 2 8 2 8 3 2 8 3 2 8 3
2 1 4 7 1 4 1 8 4 1 8 4 1 4 3 1 4 5 1 6 4 1 6 4
7 6 5 7 6 5
7 6 5 6 5 7 6 5 7 6 7 5 7 5
8 3 2 8 3 1 2 3 2 3 4 2 8 2 8 3 2 8 3 2 8 3
7 1 4 8 4 1 8 1 4 3 1 4 5 6 4 1 6
2 1 4
7 6 5 7 6 5
6 5 7 6 5 7 6 1 7 5 7 5 4
7 6 5
8 3 8 1 3 2 8 3 2 8 3 1 2 3 1 2 3
7 4 7 1 4 8 4 8 4 Computation time and memory space grow
2 1 4 2 4
7 6 5 7 6 5 exponentially with the depth of the tree.
6 1 5 6 5
7 6 5 7 6 5
DEPTH FIRST SEARCH
DEPTH FIRST SEARCH
B C D
E F G H I J
I J K L M N O P Q R S T
DEPTH FIRST SEARCH
B C D
E F G H I J
I J K L M N O P Q R S T
DEPTH FIRST SEARCH
B C D
E F G H I J
I J K L M N O P Q R S T
DEPTH FIRST SEARCH
B C D
E F G H I J
I J K L M N O P Q R S T
DEPTH FIRST SEARCH
B C D
E F G H I J
I J K L M N O P Q R S T
DEPTH FIRST SEARCH
B C D
E F G H I J
I J K L M N O P Q R S T
DEPTH FIRST SEARCH
1
A
2 3 4
B C D
5 6 11 12 17 18
E F G H I J
7 8 9 10 13 14 15 16 19 20 21 22
I J K L M N O P Q R S T
Goal
2 8 3 2 3 2 8 3 2 8 3
1 4 1 8 4 1 4 1 6 4
7 6 5 7 6 5
7 6 5 7 5
8 3 2 8 3 2 3 2 3
2 1 4 7 1 4 1 8 4 1 8 4
7 6 5 7 6 5
7 6 5 6 5
8 3 2 8 3 1 2 3
7 1 4 8 4
2 1 4
7 6 5
6 5
7 6 5
8 3 8 1 3 2 8 3 2 8 3 1 2 3
7 4 7 1 4 8 4
2 1 4 2 4
7 6 5
6 1 5 6 5
7 6 5 7 6 5
COMPARATION What is needed is an intelligent search !
DFS No No , or bds bd
Start Goal
2 8 3 1 2 3
1 4 8 4
7 6 5 7 6 5
2 1 2 5 1
4 5 8 4 3 8
7 3 6 7 6
If you are the player, which is your first move?
2 5 1 2 5 1
Why?
4 8 4 8
7 3 6 7 3 6
A* SEARCHING
start
2 8 3
First we define the cost function 1 4
7 6 5
g(s) = sum of accrued costs from the start to the current node,
2 3
1 8 4
we often use the depth of the node as the value of g(s). 7 6 5
1 2 3 1 2 3
8 4 8 4
7 6 5 7 6 5
current goal
HEURISTIC FUNCTION
Heuristic evaluation function estimates the cost of an optimal p
ath between a pair of states (e.g. a certain state and the goal stat
e) in a single-agent path-finding problem.
Start Goal
Example:
2 5 1 1 2 3
4 8 4 5 6
7 3 6 7 8
1
1 2
Search tree for the 8-puzzle 4 5 3 Initial state
7 8 6
2 3 4
1 2 1 5 2 1 2
4 5 3 4 3 4 5 3
7 8 6 7 8 6 7 8 6
E.g.
g(s1)=0 g(s3) = 1;
h(s1) =0+1+1+0+0+1+0+0+1=4; h(s3) = 0+1+0+0+1+0+0+1=3;
g(s2) = 1; g(s4) = 1;
h(s2)= 1+1+0+0+1+0+0+1=4 ; h(s4)= 0+0+0+0+1+0+0+1=2 ;
1 2 3
1
1 2
Search tree for the 8-puzzle 4 5 3 g=0
7 8 6 h=4
2 3 4
1 2 1 5 2 1 2
4 5 3
g=1 4 3
g=1 4 5 3
g=1
7 8 6
h=4 7 8 6
h=3 7 8 6
h=2
E.g.
g(s1)=0 g(s3) = 1;
h(s1) =0+1+1+0+0+1+0+0+1=4; h(s3) = 0+1+0+0+1+0+0+1=3;
g(s2) = 1; g(s4) = 1;
h(s2)= 1+1+0+0+1+0+0+1=4 ; h(s4)= 0+0+0+0+1+0+0+1=2 ;
1 2 3
1
1 2
Search tree for the 8-puzzle 4 5 3 g=0
7 8 6 h=4
2 3 4
1 2 1 5 2 1 2
4 5 3
g=1 4 3
g=1 4 5 3
g=1
7 8 6
h=3 7 8 6
h=3 7 8 6
h=2
1 2 3
4 5
g=2
7 8 6
h=1
1 2 3
4 5 6 g=3
7 8 h=0
WHY SEARCH CAN DO WITH SI
NGLE PLAYER GAME?
A static world runs like the tree bring forth to us.
You have enough time to try each possibility and needn’t to worry about
MIN
MAX
MIN
3 17 2 12 15 25 0 2 5 3 2 14
MINIMAX SEARCH
MAX
MIN
MAX
MIN 3 2
1
5 0 2 3 2
3 17 2 12 15 25 0 2 5 3 2 14
MINIMAX SEARCH
MAX
MIN
MAX 3 15 3 2
MIN 3 2
1
5 0 2 3 2
3 17 2 12 15 25 0 2 5 3 2 14
MINIMAX SEARCH
MAX
MIN 3 2
MAX 3 15 3 2
MIN 3 2
1
5 0 2 3 2
3 17 2 12 15 25 0 2 5 3 2 14
MINIMAX SEARCH
MAX 3
MIN 3 2
MAX 3 15 3 2
MIN 3 2
1
5 0 2 3 2
3 17 2 12 15 25 0 2 5 3 2 14
ALPHA-BETA PRUNING
ALGORITHM:
At every leaf node the evaluation is calculated.
For every maximum node the current largest child value is save
d in α.
For every minimum node the current smallest child value is sav
ed in β.
If at a minimum node k the current value β ≤ α, then the search
under k can end. Here α is the largest value of a maximum node
in the path from the root to k.
If at a maximum node l the current value α ≥ β, then the search
under l can end. Here β is the smallest value of a minimum node
in the path from the root to l.
ALPHA-BETA PRUNING
MAX
MIN
2
MAX 15 3 2
MIN 2
1
5 0 2 3 2
3 17 2 12 15 25 0 2 5 3 2 14
MIN
2
MAX 15 3 2
MIN 2
1
5 0 2 3 2
3 17 2 12 15 25 0 2 5 3 2 14
MIN
2
MAX 15 3 2
MIN 2
1
5 0 2 3 2
3 17 2 12 15 25 0 2 5 3 2 14
MIN
2
MAX 15 3 2
3 17 2 12 15 25 0 2 5 3 2 14
ALPHA-BETA PRUNING
MAX
MIN
2
MAX 3 15 3 2
3 17 2 12 15 25 0 2 5 3 2 14
ALPHA-BETA PRUNING
MAX
MIN
2
3
MIN 3 2
1
5 0 2 3 2
3 17 2 12 15 25 0 2 5 3 2 14
ALPHA-BETA PRUNING
MAX
MIN
2
3
MAX 3 15 3 2
MIN 3 2
1
5 0 2 3 2
3 17 2 12 15 25 0 2 5 3 2 14
ALPHA-BETA PRUNING
MAX
MIN
2
3
MAX 3 15 3 2
3 17 2 12 15 25 0 2 5 3 2 14
ALPHA-BETA PRUNING
MAX
MIN
3 2
3
MAX 3 15 3 2
MIN 3 2
1
5 0 2 3 2
3 17 2 12 15 25 0 2 5 3 2 14
ALPHA-BETA PRUNING
MAX
MIN 3 2
MAX 3 3 2
MIN 3 2 0 2 3 2
3 17 2 12 15 25 0 2 5 3 2 14
ALPHA-BETA PRUNING
MAX
MIN 3 2
MAX 3 15 3 2
If at a maximum node l the current value α ≥ β,
MIN 3 2
1
5 then
0 the search
2 under l can
3 end. Here2 β is the
smallest value of a minimum node in the path
from the root to l.
3 17 2 12 15 25 0 2 5 3 2 14
ALPHA-BETA PRUNING
MAX
MIN 3 2
MAX 3 15 3 2
MIN 3 2
1
5 0 2 3 2
3 17 2 12 15 25 0 2 5 3 2 14
ALPHA-BETA PRUNING
MAX
MIN 3 3
MAX 3 15 2
MIN 3 2
1
5 0 3 2
3 17 2 12 15 25 0 2 5 3 2 14
ALPHA-BETA PRUNING
MAX
MIN 3 3
MAX 3 15 2
MIN 3 2
1
5 0 2 3 2
3 17 2 12 15 25 0 2 5 3 2 14
ALPHA-BETA PRUNING
MAX
MIN 3 3
MAX 3 15 2 2
MIN 3 2
1
5 0 2 3 2
3 17 2 12 15 25 0 2 5 3 2 14
If at a minimum node k
the current value β ≤ α,
then the search under k
ALPHA-BETA PRUNING can end. Here α is the
largest value of a
maximum node in the
path from the root to k.
MAX
MIN 3 3 2
MAX 3 15 2 2
MIN 3 2
1
5 0 2 1 2
3 17 2 12 15 25 0 2 5 1 2 14
ALPHA-BETA PRUNING
MAX
MIN 3 3 2
MAX 3 15 2 2
MIN 3 2
1
5 0 2 1 2
3 17 2 12 15 25 0 2 5 1 2 14
If at a minimum node k
the current value β ≤ α,
then the search under k
ALPHA-BETA PRUNING can end. Here α is the
largest value of a
maximum node in the
path from the root to k.
MAX
MIN 3 3 2
MAX 3 15 2 2
MIN 3 2
1
5 0 2 1 2
3 17 2 12 15 25 0 2 5 1 2 14
If at a minimum node k
the current value β ≤ α,
then the search under k
ALPHA-BETA PRUNING can end. Here α is the
largest value of a
maximum node in the
path from the root to k.
MAX 3
MIN 3 3 2
MAX 3 15 2 2
MIN 3 2
1
5 0 2 1 2
3 17 2 12 15 25 0 2 5 1 2 14
HOW TO DESIGN THE EVALUATION
FUNCTION?
It is designed by yourself.
For example, for chess,
A linear evaluation function B(s) for positions, which could look like: