0% found this document useful (0 votes)
46 views65 pages

Chapter. 03 - Solving Problems by Searching - Uninformed Search

Uploaded by

yakool008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views65 pages

Chapter. 03 - Solving Problems by Searching - Uninformed Search

Uploaded by

yakool008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

ĐÀO TẠO TỪ XA

Môn học: TRÍ TUỆ NHÂN TẠO


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

• Can a reflex agent be rational?


Video of Demo Reflex Optimal

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

• Optimal vs. complete planning


• Planning vs. re-planning
Video of Demo Planning

12
Video of Demo Re-planning

13
2. Search
Problems
Search Problems
• A search problem consists of:

o A state space “N”, 1.0

o A successor function
(with actions, costs)
“E”, 1.0

o A start state and a goal test

• A solution is a sequence of actions (a plan) which transforms the


start state to a goal state
Search Problems
• A search problem consists of:

o A state space “N”, 1.0

o A successor function
(with actions, costs)
“E”, 1.0

o A start state and a goal test

• A solution is a sequence of actions (a plan) which transforms the


start state to a goal state
Search Problems Are Models
Example: Traveling in
Romania

• 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

• Problem: eat all dots while keeping the ghosts perma-scared


• What does the state space have to specify?
o agent position, dot Booleans, power pellet Booleans, remaining scared time
a G
b c

e
d f
S h
p r
q

3. State Space Graphs and


Search Trees
State Space Graphs
• State space graph: A mathematical
representation of a search problem
o Nodes are (abstracted) world con4igurations
o Arcs represent successors (action results)
o The goal test is a set of goal nodes (maybe only one)
• In a state space graph, each state occurs only
once!
• We can rarely build this full graph in memory
(it’s too big), but it’s a useful idea
a G
State Space Graphs b c

e
• State space graph: A mathematical d f
representation of a search problem S h

o Nodes are (abstracted) world configurations p


q
r

o Arcs represent successors (action results)


o The goal test is a set of goal nodes (maybe only one) Tiny state space graph for a 1ny
search problem
• In a state space graph, each state occurs only
once!
• We can rarely build this full graph in memory
(it’s too big), but it’s a useful idea
Search Trees This is now / start
“N”, 1.0 “E”, 1.0

Possible futures

• A search tree:
o A “what if” tree of plans and their outcomes

o The start state is the root node

o Children correspond to successors

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

Fringe a G Implementation: Fringe


b c is a LIFO stack
s
sàd e
d f
sàe S h
sàp p r
sàdàb q Node Sequence: SdbacaehpqqrfcaG
sàdàc
sàdàe S
sàdàbàa
sàdàcàa d e p
sàdàeàh
b c e h r q
sàdàeàr
sàdàeàhàp
a a h r p q f
sàdàeàhàq
sàdàeàhàpàq q c G
p q f
sàdàeàràf
sàdàeàràfàc q c G a
sàdàeàràfàG
sàdàeàràfàcàa a
Depth-First Search (DFS) Properties
• What nodes DFS expand? 1 node
b
o From the leftmost side of the tree .irst. … b nodes
o Could process the whole tree! b2 nodes
o If m is .inite, takes time O(bm)
m tiers
• How much space does the fringe take?
o Only has siblings on path to root, so O(bm)

• 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

function TREE-SEARCH(problem, strategy) returns a solution, or failure


initialize the search tree using the initial state of problem
loop do
if there are no candidates for expansion then return failure
choose a leaf node for expansion according to strategy: expand a deepest node first
if the node contains a goal state then return the corresponding solution
else expand the node and add the resulting nodes to the search tree
end

• Implementation:
o Fringe is a LIFO Stack

39
DFS Pseudocode

Iterative Pseudocode Recursive Pseudocode


DFS(Graph G, node u): DFS(Graph G, node u):
let St be stack mark u as visited
Push u in the stack for neighbors adj_node of u in Graph G:
mark u as visited. if adj_node is not visited:
while (St is not empty) DFS(G, adj_node)
v = Node at the top of stack
remove the node from stack
for all neighbors adj_node of v in Graph G:
if adj_node is not visited :
mark adj_node as visited
push adj_node in stack
Breadth-First Search
Breadth-First Search
Strategy: expand a a G
shallowest node first b c
Implementation: Fringe e
d f
is a FIFO queue S h
p q r
Node Sequence: SdepbcehrqaahrpqfpqfqcG

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)

• How much space does the fringe take? bm nodes


o Has roughly the last tier, so 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

function TREE-SEARCH(problem, strategy) returns a solution, or failure


initialize the search tree using the initial state of problem
loop do
if there are no candidates for expansion then return failure
choose a leaf node for expansion according to strategy: expand a shallowest node first
if the node contains a goal state then return the corresponding solution
else expand the node and add the resulting nodes to the search tree
end

• 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

• When will BFS outperform DFS?


• When will DFS outperform BFS?
b
Iterative Deepening …

• Idea: get DFS’s space advantage with BFS’s


time / shallow-solution advantages
o Run a DFS with depth limit 1. If no solution…
o Run a DFS with depth limit 2. If no solution…
o Run a DFS with depth limit 3. …..

• Isn’t that wastefully redundant?


o Generally most work happens in the lowest level
searched, so not so bad!
o Branching factor 10, solution 5 deep:
§ BFS: 10 + 100 + 1,000 + 10,000 + 100,000 = 111,110
§ IDS: 50 + 400 + 3,000 + 20,000 + 100,000 = 123,450
5. Uninformed Search with cost:
Uniform-Cost Search
Cost-Sensitive Search
a GOAL
2 2
b c
3
2
1 8
2 e
3 d
f
9 8 2
START h
1 4 2
p 4 r
1 q
5

• BFS finds the shortest path in terms of number of actions.


• It does not find the least-cost path.
• We will now cover a similar algorithm which does find the
least-cost path.
Uniform Cost Search
Uniform Cost Search
2 a G
Strategy: expand a b c
cheapest node first: 1 8 2
2 e
3 d f
Fringe is a priority queue 9 2
S h 8 1
(priority: cumulative cost)
1 p q r
15

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

function TREE-SEARCH(problem, strategy) returns a solution, or failure


initialize the search tree using the initial state of problem
loop do
if there are no candidates for expansion then return failure
choose a leaf node for expansion according to strategy: expand a lowest cost node first
if the node contains a goal state then return the corresponding solution
else expand the node and add the resulting nodes to the search tree
end

• 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)

• How much space does the fringe take?


o Has roughly the last tier, so O(bC*/e)

• 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

Uniform Cost Issues c£2


c£3
• Remember: UCS explores increasing cost
contours

• The good: UCS is complete and optimal!


Start Goal

• The bad:
o Explores options in every “direction”
o No information about goal location

• We’ll fix that soon!


Video of Demo Maze with Deep/Shallow Water ---
DFS, BFS, or UCS? (part 1)

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

You might also like