CS:4420 Artificial Intelligence
Spring 2018
Problem Solving by Search
Cesare Tinelli
The University of Iowa
Copyright 2004–18, Cesare Tinelli and Stuart Russell a
a These notes were originally developed by Stuart Russell and are used with permission. They are
copyrighted material and may not be used in other course settings outside of the University of Iowa in their
current or modified form without the express written consent of the copyright holders.
CS:4420 Spring 2018 – p.1/34
Readings
• Chap. 3 of [Russell and Norvig, 2012]
CS:4420 Spring 2018 – p.2/34
Example: Romania
Problem: On holiday in Romania; currently in Arad. Flight leaves
tomorrow from Bucharest. Find a short route to drive to Bucharest.
Formulate problem:
states: various cities
actions: drive between cities
Formulate goal:
be in Bucarest
Formulate solution:
sequence of cities (eg, Arad, Sibiu, Fagaras, Bucharest)
CS:4420 Spring 2018 – p.3/34
Romania’s map
Oradea
71
Neamt
Zerind 87
75 151
Iasi
Arad 140
92
Sibiu Fagaras
99
118 Vaslui
80
Timisoara Rimnicu Vilcea
142
111 Pitesti 211
Lugoj 97
70 98
146 85 Hirsova
Mehadia 101 Urziceni
75 138 86
Bucharest
Dobreta 120
90
Craiova Eforie
Giurgiu
CS:4420 Spring 2018 – p.4/34
Problem-solving agents
Restricted form of general agent:
function Simple-Problem-Solving-Agent( percept) returns an action
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)
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
CS:4420 Spring 2018 – p.5/34
Problem-solving agents
Restricted form of general agent:
function Simple-Problem-Solving-Agent( percept) returns an action
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)
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.
CS:4420 Spring 2018 – p.5/34
Problem Types
• Deterministic, fully observable environment =⇒ single-state
problem
• Agent knows exactly which state it will be in
• Solution is a sequence of actions
• Non-observable environment =⇒ conformant problem
• Agent know it may be in any of a number of states
• Solution, if any, is a sequence of actions
• Nondeterministic and/or partially observable environment =⇒
contingency problem
• Percepts provide new information about current state
• Solution is a tree or policy
• Often interleave search and execution
CS:4420 Spring 2018 – p.6/34
Problem Types (cont.)
• Unknown state space =⇒ exploration problem (“online”)
CS:4420 Spring 2018 – p.7/34
Example: Vacuum World
Single-state problem
initial state = 5
goal states = {7, 8}
Solution?
1 2
3 4
5 6
7 8
CS:4420 Spring 2018 – p.8/34
Example: Vacuum World
Single-state problem
initial state = 5
goal states = {7, 8}
Solution? [Right, Suck ]
1 2
3 4
5 6
7 8
CS:4420 Spring 2018 – p.8/34
Example: Vacuum World
Conformant problem, initial state = {1, 2, 3, 4, 5, 6, 7, 8}
Right =⇒ {2, 4, 6, 8}, Left =⇒ {1, 3, 5, 7}, Suck =⇒ {4, 5, 7, 8}
Solution?
1 2
3 4
5 6
7 8
CS:4420 Spring 2018 – p.9/34
Example: Vacuum World
Conformant problem, initial state = {1, 2, 3, 4, 5, 6, 7, 8}
Right =⇒ {2, 4, 6, 8}, Left =⇒ {1, 3, 5, 7}, Suck =⇒ {4, 5, 7, 8}
Solution? [Right, Suck , Left , Suck ]
1 2
3 4
5 6
7 8
CS:4420 Spring 2018 – p.9/34
Example: Vacuum World
Contingency problem, initial state = 5
Suck occasionally fails. Local sensing: dirt, location.
Solution?
1 2
3 4
5 6
7 8
CS:4420 Spring 2018 – p.10/34
Example: Vacuum World
Contingency problem, initial state = 5
Suck occasionally fails. Local sensing: dirt, location.
Solution? [Right, if dirt then Suck ]
1 2
3 4
5 6
7 8
CS:4420 Spring 2018 – p.10/34
Problem Solving
We start by considering the simpler cases in which the environment is
fully observable, static and deterministic
In such environments the following holds for an agent A:
• A’s world is representable by a discrete set of states
• A’s actions are representable by a discrete set of operators
• the next world state is completely determined by the current
state and A’s actions
• the world’s state transitions are caused exclusively by A’s actions
CS:4420 Spring 2018 – p.11/34
Single-state Problem Formulation
Formally, a problem is defined by four components:
• An initial state (eg, In(Arad))
• A successor function S returning sets of action–state pairs
(eg, S(Arad) = {hGoT o(Zerind), In(Zerind)i, . . .})
• A goal test, explicit (eg, x = In(Bucharest)) or
implicit, (eg, N oDirt(x))
• A path cost
(eg, sum of distances, number of actions executed, . . . ) Usually
additive and given as c(x, a, y), the step cost from x to y by
action a, assumed to be ≥ 0
A solution is a sequence of actions leading from the initial state to a
goal state
CS:4420 Spring 2018 – p.12/34
Selecting a State Space
Since the real world is absurdly complex the state space must be
abstracted for problem solving.
• Abstract state = set of real states
• (Abstract) action = complex combination of real actions eg,
GoT o(Zerind) from Arad represents a complex set of possible
routes, detours, rest stops, etc.
• For guaranteed realizability, any real state corresponding to
In(Arad) must get to some real state corresponding to
In(Zerind)
• Each abstract action should be “easier” than the original
problem!
• (Abstract) solution = set of real paths that are solutions in the
real world
CS:4420 Spring 2018 – p.13/34
Example: vacuum world state space graph
R
L R
S S
R R
L R L R
L L
S S
S S
R
L R
S S
States?
Actions?
Goal test?
Path cost?
CS:4420 Spring 2018 – p.14/34
Example: vacuum world state space graph
R
L R
S S
R R
L R L R
L L
S S
S S
R
L R
S S
States? hdirt flag, robot locationi (ignore dirt amount)
Actions? Left , Right , Suck , NoOp
Goal test? ¬dirty
Path cost? 1 per action (0 for NoOp )
CS:4420 Spring 2018 – p.15/34
Formulating Problem as a Labeled Graph
In the graph
• each node represents a possible state
• a node is designated as the initial state
• one or more nodes represent goal states, states in which the
agent’s goal is considered accomplished
• each edge represents a state transition caused by a specific agent
action
• associated to each edge is the cost of performing that transition
CS:4420 Spring 2018 – p.16/34
Search Graph
How do we reach a goal state?
initial state 4 C
4
A B
3
7
S 5 5
F
2
goal states
4
D E G
2 3
There may be several possible ways. Or none!
Factors to consider:
• cost of finding a path
• cost of traversing a path
CS:4420 Spring 2018 – p.17/34
Problem Solving as Search
Search space: set of states reachable from an initial state S0 via a
(possibly empty/finite/infinite) sequence of state transitions
To achieve the problem’s goal
1. search the space for a (ideally optimal) sequence of transitions
starting from S0 and leading to a goal state
2. execute (in order) the actions associated to each transition in the
identified sequence
For contingency problems, two steps above need to be interleaved
CS:4420 Spring 2018 – p.18/34
Example: The 8-puzzle
2 8 3
1 6 4
7 5
CS:4420 Spring 2018 – p.19/34
Example: The 8-puzzle
Problem: Go from state S to state G.
2 8 3 1 2 3
1 6 4 8 4
7 5 7 6 5
(S) (G)
2 8 3
1 6 4
7 5
L
R
D U
L R
2 8 3 2 8 3 2 8 3
1 6 4 1 4 1 6 4
7 5 7 6 5 L 7 5
R
D U D U D U
L R
2 8 3 2 8 3 2 3 2 8 3 2 8 3
6 4 1 4 1 8 4 1 4 1 6
1 7 5 7 6 5 7 6 5 7 6 5 7 5 4
CS:4420 Spring 2018 – p.20/34
Example: The 8-puzzle
States: configurations of tiles
Operators: move one tile Up/Down/Left/Right
Note:
• There are 9! = 362, 880 possible states: all permutations of
{0, 1, 2, 3, 4, 5, 6, 7, 8} where 0 is the empty space
• Not all states are directly reachable from a given state
How can an artificial agent represent the states and the state space for
this problem?
CS:4420 Spring 2018 – p.21/34
Problem Formulation
1. Choose an appropriate data structure to represent the world
states
2. Define each operator as a precondition/effects pair where the
• precondition holds exactly in the states the operator is
applicable to
• effects describe how a state changes into a successor state
by the application of the operator
3. Specify an initial state
4. Provide a description of the goal—to check if a reached state is
a goal state
CS:4420 Spring 2018 – p.22/34
Formulating the 8-puzzle Problem
States: each represented by a 3 × 3 array of numbers in [0 . . . 8],
where value 0 is for the empty cell
2 8 3 2 8 3
1 6 4 becomes A= 1 6 4
7 5 7 0 5
CS:4420 Spring 2018 – p.23/34
Formulating the 8-puzzle Problem
• Operators: 24 operators of the form OP r,c,d
where r, c ∈ {1, 2, 3}, d ∈ {L, R, U, D}
• If the empty space is at position (r, c), OP r,c,d moves it in
direction d
Example:
2 8 3 2 8 3
OP 3,2,L
1 6 4 =⇒ 1 6 4
7 0 5 0 7 5
CS:4420 Spring 2018 – p.24/34
Preconditions and Effects
Example: OP 3,2,R
2 8 3 2 8 3
OP 3,2,R
1 6 4 =⇒ 1 6 4
7 0 5 7 5 0
Preconditions: A[3, 2] = 0
(
A[3, 2] ← A[3, 3]
Effects:
A[3, 3] ← 0
CS:4420 Spring 2018 – p.25/34
Preconditions and Effects
Example: OP 3,2,R
2 8 3 2 8 3
OP 3,2,R
1 6 4 =⇒ 1 6 4
7 0 5 7 5 0
Preconditions: A[3, 2] = 0
(
A[3, 2] ← A[3, 3]
Effects:
A[3, 3] ← 0
We have 24 operators in this problem formulation . . . 20 too many!
CS:4420 Spring 2018 – p.25/34
A Better Formulation
States: each represented by a pair (A, (i, j)) where:
• A is a 3 × 3 array of numbers in [0 . . . 8]
• (i, j) is the position of the empty space (0) in the array
2 8 3 2 8 3
1 6 4 becomes ( 1 6 4 , (3, 2) )
7 5 7 0 5
CS:4420 Spring 2018 – p.26/34
A Better Formulation
Operators: 4 operators of the form OP d where d ∈ {L, R, U, D}
OP d moves the empty space in the direction d
Example:
2 8 3 2 8 3
OP
( 1 6 4 , (3, 2) ) =⇒L ( 1 6 4 , (3, 1) )
7 0 5 0 7 5
CS:4420 Spring 2018 – p.27/34
Preconditions and Effects
Example: OP L
2 8 3 2 8 3
OP
( 1 6 4 , (3, 2) ) =⇒L ( 1 6 4 , (3, 1) )
7 0 5 0 7 5
Let (r0 , c0 ) be the position of 0 in A
Preconditions: c0 > 1
A[r0 , c0 ]
← A[r0 , c0 − 1]
Effects: A[r0 , c0 − 1] ← 0
(r0 , c0 ) ← (r0 , c0 − 1)
CS:4420 Spring 2018 – p.28/34
The Water Jugs Problem
3gl 4gl
Get exactly 2 gallons of water into the 4gl jug
CS:4420 Spring 2018 – p.29/34
The Water Jugs Problem
States: Determined by the amount of water in each jug
State Representation: Two real-valued variables, J3 , J4 , indicating
the amount of water in the two jugs, with the constraints:
0 ≤ J3 ≤ 3, 0 ≤ J4 ≤ 4
Initial State Description
J3 = 0, J4 = 0
Goal State Description:
J4 = 2 (non exhaustive description)
CS:4420 Spring 2018 – p.30/34
The Water Jugs Problem: Operators
E4: empty jug4 on the ground
precond: J4 > 0 effect: J4′ = 0
E4-3: pour water from jug4 into jug3 until jug3 is full
precond: J3 < 3, effect: J3′ = 3,
J4 ≥ 3 − J3 J4′ = J4 − (3 − J3 )
P3-4: pour water from jug3 into jug4 until jug4 is full
precond: J4 < 4, effect: J4′ = 4,
J3 ≥ 4 − J4 J3′ = J3 − (4 − J4 )
E3-4: pour water from jug3 into jug4 until jug3 is empty
precond: J3 + J4 < 4, effect: J4′ = J3 + J4 ,
J3 > 0 J3′ = 0
...
CS:4420 Spring 2018 – p.31/34
The Water Jugs Problem
Problem Search Graph
J_3 = 0 J_3 = 0
J_4 = 0 J_4 = 2 F4 J_4 = 0 F3
J_3 = 0 J_3 = 3
J_4 = 4 J_4 = 0
F3 P4-3
F4 E3-4
J_3 = 3 J_3 = 3 J_3 = 0 J_3 = 0
J_4 = 4 J_4 = 1 J_4 = 4 J_4 = 3
F3
... ... ...
J_3 = 3
J_4 = 3
P3-4
J_3 = 2 ...
E4 J_4 = 4
J_3 = 2
J_4 = 0
E3-4
J_3 = 0
J_4 = 2
CS:4420 Spring 2018 – p.32/34
Real-World Search Problems
• Route Finding
(computer networks, airline travel planning system, . . . )
• Travelling Salesman Optimization Problem
(package delivery, automatic drills, . . . )
• Layout Problems
(VLSI layout, furniture layout, packaging, . . . )
• Assembly Sequencing
(assembly of electric motors, . . . )
• Task Scheduling
(manufacturing, timetables, . . . )
• ...
CS:4420 Spring 2018 – p.33/34
Problem Solution
Typically, a problem’s solution is a description of how to reach a goal
state from the initial state:
Examples:
• n-puzzle
• route-finding problem
• assembly sequencing
Occasionally, a problem’s solution is simply a description of the goal
state itself:
Examples:
• 8-queen problem
• scheduling problems
• layout problems
CS:4420 Spring 2018 – p.34/34