Sie sind auf Seite 1von 17

Heuristic Approach

• An approach in which we know in advance the


estimated total cost of path on which we have to
travel to reach the goal
• xxx
Informed Search

Muhammad Bux Alvi

function Uniform-Cost-Search (problem)


loop do Informed Search
if Empty?(frontier) then return failure
node = Pop(frontier)
if Goal?(node) then return Solution(node)
• Informed searches use domain knowledge
Insert node in explored
to guide selection of the best path to continue
foreach child of node do
searching
if child not in frontier or explored then
Insert child in frontier
else if child in frontier with higher cost then • Heuristics are used, which are informed guesses
Remove that old node from frontier
Insert child in frontier
• Heuristic means "serving to aid discovery"
This is the algorithm in Figure 3.14 in the textbook; note that if
child is not in frontier but is in explored, this algorithm will
throw away child

1
Informed Search Informed Search
Define a heuristic function, h(n) • h(n) ≥ 0 for all nodes n
– uses domain-specific information in some way • 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
– is (easily) computable from the current state
description
• All domain knowledge used in the search is encoded in
– estimates the heuristic function, h
• the "goodness" of node n
• how close node n is to a goal • An example of a “weak method” for AI because of the
• the cost of minimal cost path from node n to a goal limited way that domain-specific information is
state used to solve a problem (i.e., entirely contained in one
function definition)

Best-First Search
• It combines the best features of BFS and DFS to Best-First Search
follow a single path at a time but switch whenever
some competing path looks more promising than
the current one. • Sort nodes in the Frontier list by increasing
– BFS values of an evaluation function, f(n), that
• No Dead End incorporates domain-specific information
– DFS
• Get solution without expanding all competing branches
• This is a generic way of referring to the class
• Best first search is a kind of Hill Climbing of informed search methods
– but in pure hill climbing, we move on one path and
reject other paths
– but in best first search, other paths are not ignored

2
Greedy Best-First Search Greedy Best-First Search

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


S
# of nodes tested: 0, expanded: 0
sorting nodes in the Frontier by increasing h=8
expnd. node Frontier
values of f {S:8}
1 5 8
A B C
h=8 h=4 h=3

• Selects the node to expand that is believed 3 7 9 4 5


D E G
to be closest (i.e., smallest f value) to a goal h=∞ h=∞ h=0
node

Greedy Best-First Search Greedy Best-First Search

f(n) = h(n) f(n) = h(n)


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

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

3
Greedy Best-First Search Greedy Best-First Search

f(n) = h(n) f(n) = h(n)


S S
# of nodes tested: 3, expanded: 2 h=8 # of nodes tested: 3, expanded: 2 h=8
expnd. node Frontier expnd. node Frontier
1 5 8 1 5 8
{S:8} {S:8}
S {C:3,B:4,A:8} A B C S {C:3,B:4,A:8} A B C
C {G:0,B:4, A:8} h=8 h=4 h=3 C {G:0,B:4, A:8} h=8 h=4 h=3
G goal {B:4, A:8} not expanded G {B:4, A:8}
3 7 9 4 5 3 7 9 4 5
D E G D E G
h=∞ h=∞ h=0 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 = ?

4
Greedy Best-First Search Beam Search
• Use an evaluation function f(n) = h(n) as in
Greedy Best-First search, and restrict the
S maximum size of the Frontier to a constant, k
h=5
• Not complete 2 2 • Only keep the k best nodes as candidates for
A B
• Not optimal/admissible h=3 h=4
expansion, and throw away the rest
Greedy search finds the left goal 1 2 • More space efficient than Greedy Best-First
(solution cost of 7) C D Search, but may throw away a node on a solution
h=3 h=1 path
Optimal solution is the path to the 1 1
• Not complete
right goal (solution cost of 5) E G
h=2 goal • Not optimal/admissible
3
G
goal

Algorithm A Search Is Algorithm A Optimal?


• Use as an evaluation function f(n) = g(n) + h=6
h(n), where g(n) is minimum cost path from
start to current node n (as defined in UCS) 1 A 3
• The g term adds a “breadth-first component”
to the evaluation function
S G
• Nodes in Frontier are ranked by the estimated 5
cost of a solution, where g(n) is the cost from h=7 h=0
the start node to node n, and h(n) is the
estimated cost from node n to a goal

5
Algorithm A* Search Admissible Heuristics are Good for
• Use the same evaluation function used by Algorithm A,
Playing The Price is Right
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 that it is never
considered

Algorithm A* Search Example

• Complete S
n g(n) h(n) f(n) h*(n)
h=8
• Optimal / Admissible 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

6
Example Example

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


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

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

Example Example

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


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

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

7
Example Example

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


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

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

Example Example

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


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

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

8
Example Example

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


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

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


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

Example Example

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


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

h*(n) = true cost of minimum-cost path h*(n) = true cost of minimum-cost path
from n to a goal from n to a goal

9
Example Example

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


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

h*(n) = true cost of minimum-cost path h*(n) = true cost of minimum-cost path
from n to a goal from n to a goal

Example Example

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


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

h*(n) = true cost of minimum-cost path optimal path = S,B,G


from n to a goal cost = 9

10
Example Example: Find Path from S to G

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

Admissible Heuristic Functions, h


• 8-Puzzle example
1 5 Goal 1 2 3
Example
• A* = ? State 2 6 3
State
4 5 6

7 4 8 7 8

• Which of the following are admissible heuristics?

h(n) = number of tiles in wrong position


h(n) = 0
h(n) = 1
h(n) = sum of “Manhattan distance” between
each tile and its goal location

Note: Manhattan distance = L1 norm

11
Admissible Heuristic Functions, h Admissible Heuristic Functions, h
• 8-Puzzle example

Example
1 5 Goal 1 2 3 Which of the following are admissible heuristics?
State State
2 6 3 4 5 6

7 4 8 7 8 h(n) = h*(n)

• Which of the following are admissible heuristics? h(n) = max(2, h*(n))


h(n) = number of tiles in wrong position YES
h(n) = 0 YES, uniform-cost search h(n) = min(2, h*(n))
h(n) = 1 NO, goal state
h(n) = sum of “Manhattan distance” between h(n) = h*(n) – 2
each tile and its goal location YES
h(n) = h* (n)

Admissible Heuristic Functions, h When should A* Stop?


Which of the following are admissible heuristics? • A* should terminate only when a goal is
removed from the priority queue
h(n) = h*(n) YES
B
1 999
h(n) = max(2, h*(n)) NO h=1
S G
h(n) = min(2, h*(n)) YES h=2 h=0
1 C 1
h(n) = h*(n) - 2 NO, possibly negative h=2
*
h(n)== ℎh∗((n)
h(n) ) NO if h*(n)<1 • Same rule as for Uniform-Cost Search (UCS)
• A* with h() = 0 is Uniform-Cost Search

12
A and A* Algorithm for
A* Revisiting States
General State-Space Graphs
• One more complication: A* might revisit a Frontier = {S} where S is the start node
state (in Frontier or Explored), and discover Explored ={}
a better path Loop do
if Frontier is empty then return failure
B pick node, n, with min f value from Frontier
1 2 if n is a goal node then return solution
h=1
foreach each child, n’, of n do
S D 999 G
if n’ is not in Explored or Frontier
h=1 h=1 h=0 then add n’ to Frontier
1 C 1
else if g(n’) ≥ g(m) then throw n’ away
h=900 else add n’ to Frontier and remove m
Remove n from Frontier and add n to Explored
• Solution: Put D back in the priority queue,
Note: m is the node in Frontier or
using the smaller g value (and path) Variant of algorithm in Fig. 3.14 Explored that is the same state as n’

Consistency Consistency
• A heuristic, h, is called consistent (aka monotonic) if,
for every node n and every successor n’ of n, the Is this h consistent?
estimated cost of reaching the goal from n is no
greater than the step cost of getting to n’ plus the B
1 2
estimated cost of reaching the goal from n’: h=1
c(n, n’) ≥ h(n) − h(n’) S D 999 G
h=1 h=1 h=0
or, equivalently: h(n) ≤ c(n, n’) + h(n’) 1 C 1
• Triangle inequality for heuristics h=900
• Implies values of f along any path are nondecreasing h(C)=900, h(D)=1, c(C, D) = 1
• When a node is expanded by A*, the optimal path to but h(C) ≰ c(C,D) + h(D) since
that node has been found
900 ≰ 1 + 1, so h is NOT consistent
• Consistency is a stronger condition than admissibility (but h is admissible)

13
A* Search Is h is admissible A* Search
and/or
consistent?

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


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

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

h is consistent since h(S) – h(A) = 8 – 8 ≤ 1, etc.


and therefore is also admissible

A* Search A* Search

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


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

14
A* Search A* Search

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


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

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


cost: 9

Example: Find Shortest Route from Heuristic: Straight-Line Distance


Arad to Bucharest 380
to Bucharest
This image cannot currently be display ed.

Oradea
71 234
Neamt
374 Zerind 87 226
151
75
Iasi
Arad
140 253 176
366 Sibiu
92
99 Fagaras 199
118
Vaslui
80
Rimnicu Vilcea
Timisoara 193
329 100
111
244 Pitesti 211
142
Lugoj 97
70 80 151
98
Hirsova
241 Mehadia 146 101 85
Urziceni
75 138 86
Bucharest
Drobeta 120
90 161
242 Craiova
160 Giurgiu 77 Eforie

15
16
Thank You

17

Das könnte Ihnen auch gefallen