You are on page 1of 62

Artificial Intelligence

Informed/Heuristic search Strategies


Chapter 4
Outline
 Best-first search
 Greedy best-first search
 A* search
 Local search algorithms
 Hill-climbing search
 Simulated annealing search
 Local beam search
 Genetic algorithms
Informed Search
 Informed searches use domain knowledge
to guide selection of the best path to continue
searching

• Heuristics are used, which are informed guesses

• Heuristic means "serving to aid discovery"


Informed Search
 Define a heuristic function, h(n)
 it estimates
 the "goodness" of node n
 how close node n is to a goal
 the estimated cost of minimal cost path from node n to a goal
state
Informed Search
• h(n) ≥ 0 for all nodes n
• h(n)=0 means n is a goal node
• h(n) close to 0 means we think n is close to a goal
state
• h(n) very big means we think n is far from a goal
state

• All domain knowledge used in the search is encoded


in the heuristic function, h
Best-First Search

 Sort nodes in the Frontier list by increasing values of an


evaluation function, f(n), that incorporates domain-
specific information

 This is a generic way of referring to the class of


informed search methods
Best-first search
 Idea: use an evaluation function f(n) for each node
 estimate of "desirability"
 Expand most desirable unexpanded node

 Implementation:
Order the nodes in fringe in decreasing order of
desirability
 Special cases:
 greedy best-first search
 A* search

1. Greedy Best-First Search

 Use as an evaluation function, f(n) = h(n), sorting nodes


in the Frontier by increasing values of f

 Heuristic Function
h(n)= estimated cost of the cheapest path from node n to a
goal node
Greedy Best-First Search
 Selects the node to expand that is believed to be
closest (i.e., smallest f value) to a goal node
Greedy best-first search
 Evaluation function f(n) = h(n)
 (heuristic) = estimate of cost from n to goal

 e.g., hSLD(n) = straight-line distance from n to


Bucharest

 Greedy best-first search expands the node that appears


to be closest to goal
Greedy best-first search example
Greedy best-first search example
Greedy best-first search example
Greedy best-first search example
Greedy best-first search
 For this particular problem, greedy best-first search
using hsLD finds a solution without ever expanding a
node that is not on the solution path; hence, its search
cost is minimal.
Greedy best-first search
 It is not optimal, however: the path via Sibiu and
Fagaras to Bucharest is 32 kilometers longer than the
path through Rimnicu Vilcea and Pitesti.

 This shows why the algorithm is called "greedy”- at


each step it tries to get as close to the goal as it can.
Greedy Best-First Search

f(n) = h(n)
S
# of nodes tested: 0, expanded: 0 h=8
expnd. node Frontier
{S:8} 1 5 8
A B C
h=8 h=4 h=3

3 7 9 4 5
D E G
h=∞ h=∞ h=0
Greedy Best-First Search

f(n) = h(n)
S
# of nodes tested: 1, expanded: 1 h=8
expnd. node Frontier
{S:8} 1 5 8
S not goal {C:3,B:4,A:8} A B C
h=8 h=4 h=3

3 7 9 4 5
D E G
h=∞ h=∞ h=0
Greedy Best-First Search

f(n) = h(n)
S
# of nodes tested: 2, expanded: 2 h=8
expnd. node Frontier
{S:8} 1 5 8
S {C:3,B:4,A:8} A B C
C not goal {G:0,B:4,A:8} h=8 h=4 h=3

3 7 9 4 5
D E G
h=∞ h=∞ h=0
Greedy Best-First Search

f(n) = h(n)
S
# of nodes tested: 3, expanded: 2 h=8
expnd. node Frontier
{S:8} 1 5 8
S {C:3,B:4,A:8} A B C
C {G:0,B:4, A:8} h=8 h=4 h=3
G goal {B:4, A:8} not expanded
3 7 9 4 5
D E G
h=∞ h=∞ h=0
Greedy Best-First Search

f(n) = h(n)
S
# of nodes tested: 3, expanded: 2 h=8
expnd. node Frontier
{S:8} 1 5 8
S {C:3,B:4,A:8} A B C
C {G:0,B:4, A:8} h=8 h=4 h=3
G {B:4, A:8}
3 7 9 4 5
D E G
h=∞ h=∞ h=0
 Fast but not optimal
path: S,C,G
cost: 13
Greedy Best-First Search
• Not complete
• Not optimal/admissible S
h=5
2 2
A B
Greedy search finds the left goal h=3 h=4
(solution cost of 7) 1 2

Optimal solution is the path to the C D


h=3 h=1
right goal (solution cost of 5)
1 1
E G
h=2 goal

3
G
goal
Greedy Best-First Search
 Greedy best-first search resembles depth-first search
in the way it prefers to follow a single path all the
way to the goal, but will back up when it hits a dead
end.

 It suffers from the same defects as depth-first search-it


is not optimal, and it is incomplete (because it can
start down an infinite path and never return to try
other possibilities).
Properties of greedy best-first search

 Complete? No – can get stuck in loops

 Time? O(bm), but a good heuristic can give dramatic


improvement

 Space? O(bm) -- keeps all nodes in memory

 Optimal? No
2. A* search: Minimizing the total estimated
solution cost

 Idea: avoid expanding paths that are already expensive

 Evaluation function f(n) = g(n) + h(n)


g(n) = cost so far to reach n from start node
h(n) = estimated cost of cheapest path from n to goal
f(n) = estimated cost of the cheapest path through n to goal
A* search
 Cheapest solution is found by identifying the node
with lowest value of g(n)+h(n)

 A* is both complete and optimal

 Admissible heuristic
 A* is optimal if h(n) is admissible heuristic.
 h(n) never overestimates the actual cost to reach the
goal
A* search
• When h(n) ≤ h*(n) holds true for all n, h is called an
admissible heuristic function

• where h*(n) is the actual cost of the minimum-cost


path from n to a goal
Admissible Heuristic-Example

n g(n) h(n) f(n) h*(n) S


h=8
S
A 1 5 8
B
C A B C
h=8 h=4 h=3
D
E 3 7 9 4 5
G D E G
h=∞ h=∞ h=0

g(n) = actual cost to get to node n


from start
Example

n g(n) h(n) f(n) h*(n) S


h=8
S 0
A 1 5 8
B
C A B C
h=8 h=4 h=3
D
E 3 7 9 4 5
G D E G
h=∞ h=∞ h=0

g(n) = actual cost to get to node n


from start
Example

n g(n) h(n) f(n) h*(n) S


h=8
S 0
A 1 1 5 8
B
C A B C
h=8 h=4 h=3
D
E 3 7 9 4 5
G D E G
h=∞ h=∞ h=0

g(n) = actual cost to get to node n


from start
Example

n g(n) h(n) f(n) h*(n) S


h=8
S 0
A 1 1 5 8
B 5
C 8 A B C
h=8 h=4 h=3
D
E 3 7 9 4 5
G D E G
h=∞ h=∞ h=0

g(n) = actual cost to get to node n


from start
Example

n g(n) h(n) f(n) h*(n) S


h=8
S 0
A 1 1 5 8
B 5
C 8 A B C
h=8 h=4 h=3
D 1+3 = 4
E 3 7 9 4 5
G D E G
h=∞ h=∞ h=0

g(n) = actual cost to get to node n


from start
Example

n g(n) h(n) f(n) h*(n) S


h=8
S 0
A 1 1 5 8
B 5
C 8 A B C
h=8 h=4 h=3
D 4
E 1+7 = 8 3 7 9 4 5
G D E G
h=∞ h=∞ h=0

g(n) = actual cost to get to node n


from start
Example

n g(n) h(n) f(n) h*(n) S


h=8
S 0
A 1 1 5 8
B 5
C 8 A B C
h=8 h=4 h=3
D 4
E 8 3 7 9 4 5
G 10/9/13 D E G
h=∞ h=∞ h=0

g(n) = actual cost to get to node n


from start
Example

n g(n) h(n) f(n) h*(n) S


h=8
S 0
A 1 1 5 8
B 5
C 8 A B C
h=8 h=4 h=3
D 4
E 8 3 7 9 4 5
G 10/9/13 D E G
h=∞ h=∞ h=0

h(n) = estimated cost to get to a goal


from node n
Example

n g(n) h(n) f(n) h*(n) S


h=8
S 0 8
A 1 8 1 5 8
B 5 4
C 8 3 A B C
h=8 h=4 h=3
D 4 ∞
E 8 ∞ 3 7 9 4 5
G 10/9/13 0 D E G
h=∞ h=∞ h=0

h(n) = estimated cost to get to a goal


from node n
Example

n g(n) h(n) f(n) h*(n) S


h=8
S 0 8 8
A 1 8 9 1 5 8
B 5 4 9
C 8 3 11 A B C
h=8 h=4 h=3
D 4 ∞ ∞
E 8 ∞ ∞ 3 7 9 4 5
G 10/9/13 0 10/9/13 D E G
h=∞ h=∞ h=0

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


actual cost to get from start to n
plus estimated cost from n to goal
Example

n g(n) h(n) f(n) h*(n) S


h=8
S 0 8 8
A 1 8 9 1 5 8
B 5 4 9
C 8 3 11 A B C
h=8 h=4 h=3
D 4 ∞ ∞
E 8 ∞ ∞ 3 7 9 4 5
G 10/9/13 0 10/9/13 D E G
h=∞ h=∞ h=0

h*(n) = true cost of minimum-cost path


from n to a goal
Example

n g(n) h(n) f(n) h*(n) S


h=8
S 0 8 8 9
A 1 8 9 1 5 8
B 5 4 9
C 8 3 11 A B C
h=8 h=4 h=3
D 4 ∞ ∞
E 8 ∞ ∞ 3 7 9 4 5
G 10/9/13 0 10/9/13 D E G
h=∞ h=∞ h=0

h*(n) = true cost of minimum-cost path


from n to a goal
Example

n g(n) h(n) f(n) h*(n) S


h=8
S 0 8 8 9
A 1 8 9 9 1 5 8
B 5 4 9
C 8 3 11 A B C
h=8 h=4 h=3
D 4 ∞ ∞
E 8 ∞ ∞ 3 7 9 4 5
G 10/9/13 0 10/9/13 D E G
h=∞ h=∞ h=0

h*(n) = true cost of minimum-cost path


from n to a goal
Example

n g(n) h(n) f(n) h*(n) S


h=8
S 0 8 8 9
A 1 8 9 9 1 5 8
B 5 4 9 4
C 8 3 11 A B C
h=8 h=4 h=3
D 4 ∞ ∞
E 8 ∞ ∞ 3 7 9 4 5
G 10/9/13 0 10/9/13 D E G
h=∞ h=∞ h=0

h*(n) = true cost of minimum-cost path


from n to a goal
Example

n g(n) h(n) f(n) h*(n) S


h=8
S 0 8 8 9
A 1 8 9 9 1 5 8
B 5 4 9 4
C 8 3 11 5 A B C
h=8 h=4 h=3
D 4 ∞ ∞
E 8 ∞ ∞ 3 7 9 4 5
G 10/9/13 0 10/9/13 D E G
h=∞ h=∞ h=0

h*(n) = true cost of minimum-cost path


from n to a goal
Example

n g(n) h(n) f(n) h*(n) S


h=8
S 0 8 8 9
A 1 8 9 9 1 5 8
B 5 4 9 4
C 8 3 11 5 A B C
h=8 h=4 h=3
D 4 ∞ ∞ ∞
E 8 ∞ ∞ ∞ 3 7 9 4 5
G 10/9/13 0 10/9/13 0 D E G
h=∞ h=∞ h=0

h*(n) = true cost of minimum-cost path


from n to a goal
Example

n g(n) h(n) f(n) h*(n) S


h=8
S 0 8 8 9
A 1 8 9 9 1 5 8
B 5 4 9 4
C 8 3 11 5 A B C
h=8 h=4 h=3
D 4 ∞ ∞ ∞
E 8 ∞ ∞ ∞ 3 7 9 4 5
G 10/9/13 0 10/9/13 0 D E G
h=∞ h=∞ h=0

optimal path = S,B,G


cost = 9
Example

n g(n) h(n) f(n) h*(n) S


h=8
S 0 8 8 9
A 1 8 9 9 1 5 8
B 5 4 9 4
C 8 3 11 5 A B C
h=8 h=4 h=3
D 4 ∞ ∞ ∞
E 8 ∞ ∞ ∞ 3 7 9 4 5
G 10/9/13 0 10/9/13 0 D E G
h=∞ h=∞ h=0
Since h(n) ≤ h*(n) for all n,
h is admissible
A* Search Is h is admissible
and/or consistent?

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


S
# of nodes tested: 0, expanded: 0 h=8
expnd. Frontier
node 1 5 8
{S:0+8} A B C
h=8 h=4 h=3

3 7 9 4 5
D E G
h=∞ h=∞ h=0
A* Search

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


S
# of nodes tested: 1, expanded: 1 h=8
expnd. Frontier
node 1 5 8
{S:8} A B C
S not goal {A:1+8,B:5+4,C:8+3} h=8 h=4 h=3

3 7 9 4 5
D E G
h=∞ h=∞ h=0
A* Search

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


S
# of nodes tested: 2, expanded: 2 h=8
expnd. Frontier
node 1 5 8
{S:8} A B C
S {A:9,B:9,C:11} h=8 h=4 h=3
A not goal {B:9,G:1+9+0,C:11,
3 7 9 4 5
D:1+3+∞,E:1+7+∞}
D E G
h=∞ h=∞ h=0
A* Search

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


S
# of nodes tested: 3, expanded: 3 h=8
expnd. Frontier
node 1 5 8
{S:8} A B C
S {A:9,B:9,C:11} h=8 h=4 h=3
A {B:9,G:10,C:11,D:∞,E:∞} 3 7 9 4 5
B not goal {G:5+4+0,G:10,C:11,
D E G
D:∞,E:∞} replace h=∞ h=∞ h=0
A* Search

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


S
# of nodes tested: 4, expanded: 3 h=8
expnd. Frontier
node 1 5 8
{S:8} A B C
S {A:9,B:9,C:11} h=8 h=4 h=3
A {B:9,G:10,C:11,D:∞,E:∞} 3 7 9 4 5
B {G:9,C:11,D:∞,E:∞} D E G
G goal {C:11,D:∞,E:∞} h=∞ h=∞ h=0
not expanded
A* Search

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


S
# of nodes tested: 4, expanded: 3 h=8
expnd. Frontier
node 1 5 8
{S:8} A B C
S {A:9,B:9,C:11} h=8 h=4 h=3
A {B:9,G:10,C:11,D:∞,E:∞} 3 7 9 4 5
B {G:9,C:11,D:∞,E:∞} D E G
G {C:11,D:∞,E:∞} h=∞ h=∞ h=0

 Pretty fast and optimal path: S,B,G


cost: 9
Example: Find Path from Arad to
Bucharest
Oradea
71
Neamt

Zerind 87
75 151
Iasi
Arad
140
92
Sibiu Fagaras
99
118
Vaslui
80
Rimnicu Vilcea
Timisoara
142
111 Pitesti 211
Lugoj 97
70 98
85 Hirsova
Mehadia 146 101 Urziceni
75 138 86
Bucharest
Drobeta 120
90
Craiova Eforie
Giurgiu
Heuristic: Straight-Line Distance to
Bucharest
380
Oradea
71 234
Neamt
374 Zerind 87
226
75 151
Iasi
Arad
366
140 253 176 92
Sibiu
99 Fagaras 199
118
Vaslui
80

329
Timisoara 193 Rimnicu Vilcea

244 100 211


142
111 Pitesti
Lugoj 97
70 80 151
98
241 Mehadia 146 101 85
Urziceni
Hirsova

75 138 86
Bucharest
Drobeta 120

242
90 161
Craiova 160 Giurgiu 77 Eforie
Example: Find Path from Arad to
Bucharest
Example: Find Path from Arad to
Bucharest
Example: Find Path from Arad to
Bucharest
Example: Find Path from Arad to
Bucharest
Example: Find Path from Arad to
Bucharest
Visualizing Search Methods
 http://qiao.github.io/PathFinding.js/visual/

 BFS, UCS, Greedy Best-First, A*

You might also like