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

Artificial Intelligence

CSAL3243
Dr. Anam Nazir

1
INFORMED SEARCH

Chapter 3, Section 3.5


Informed (Heuristic) search: search is guided by
an evaluation function: Greedy best-first, A*

2
Using problem specific knowledge to aid
searching
• With knowledge, one can search the state space as
if he was given “hints” when exploring a maze.
– Heuristic information in search = Hints
• Leads to dramatic speed up in efficiency.

B C D E
Search only in
F G H I J
this subtree!!
K L M N

O 3
More formally, why heuristic functions work?
• Previous: at most b choices at each node
and a depth of d at the goal node
– An uninformed search algorithm would have to,
in the worst case, search around O(bd) nodes
before finding a solution (Exponential Time
Complexity).

• Heuristics improve the efficiency of search


algorithms by reducing the effective
branching factor from b to (ideally) a lower
constant b* such that
1 b* b 4
Evaluation Function
• An evaluation function gives an estimation on the
“cost” of node in a tree/graph
• So that the node with the least cost among all
possible choices can be selected for expansion first
• Three approaches to define

5
Approach 1
• measures the value of the current state (i.e. its
“goodness”)
• Usually the case when solving optimization
problems
– Finding a state such that the value of the heuristic
function is optimized
• Often, in these cases, f could be a weighted sum of
a set of component values:
– N-Queens
• Example: the number of queens under attack …
– Used in Local search (we’ll study it later)
6
Approach 2
• Evaluation function measures the
estimated cost of getting to the goal
from the current state:


• where
• an estimate of the cost to get from state
to a goal state

7
Example 8–Puzzle tree

8
Possible heuristics for 8–Puzzle
• h1: The number of misplaced tiles (squares with
number).
• h2: The sum of the distances of the tiles from their
goal positions.

9
h1: The number of misplaced tiles (not including the blank)

• Notation:
• h(current state) = 1
• Because this state has one Y.

• Only “8” is misplaced


• So the heuristic function evaluates to 1.
• The heuristic is telling us, that it thinks a solution might be
available in just 1 more move.
10
h2: The sum of the distances of tiles from goal positions

Notation: , h(current state) = 8 11


Approach 3
• Evaluation function measures the estimated cost
of getting to the goal state from the current state
and the cost of the existing path to it:

where
an exact cost to get to (from initial state)
an estimate of the cost to get from state to a
goal state

12
Summary of Evaluation / Heuristic Function
• Evaluation function f(n) = g(n) + h(n)
– g(n) = exact cost so far to reach n
– h(n) = estimated cost to goal from n
– f(n) = estimated total cost of cheapest
path through n to goal

• Special cases:
– Greedy (best-first) Search:
– A* Search:
How it works?
• Idea: use heuristic function for each node
– that estimates desirability
• Expands most desirable unexpanded node
• Implementation:
– fringe is a queue sorted in order of desirability
• Special cases:
– Greedy (best-first) Search (informed)
– A* Search (informed)

14
Informed Search Strategies

Greedy Best First Search


eval-fn:
Greedy Search
State Heuristic: h(n)
Start
A A 366
118 75
B 374
140 B
C C 329
111
E D 244
D 80 99
E 253

G F F 178
G 193
97
H 98
H 211
I 0
101
I
Goal f(n) = h (n) = straight-line distance heuristic
16
Greedy Search
State Heuristic: h(n)
Start
A A 366
118 75
B 374
140 B
C C 329
111
E D 244
D 80 99
E 253

G F F 178
G 193
97
H 98
H 211
I 0
101
I
Goal f(n) = h (n) = straight-line distance heuristic
17
Greedy Search
State Heuristic: h(n)
Start
A A 366
118 75
B 374
140 B
C C 329
111
E D 244
D 80 99
E 253

G F F 178
G 193
97
H 98
H 211
I 0
101
I
Goal f(n) = h (n) = straight-line distance heuristic
18
Greedy Search
State Heuristic: h(n)
Start
A A 366
118 75
B 374
140 B
C C 329
111
E D 244
D 80 99
E 253

G F F 178
G 193
97
H 98
H 211
I 0
101
I
Goal f(n) = h (n) = straight-line distance heuristic
19
Greedy Search
State Heuristic: h(n)
Start
A A 366
118 75
B 374
140 B
C C 329
111
E D 244
D 80 99
E 253

G F F 178
G 193
97
H 98
H 211
I 0
101
I
Goal f(n) = h (n) = straight-line distance heuristic
20
Greedy Search
State Heuristic: h(n)
Start
A A 366
118 75
B 374
140 B
C C 329
111
E D 244
D 80 99
E 253

G F F 178
G 193
97
H 98
H 211
I 0
101
I
Goal f(n) = h (n) = straight-line distance heuristic
21
Greedy Search
State Heuristic: h(n)
Start
A A 366
118 75
B 374
140 B
C C 329
111
E D 244
D 80 99
E 253

G F F 178
G 193
97
H 98
H 211
I 0
101
I
Goal f(n) = h (n) = straight-line distance heuristic
22
Greedy Search
State Heuristic: h(n)
Start
A A 366
118 75
B 374
140 B
C C 329
111
E D 244
D 80 99
E 253

G F F 178
G 193
97
H 98
H 211
I 0
101
I
Goal f(n) = h (n) = straight-line distance heuristic
23
Greedy Search
State Heuristic: h(n)
Start
A A 366
118 75
B 374
140 B
C C 329
111
E D 244
D 80 99
E 253

G F F 178
G 193
97
H 98
H 211
I 0
101
I
Goal f(n) = h (n) = straight-line distance heuristic
24
Greedy Search
State Heuristic: h(n)
Start
A A 366
118 75
B 374
140 B
C C 329
111
E D 244
D 80 99
E 253

G F F 178
G 193
97
H 98
H 211
I 0
101
I
Goal f(n) = h (n) = straight-line distance heuristic
25
Greedy Search: Tree Search

Start
A

26
Greedy Search: Tree Search

Start
A 75
118

[329] 140 [374] B


C

[253] E

27
Greedy Search: Tree Search

Start
A 75
118

[329] 140 [374] B


C

[253] E
80 99

[193] [178]
G F
[366] A

28
Greedy Search: Tree Search

Start
A 75
118

[329] 140 [374] B


C

[253] E
80 99

[193] [178]
G F
[366] A
211

[253] I [0]
E

Goal

29
Greedy Search: Tree Search

Start
A 75
118

[329] 140 [374] B


C

[253] E
80 99

[193] [178]
G F
[366] A
211

[253] I [0]
E
Path cost(A-E-F-I) = 253 + 178 + 0 = 431
Goal
dist(A-E-F-I) = 140 + 99 + 211 = 450
30
Greedy Search: Optimal?
State Heuristic: h(n)
Start
A A 366
118 75
B 374
140 B
C C 329
111
E D 244
D 80 99
E 253

G F F 178
G 193
97
H 98
H 211
I 0
101
I
Goal f(n) = h (n) = straight-line distance heuristic
Not optimal! dist(A-E-G-H-I) =140+80+97+101=418 31
Greedy Search: Complete ?
State Heuristic: h(n)
Start
A A 366
118 75
B 374
140 B
C ** C 250
111
E D 244
D 80 99
E 253

G F F 178
G 193
97
H 98
H 211
I 0
101
I
Goal f(n) = h (n) = straight-line distance heuristic
32
Greedy Search: Tree Search

Start
A

33
Greedy Search: Tree Search

Start
A 75
118

[250] 140 [374] B


C

[253] E

34
Greedy Search: Tree Search

Start
A 75
118

[250] 140 [374] B


C

[253] E
111
[244] D

35
Greedy Search: Tree Search

Start
A 75
118

[250] 140 [374] B


C

[253] E
111
[244] D

Infinite Branch !
[250] C

36
Greedy Search: Tree Search

Start
A 75
118

[250] 140 [374] B


C

[253] E
111
[244] D

Infinite Branch !
[250] C

[244] D

37
Greedy Search: Tree Search

Start
A 75
118

[250] 140 [374] B


C

[253] E
111
[244] D

Infinite Branch !
[250] C
Even Incomplete in finite state
space much like depth first search
[244] D (Complete in finite space with
repeated-state checking) 38
Greedy Search: Time and Space Complexity ?

Start
A
118 75 • Greedy search is not optimal.
140 B
C • Greedy search is incomplete
111
E
D 80 99 • In the worst case, the Time and
Space Complexity of Greedy
G F
Search are both O(bm)
97
Where b is the branching factor and m
H 211 the maximum path length
101
I
Goal
39
Informed Search Strategies

A* Search
eval-fn:
A* (A Star)
• Although Greedy Search can considerably cut the
search time (efficient), it is neither optimal nor
complete.
• Uniform Cost Search minimizes the cost from the
initial state to . UCS is optimal and complete but
not efficient.
• New Strategy: Combine Greedy Search and UCS to
get an efficient algorithm which is complete and
optimal.
41
A* (A Star)
• A* uses a heuristic function which combines g(n)
and h(n):
• g(n) is the exact cost to reach node n from the
initial state. Cost so far up to node n.
• h(n) is an estimation of the remaining cost to
reach the goal.

42
A* (A Star)

g(n)

f(n) = g(n)+h(n) n

h(n)

43
A* Search
State Heuristic: h(n)
Start
A A 366
118 75
B 374
140 B
C C 329
111
E D 244
D 80 99
E 253

G F F 178
G 193
97
H 98
H 211
I 0
101
I
Goal f(n) = g(n) + h (n)
g(n): is the exact cost to reach node n from the initial state. 44
A* Search: Tree Search

A Start

45
A* Search: Tree Search

A Start

118 75
140

E [140+253=393] B [75+374=449]
[118+329=447] C

46
A* Search: Tree Search

A Start

118 75
140

E [140+253=393] B [75+374=449]
[118+329=447] C
80 99

G F [239+178=417]
[220+193=413]

47
A* Search: Tree Search

A Start

118 75
140

E [140+253=393] B [75+374=449]
[118+329=447] C
80 99

G F [239+178=417]
[220+193=413]
97
[317+98=415] H

48
A* Search: Tree Search

A Start

118 75
140

E [140+253=393] B [75+374=449]
[118+329=447] C
80 99

G F [239+178=417]
[220+193=413]
97
[317+98=415] H

101
Goal I [418+0=418]

49
A* Search: Tree Search

A Start

118 75
140

E [140+253=393] B [75+374=449]
[118+329=447] C
80 99

G F [239+178=417]
[220+193=413]
97 211
[317+98=415] H I [450+0=450]

101
Goal I [418+0=418]

50
A* Search: Tree Search

A Start

118 75
140

E [140+253=393] B [75+374=449]
[118+329=447] C
80 99

G F [239+178=417]
[220+193=413]
97 211
[317+98=415] H I [450+0=450]

101
Goal I [418+0=418]

51
A* Search: Tree Search

A Start

118 75
140

E [140+253=393] B [75+374=449]
[118+329=447] C
80 99

G F [239+178=417]
[220+193=413]
97 211
[317+98=415] H I [450+0=450]

101
Goal I [418+0=418]

dist(A-E-G-H-I) =140+80+97+101=418 52
A* with h() not Admissible
If h() overestimates the cost to reach
the goal state
Admissible Heuristic Functions
• A heuristic is admissible if never overestimates the
cost to reach the goal.
– Admissible heuristics are “optimistic” approximation

• Mathematically h is admissible if
For all n,
Where
• n is the node, h is heuristic
• h(n) is the cost indicated by h to reach goal from n
• C(n) is actual cost to reach a goal from n
54
Admissible heuristics Examples
• hSLD(n) Straight line distance heuristic
– Because never overestimate the actual road distance.

• Number of misplaced tiles in n-puzzle problem



Admissible Heuristic Functions
• Evaluation function is admissible if is admissible.
• However, is the exact cost to reach node n from
the initial state.
• Therefore, never over-estimate the true cost to
reach the goal state through node n.

• Theorem: If h(n) is admissible, A* using TREE-


SEARCH is optimal

56
Consistent Heuristic
• The admissible heuristic h is consistent if for every node N
and every successor N’ of N:

(triangular inequality)
N
c(N,N’)
• A consistent heuristic is admissible. N’ h(N)

h(N’)

• But an admissible heuristic is not always consistent

57
A* Search: if h not admissible
State Heuristic: h(n)
Start
A A 366
118 75
B 374
140 B
C C 329
111
E D 244
D 80 99
E 253

G F F 178
G 193
97
H 138
H 211
I 0
101
I
f(n) = g(n) + h (n) – (H-I) Overestimated
Goal
g(n): is the exact cost to reach node n from the initial state. 58
A* Search: Tree Search

A Start

59
A* Search: Tree Search

A Start

118 75
140

E [393] B [449]
[447] C

60
A* Search: Tree Search

A Start

118 75
140

E [393] B [449]
[447] C
80 99

[413] G F [417]

61
A* Search: Tree Search

A Start

118 75
140

E [393] B [449]
[447] C
80 99

[413] G F [417]

97
[455] H

62
A* Search: Tree Search

A Start

118 75
140

E [393] B [449]
[447] C
80 99

[413] G F [417]

97 211

[455] H Goal I [450]

63
A* Search: Tree Search

A Start

118 75
140

E [393] B [449]
[447] C
80 99

[473] [413] G F [417]


D

97 211

[455] H Goal I [450]

64
A* Search: Tree Search

A Start

118 75
140

E [393] B [449]
[447] C
80 99

[473] [413] G F [417]


D

97 211

[455] H Goal I [450]

65
A* Search: Tree Search

A Start

118 75
140

E [393] B [449]
[447] C
80 99

[473] [413] G F [417]


D

97 211

[455] H Goal I [450]

66
A* Search: Tree Search

A Start

118 75
140

E [393] B [449]
[447] C
80 99

[473] [413] G F [417]


D

97 211

[455] H Goal I [450]

A* not optimal !!!


dist(A-E-F-I) =140+99+211=450
When heuristic h is not admissible 67
A* Algorithm
1. Search queue Q is empty.
2. Place the start state s in Q with f value h(s).
3. If Q is empty, return failure.
4. Take node n from Q with lowest f value.
(Keep Q sorted by f values and pick the first element).
5. If n is a goal node, stop and return solution.
6. Generate successors of node n.
7. For each successor n’ of n do:
a) Compute f(n’) = g(n) + h(n’).
b) If n’ is new (never generated before), add n’ to Q.
c) If node n’ is already in Q with a higher f value, replace it with current f(n’)
and place it in sorted order in Q.
End for
8. Go back to step 3. 68
Properties of A*
• Complete? Yes
– (unless there are infinitely many nodes with f ≤ f(G) )

• Time? Exponential
– It depends on the quality of heuristic but is still exponential.

• Space? Keeps all nodes in memory


– O(bd) space complexity,
– But an iterative deepening version is possible (IDA*).

• Optimal? Yes
– A* is optimal if heuristic h is admissible (optimal).
Conclusions
• Frustration with uninformed search led to the idea of using
domain specific knowledge in a search
• so that one can intelligently explore only the relevant part of
the search space that has a good chance of containing the
goal state.
• These new techniques are called informed (heuristic) search
strategies.

• Even though heuristics improve the performance of


informed search algorithms, they are still time consuming
especially for large size instances.
70

You might also like