Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node...

24
Greedy best-first search • Use the heuristic function to rank the nodes • Search strategy – Expand node with lowest h-value • Greedily trying to find the least-cost solution

Transcript of Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node...

Page 1: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

Greedy best-first search

• Use the heuristic function to rank the nodes

• Search strategy– Expand node with lowest h-value

• Greedily trying to find the least-cost solution

Page 2: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

Greedy Best-First Search

Path search(start, operators, is_goal) { fringe = makeList(start);

while (state=fringe.popFirst()) { if (is_goal(state))

return pathTo(state); S = successors(state, operators);

fringe = insert(S, fringe); }

return NULL;}

Order the nodes in increasing values of h(N)

Page 3: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

Procedure Best-First Search

begin

open [Start]

closed [ ]

while open [ ] do begin

remove leftmost state from open and call it X

if X is a goal then return the path from Start to X

else begin

generate children of X

for each child of X do begin

switch

case the child is not on open or closed

assign the child a heuristic value and add the child to open

case the child is already on open if the child was reached by a shorter path then

give the state on open the shorter path

case the child is already on closed if the child was reached by a shorter path then

remove the state from closed and add the child to open

put X on closed

re-order states on open by heuristic merit (best leftmost)

end

end

end

return failure

end

Page 4: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

Example

A-5

B-4 C-4 D-6

E-5F-5

G-4 H-3

1. Open = [A5];closed = []

2. Evl A5; open=[B4,C4,D6];closed=[A5]

3. Eval B4;open=[C4,E5,F5,D6]; closed=[B4,A5]

4. Eval C4; open = [H3,G4,E5,F5,D6] ; closed=[C4,B4,A5]

Page 5: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

• A search algorithm is admissible if it is guaranteed to find a minimal path to a solution whenever such a path exist.

• f*(n) = g*(n) + h*(n)g*(n) is the cost of the shortest path from

the start node to n.h*(n) is the actual cost of the shortest

path n to the goal.Then f*(n) is the actual cost of the optimal

path from a start node to a goal node that passes through node n.

Page 6: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

• f* does not exist.

• Algorithm A– Use f(n) = g(n) + h(n) and best-first-search

algorithm

• Algorithm A*– If h(n) h*(n) in the algorithm A then it is called

Algorithm A*

• Theorem: All A* algorithms are admissible

Page 7: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

Implementing Heuristic Evaluations

• For 8-puzzle, two heuristics are …– Counts the tiles out of place in each state when it is compared

with the goal.– Sum all the distances by which the tiles are out of place, one for

each square a tile must be moved to reach its position in the goal state.

2 8 31 47 5

6

left

2 8 31 4

7 56

2 8 31 47 56

2 8 31 47 5

6

Page 8: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

Example

h1(S)=7

h2(S)=4+0+3+3+1+0+2+1=14

Page 9: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

Comparison of Heuristics applied to states in the 8-puzzle

2 8 31 4

7 56

2 8 31 47 56

2 8 31 47 5

6

5 6

3 4

5 6

Tile out of placeSum of distances out of place

h1 h2

Page 10: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

Admissible Heuristic

Page 11: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

A* Search

• Greedy best-first search is too greedy– It does not take into account the cost of the

path so far!

• Define– f(n)=g(n)+h(n)– g(n) is the cost of the path to node n– h(n) is the heuristic estimate of the cost of

reaching the goal from node n

• A* search – Expand node in fringe (queue) with lowest f

value

Page 12: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

2 8 31 47 5

6

left

2 8 31 4

7 56

2 8 31 47 56

2 8 31 47 5

66 4 6

2 8 31 4

7 56

2 8 31 47 56

28

31 47 56

5 5 6

28 31 4

7 56

2 8 31 47

56

28

31 47 56

5

1

2

3 4

5

Page 13: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.
Page 14: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

A*Another Example

Page 15: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

A*

Page 16: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

A*

Page 17: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

A*

Page 18: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

A*

Page 19: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

A*

Page 20: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

A*

Page 21: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

A* Search• Complete: Yes

• Time Complexity: Exponential

- Depends on h(n)

• Space Complexity: Exponential - Keeps all nodes in memory

• Optimal: Yes. If h(n) is an admissible heuristic

• Optimally efficient

Page 22: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

How fast is A*?A* is the fastest search algorithm. That is, for any given heuristic, no algorithm can expand fewer nodes than A*.

How fast is it? Depends of the quality of the heuristic.[(relative error in h) x (length of solution)]

Exponential time complexity in worst case. A good heuristic will help a lot here

O(bm) if the heuristic is perfect

•If the heuristic is useless (ie h(n) is hardcoded to equal 0 ), the algorithm degenerates to uniform cost.

•If the heuristic is perfect, there is no real search, we just march down the tree to the goal.

Generally we are somewhere in between the two situations above. The time taken depends on the quality of the heuristic.

Page 23: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

Dominance• Given two admissible

heuristics h1(n) and h2(n), which is better?

• If h2(n) h1(n) for all n, then– h2 is said to dominate h1– h2 is better for search

• For our 8-puzzle heuristic, does h2 dominate h1?

Page 24: Greedy best-first search Use the heuristic function to rank the nodes Search strategy –Expand node with lowest h-value Greedily trying to find the least-cost.

A*’s space complexity

Main problem: space complexity

A* has worst case O(bd) space complexity, but an iterative deepening version is possible ( IDA* )