You are on page 1of 56

Informed Search

Dr. Amar Singh


Associate Professor,
School of Computer Applications,
Lovely Professional University
Heuristics
• “Rule of Thumb”.
• “Heuristics are the criteria, methods and
principles for deciding which among several
alternative courses of action promises to be
most effective in order to achieve some goal.”
pearl.
– Can use heuristics to identify the most promising
search path.

2
Example of Heuristic Function
• A heuristic function at node n is an estimate of
the optimum cost current node to goal.
Denoted by h(n).
H(n) = estimated cost of the cheapest path from node n to
goal node.
• Example:
– Want path form Jalandhar to chandigarh.
– Heuristic to chandigarh may be staright line distance
between jalandhar to chandigarh.
– H(jalandhar) = euclideandistance(jalandhar,
chandigarh). 3
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)


– uses domain-specific information in some way
– is computable from the current state description
– it estimates
• the "goodness" of node n
• how close node n is to a goal
• the cost of minimal cost path from node n to a goal
state
Informed Search
• h(n) ≥ 0 for all nodes n
• 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

• An example of a “weak method” for AI because of the


limited way that domain-specific information is
used to solve a problem
Best-First Search
• Depth-first search: not all competing branches
having to be expanded.

• Breadth-first search: not getting trapped on dead-


end paths.
 Combining the two is to follow a single path at a
time, but switch paths whenever some
competing path look more promising than the
current one.
Best-First Search (OR Graphs)
A A

B C D
3 5 1
Best-First Search (OR Graphs)
A A A

B C D B C D
3 5 1 3 5
E F
4 6
Best-First Search (OR Graphs)
A A A

B C D B C D
3 5 1 3 5
E F
4 6
A

B C D
5
G H E F
6 5 4 6
Best-First Search (OR Graphs)
A A A

B C D B C D
3 5 1 3 5
E F
4 6
A A

B C D B C D
5 5
G H E F G H E F
6 5 4 6 6 5 6
I J
2 1
Best-First Search
• OPEN: nodes that have been generated, but have not
examined.
This is organized as a priority queue.

• CLOSED: nodes that have already been examined.


Whenever a new node is generated, check whether it
has been generated before.
Best-First Search
Algorithm
1. OPEN = {initial state}.
2. Loop until a goal is found or there are no nodes left in
OPEN:
- Pick the best node in OPEN
- Generate its successors
- For each successor:
new  evaluate it, add it to OPEN, record its parent.
generated before  change parent, update
successors
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

• Use as an evaluation function, f(n) = h(n),


sorting nodes in the Frontier by increasing
values of f

• Selects the node to expand that is believed


to be closest (i.e., smallest f value) to a goal
node
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
Example: Find Path from S to G in
State-Space Graph
Greedy Best-First Search

S
h=5
• Not complete 2 2
A B
• Not optimal/admissible h=3 h=4
Greedy search finds the left goal 1 2
(solution cost of 7) C D
h=3 h=1
Optimal solution is the path to the 1 1
right goal (solution cost of 5) E G
h=2 goal

3
G
goal
Best-First Search
• Algorithm A* (Hart et al., 1968):
f(n) = g(n) + h(n)

h(n) = cost of the cheapest path from node n to a


goal state.
g(n) = cost of the cheapest path from the initial state
to node n.
Best-First Search
• Algorithm A*:
f’(n) = g’(n) + h’(n)

h’(n) (heuristic factor) = estimate of h(n).

g’(n) (depth factor) = approximation of g(n) found


by A* so far.
Algorithm A* Search
• Use as an evaluation function f(n) = g(n) +
h(n), where g(n) is minimum cost path from
start to current node n (as defined in UCS)
• The g term adds a “breadth-first component”
to the evaluation function
• Nodes in Frontier are ranked by the estimated
cost of a solution, where g(n) is the cost from
the start node to node n, and h(n) is the
estimated cost from node n to a goal
Is Algorithm A Optimal?
h=6

1 A 3

S G
5
h=7 h=0
Algorithm A Search

S
h=5
• Not complete 2 1
A B
 Not optimal/admissible h=3 h=1
Algorithm A never expands 1 1
E because f(E) is bigger than all C D
h=3 h=1
nodes below F
1 1
E F
h=∞ h=1
99

G
h=0
Algorithm A* Search
• Use the same evaluation function used by Algorithm A,
except add the constraint that for all nodes n in the
search space, h(n) ≤ h*(n), where h*(n) is the actual
cost of the minimum-cost path from n to a goal

• The cost to the nearest goal is never over-estimated

• When h(n) ≤ h*(n) holds true for all n, h is called an


admissible heuristic function

• An admissible heuristic guarantees that a node on the


optimal path cannot look so bad so that it is never
considered
Admissible Heuristics are Good for
Playing The Price is Right
Algorithm A* Search

• Complete
• Optimal / Admissible
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
Example: Find Path from S to G
Example: Find Path from Arad to
Bucharest
Oradea
71
Neamt

Zerind 87
151
75
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
380
to Bucharest
Oradea
71 234
Neamt
374
75
Zerind 151
87 226
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 160 90 161
Craiova Giurgiu 77 Eforie

You might also like