Artificial Intelligence
CSAL3243
Dr. Muhammad Humayoun
Assistant Professor
University of Central Punjab, Lahore.
[email protected]
https://sites.google.com/a/ucp.edu.pk/ai/
INFORMED SEARCH
Chapter 3, Section 3.5
2
Informed (Heuristic) search: search is guided by
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.
3 A B C D E F G H I J K L O M N
Search only in
this subtree!!
4
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(b
d) 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
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 𝑓(𝑛)
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 …
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
Example 8–Puzzle tree
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.
10
h1: The number of misplaced tiles (not including the blank)
• 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.
• Notation: 𝑓(𝑛) = (𝑛)
• h(current state) = 1
11
h2: The sum of the distances of tiles from goal positions
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
:
12
𝑓(𝑛) = 𝑔(𝑛) + (𝑛)
where
𝑔(𝑛) = an exact cost to get to 𝑛 (from initial
state)
(𝑛) = an estimate of the cost to get from
state 𝑛 to a goal state
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:
– Uniform Cost Search
: 𝑓(𝑛) = 𝑔(𝑛)
– Greedy (best-first) 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:
– Uniform Cost Search (uninformed)
– Greedy (best-first) Search (informed)
– A* Search (informed)
Informed Search Strategies
Greedy Best First Search
Greedy Search
State Heuristic: h(n) A 366 B 374 C 329 D 244 E 253 F 178 G 193 H 98 I 0 16 A B D C E F I 99 211 G H 80 Start Goal 97 101 75 118 111f(n) = h (n) = straight-line distance heuristic
Greedy Search
State Heuristic: h(n) A 366 B 374 C 329 D 244 E 253 F 178 G 193 H 98 I 0 17 A B D C E F I 99 211 G H 80 Start Goal 97 101 75 118 111 140Greedy Search
State Heuristic: h(n) A 366 B 374 C 329 D 244 E 253 F 178 G 193 H 98 I 0 18 A B D C E F I 99 211 G H 80 Start Goal 97 101 75 118 111 140Greedy Search
State Heuristic: h(n) A 366 B 374 C 329 D 244 E 253 F 178 G 193 H 98 I 0 19 A B D C E F I 99 211 G H 80 Start Goal 97 101 75 118 111 140Greedy Search
State Heuristic: h(n) A 366 B 374 C 329 D 244 E 253 F 178 G 193 H 98 I 0 20 A B D C E F I 99 211 G H 80 Start Goal 97 101 75 118 111 140Greedy Search
State Heuristic: h(n) A 366 B 374 C 329 D 244 E 253 F 178 G 193 H 98 I 0 21 A B D C E F I 99 211 G H 80 Start Goal 97 101 75 118 111 140Greedy Search
State Heuristic: h(n) A 366 B 374 C 329 D 244 E 253 F 178 G 193 H 98 I 0 22 A B D C E F I 99 211 G H 80 Start Goal 97 101 75 118 111 140Greedy Search
State Heuristic: h(n) A 366 B 374 C 329 D 244 E 253 F 178 G 193 H 98 I 0 23 A B D C E F I 99 211 G H 80 Start Goal 97 101 75 118 111 140Greedy Search
State Heuristic: h(n) A 366 B 374 C 329 D 244 E 253 F 178 G 193 H 98 I 0 24 A B D C E F I 99 211 G H 80 Start Goal 97 101 75 118 111 140Greedy Search
State Heuristic: h(n) A 366 B 374 C 329 D 244 E 253 F 178 G 193 H 98 I 0 25 A B D C E F I 99 211 G H 80 Start Goal 97 101 75 118 111 140Greedy Search: Tree Search
26
Greedy Search: Tree Search
27 A B C E Start 75 118 140 [374] [329] [253]Greedy Search: Tree Search
28 A B C E F 99 G A 80 Start 75 118 140 [374] [329] [253] [193] [366] [178]Greedy Search: Tree Search
29 A B C E F I 99 211 G A 80 Start Goal 75 118 140 [374] [329] [253] [193] [366] [178] E [0] [253]Greedy Search: Tree Search
30 A B C E F I 99 211 G A 80 Start Goal 75 118 140 [374] [329] [253] [193] [366] [178] E [0] [253]Path cost(A-E-F-I) = 253 + 178 + 0 =
431
dist(A-E-F-I) = 140 + 99 + 211 =
450
Greedy Search: Optimal?
State Heuristic: h(n) A 366 B 374 C 329 D 244 E 253 F 178 G 193 H 98 I 0 31 A B D C E F I 99 211 G H 80 Start Goal 97 101 75 118 111f(n) = h (n) = straight-line distance heuristic
dist(A-E-G-H-I) =140+80+97+101=418
140
Greedy Search: Complete ?
State Heuristic: h(n) A 366 B 374 ** C 250 D 244 E 253 F 178 G 193 H 98 I 0 32 A B D C E F I 99 211 G H 80 Start Goal 97 101 75 118 111f(n) = h (n) = straight-line distance heuristic
Greedy Search: Tree Search
33
Greedy Search: Tree Search
34 A B C E Start 75 118 140 [374] [250] [253]Greedy Search: Tree Search
35 A B C E D 111 Start 75 118 140 [374] [250] [253] [244]Greedy Search: Tree Search
36 A B C E D 111 Start 75 118 140 [374] [250] [253] [244] C [250] Infinite Branch !Greedy Search: Tree Search
37 A B C E D 111 Start 75 118 140 [374] [250] [253] [244] C D [250] [244] Infinite Branch !Greedy Search: Tree Search
38 A B C E D 111 Start 75 118 140 [374] [250] [253] [244] C D [250] [244] Infinite Branch !Even Incomplete in finite state
space much like depth first search
(Complete in finite space with
repeated-state checking)
Greedy Search: Time and Space
Complexity ?
39 A B D C E F I 99 211 G H 80 Start Goal 97 101 75 118 111 140• Greedy search is
not optimal.
• Greedy search is
incomplete
• In the worst case, the Time and
Space Complexity of Greedy
Search are both
O(b
m)
Where b is the branching factor and m
the maximum path length
Informed Search Strategies
A* Search
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
.
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
.
A* (A Star)
43 n g(n) h(n)f(n) = g(n)+h(n)
A* Search
State Heuristic: h(n) A 366 B 374 C 329 D 244 E 253 F 178 G 193 H 98 I 0 44f(n) = g(n) + h (n)
g(n): is the exact cost to reach node n from the initial state.
A B D C E F I 99 211 G H 80 Start Goal 97 101 75 118 111 140
A* Search: Tree Search
45
A* Search: Tree Search
46 A B C E Start 75 118 140 [140+253=393] [75+374=449] [118+329=447]A* Search: Tree Search
47 A B C E F 99 G 80 Start 75 118 140 [239+178=417] [220+193=413] [140+253=393] [75+374=449] [118+329=447]A* Search: Tree Search
48 A B C E F 99 G 80 Start 75 118 140 H 97 [317+98=415] [140+253=393] [75+374=449] [118+329=447] [239+178=417] [220+193=413]A* Search: Tree Search
49 A B C E F I 99 G H 80 Start 97 101 75 118 140 Goal [418+0=418] [140+253=393] [75+374=449] [118+329=447] [317+98=415] [239+178=417] [220+193=413]A* Search: Tree Search
50 A B C E F I 99 G H 80 Start 97 101 75 118 140 Goal I [418+0=418] [140+253=393] [75+374=449] [118+329=447] [317+98=415] [239+178=417] [220+193=413] [450+0=450] 211A* Search: Tree Search
51 A B C E F I 99 G H 80 Start 97 101 75 118 140 Goal I [418+0=418] [140+253=393] [75+374=449] [118+329=447] [317+98=415] [239+178=417] [220+193=413] [450+0=450] 211A* Search: Tree Search
52 A B C E F I 99 G H 80 Start 97 101 75 118 140 Goal I [418+0=418] [140+253=393] [75+374=449] [118+329=447] [317+98=415] [239+178=417] [220+193=413] [450+0=450] 211 dist(A-E-G-H-I) =140+80+97+101=418A* 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
Admissible heuristics Examples
• h
SLD
(n) Straight line distance heuristic
– Because never overestimate the actual road distance.
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
57
Consistent Heuristic
• The admissible heuristic h is
consistent
if for every node
N and every successor N’ of N:
(𝑁)
𝑐(𝑁, 𝑁’) + (𝑁’)
(triangular inequality)
• A
consistent heuristic is admissible
.
• But an
admissible heuristic is not always consistent
N
N’
h(N) h(N’)A* Search: if h not admissible
State Heuristic: h(n) A 366 B 374 C 329 D 244 E 253 F 178 G 193 H 138 I 0 58 A B D C E F I 99 211 G H 80 Start Goal 97 101 75 118 111 f(n) = g(n) + h (n) – (H-I) Overestimatedg(n): is the exact cost to reach node n from the initial state.
A* Search: Tree Search
59
A* Search: Tree Search
60 A B C E Start 75 118 140 [393] [449] [447]A* Search: Tree Search
61 A B C E F 99 G 80 Start 75 118 140 [393] [449] [447] [417] [413]A* Search: Tree Search
62 A B C E F 99 G 80 Start 75 118 140 [393] [449] [447] [417] [413] H 97 [455]A* Search: Tree Search
63 A B C E F 99 G H 80 Start 97 75 118 140 [393] [449] [447] [417] [413] [455] Goal I [450] 211A* Search: Tree Search
64 A B C E F 99 G H 80 Start 97 75 118 140 [393] [449] [447] [417] [413] [455] Goal I [450] D [473] 211A* Search: Tree Search
65 A B C E F 99 G H 80 Start 97 75 118 140 [393] [449] [447] [417] [413] [455] Goal I [450] D [473] 211A* Search: Tree Search
66 A B C E F 99 G H 80 Start 97 75 118 140 [393] [449] [447] [417] [413] [455] Goal I [450] D [473] 211A* Search: Tree Search
67 A B C E F 99 G H 80 Start 97 75 118 140 [393] [449] [447] [417] [413] [455] Goal I [450] D [473]A* not optimal !!!
When heuristic h is not admissible
dist(A-E-F-I) =140+99+211=450
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
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(b
d
)
space complexity
,
– But an iterative deepening version is possible (IDA*).
• Optimal?
Yes
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.
SLD Heuristic: h()
Straight Line Distances to Bucharest
71
Town
SLD
Arad 366 Bucharest 0 Craiova 160 Dobreta 242 Eforie 161 Fagaras 178 Giurgiu 77 Hirsova 151 Iasi 226 Lugoj 244Town
SLD
Mehadai 241 Neamt 234 Oradea 380 Pitesti 98 Rimnicu 193 Sibiu 253 Timisoara 329 Urziceni 80 Vaslui 199 Zerind 374We can use straight line distances as an admissible heuristic as they will never
overestimate the cost to the goal. This is because there is no shorter distance between two
Distances Between Cities
72Arad
Bucharest
Oradea Zerind Faragas Neamt Iasi Vaslui Hirsova Eforie Urziceni Giurgui Pitesti Sibiu Dobreta Craiova Rimnicu Mehadia Timisoara Lugoj 87 92 142 86 98 86 211 101 90 99 151 71 75 140 118 111 70 75 120 138 146 97 80 140 80 97 101 Sibiu Rimnicu PitestiGreedy Search in Action …
74
Press space to see an Greedy search of the Romanian map featured in the previous slide.
Note: Throughout the animation all nodes are labelled with f(n) = h(n). However,we will
be using the abbreviations f, and h to make the notation simpler
Oradea Zerind Fagaras Sibiu Rimnicu Timisoara
Arad
We begin with the initial state of Arad. The straight line distance from Arad to Bucharest (or
h value) is 366 miles. This gives us a total value of ( f = h ) 366 miles. Press space to expand
the initial state of Arad.
F= 366 F= 366 F= 374 F= 374 F= 253 F=253 F= 329 F= 329
Once Arad is expanded we look for the node with the lowest cost. Sibiu has the lowest value for f. (The straight line distance from Sibiu to the goal state is 253 miles. This gives a total
of 253 miles). Press space to move to this node and expand it.
We now expand Sibiu (that is, we expand the node with the lowest value of f ). Press space to continue the search.
F= 178 F= 178 F= 380 F= 380 F= 193 F= 193
We now expand Fagaras (that is, we expand the node with the lowest value of f ). Press space to continue the search.
Bucharest
F= 0
A* in Action …
76
Press space to see an A* search of the Romanian map featured in the previous slide. Note:
Throughout the animation all nodes are labelled with f(n) = g(n) + h(n). However,we will
be using the abbreviations f, g and h to make the notation simpler
Oradea Zerind Fagaras Pitesti Sibiu Craiova Rimnicu Timisoara
Bucharest
Arad
We begin with the initial state of Arad. The cost of reaching Arad from Arad (or g value) is 0
miles. The straight line distance from Arad to Bucharest (or h value) is 366 miles. This gives
us a total value of ( f = g + h ) 366 miles. Press space to expand the initial state of Arad.
F= 0 + 366 F= 366 F= 75 + 374 F= 449 F= 140 + 253 F= 393 F= 118 + 329 F= 447
Once Arad is expanded we look for the node with the lowest cost. Sibiu has the lowest value for f. (The cost to reach Sibiu from Arad is 140 miles, and the straight line distance from Sibiu to the goal state is 253 miles. This gives a total of 393 miles). Press space to move to this node and expand it.
We now expand Sibiu (that is, we expand the node with the lowest value of f ). Press space to continue the search.
F= 239 + 178 F= 417 F= 291 + 380 F= 671 F= 220 + 193 F= 413
We now expand Rimnicu (that is, we expand the node with the lowest value of f ). Press space to continue the search.
F= 317 + 98
F= 415
F= 366 + 160
F= 526
Once Rimnicu is expanded we look for the node with the lowest cost. As you can see, Pitesti has the lowest value for f. (The cost to reach Pitesti from Arad is 317 miles, and the straight line distance from Pitesti to the goal state is 98 miles. This gives a total of 415 miles). Press space to move to this node and expand it.
We now expand Pitesti (that is, we expand the node with the lowest value of f ). Press space to continue the search.
We have just expanded a node (Pitesti) that revealed Bucharest, but it has a cost of 418. If there is any other lower cost node (and in this case there is one cheaper node, Fagaras, with a cost of 417) then we need to expand it in case it leads to a better solution to Bucharest than the 418 solution we have already found. Press space to continue.
F= 418 + 0
F= 418
In actual fact, the algorithm will not really recognise that we have found Bucharest. It just keeps expanding the lowest cost nodes (based on f ) until it finds a goal state AND it has the lowest value of f. So, we must now move to Fagaras and expand it. Press space to continue. We now expand Fagaras (that is, we expand the node with the lowest value of f ). Press space to continue the search.
Bucharest
(2)F= 450 + 0
F= 450
Once Fagaras is expanded we look for the lowest cost node. As you can see, we now have two Bucharest nodes. One of these nodes ( Arad – Sibiu – Rimnicu – Pitesti – Bucharest ) has an f value of 418. The other node (Arad – Sibiu – Fagaras – Bucharest(2) ) has an f value
of 450. We therefore move to the first Bucharest node and expand it. Press space to continue
Bucharest
Bucharest
Bucharest
We have now arrived at Bucharest. As this is the lowest cost node AND the goal state we can terminate the search. If you look back over the slides you will see that the solution
returned by the A* search pattern ( Arad – Sibiu – Rimnicu – Pitesti – Bucharest ), is in fact the optimal solution. Press space to continue with the slideshow.