Chapter. 03 - Solving Problems by Searching - Uninformed Search
Chapter. 03 - Solving Problems by Searching - Uninformed Search
Chapter 03
Objectives
• Before an agent can start searching, a well-de4ined problem must be
formulated.
• A problem consists of 4ive parts: the initial state, a set of actions, a transition
model describing the results of those actions, a set of goal states, and an
action cost function.
• The environment of the problem is represented by a state space graph. A
path through the state space (a sequence of actions) from the initial state to
a goal state is a solution.
• Uninformed search methods have access only to the problem de4inition.
Algorithms build a search tree in an attempt to 4ind a solution. Algorithms
differ based on which node they expand 4irst:
Objectives
• Best-first search selects nodes for expansion using an evaluation function.
• Breadth-first search expands the shallowest nodes first; it is complete,
optimal for unit action costs, but has exponential space complexity.
• Uniform-cost search expands the node with lowest path cost, and is optimal
for general action costs.
• Depth-first search expands the deepest unexpanded node first. It is neither
complete nor optimal, but has linear space complexity. Depth-limited
search adds a depth bound.
• Iterative deepening search calls depth-first search with increasing depth
limits until a goal is found. It is complete when full cycle checking is done,
optimal for unit action costs, has time complexity comparable to breadth-
first search, and has linear space complexity.
Contents 1. Agents that Plan Ahead
2. Search Problems
3. State Space Graphs and Search Trees
4. Uninformed Search Methods
o Depth-First Search
o Breadth-First Search
o Iterative Deepening
o Uniform-Cost Search
1. Agents that
Plan
Agents that Plan
• When the correct action to take is not
immediately obvious, an agent may need
to plan ahead:
o to consider a sequence of actions that form a
path to a goal state.
• Such an agent is called a problem-solving agent, and the
computational process it undertakes is called search.
• In this chapter, we consider only the simplest environments:
episodic, single agent, fully observable, deterministic, static,
discrete, and known.
Reflex Agents
• Reflex agents:
o Choose action based on current percept (and
maybe memory)
o May have memory or a model of the world’s
current state
o Do not consider the future consequences of
their actions
9
Video of Demo Reflex Odd
10
Planning Agents
• Planning agents:
o Ask “what if”
o Decisions based on (hypothesized)
consequences of actions
o Must have a model of how the world evolves in
response to actions
o Must formulate a goal (test)
o Consider how the world WOULD BE
12
Video of Demo Re-planning
13
2. Search
Problems
Search Problems
• A search problem consists of:
o A successor function
(with actions, costs)
“E”, 1.0
o A successor function
(with actions, costs)
“E”, 1.0
• State space:
o Cities
• Successor function:
o Roads: Go to adjacent city with cost = distance
• Start state:
o Arad
• Goal test:
o Is state == Bucharest?
• Solution?
What’s in a State Space?
The world state includes every last detail of the environment
A search state keeps only the details needed for planning (abstrac;on)
• Problem: Pathing • Problem: Eat-All-Dots
- States: (x,y) location - States: {(x,y), dot Booleans}
- Actions: NSEW - Actions: NSEW
- Successor: update location only - Successor: update location and possibly a dot Boolean
- Goal test: is (x,y)=END - Goal test: dots all false
State Space Sizes?
• World state:
o Agent positions: 120
o Food count: 30
o Ghost positions: 12
o Agent facing: NSEW
• How many
o World states?
120x(230)x(122)x4
o States for pathing?
120
o States for eat-all-dots?
120x(230)
Exercise: Safe Passage
e
d f
S h
p r
q
e
• State space graph: A mathematical d f
representation of a search problem S h
Possible futures
• A search tree:
o A “what if” tree of plans and their outcomes
o Nodes show states, but correspond to PLANS that achieve those states
o For most problems, we can never actually build the whole tree
State Space Graphs vs. Search Trees
Each NODE in in
State Space Graph the search tree is Search Tree
an entire PATH in
the state space S
G graph. e p
a d
b c
b c e h r q
e
d f a a h r p q f
S h We construct both
on demand – and p q f q c G
p q r
we construct as q c a
G
li:le as possible.
a
26
Exercise: State Space Graphs vs. Search Trees
Consider this 4-state graph: How big is its search tree (from S)?
a s
a b
S G
b G a G
b a G b G
… …
Important: Lots of repeated structure in the search tree!
Tree Search
Search Example: Romania
Searching with a Search Tree
• Search:
o Expand out potential plans (tree nodes)
o Maintain a fringe of partial plans under consideration
o Try to expand as few tree nodes as possible
General Tree Search
• Important ideas:
o Fringe
o Expansion
o Exploration strategy
• Main question: which fringe nodes to explore?
Example: Tree Search
a G
b c
e
d f
S h
Search Tree Fringe
p q r
s
S sàd
sàe
d e p
sàp
q sàdàb
b c e h r
sàdàc
a a h r p q f sàdàe
sàdàeàh
p q f q c G sàdàeàr
sàdàeàràf
q c G a sàdàeàràfàc
sàdàeàràfàG
a
32
4. Uninformed Search:
Depth-First Search
Breadth-First Search
Iterative Deepening
Uninformed Search
• An uninformed search algorithm
is given no clue about how close
a state is to the goal(s).
• For example, consider our agent
in Arad with the goal of reaching
Bucharest.
o An uninformed agent with no knowledge of Romanian geography has no clue
whether going to Zerind or Sibiu is a better first step.
o In contrast, an informed agent who knows the location of each city knows that
Sibiu is much closer to Bucharest and thus more likely to be on the shortest path.
Search Algorithm Properties 1 node
• Complete: Guaranteed to 4ind a b
… b nodes
solution if one exists? b2 nodes
• Optimal: Guaranteed to 4ind the least m tiers
cost path?
• Time complexity?
• Space complexity? bm nodes
• Cartoon of search tree:
o b is the branching factor
o m is the maximum depth
o solutions at various depths
• Number of nodes in entire tree?
o 1 + b + b2 + …. bm = O(bm)
Depth-First Search
Strategy: expand a
Depth-First Search deepest node first
• Is it complete? bm nodes
o m could be in.inite, so only if we prevent
that
• Is it optimal?
o No, it .inds the “leftmost” solution,
regardless of depth or cost
DFS Pseudocode
• Implementation:
o Fringe is a LIFO Stack
39
DFS Pseudocode
d e p
Search
b c e h r q
Tiers
a a h r p q f
p q f q c G
q c G a
a
Breadth-First Search (BFS) Properties
1 node
b
• What nodes does BFS expand? … b nodes
o Processes all nodes above shallowest s tiers
b2 nodes
solution
o Let depth of shallowest solution be s bs nodes
o Search takes time O(bs)
• Is it complete?
o s must be Pinite if a solution exists, so yes!
• Is it optimal?
o Only if costs are all 1 (more on costs later)
BFS Pseudocode
• Implementation:
o Fringe is a FIFO Queue
44
BFS Pseudocode
Iterative Pseudocode
BFS(Graph G, node u)
Let q be the queue
insert node u to the queue
mark u node as visited.
while (q is not empty):
B = Node at the head of queue
remove the node from queue
for all neighbors C of B in Graph G :
if C is not visited:
insert C to the queue
mark C as visited
Video of Demo Empty DFS/BFS (part 1)
46
Video of Demo Empty DFS/BFS (part 2)
47
Exercise: DFS vs BFS
S 0
d 3 e 9 p 1
b 4 c e 5 h 17 r 11 q 16
11
Cost a 6 a h 13 r 7 p q f
contours
p q f 8 q c G
q 11 c G 10 a
a
UCS Pseudocode
• Implementation:
o Fringe is a priority Queue (priority: cumulative cost)
54
UCS Pseudocode
Iterative Pseudocode
UCS(Graph G, node u)
Let q be the priority queue
insert node u to the priority queue
mark u node as visited.
while (q is not empty):
B = Node at the head of queue
remove the node from queue
for all neighbors C of B in Graph G :
if C is not visited:
if C is not in the priority queue:
insert C to the priority queue
sort the priority queue with cost as criterion
else if C is in the priority queue with higher cost:
replace existing node with C
mark C as visited
Uniform Cost Search (UCS) Properties
b
• What nodes does UCS expand? …
c£1
o Processes all nodes with cost less than cheapest solution! c£2
C*/e “tiers”
o If that solution costs C* and arcs cost at least e , then the
“effective depth” is roughly C*/e c£3
o Takes time O(bC*/e) (exponential in effective depth)
• Is it complete?
o Assuming best solution has a .inite cost and minimum arc cost
is positive, yes!
• Is it optimal?
o Yes! (Proof next lecture via A*)
… c£1
• The bad:
o Explores options in every “direction”
o No information about goal location
58
Video of Demo Maze with Deep/Shallow Water ---
DFS, BFS, or UCS? (part 2)
59
Video of Demo Maze with Deep/Shallow Water ---
DFS, BFS, or UCS? (part 3)
60
The One Queue
• All these search algorithms are the
same except for fringe strategies
o Conceptually, all fringes are priority
queues (i.e. collections of nodes with
attached priorities)
o Practically, for DFS and BFS, you can
avoid the log(n) overhead from an
actual priority queue, by using stacks
and queues
o Can even code one implementation
that takes a variable queuing object
Comparing uninformed search algorithms
BFS Pseudocode
UCS
DFS & IDS