5 Inf - Search Aif18
5 Inf - Search Aif18
5 Inf - Search Aif18
Things to Differentiate
Questions? • Goal testing
• Expanding
• Generating
3
4
5 6
1
Depth-First Iterative Deepening (DFID) Depth-First Iterative Deepening (DFID)
1. DFS to depth 0 (i.e., treat start node as until solution found do: 1. DFS to depth 0 (i.e., treat start node as until solution found do:
having no successors) DFS with depth cutoff c; having no successors) DFS with depth cutoff c;
2. Iff no solution found, do DFS to depth 1 c = c+1 2. Iff no solution found, do DFS to depth 1 c = c+1
• Complete • Complete
• Optimal/Admissible if all operators have the same cost • Optimal/Admissible if all operators have the same cost
• Otherwise, not optimal, but guarantees finding solution of shortest length
The key: at every stage,
• Otherwise, not optimal, but guarantees finding solution of shortest length
• Time complexity is a little worse than BFS or DFS because nodes near
the top of the search tree are generated multiple times
throw away work from
• Time complexity is a little worse than BFS or DFS because nodes near
the top of the search tree are generated multiple times
• Because most nodes are near the bottom of a tree, worst case time •
previous stages (or you
Because most nodes are near the bottom of a tree, worst case time
complexity is still exponential, O(bd) don’t save
complexity is still exponential, O(bd) anything!)
7 8
12
2
S
Depth-First Search 3 8
Example for Illustrating Search Strategies 1
A B C
3
7 15 20 5
D E G
S Expanded node Nodes list
8 { S0 }
3 1 S0 { A3 B1 C8 }
A B C A3 { D6 E10 G18 B1 C8 }
3 D6 { E10 G18 B1 C8 }
15
7 20 5 E10 { G18 B1 C8 }
G18 { B1 C8 }
D E G Solution path found is S A G, cost 18
Number of nodes expanded (including goal node) = 5
13 14
S S
A B C A B C
3 3
7 15 20 7 15 20
5 5
D E G Expanded node Nodes list D E G
Expanded node Nodes list { S0 }
{ S0 } S0 { A3 B1 C8 }
S0 { A3 B1 C8 } A3 { B1 C8 D6 E10 G18 }
A3We won’t go through these in
{ D6 E10 G18 B1 C8 } B1 { C8 D6 E10 G18 G21 }
E10
detail, but please make sure
{ G18 B1 C8 } D6 { E10 G18 G21 G13 }
A B C
3
7 15 20 5
Expanded node Nodes list D E • Depth-First Search:
G
{ S0 } S
• Expanded nodes: S A D E G
• Solution found: S A G (cost 18) 3 1 8
S0 { B1 A3 C8 }
B1 { A3 C8 G21 } • Breadth-First Search: A B C
A3 { D6 C8 E10 G18 G21 } • Expanded nodes: S A B C D E G 3 15
7 20
• Solution found: S A G (cost 18) 5
D6 { C8 E10 G18 G1 }
D E G
C8 { E10 G13 G18 G21 } • Uniform-Cost Search:
• Expanded nodes: S A D B C E G
E10 { G13 G18 G21 }
• Solution found: S C G (cost 13)
G13 { G18 G21 } This is the only uninformed search that worries about costs.
Solution path found is S C G, cost 13
• Iterative-Deepening Search:
Number of nodes expanded (including goal node) = 7 • nodes expanded: S S A B C S A D E G
• Solution found: S A G (cost 18)
17
3
Bi-directional Search Bi-directional Search
• Alternate searching from • Alternate searching from
• start state à goal • start state à goal
• goal state à start • goal state à start
• Stop when the frontiers intersect. • Stop when the frontiers intersect.
Thought problems: What’s a real-
• Works well only when there are • Works well only when there are
unique start and goal states world
unique start problem where
and goal states you can’t
• Requires ability to generate • generate
Requires ability predecessors?
to generate
“predecessor” states. “predecessor” states.
• Can (sometimes) find a solution fast • Can (sometimes) find a solution fast
19 20
21 22
S
A State Space that Generates an
Holy Grail Search 3 1 8
4
Holy Grail Search
Why not go straight to the solution, without
Informed Search
any wasted detours off to the side?
27
25
30 31
5
Heuristic Search Heuristics Examples
• 8-puzzle:
• Romania:
• # of tiles in wrong place
• Eyeballing it à certain cities first
• They “look closer” to where we are going • 8-puzzle (better):
• Sum of distances from goal
• Can domain
• Captures distance and
knowledge be number of nodes
captured in a
heuristic? • Romania:
• Straight-line distance from
S
34 35 goal state
36 37
6
Straight Lines to Budapest (km) Admissible Heuristics
hSLD(n)
• Admissible heuristics never overestimate cost
• They are optimistic – think goal is closer than it is
• h(n) ≤ h*(n)
• where h*(n) is true cost to reach goal from n
• hLSD(Lugoj) = 244
• Can there be a shorter path?
40 41
7
Straight Lines to Budapest (km) Greedy Best-First Search: Ex. 1
hSLD(n) What can we
say about the
search space?
224
G
242
hSLD(n)
46 47
48 49
8
Beam Search Quick Terminology Reminders
• Use an evaluation function f (n) = h(n), but the • What is f (n)? • What is h*(n)?
maximum size of the nodes list is k, a fixed constant • An evaluation function • A heuristic function that
that gives… gives the…
• Only keeps k best nodes as candidates for expansion, • A cost estimate of...
• True cost to reach goal from n
and throws the rest away • The distance from n to G
• Why don’t we just use that?
• More space-efficient than greedy search, but may • What is h(n)?
• What is g(n)?
throw away a node that is on a solution path • A heuristic function
that… • The path cost of getting from
• Not complete • Encodes domain S to n
knowledge about... • describes the “already spent”
• Not admissible • The search space costs of the current search
50
Algorithm A* A* Search
• Use evaluation function f (n) = g(n) + h(n) • Idea: Evaluate nodes by combining g(n), the cost of
reaching the node, with h(n), the cost of getting from
• g(n) = minimal-cost path from any S to state n the node to the goal.
• That is, the cost of getting to the node so far 0 S cost
• Evaluation function: 8 h
• Ranks nodes on frontier by estimated cost of solution
f(n) = g(n) + h(n)
• From start node, through given node, to goal • g(n) = cost so far to reach n
8 C 3
• h(n) = estimated cost from n to goal 5
• Not complete if h(n) can = ∞ g
• f(n) = estimated total cost of path
through n to goal 9 G 0
59 60
A* Search A* Example 1
• Avoid expanding paths that are already expensive
• Combines costs-so-far with expected-costs
61 62
9
A* Example 1 A* Example 1
63 64
A* Example 1 A* Example 1
65 66
A* Example 1 Algorithm A*
• Algorithm A with constraint that h(n) ≤ h*(n)
• h*(n) = true cost of the minimal cost path from n to a goal.
67 68
10
Example Search Space Revisited Example
start state n g(n) h(n) f (n) h*(n)
parent pointer 0 S 8
S 0 8 8 9 cost
0 S 8 arc cost A1 8 9 9 1 5 8 h
B5 4 9 4 1 A 8 5 B 4 8 C 3
1 5 8 C8 3 11 5 3 9
7 4 5
D4 ∞ ∞ ∞ g
4 D ∞ 8 E ∞ 9 G 0
1 A 8 E8 ∞ ∞ ∞
5 B 4 8 C 3 G9 0 9 0
3 9 h value
7 4 5 • h*(n) is the (hypothetical) perfect heuristic.
g value
• Since h(n) ≤ h*(n) for all n, h is admissible
4 D ∞ 8 E ∞ 9 G 0
• Optimal path = S B G with cost 9.
69 goal state 70
11
Admissible heuristics Dealing with Hard Problems
E.g., for the 8-puzzle: • For large problems, A* often requires too much space.
• h1(n) = number of misplaced tiles • Two variations conserve memory: IDA* and SMA*
• h2(n) = total Manhattan distance • IDA* – iterative deepening A*
(i.e., # of squares each tile is • uses successive iteration with growing limits on f. For example,
from desired location) Start • A* but don’t consider any node n where f (n) > 10
• A* but don’t consider any node n where f (n) > 20
• A* but don’t consider any node n where f (n) > 30, ...
77 78
12
In-Class 8
In-class Exercise: Creating Heuristics S
Remove 5 Exercise 8
8-Puzzle Boat Problems Sticks 3 1
arc 4
cabbage
sheep cost A 8 B C 3
wolf
3 15
7 20 5 h value
Route Planning
D ∞ E ∞ G 0
N-Queens Water Jug Problem
Apply the following to search this space. At each search step, show:
the current node being expanded, g(n) (path cost so far), h(n) (heuristic
estimate), f (n) (evaluation function), and h*(n) (true goal distance).
5
2 Depth-first search Breadth-first search A* search
Uniform-cost search Greedy search
81
13