Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 69

CHAPTER 3

AI GAMES AND SEARCHING

CHEN, Yuanyuan
[email protected]
OUTLINE
 AI GAMES
 Brute searching
 Herustic searching
 Alpha-beta pruning
ARE YOU GOOD AT PLAYING GAMES?
WHAT IS GAME?

 Games have been part of the w


orld of artificial intelligence al
most from its inception. 
 Getting a computer to play a g
ame, such as chess, was believ
ed to require intelligence.
TYPE OF GAMES

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

HOW to find a path


from the initial state
Start Goal
to the goal ?
2 5 1 1 2 3
4 8 4 5 6
7 3 6 7 8
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

EXAMPLE: THE 8-PUZZLE 1


4
2
5
3
6
1 2 7 8
Searching tree for the 8-puzzle 4 5 3 Initial state
7 8 6

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

EXAMPLE: THE 8-PUZZLE 1


4
2
5
3
6
1 2 7 8
Search tree for the 8-puzzle 4 5 3 Initial state
7 8 6

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

 Uninformed, brute-force, or blind search


 Heuristic, informed, or directed search
 ……
UNINFORMED: BREADTH FIR
ST SEARCH
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
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

EXAMPLE: THE 8-PUZZLE 1


8
2 3
4
2 8 3
1 4 7 6 5
7 6 5

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

EXAMPLE: THE 8-PUZZLE 1


8
2 3
4
2 8 3
1 4 7 6 5
7 6 5

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 !

The search tree with breadth b and depth d


Completeness Optimal solution Computation time Memory use

BFS Yes Yes bd bd

DFS No No , or bds bd

 For non-trivial inference systems the state space is many orders


of magnitude bigger.
 what is needed is an intelligent search that only explores a tiny
fraction of the search space and find a solution there.
HEURISTIC SEARCH
He: “Dear, think of the fuel cost! I’ll
pluck one for you somewhere else.”
She: “No, I want that one over there!”

 Heuristics are problem-solving strategies which in many cases find a sol


ution faster than uninformed search.
 We humans successfully use heuristic processes for all kinds of things.
 The function HEURISTICSEARCH together with an evaluation and an a
dmissible heuristic function h is called A* algorithm.
HEURISTIC SEARCH

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

 h(s) estimate the actual cost from state s to the goal


2 3
1 8 4

 Heuristic evaluation function: f (s) = g(s) + h(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

h1(S) = number of numbers not in position


S: state
OTHER EXAMPLES
5 8 1 2 3
4 2 1 4 5 6
7 3 6 7 8

A certain state Goal state


 h1(S) = number of numbers not in position = 6
 h2(S) = sum of the Manhattan distances between state S and the goal state
= 2 + 3 + 0 + 1 + 3 + 0 + 3 + 1 = 13

What are the drawbacks of these two h(s), respectively?


The A algorithm is optimal. That is, it

A* ALGORITHM always find the solution with the lowest


total cost if the heuristic h is admissible.

1. Initialization: The open list and the closed list.


2. while the open list is not empty
a) find the node with the least f on the open list, call it "q"
b) pop q off the open list and generate q's successors
d) for each successor
i) if successor is the goal, stop search successor.
ii) if a node with the same position as successor is in the OPEN list
which has a lower f than successor, skip this successor.
iii) if a node with the same position as successor is in the CLOSED l
ist which has a lower f than successor, skip this successor otherwise, add
the node to the open list end
e) push q on the closed list end
1 2 3

EXAMPLE: THE 8-PUZZLE 4


7
5
8
6

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

EXAMPLE: THE 8-PUZZLE 4


7
5
8
6

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

EXAMPLE: THE 8-PUZZLE 4


7
5
8
6

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

another opponent win the game or change the situation.

 So the only thing to do is try systematically and try to save time.


TWO-PERSON GAME
(ADVERSARIAL SEARCH)
 In games with two players often called MIN an
d MAX usually MAX moves first, then they ta
ke turns
 MAX must find a strategy to find a winning st
ate no matter what MIN does
 MIN does the same or at least tries to prevent
MAX from winning
MINIMAX SEARCH
MAX

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

 At every leaf node the evaluation is calculated.


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

 At every leaf node the evaluation is calculated.


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

 At every leaf node the evaluation is calculated.


ALPHA-BETA PRUNING
 
MAX

MIN  
2
 

MAX 15 3 2

 For every minimum node the current smallest


 
child value is saved in β.
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
 

MAX 3 15 3 2

 For every minimum node the current smallest


 
child value is saved in β.
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  For every maximum


15 node the current
3 largest 2
child value is saved in α.
 

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

If at a minimum node k the current value β ≤ α,


   
then the search under k can end. Here α is the
MIN 3 2
1 largest0 value of a maximum
2
node
3
in the path
2
from
5
the root to k.

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:

 B(s) = a1 · material+a2 · pawn_ structure +a3 · king_safety

 +a4 · knight_in_center+a5 · bishop_diagonal_coverage+· · · ,

 where “material” is by far the most important feature and is calculated by


 material = material(own_team)−material(opponent)
 with
 material(team) = num_pawns(team) · 100+ num_knights(team) · 300
 + num_bishops(team) · 300+ num_rooks(team) · 500
 + num_queens(team) · 900
APPLICATIONS

Try to design an AI game using search method.

You might also like