Solving Problems by Searching: The Theory and Technology of Building Agents That Can Plan Ahead To Solve Problems
Solving Problems by Searching: The Theory and Technology of Building Agents That Can Plan Ahead To Solve Problems
CS 370 1
Example - Navigation Problem
Given a map, pick the best route from start
state to the goal state.
Assumptions
Environment is Fully observable
State space Known
Environment is Discrete
Environment is Deterministic
Environment is static
CS 370 2
Example: Romania
CS 370 3
Problem formulation
A problem is defined by five items:
CS 370 4
Example: Romania
Frontier
Explored set
Unexplored set
CS 370 5
Tree Search Algorithm
Tree-search (problem p) returns path
frontier = {path(p.initial)}
loop:
if frontier is empty; return fail
path = frontier.pop()
s = path.end
if goaltest(s); return path
for a in p.action(s)
add [path+p.result(s,a)]to frontier
CS 370 6
Example: Romania – Tree Search
CS 370 7
Tree search example
CS 370 8
Tree search example
CS 370 9
Tree search example
CS 370 10
Graph Search Algorithm
Tree -search (problem p) returns path
frontier = {path(p.initial)}, explored = {}
loop:
if frontier is empty; return fail
path = frontier.pop()
s = path.end
CS 370 11
Graph Search Algorithm
Graph-search (problem p) returns path
frontier = {path(p.initial)}, explored = {}
loop:
if frontier is empty; return fail
path = frontier.pop()
s = path.end
add s to explored list
if goaltest(s); return path
for a in p.action(s)
if p.result(s,a)] not in explored list:
frontier.update([path+p.result(s,a)])
CS 370 12
Graph Search Algorithm
Graph-search (problem p) returns path
frontier = {path(p.initial)}, explored = {}
loop:
if frontier is empty; return fail
path = frontier.pop()
s = path.end
add s to explored list Different Search Strategies
if goaltest(s); return path differ in the way elements are
for a in p.action(s) removed from the frontier
if p.result(s,a)] not in explored list:
frontier.update([path+p.result(s,a)])
CS 370 13
Search strategies
A search strategy is defined by order of paths are removed
from the frontier.
CS 370 14
Uninformed search strategies
Uninformed search strategies use only the
information available in the problem
definition
Breadth-first search (Uses Queue)
Uniform-cost search (Uses Priority Queue)
Depth-first search (Uses Stack)
CS 370 15
Breadth-first search
The BFS begins at the initial state/node s,
and inspects all the neighboring nodes. Then
for each of those neighbor nodes in turn, it
inspects their neighbor nodes which were
unvisited, and so on.
CS 370 16
Breadth-first search
Implementation:
Use FIFO queue to implement frontier, i.e.,
paths are removed based on first in first out.
CS 370 17
Breadth-first search
CS 370 18
Breadth-first search
CS 370 19
Breadth-first search
CS 370 20
Properties of breadth-first search
Complete? Yes (if b is finite)
CS 370 21
Properties of breadth-first search
Time? 1+b+b2+b3+… +bd + bd+1 = O(bd+1)
Imagine searching a uniform tree where every state has “b”
successors. The root of the search tree generates “b” nodes at the first
level, each of which generates “b” more nodes, for a total of “b2” at the
second level. Each of these generates “b” more nodes, yielding “b3”
nodes at the third level, and so on. Now suppose that the solution is at
depth “d”. In the worst case, it is the last node generated at that level.
Then the total number of nodes generated is
If the algorithm were to apply the goal test to nodes when selected for
expansion, rather than when generated, the whole layer of nodes at
depth d would be expanded before the goal was detected and the time
complexity would be O(bd+1).
CS 370 22
Properties of breadth-first search
Complete? Yes (if b is finite)
CS 370 24
Uniform-cost search
Expand least-cost unexpanded node
Implementation:
Use priority queue = queue ordered by path cost
Equivalent to breadth-first if step costs all equal
CS 370 25
Example: Romania – Uniform Cost
Next, adding Pitesti with cost 80 + 97=177. The least-cost node is now Fagaras, so it
is expanded, adding Bucharest with cost 99+211=310. Now a goal node has been
generated.
BUT uniform-cost search keeps going, choosing Pitesti for expansion and adding a
second path to Bucharest with cost 80+97+101= 278.
Now the algorithm checks to see if this new path is better than the old one; it is, so
the old one is discarded. Bucharest, now with g-cost 278, is selected for expansion
and the solution is returned.
CS 370 26
Properties: Uniform Cost Search
Complete? Yes, if step cost greater than small
positive constant
CS 370 28
Depth-first search
Expand longest/deepest unexpanded node
CS 370 29
Depth-first search
Expand deepest unexpanded node
CS 370 30
Depth-first search
Expand deepest unexpanded node
CS 370 31
Depth-first search
Expand deepest unexpanded node
CS 370 32
Depth-first search
Expand deepest unexpanded node
CS 370 33
Depth-first search
Expand deepest unexpanded node
CS 370 34
Depth-first search
Expand deepest unexpanded node
CS 370 35
Depth-first search
Expand deepest unexpanded node
CS 370 36
Depth-first search
Expand deepest unexpanded node
CS 370 37
Depth-first search
Expand deepest unexpanded node
CS 370 38
Depth-first search
Expand deepest unexpanded node
CS 370 39
Properties of depth-first search
Complete? No: fails in infinite-depth spaces, spaces
with loops
complete in finite spaces
Optimal? No
CS 370 40
Summary of algorithms
CS 370 41
Informed search algorithms
Outline
• Best-first search
• Greedy best-first search
• A* search
CS 370 42
Best-first search
Idea: use an evaluation function f(n) for each node as
estimate of "desirability”
Expand most desirable unexpanded node
Implementation:
Order the nodes in priority Queue in decreasing order of
desirability
Special cases:
greedy best-first search
A* search
CS 370 43
Romania with step costs in km
CS 370 44
Greedy best-first search
Evaluation function f(n) = h(n) (heuristic)
f(n)= estimate of cost from node 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
CS 370 45
Romania with step costs in km
CS 370 46
Greedy best-first search example
CS 370 47
Greedy best-first search example
CS 370 48
Greedy best-first search example
CS 370 49
Greedy best-first search example
CS 370 50
Properties of greedy best-first search
Complete?
Tree search: No – can get stuck in loops, e.g.,
Iasi Neamt Iasi Neamt
Graph search: yes in finite spaces
Time? O(bm),
Space? O(bm) -- keeps all nodes in memory
Optimal? No
CS 370 51
A* search
Idea: combine uniform-cost and greedy
best-first search
Evaluation function f(n) = g(n) + h(n)
g(n) = cost so far to reach n
h(n) = estimated cost from n to goal
f(n) = estimated total cost of path through n
to goal
CS 370 52
Romania with step costs in km
CS 370 53
A* search example
CS 370 54
A* search example
CS 370 55
A* search example
CS 370 56
A* search example
CS 370 57
A* search example
CS 370 58
A* search example
CS 370 59
Admissible heuristics
A heuristic h(n) is admissible if for every node n,
h(n) ≤ h*(n), where h*(n) is the true cost to reach
the goal state from n.
If h is consistent, we have
CS 370 63
CS 370 64
Summary
Problem formulation usually requires abstracting away real-
world details to define a state space that can feasibly be
explored
Uninformed search strategies
Informed search strategies
CS 370 65
Example 1