You are on page 1of 21

Fundamentals of Ar tificial Intelligence

A* Search
• Greedy search minimizes the estimated
cost h(n) from the node n to the goal. So, it
reduces considerably the search cost. But it
is neither optimal nor complete
• Uniform-cost search minimizes the cost g(n)
from the initial state to the node n. It is
optimal and complete. But, generally it is
not efficient

1
Fundamentals of Ar tificial Intelligence

A* Search
• A* combines the two algorithms in order to minimize
the total cost f(n) through the node n
• Evaluation function f(n) = g(n) + h(n)
– g(n) = cost so far to reach n

– h(n) = estimated cost to goal from n

– f(n) = estimated total cost of path through n to goal

2
Fundamentals of Ar tificial Intelligence

A* Search
s
g(n): actual
cost
Heuristic f(n)
n
f(n)= g(n) + h(n)
h(n): estimated
cost
g

3
Fundamentals of Ar tificial Intelligence

A* Search: Example

8 S
1 5 8

9 A 4 B 3 C
3 9
7 4 5
11 D 10 E 0 G

4
Fundamentals of Ar tificial Intelligence

A* Search: Example
Stage 0: Starting from node S
f(s) = g(s) + h(s) = 0 + 8 = 8

8 S

5
Fundamentals of Ar tificial Intelligence

A* Search: Example

Stage 1: Selecting node B


f(A) = 1 + 8 = 10
f(B) = 5 + 4 = 9 8 S
1 5 8
f(C) = 8 + 3 = 11

10 A 9 B 11 C

6
Fundamentals of Ar tificial Intelligence

A* Search: Example
Stage 2: Selecting node G (the goal)
f(G) = (5 + 4) + 0 = 9
8 S
1 5 8

8 A 4 B 3 C
4
9 G

7
Fundamentals of Ar tificial Intelligence

A* Search: Properties
• Complete: Yes

• Time: exponential

• Space: keeps all the nodes in memory

• Optimal: Yes

8
Fundamentals of Ar tificial Intelligence

Admissible Heuristic Functions

Admissible heuristic function is the one


that does not overestimate the actual
cost of the search

h(n) <= actual path-cost

9
Fundamentals of Ar tificial Intelligence

Admissible Heuristics: Example


• Problem of the 8-puzzle
• There are 9! = 362880 possible states
• Because of such huge number state space
search, we need to use heuristics

1 2 3 1 2 3
8 6 8 4
7 5 4 7 6 5
Initial state Final state

10
Fundamentals of Ar tificial Intelligence

Admissible Heuristic: Example


• Two different heuristics (h1 and h2) will be
used for the 8-puzzle problem namely:

– h1(n) = number of tiles badly placed

– h2(n) = total Manhattan distance

11
Fundamentals of Ar tificial Intelligence

Admissible Heuristic: Example


• h1(n) = number of tiles badly placed
• For each current sate, we count the number of tiles
that are not in the good place (required in the final
state)
h1(n) = 3
1 2 3
Because tiles 8 6
4, 5, and 6 are 7 5 4
not in a good
place

12
Fundamentals of Ar tificial Intelligence

Admissible Heuristic: Example


• h2(n) = total Manhattan distance
• Manhattan distance = the sum of the distances of
tiles from their goal position

h2(n) = 18 5 4
6 1 8
h1(n) = 2 + 3 + 3 + 2 + 4 + 2
+ 0 + 2 = 18 7 3 2

13
Fundamentals of Ar tificial Intelligence

Dominance

h1 and h2 are two heuristics

If h2(n) >= h1(n) for all n (both heuristics


are admissible) then h2 dominates h1
and it is better for the search

14
Fundamentals of Ar tificial Intelligence

Dominance
• Comparison of the search cost and the
effective branching factor (d) for the Iterative
Deepening Search (IDS) and A* algorithms with
heuristics h1, h2
d IDS A*(h1) A*(h2)
14 3473941 539 113
24 Too 39135 1641
many
The numbers in red represents number of visited
nodes for the 8-puzzle problem

15
Fundamentals of Ar tificial Intelligence

8-Puzzle Example
• To solve this problem, we use the heuristic h1
(number of tiles badly placed)

• This example will show you the steps required


to reach the goal

16
Fundamentals of Ar tificial Intelligence

8-Puzzle Example

Step 0

1 2 3 1 2 3

8 6 8 4

7 5 4 7 6 5

Initial state Final state

17
Fundamentals of Ar tificial Intelligence

8-Puzzle Example

Step 1

1 2 3

h(n) = 3 8 6

7 5 4

18
Fundamentals of Ar tificial Intelligence

8-Puzzle Example
Step 2

1 2 3 h(n) = 3
8 6
7 5 4
h(n) = 2 h(n) = 4
h(n) = 3
1 2 3 1 2
1 2 3
8 6 4 8 6 3
8 6
7 5 7 5 4
7 5 4

19
Fundamentals of Ar tificial Intelligence

8-Puzzle Example
Step 3 1 2 3 h(n) = 3
8 6
7 5 4
h(n) = 2 h(n) = 4
h(n) = 3
1 2 3 1 2
1 2 3
8 6 4 8 6 3
8 6
7 5 7 5 4
7 5 4
h(n) = 3 h(n) = 1
1 2 3 1 2 3
8 6 8 6 4
7 5 4 7 5

20
1 2 3 h(n)Fundamentals
=3 of Ar tificial Intelligence
Step 4
8 6
7 5 4

h(n) = 3 h(n) = 2 h(n) = 4


1 2 3 1 2
1 2 3
8 6 4 8 6 3
8 6
7 5 7 5 4
7 5 4
h(n) = 3 h(n) = 1
1 2 3 1 2 3
8 6 8 6 4
7 5 4 7 5
h(n) = 0 h(n) = 2
h(n) = 2 1 2 3 1 2 3 1 2 3
8 6 4 8 4 8 6 4
7 5 7 6 5 7 5

21

You might also like