Professional Documents
Culture Documents
Uninformed Search
State-based Models
– Model task as a graph of all possible states
l Called a “state-space graph”
Chapter 3.1 – 3.4
– A state captures all the relevant information about
the past in order to act (optimally) in the future
– Actions correspond to transitions from one state to
another
– Solutions are defined as a sequence of
steps/actions (i.e., a path in the graph)
l Puzzles
l Games
l Navigation
l Assignment
l Motion planning
l Scheduling
l Routing Actions: go straight, turn left, turn right
Goal: shortest? fastest? most scenic?
1
Search Example: River Crossing Problem Search Example: 8-Puzzle
Goal: All on
right side of river
Rules:
1) Farmer must row the boat
2) Only room for one other Actions: F>, F<,
3) Without the farmer present: FC>, FC<, FD>,
• Dog bites sheep FD<, FS>, FS< Actions: move tiles (e.g., Move2Down)
• Sheep eats cabbage Goal: reach a certain configuration
Search Example: Water Jugs Problem Search Example: Robot Motion Planning
4 3
2
Search Example: 8-Queens
What Knowledge does the Agent
Need?
How should the Environment be What Goal does the Agent want to
Represented? Achieve?
l Determining what to represent is difficult and is l How do you know when the goal is reached?
usually left to the system designer to specify – with a goal test that defines what it means
to have achieved the goal
l Problem State = representation of all necessary – or, with a set of goal states
information about the environment
l Determining the goal is usually left to the system
designer or user to specify
l State Space (aka Problem Space) = all possible valid
configurations of the environment
15 16
3
What Actions does the Agent Need? What Actions does the Agent Need?
l Discrete and Deterministic task assumptions imply l A finite set of actions/operators needs to be
– decomposed into atomic steps that are discrete and
indivisible, and therefore can be treated as
l Given:
– an action (aka operator or move)
instantaneous
– sufficient to describe all necessary changes
– a description of the current state of the world
l States = configurations
l Actions = up to 4 kinds of moves: up, down, left,
4 3
right State: (x, y) for # liters in 4-liter and 3-liter pitchers, respectively
Actions: empty, fill, pour water between pitchers
Initial state: (0, 0)
Goal state: (2, *)
4
Action / Successor Functions
Formalizing Search in a State Space
1. (x, y | x < 4) (4, y) “Fill 4”
l A state space is a directed graph: (V, E)
2. (x, y | y < 3) (x, 3) “Fill 3” – V is a set of nodes (vertices)
3. (x, y | x > 0) (0, y) “Empty 4” – E is a set of arcs (edges)
4. (x, y | y > 0) (x, 0) “Empty 3” each arc is directed from one node to another node
5. (x, y | x+y ≥ 4 and y > 0) (4, y - (4 - x)) l Each node is a data structure that contains:
– a state description
“Pour from 3 to 4 until 4 is full”
– other information such as:
6. (x, y | x+y ≥ 3 and x > 0) (x - (3 - y), 3) l link to parent node
“Pour from 4 to 3 until 3 is full” l name of action that generated this node (from its
7. (x, y | x+y ≤ 4 and y > 0) (x+y, 0) parent)
l other bookkeeping data
“Pour all water from 3 to 4”
22
l Each arc corresponds to one of the finite number of l Each node has a finite set of successor nodes:
actions: – corresponding to all the legal actions
– when the action is applied to the state associated that can be applied at the source node's state
with the arc's source node
– then the resulting state is the state associated with l Expanding a node means:
the arc's destination node – generate all successor nodes
– add them and their associated arcs to the state-
l Each arc has a fixed, positive cost: space search tree
– corresponds to the cost of the action
23 24
5
Formalizing Search in a State Space Search Summary
l One or more nodes are designated as start nodes • Solution is an ordered sequence of
l A goal test is applied to a node's state to determine primitive actions (steps)
if it is a goal node f(x) = a1, a2, …, an where x is the input
l A solution is a sequence of actions associated with
• Model task as a graph of all possible states
a path in the state space from a start to a goal node:
– just the goal state (e.g., cryptarithmetic)
and actions, and a solution as a path
– a path from start to goal state (e.g., 8-puzzle) • A state captures all the relevant information
l The cost of a solution is the sum of the arc costs about the past
on the solution path
25
6
State Space = A Directed Graph
Formalizing Search
F C D S
A search problem has five components:
S, I, G, actions, cost
? F C D S
1. State space S : all valid configurations
D, CFS DFS, C
2. Initial states I ⊆ S: a set of start states I = {(FCDS,)} ⊆ S
3. Goal states G ⊆ S: a set of goal states G = {(,FCDS)} ⊆ S CSDF, CD,SF CDF, S S, CFD SF, CD , CSDF
successors((FCDS,)) = {(CD,FS)}
successors((CDF,S)) = {(CD,FS), (D,FCS), (C,FSD)}
l In general, there will be many generated, but un-
5. A cost function cost(s, s’ ): The cost of moving from expanded, states at any given time during a search
s to s’
l One has to choose which one to “expand” next
l The goal of search is to find a solution path from a
state in I to a state in G
called
if Frontier is empty then return failure
“expanding” pick a node, n, from Frontier
Start C, DSF CSF, D Goal
node n
if n is a goal node then return solution
Generate all n’s successor nodes and add
them all to Frontier
Remove n from Frontier
33
7
A State Space Graph
Formalizing Search in a State Space
a GOAL
t
l This algorithm does NOT detect a goal when
the node is generated b c
41
8
8-Puzzle State-Space Search Tree
Uninformed Search Strategies
(Not all nodes shown;
e.g., no “backwards”
moves) Uninformed Search: strategies that order nodes
without using any domain specific information, i.e.,
don’t use any information stored in a state
9
Breadth-First Search (BFS) Breadth-First Search (BFS)
9 4 6 2 9 4 6 2
6 G 1 6 G 1
D E goal F D E goal F
7 7
H H
46 47
7 7
H H
48 49
10
Breadth-First Search (BFS) Breadth-First Search (BFS)
7 7
H H
50 51
H H
52 53
11
Breadth-First Search (BFS) Evaluating Search Strategies
l Completeness
generalSearch(problem, queue)
S
If a solution exists, will it be found?
# of nodes tested: 7, expanded: 6 start – a complete algorithm will find a solution (not all)
expnd. node Frontier list
5 2 4
{S}
S {A,B,C} l Optimality / Admissibility
A B C
A {B,C,D,E} If a solution is found, is it guaranteed to be optimal?
B {C,D,E,G} – an admissible algorithm will find a solution with
9 4 6 2
C {D,E,G,F} minimum cost
6 G 1
D {E,G,F,H} D E goal F
E {G,F,H,G}
G {F,H,G} 7
path: S,B,G
H cost: 8
54 56
l Space Complexity
How much space is used by the algorithm?
– measured in terms of the maximum size Goal
of Frontier during the search
57
12
Breadth-First Search (BFS) Breadth-First Search (BFS)
l Optimal / Admissible?
– Yes, if all operators (i.e., arcs) have the same l Very slow to find solutions with a large number of steps
constant cost, or costs are positive, non-decreasing because must look at all shorter length possibilities first
with depth
– otherwise, not optimal but does guarantee finding
solution of shortest length (i.e., fewest arcs)
59 60
Depth-First Search
Expand the deepest node first
Breadth-First Search (BFS)
1. Select a direction, go deep to the end
2. Slightly change the end
l A complete search tree has a total # of nodes =
1 + b + b2 + ... + bd = (b(d+1) - 1) / (b-1) 3.Slightly change the end some more…
– d: the tree's depth Use a Stack to order nodes in Frontier
– b: the branching factor at each non-leaf node
l For example: d = 12, b = 10
1 + 10 + 100 + ... + 1012 = (1013 - 1)/9 = O(1012)
– If BFS expands 1,000 nodes/sec and each node
uses 100 bytes of storage, then BFS will take 35
years to run in the worst case, and it will use 111
terabytes of memory!
Goal
61
13
Depth-First Search (DFS) Depth-First Search (DFS)
9 4 6 2 9 4 6 2
6 G 1 6 G 1
D E goal F D E goal F
7 7
H H
66 67
7 7
H H
68 69
14
Depth-First Search (DFS) Depth-First Search (DFS)
7 7
H H
70 71
15
Depth-First Search (DFS) Depth-First Search (DFS)
l May not terminate without a depth bound l Time complexity: O(bd) exponential
i.e., cutting off search below a fixed depth, D Space complexity: O(bd) linear
– d is the depth of the solution
l Not complete – b is the branching factor at each non-leaf node
– with or without cycle detection l Performs “chronological backtracking”
– and, with or without a depth cutoff
– i.e., when search hits a dead end, backs up one
level at a time
l Not optimal / admissible
– problematic if the mistake occurs because of a bad
action choice near the top of search tree
l Can find long solutions quickly if lucky
76 77
H
78 79
16
Uniform-Cost Search (UCS) Uniform-Cost Search (UCS)
7 7
H H
80 81
7 7
H H
82 83
17
Uniform-Cost Search (UCS) Uniform-Cost Search (UCS)
H H
84 85
18
Uniform-Cost Search (UCS) Iterative-Deepening Search (IDS)
l Time and space complexity: O(bd) (i.e., exponential) l requires modification to DFS search algorithm:
– d is the depth of the solution – do DFS to depth 1
– b is the branching factor at each non-leaf node and treat all children of the start node as leaves
– if no solution found, do DFS to depth 2
l More precisely, time and space complexity is – repeat by increasing “depth bound” until a solution
90 91
deepeningSearch(problem) deepeningSearch(problem)
S S
depth: 1, # of nodes expanded: 0, tested: 0 start depth: 1, # of nodes tested: 1, expanded: 1 start
expnd. node Frontier expnd. node Frontier
{S} 5 2 4 {S} 5 2 4
S not goal {A,B,C}
A B C A B C
9 4 6 2 9 4 6 2
6 G 1 6 G 1
D E goal F D E goal F
7 7
H H
92 93
19
Iterative-Deepening Search (IDS) Iterative-Deepening Search (IDS)
deepeningSearch(problem) deepeningSearch(problem)
S S
depth: 1, # of nodes tested: 2, expanded: 1 start depth: 1, # of nodes tested: 3, expanded: 1 start
expnd. node Frontier expnd. node Frontier
{S} 5 2 4 {S} 5 2 4
S {A,B,C} S {A,B,C}
A not goal {B,C} no expand A B C A {B,C} A B C
B not goal {C} no expand
9 4 6 2 9 4 6 2
6 G 1 6 G 1
D E goal F D E goal F
7 7
H H
94 95
deepeningSearch(problem) deepeningSearch(problem)
S S
depth: 1, # of nodes tested: 4, expanded: 1 start depth: 2, # of nodes tested: 4(1), expanded: 2 start
expnd. node Frontier expnd. node Frontier
{S} 5 2 4 {S} 5 2 4
S {A,B,C} S {A,B,C}
A {B,C} A B C A {B,C} A B C
B {C} B {C}
C not goal { } no expand-FAIL 9 4 6 2 9 4 6 2
C {}
6 G 1 S no test {A,B,C} 6 G 1
D E goal F D E goal F
7 7
H H
96 97
20
Iterative-Deepening Search (IDS) Iterative-Deepening Search (IDS)
deepeningSearch(problem) deepeningSearch(problem)
S S
depth: 2, # of nodes tested: 4(2), expanded: 3 start depth: 2, # of nodes tested: 5(2), expanded: 3 start
expnd. node Frontier expnd. node Frontier
{S} 5 2 4 {S} 5 2 4
S {A,B,C} S {A,B,C}
A {B,C} A B C A {B,C} A B C
B {C} B {C}
9 4 6 2 9 4 6 2
C {} C {}
S {A,B,C} 6 G 1 S {A,B,C} 6 G 1
D E goal F D E goal F
A no test {D,E,B,C} A {D,E,B,C}
7 D not goal {E,B,C} no expand 7
H H
98 99
deepeningSearch(problem) deepeningSearch(problem)
S S
depth: 2, # of nodes tested: 6(2), expanded: 3 start depth: 2, # of nodes tested: 6(3), expanded: 4 start
expnd. node Frontier expnd. node Frontier
{S} 5 2 4 {S} 5 2 4
S {A,B,C} S {A,B,C}
A {B,C} A B C A {B,C} A B C
B {C} B {C}
9 4 6 2 9 4 6 2
C {} C {}
S {A,B,C} 6 G 1 S {A,B,C} 6 G 1
D E goal F D E goal F
A {D,E,B,C} A {D,E,B,C}
D {E,B,C} 7 D {E,B,C} 7
E not goal {B,C} no expand E {B,C}
10 H 10 B no test {G,C} H
0 1
21
Iterative-Deepening Search (IDS) Iterative-Deepening Search (IDS)
deepeningSearch(problem) deepeningSearch(problem)
S S
depth: 2, # of nodes tested: 7(3), expanded: 4 start depth: 2, # of nodes tested: 7(3), expanded: 4 start
expnd. node Frontier expnd. node Frontier
{S} 5 2 4 {S} 5 2 4
S {A,B,C} S {A,B,C}
A {B,C} A B C A {B,C} A B C
B {C} B {C}
9 4 6 2 9 4 6 2
C {} C {}
S {A,B,C} 6 G 1 S {A,B,C} 6 G 1
D E goal F D E goal F
A {D,E,B,C} A {D,E,B,C}
D {E,B,C} 7 D {E,B,C} 7
E {B,C} E {B,C} path: S,B,G
10 B {G,C} H 10 B {G,C} H cost: 8
2 G goal {C} no expand 3 G {C}
l Has advantages of BFS l Space complexity: O(bd) (i.e., linear like DFS)
– completeness
– optimality as stated for BFS
l Time complexity is a little worse than BFS or DFS
– because nodes near the top of the search tree
l Has advantages of DFS are generated multiple times (redundant effort)
– limited space
– in practice, even with redundant effort it still finds l Worst case time complexity: O(bd) exponential
longer paths more quickly than BFS – because most nodes are near the bottom of tree
10 10
4 5
22
Iterative-Deepening Search (IDS) Iterative-Deepening Search
l Trades a little time for a huge reduction in space
How much redundant effort is done? – lets you do breadth-first search with (more space
l The number of times the nodes are generated: efficient) depth-first search
1bd + 2b(d-1) + ... + db ≤ bd / (1 – 1/b)2 = O(bd)
l “Anytime” algorithm: good for response-time
– d: the solution's depth
critical applications, e.g., games
– b: the branching factor at each non-leaf node
l For example: b = 4
4d / (1 – ¼)2 = 4d / (.75)2 = 1.78 × 4d
– in the worst case, 78% more nodes are l An “anytime” algorithm is an algorithm that can return a
searched (redundant effort) than exist at depth d valid solution to a problem even if it's interrupted at any
– as b increases, this % decreases time before it ends. The algorithm is expected to find
better and better solutions the longer it runs.
10
6
start goal
C, DSF CSF, D
23
If State Space is Not a Tree function Uniform-Cost-Search (problem)
loop do
l We have to remember already-expanded states
(called Explored (aka Closed) set) too if Empty?(frontier) then return failure
node = Pop(frontier)
l When we pick a node from Frontier if Goal?(node) then return Solution(node)
– Remove it from Frontier Insert node in explored
– Add it to Explored
foreach child of node do
– Expand node, generating all successors
– For each successor, child,
if child not in frontier or explored then
l If child is in Explored or in Frontier, throw child Insert child in frontier
away // for BFS and DFS else if child in frontier with higher cost then
l Otherwise, add it to Frontier
Remove that old node from frontier
Insert child in frontier
l Called Graph-Search algorithm in Figure 3.7
and Uniform-Cost-Search in Figure 3.14 This is the algorithm in Figure 3.14 in the textbook; note that if
child is not in frontier but is in explored, this algorithm will
throw away child
24