You are on page 1of 13

Problem-solving agents

Restricted form of general agent:

function Simple-Problem-Solving-Agent( percept) returns an action


Problem solving and search static: seq, an action sequence, initially empty
state, some description of the current world state
goal, a goal, initially null
problem, a problem formulation
state ← Update-State(state, percept)
Chapter 3 if seq is empty then
goal ← Formulate-Goal(state)
problem ← Formulate-Problem(state, goal)
seq ← Search( problem)
action ← Recommendation(seq, state)
seq ← Remainder(seq, state)
return action

Note: this is offline problem solving; solution executed “eyes closed.”


Online problem solving involves acting without complete knowledge.
Chapter 3 1 Chapter 3 4

Reminders Example: Romania


Assignment 0 due 5pm today On holiday in Romania; currently in Arad.
Flight leaves tomorrow from Bucharest
Assignment 1 posted, due 2/9
Formulate goal:
Section 105 will move to 9-10am starting next week
be in Bucharest
Formulate problem:
states: various cities
actions: drive between cities
Find solution:
sequence of cities, e.g., Arad, Sibiu, Fagaras, Bucharest

Chapter 3 2 Chapter 3 5

Outline Example: Romania


Oradea
♦ Problem-solving agents 71
Neamt

♦ Problem types Zerind 151


87
75
Iasi
♦ Problem formulation Arad 140
92
Sibiu Fagaras
♦ Example problems 99
118 Vaslui
80
♦ Basic search algorithms Rimnicu Vilcea
Timisoara
142
111 Pitesti 211
Lugoj 97
70 98
146 85 Hirsova
Mehadia 101 Urziceni
75 138 86
Bucharest
Dobreta 120
90
Craiova Eforie
Giurgiu

Chapter 3 3 Chapter 3 6
Problem types Example: vacuum world
Deterministic, fully observable =⇒ single-state problem Single-state, start in #5. Solution??
Agent knows exactly which state it will be in; solution is a sequence [Right, Suck] 1 2

Non-observable =⇒ conformant problem Conformant, start in {1, 2, 3, 4, 5, 6, 7, 8}


3 4
Agent may have no idea where it is; solution (if any) is a sequence e.g., Right goes to {2, 4, 6, 8}. Solution??
[Right, Suck, Lef t, Suck]
Nondeterministic and/or partially observable =⇒ contingency problem 5 6
percepts provide new information about current state Contingency, start in #5
solution is a contingent plan or a policy Murphy’s Law: Suck can dirty a clean carpet
7 8
often interleave search, execution Local sensing: dirt, location only.
Solution??
Unknown state space =⇒ exploration problem (“online”)

Chapter 3 7 Chapter 3 10

Example: vacuum world Example: vacuum world

Single-state, start in #5. Solution?? Single-state, start in #5. Solution??


1 2 [Right, Suck] 1 2

Conformant, start in {1, 2, 3, 4, 5, 6, 7, 8}


3 4 3 4
e.g., Right goes to {2, 4, 6, 8}. Solution??
[Right, Suck, Lef t, Suck]
5 6 5 6
Contingency, start in #5
Murphy’s Law: Suck can dirty a clean carpet
7 8 7 8
Local sensing: dirt, location only.
Solution??
[Right, if dirt then Suck]

Chapter 3 8 Chapter 3 11

Example: vacuum world Single-state problem formulation

Single-state, start in #5. Solution?? A problem is defined by four items:


[Right, Suck] 1 2
initial state e.g., “at Arad”
Conformant, start in {1, 2, 3, 4, 5, 6, 7, 8}
e.g., Right goes to {2, 4, 6, 8}. Solution??
3 4 successor function S(x) = set of action–state pairs
e.g., S(Arad) = {hArad → Zerind, Zerindi, . . .}
5 6
goal test, can be
explicit, e.g., x = “at Bucharest”
7 8 implicit, e.g., N oDirt(x)
path cost (additive)
e.g., sum of distances, number of actions executed, etc.
c(x, a, y) is the step cost, assumed to be ≥ 0
A solution is a sequence of actions
leading from the initial state to a goal state

Chapter 3 9 Chapter 3 12
Selecting a state space Example: vacuum world state space graph
R

Real world is absurdly complex L R

L
⇒ state space must be abstracted for problem solving S S

(Abstract) state = set of real states L


R
R L
R
R

L L
(Abstract) action = complex combination of real actions S S
S S
e.g., “Arad → Zerind” represents a complex set R
of possible routes, detours, rest stops, etc. L R

For guaranteed realizability, any real state “in Arad” L

must get to some real state “in Zerind” S S

(Abstract) solution = states??: integer dirt and robot locations (ignore dirt amounts etc.)
set of real paths that are solutions in the real world actions??: Lef t, Right, Suck, N oOp
goal test??
Each abstract action should be “easier” than the original problem!
path cost??

Chapter 3 13 Chapter 3 16

Example: vacuum world state space graph Example: vacuum world state space graph
R R
L R L R

L L

S S S S

R R R R
L R L R L R L R

L L L L
S S S S
S S S S

R R
L R L R

L L

S S S S

states?? states??: integer dirt and robot locations (ignore dirt amounts etc.)
actions?? actions??: Lef t, Right, Suck, N oOp
goal test?? goal test??: no dirt
path cost?? path cost??

Chapter 3 14 Chapter 3 17

Example: vacuum world state space graph Example: vacuum world state space graph
R R
L R L R

L L

S S S S

R R R R
L R L R L R L R

L L L L
S S S S
S S S S

R R
L R L R

L L

S S S S

states??: integer dirt and robot locations (ignore dirt amounts etc.) states??: integer dirt and robot locations (ignore dirt amounts etc.)
actions?? actions??: Lef t, Right, Suck, N oOp
goal test?? goal test??: no dirt
path cost?? path cost??: 1 per action (0 for N oOp)

Chapter 3 15 Chapter 3 18
Example: The 8-puzzle Example: The 8-puzzle
7 2 4 5
1 2 3 7 2 4 5
1 2 3

5 6 4 5 6 5 6 4 5 6

8 3 1 7 8 8 3 1 7 8

Start State Goal State Start State Goal State

states?? states??: integer locations of tiles (ignore intermediate positions)


actions?? actions??: move blank left, right, up, down (ignore unjamming etc.)
goal test?? goal test??: = goal state (given)
path cost?? path cost??

Chapter 3 19 Chapter 3 22

Example: The 8-puzzle Example: The 8-puzzle


7 2 4 5
1 2 3 7 2 4 5
1 2 3

5 6 4 5 6 5 6 4 5 6

8 3 1 7 8 8 3 1 7 8

Start State Goal State Start State Goal State

states??: integer locations of tiles (ignore intermediate positions) states??: integer locations of tiles (ignore intermediate positions)
actions?? actions??: move blank left, right, up, down (ignore unjamming etc.)
goal test?? goal test??: = goal state (given)
path cost?? path cost??: 1 per move
[Note: optimal solution of n-Puzzle family is NP-hard]

Chapter 3 20 Chapter 3 23

Example: The 8-puzzle Example: robotic assembly


P
R R
7 2 4 5
1 2 3
R R

5 6 4 5 6
R
8 3 1 7 8

Start State Goal State

states??: real-valued coordinates of robot joint angles


states??: integer locations of tiles (ignore intermediate positions) parts of the object to be assembled
actions??: move blank left, right, up, down (ignore unjamming etc.)
goal test?? actions??: continuous motions of robot joints
path cost??
goal test??: complete assembly with no robot included!
path cost??: time to execute

Chapter 3 21 Chapter 3 24
Tree search algorithms Tree search example
Basic idea: Arad
offline, simulated exploration of state space
by generating successors of already-explored states
Sibiu Timisoara Zerind
(a.k.a. expanding states)

function Tree-Search( problem, strategy) returns a solution, or failure Arad Fagaras Oradea Rimnicu Vilcea Arad Lugoj Arad Oradea

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

Chapter 3 25 Chapter 3 28

Tree search example Implementation: states vs. nodes

Arad A state is a (representation of) a physical configuration


A node is a data structure constituting part of a search tree
includes parent, children, depth, path cost g(x)
Sibiu Timisoara Zerind
States do not have parents, children, depth, or path cost!
parent, action
Arad Fagaras Oradea Rimnicu Vilcea Arad Lugoj Arad Oradea

depth = 6
State 5 4 Node
g=6
6 1 88

state
7 3 22

The Expand function creates new nodes, filling in the various fields and
using the SuccessorFn of the problem to create the corresponding states.

Chapter 3 26 Chapter 3 29

Tree search example Implementation: general tree search

Arad function Tree-Search( problem, fringe) returns a solution, or failure


fringe ← Insert(Make-Node(Initial-State[problem]), fringe)
Sibiu Timisoara Zerind
loop do
if fringe is empty then return failure
node ← Remove-Front(fringe)
Arad Fagaras Oradea Rimnicu Vilcea Arad Lugoj Arad Oradea if Goal-Test(problem, State(node)) then return node
fringe ← InsertAll(Expand(node, problem), fringe)

function Expand( node, problem) returns a set of nodes


successors ← the empty set
for each action, result in Successor-Fn(problem, State[node]) do
s ← a new Node
Parent-Node[s] ← node; Action[s] ← action; State[s] ← result
Path-Cost[s] ← Path-Cost[node] + Step-Cost(node, action, s)
Depth[s] ← Depth[node] + 1
add s to successors
return successors

Chapter 3 27 Chapter 3 30
Search strategies Breadth-first search
A strategy is defined by picking the order of node expansion Expand shallowest unexpanded node

Strategies are evaluated along the following dimensions: Implementation:


completeness—does it always find a solution if one exists? fringe is a FIFO queue, i.e., new successors go at end
time complexity—number of nodes generated/expanded A
space complexity—maximum number of nodes in memory
optimality—does it always find a least-cost solution?
Time and space complexity are measured in terms of B C
b—maximum branching factor of the search tree
d—depth of the least-cost solution
m—maximum depth of the state space (may be ∞) D E F G

Chapter 3 31 Chapter 3 34

Uninformed search strategies Breadth-first search


Uninformed strategies use only the information available Expand shallowest unexpanded node
in the problem definition
Implementation:
Breadth-first search fringe is a FIFO queue, i.e., new successors go at end

Uniform-cost search A

Depth-first search
Depth-limited search
B C

Iterative deepening search


D E F G

Chapter 3 32 Chapter 3 35

Breadth-first search Breadth-first search


Expand shallowest unexpanded node Expand shallowest unexpanded node
Implementation: Implementation:
fringe is a FIFO queue, i.e., new successors go at end fringe is a FIFO queue, i.e., new successors go at end
A A

B C B C

D E F G
D E F G

Chapter 3 33 Chapter 3 36
Properties of breadth-first search Properties of breadth-first search
Complete?? Complete?? Yes (if b is finite)
Time?? 1 + b + b2 + b3 + . . . + bd + b(bd − 1) = O(bd+1), i.e., exp. in d
Space?? O(bd+1) (keeps every node in memory)
Optimal??

Chapter 3 37 Chapter 3 40

Properties of breadth-first search Properties of breadth-first search


Complete?? Yes (if b is finite) Complete?? Yes (if b is finite)
Time?? Time?? 1 + b + b2 + b3 + . . . + bd + b(bd − 1) = O(bd+1), i.e., exp. in d
Space?? O(bd+1) (keeps every node in memory)
Optimal?? Yes (if cost = 1 per step); not optimal in general
Space is the big problem; can easily generate nodes at 100MB/sec
so 24hrs = 8640GB.

Chapter 3 38 Chapter 3 41

Properties of breadth-first search Uniform-cost search


Complete?? Yes (if b is finite) Expand least-cost unexpanded node

Time?? 1 + b + b2 + b3 + . . . + bd + b(bd − 1) = O(bd+1), i.e., exp. in d Implementation:


fringe = queue ordered by path cost, lowest first
Space??
Equivalent to breadth-first if step costs all equal
Complete?? Yes, if step cost ≥ 
∗ /e
Time?? # of nodes with g ≤ cost of optimal solution, O(bdC )
where C ∗ is the cost of the optimal solution
∗ /e
Space?? # of nodes with g ≤ cost of optimal solution, O(bdC )
Optimal?? Yes—nodes expanded in increasing order of g(n)

Chapter 3 39 Chapter 3 42
Depth-first search Depth-first search
Expand deepest unexpanded node Expand deepest unexpanded node
Implementation: Implementation:
fringe = LIFO queue, i.e., put successors at front fringe = LIFO queue, i.e., put successors at front
A A

B C B C

D E F G D E F G

H I J K L M N O H I J K L M N O

Chapter 3 43 Chapter 3 46

Depth-first search Depth-first search


Expand deepest unexpanded node Expand deepest unexpanded node
Implementation: Implementation:
fringe = LIFO queue, i.e., put successors at front fringe = LIFO queue, i.e., put successors at front
A A

B C B C

D E F G D E F G

H I J K L M N O H I J K L M N O

Chapter 3 44 Chapter 3 47

Depth-first search Depth-first search


Expand deepest unexpanded node Expand deepest unexpanded node
Implementation: Implementation:
fringe = LIFO queue, i.e., put successors at front fringe = LIFO queue, i.e., put successors at front
A A

B C B C

D E F G D E F G

H I J K L M N O H I J K L M N O

Chapter 3 45 Chapter 3 48
Depth-first search Depth-first search
Expand deepest unexpanded node Expand deepest unexpanded node
Implementation: Implementation:
fringe = LIFO queue, i.e., put successors at front fringe = LIFO queue, i.e., put successors at front
A A

B C B C

D E F G D E F G

H I J K L M N O H I J K L M N O

Chapter 3 49 Chapter 3 52

Depth-first search Depth-first search


Expand deepest unexpanded node Expand deepest unexpanded node
Implementation: Implementation:
fringe = LIFO queue, i.e., put successors at front fringe = LIFO queue, i.e., put successors at front
A A

B C B C

D E F G D E F G

H I J K L M N O H I J K L M N O

Chapter 3 50 Chapter 3 53

Depth-first search Depth-first search


Expand deepest unexpanded node Expand deepest unexpanded node
Implementation: Implementation:
fringe = LIFO queue, i.e., put successors at front fringe = LIFO queue, i.e., put successors at front
A A

B C B C

D E F G D E F G

H I J K L M N O H I J K L M N O

Chapter 3 51 Chapter 3 54
Properties of depth-first search Properties of depth-first search
Complete?? Complete?? No: fails in infinite-depth spaces, spaces with loops
Modify to avoid repeated states along path
⇒ complete in finite spaces
Time?? O(bm): terrible if m is much larger than d
but if solutions are dense, may be much faster than breadth-first
Space?? O(bm), i.e., linear space!
Optimal??

Chapter 3 55 Chapter 3 58

Properties of depth-first search Properties of depth-first search


Complete?? No: fails in infinite-depth spaces, spaces with loops Complete?? No: fails in infinite-depth spaces, spaces with loops
Modify to avoid repeated states along path Modify to avoid repeated states along path
⇒ complete in finite spaces ⇒ complete in finite spaces
Time?? Time?? O(bm): terrible if m is much larger than d
but if solutions are dense, may be much faster than breadth-first
Space?? O(bm), i.e., linear space!
Optimal?? No

Chapter 3 56 Chapter 3 59

Properties of depth-first search Depth-limited search


Complete?? No: fails in infinite-depth spaces, spaces with loops = depth-first search with depth limit l,
Modify to avoid repeated states along path i.e., nodes at depth l have no successors
⇒ complete in finite spaces
Recursive implementation:
Time?? O(bm): terrible if m is much larger than d
but if solutions are dense, may be much faster than breadth-first function Depth-Limited-Search( problem, limit) returns soln/fail/cutoff
Recursive-DLS(Make-Node(Initial-State[problem]), problem, limit)
Space?? function Recursive-DLS(node, problem, limit) returns soln/fail/cutoff
cutoff-occurred? ← false
if Goal-Test(problem, State[node]) then return node
else if Depth[node] = limit then return cutoff
else for each successor in Expand(node, problem) do
result ← Recursive-DLS(successor, problem, limit)
if result = cutoff then cutoff-occurred? ← true
else if result 6= failure then return result
if cutoff-occurred? then return cutoff else return failure

Chapter 3 57 Chapter 3 60
Iterative deepening search Iterative deepening search l = 2

function Iterative-Deepening-Search( problem) returns a solution Limit = 2 A A A A

inputs: problem, a problem B C B C B C B C

for depth ← 0 to ∞ do D E F G D E F G D E F G D E F G

result ← Depth-Limited-Search( problem, depth)


A A A A
if result 6= cutoff then return result
B C B C B C B C
end
D E F G D E F G D E F G D E F G

Chapter 3 61 Chapter 3 64

Iterative deepening search l = 0 Iterative deepening search l = 3

Limit = 0 A A Limit = 3 A A A A

B C B C B C B C

D E F G D E F G D E F G D E F G

H I J K L M N O H I J K L M N O H I J K L M N O H I J K L M N O

A A A A

B C B C B C B C

D E F G D E F G D E F G D E F G

H I J K L M N O H I J K L M N O H I J K L M N O H I J K L M N O

A A A A

B C B C B C B C

D E F G D E F G D E F G D E F G

H I J K L M N O H I J K L M N O H I J K L M N O H I J K L M N O

Chapter 3 62 Chapter 3 65

Iterative deepening search l = 1 Properties of iterative deepening search


Complete??
Limit = 1 A A A A

B C B C B C B C

Chapter 3 63 Chapter 3 66
Properties of iterative deepening search Properties of iterative deepening search
Complete?? Yes Complete?? Yes
Time?? Time?? (d + 1)b0 + db1 + (d − 1)b2 + . . . + bd = O(bd)
Space?? O(bd)
Optimal?? Yes, if step cost = 1
Can be modified to explore uniform-cost tree
Numerical comparison for b = 10 and d = 5, solution at far right leaf:
N (IDS) = 50 + 400 + 3, 000 + 20, 000 + 100, 000 = 123, 450
N (BFS) = 10 + 100 + 1, 000 + 10, 000 + 100, 000 + 999, 990 = 1, 111, 100

IDS does better because other nodes at depth d are not expanded
BFS can be modified to apply goal test when a node is generated

Chapter 3 67 Chapter 3 70

Properties of iterative deepening search Summary of algorithms


Complete?? Yes Criterion Breadth- Uniform- Depth- Depth- Iterative
First Cost First Limited Deepening
Time?? (d + 1)b0 + db1 + (d − 1)b2 + . . . + bd = O(bd)
Complete? Yes∗ Yes∗ No Yes, if l ≥ d Yes
Space?? Time bd+1

bdC /e bm bl bd
dC ∗/e
Space bd+1 b bm bl bd
Optimal? Yes∗ Yes No No Yes∗

Chapter 3 68 Chapter 3 71

Properties of iterative deepening search Repeated states


Complete?? Yes Failure to detect repeated states can turn a linear problem into an exponential
0 1 2 d d
one!
Time?? (d + 1)b + db + (d − 1)b + . . . + b = O(b )
A A
Space?? O(bd)
B B B
Optimal??
C C C C C

Chapter 3 69 Chapter 3 72
Graph search

function Graph-Search( problem, fringe) returns a solution, or failure


closed ← an empty set
fringe ← Insert(Make-Node(Initial-State[problem]), fringe)
loop do
if fringe is empty then return failure
node ← Remove-Front(fringe)
if Goal-Test(problem, State[node]) then return node
if State[node] is not in closed then
add State[node] to closed
fringe ← InsertAll(Expand(node, problem), fringe)
end

Chapter 3 73

Summary
Problem formulation usually requires abstracting away real-world details to
define a state space that can feasibly be explored
Variety of uninformed search strategies
Iterative deepening search uses only linear space
and not much more time than other uninformed algorithms
Graph search can be exponentially more efficient than tree search

Chapter 3 74

You might also like