Moule 3 Problem Solving
Moule 3 Problem Solving
Russel-Norvig
Module No :3
Problem-Solving Agent
sensors
?
environment
agent
actuators
• Formulate Goal
• Formulate Problem
• States
• Actions
• Find Solution
PROBLEM-SOLVING AGENTS
Intelligent agents are supposed to act in such a way that the environment goes through a sequence of states that maximizes the performance measure.
What is Problem solving agent?
• It is a kind of Goal-based Agents
(finding action sequences that achieve the agent goals)
• 4 general steps in problem-solving:
– Goal Formulation
– Problem Formulation
– Search
– Execute
E.g. Driving from Arad to Bucharest ...
Holiday Planning
• On holiday in Romania; Currently in Arad. Flight
leaves tomorrow from Bucharest.
• Formulate Goal:
Be in Bucharest
• Formulate Problem:
States: various cities
Actions: drive between cities
• Find solution:
Sequence of cities: Arad, Sibiu, Fagaras, Bucharest
Problem Solving
States
Actions
Start Solution
Goal
Problem-solving agent
function SIMPLE-PROBLEM-SOLVING-AGENT(percept) return an action
static: seq, an action sequence
state, some description of the current world state
goal, a goal
problem, a problem formulation
-Goal formulation
-Problem formulation
-Example problems
-- Toy problems
– Real-world problems
-Search
– search strategies
– Constraint satisfaction
-Solution
• Goal Formulation
-Goal formulation, based on the current situation,
is the first step in problem solving. As well as
formulating a goal, the agent may wish to
decide on some other factors that affect the
desirability of different ways of achieving the
goal.
-let us assume that the agent will consider actions
at the level of driving from one major town to
another. The states it will consider therefore
correspond to being in a particular town.
• Declaring the Goal: Goal information given to agent
i.e. start from Arad and reach to Bucharest.
• States??
• Initial state??
• Actions??
• Goal test??
• Path cost??
Example: 8-puzzle
8 2 1 2 3
3 4 7 4 5 6
5 1 6 7 8
8 2 7
3 4
8 2 5 1 6
3 4 7
5 1 6 8 2 8 2
3 4 7 3 4 7
5 1 6 5 1 6
Example: vacuum world
• States??
• Initial state??
• Actions??
• Goal test??
• Path cost??
Example: vacuum world
Path Cost:
Each step costs 1
Example: 8-queens
Formulation #1:
• States: any arrangement of
0 to 8 queens on the board
• Initial state: 0 queens on the
board
• Actions: add a
queen in any square
• Goal test: 8 queens on the
board, none attacked
• Path cost: none
2,067 states
Real-world Problems
• Route finding
• Touring problems
• VLSI layout
• Robot Navigation
• Automatic assembly sequencing
• Drug design
• Internet searching
• …
Example: robot assembly
• States??
• Initial state??
• Actions??
• Goal test??
• Path cost??
Example: robot assembly
s 5 7
3 6 9
37
Shortest path Breadth First Search
1
from s
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: s
Top of queue
38
Finished
Breadth First Search
1
2 4 8
0 s 5 7
3 6 9
Undiscovered
Discovered Queue: s 2
Top of queue
39
Finished
Breadth First Search
1
2 4 8
0 s 5 7
1
3 6 9
Undiscovered
Discovered Queue: s 2 3
Top of queue
40
Finished
Breadth First Search
1
2 4 8
0 s 5 7
1
3 6 9
Undiscovered
Discovered Queue: 2 3 5
Top of queue
41
Finished
Breadth First Search
1 2
2 4 8
0 s 5 7
1
3 6 9
Undiscovered
Discovered Queue: 2 3 5
Top of queue
42
Finished
Breadth First Search
1 2
2 4 8
5 already discovered:
0 s 5 7
don't enqueue
1
3 6 9
Undiscovered
Discovered Queue: 2 3 5 4
Top of queue
43
Finished
Breadth First Search
1 2
2 4 8
0 s 5 7
1
3 6 9
Undiscovered
Discovered Queue: 2 3 5 4
Top of queue
44
Finished
Breadth First Search
1 2
2 4 8
0 s 5 7
1
3 6 9
Undiscovered
Discovered Queue: 3 5 4
Top of queue
45
Finished
Breadth First Search
1 2
2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered Queue: 3 5 4
Top of queue
46
Finished
Breadth First Search
1 2
2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered Queue: 3 5 4 6
Top of queue
47
Finished
Breadth First Search
1 2
2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered Queue: 5 4 6
Top of queue
48
Finished
Breadth First Search
1 2
2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered Queue: 5 4 6
Top of queue
49
Finished
Breadth First Search
1 2
2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered Queue: 4 6
Top of queue
50
Finished
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered Queue: 4 6
Top of queue
51
Finished
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1
3 6 9
1 2
Undiscovered
Discovered Queue: 4 6 8
Top of queue
52
Finished
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2
Undiscovered
Discovered Queue: 6 8
Top of queue
53
Finished
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue: 6 8 7
Top of queue
54
Finished
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue: 6 8 7 9
Top of queue
55
Finished
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue: 8 7 9
Top of queue
56
Finished
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue: 7 9
Top of queue
57
Finished
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue: 7 9
Top of queue
58
Finished
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue: 7 9
Top of queue
59
Finished
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue: 7 9
Top of queue
60
Finished
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue: 9
Top of queue
61
Finished
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue: 9
Top of queue
62
Finished
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue: 9
Top of queue
63
Finished
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Undiscovered
Discovered Queue:
Top of queue
64
Finished
Breadth First Search
1 2 3
2 4 8
0 s 5 7
1 3
3 6 9
1 2 3
Level Graph
65
Depth-First Graph
Traversal Algorithm
Search vs Traversal
depth-first-search
mark vertex as visited
for each adjacent vertex
if unvisited
do a depth-first search on adjacent vertex
Depth-First Search
B C
D E F G
Depth-First Search
v
A
B C
D E F G
A
Depth-First Search
v
A
B C
D E F G
A
Depth-First Search
v
A
v
B C
D E F G
A B
Depth-First Search
v
A
v
B C
D E F G
A B
Depth-First Search
v
A
v
B C
D E F G
A B
Depth-First Search
v
A
v
B C
v
D E F G
A B D
Depth-First Search
v
A
v
B C
v
D E F G
A B D
Depth-First Search
v
A
v
B C
v
D E F G
A B D
Depth-First Search
v
A
v
B C
v
D E v F G
A B D E
Depth-First Search
v
A
v
B C
v
D E v F G
A B D E
Depth-First Search
v
A
v
B C
v
D E v F G
A B D E
Depth-First Search
v
A
v
B C
v
D E F G
A B D E
Depth-First Search
v
A
v
B C
v
D E v F G
A B D E
Depth-First Search
v
A
v
B C
v
D E v F G
A B D E
Depth-First Search
v
A
v
B C
v
D E v F G
A B D E
Depth-First Search
v
A
v
B C
v
D E v F G
v
A B D E F
Depth-First Search
v
A
v
B C
v
D E v F G
v
A B D E F
Depth-First Search
v
A
v
B C
v
D E v F G
v
A B D E F
Depth-First Search
v
A
v v
B C
v
D E v F G
v
A B D E F C
Depth-First Search
v
A
v v
B C
v
D E v F G
v
A B D E F C
Depth-First Search
v
A
v v
B C
v
D E v F G
v
A B D E F C
Depth-First Search
v
A
v v
B C
v
D E v F G
v
A B D E F C
Depth-First Search
v
A
v v
B C
v
v
D E v F G
v
A B D E F C G
Depth-First Search
v
A
v v
B C
v
v
D E v F G
v
A B D E F C G
Depth-First Search
v
A
v v
B C
v
v
D E v F G
v
A B D E F C G
Depth-First Search
v
A
v v
B C
v
v
D E v F G
v
A B D E F C G
Depth-First Search
v
A
v v
B C
v
v
D E v F G
v
A B D E F C G
Depth-First Search
v
A
v v
B C
v
v
D E v F G
v
A B D E F C G
Depth-First Search
v
A
v v
B C
v
v
D E v F G
v
A B D E F C G
Depth-First Search
v
A
v v
B C
v
v
D E v F G
v
A B D E F C G
Depth-First Search
B C
D E F G
A B D E F C G
Time and Space Complexity
for Depth-First Search
• Time Complexity
– Adjacency Lists
• Each node is marked visited once
• Each node is checked for each incoming edge
• O (v + e)
– Adjacency Matrix
• Have to check all entries in matrix: O(n2)
Time and Space Complexity
for Depth-First Search
• Space Complexity
– Stack to handle nodes as they are explored
• Worst case: all nodes put on stack (if graph is linear)
• O(n)
Bidirectional search
S S1 E1 E
new_s( S - E, S1 - E1) :-
s( S, S1), % One step forward
s( E1, E). % One step backward
new_goal( S - E) :-
s( S, E). % Ends sufficiently close
Complexity of bidirectional search
Consider the case: forward and backward
branching both b, uniform
d/2 d/2
Uniform-cost Search:
Expand node with smallest path cost g(n).
109
Example of Uniform Cost Search
• Assume an example tree with different edge costs, represented by
numbers next to the edges.
2 a
1
b c
1 2 1 2
f gc dc ec
2 a
1
Closed list:
a
Open list: 0
Example of Uniform Cost Search
2 a
1
b c
1 2 1 2
a
Closed list:
b c
Open list: 2 1
Example of Uniform Cost Search
2 a
1
b c
1 2 1 2
dc ec
a c
Closed list:
b d e
Open list: 2 2 3
Example of Uniform Cost Search
2 a
1
b c
1 2 1 2
f gc dc ec
a c b
Closed list:
d e f g
Open list: 2 3 3 4
Example of Uniform Cost Search
2 a
1
b c
1 2 1 2
f gc dc ec
a c b d
Closed list:
e f g
Open list: 3 3 4
Example of Uniform Cost Search
2 a
1
b c
1 2 1 2
f gc dc ec
a c b d e
Closed list:
f g
Open list: 3 4
Example of Uniform Cost Search
2 a
1
b c
1 2 1 2
f gc dc ec
a c b d e f
Closed list:
g
Open list: 4
Example of Uniform Cost Search
2 a
1
b c
1 2 1 2
f gc dc ec
a c b d e f g
Closed list:
Open list:
Uniform Cost Search
• Questions to ask:
– Whether Uniform cost always terminates?
– Whether it is guaranteed to find a goal state?
Uniform Cost Search - Termination
• The algorithm will find a goal node or report than there is no goal
node under following conditions:
– the problem space is finite
– there must exist a path to a goal with finite length and finite cost
– there must not be any infinitely long paths of finite cost
• We will assume that all the edges have a minimum non-zero edge
cost e to a solve a problem of infinite chains of nodes with zero-
cost edges.
Then, UCS will eventually reach a goal of finite cost if one exists in
the graph.
6 1
3 A D F 1
2 4 8
S B E G
1 20
C
The graph above shows the step-costs for different paths going from the start (S) to
the goal (G).
Use uniform cost search to find the optimal path to the goal.
121
Depth-Limited Search
• Same as depth-first search with depth limit l.
• Implementation:
– Nodes at depth l have no successors.
Iterative Deepening Search
Repeated depth-limited search with increasing depth
Function
FunctionITERATIVE-DEEPENING-SEARCH
ITERATIVE-DEEPENING-SEARCH(problem)
(problem)
returns
returnsaasolution
solutionor
orfailure
failure
for depth 0 to MAXDEPTH
for depth 0 to MAXDEPTH do do
result
resultDEPTH-LIMIT-SEARCH
DEPTH-LIMIT-SEARCH(problem,
(problem,depth)
depth)
ififresult
resultthen
thenreturn
returnresult
result
end
end
return
returnFAILURE
FAILURE
Iterative Deepening Search: depth=0
Arad
Iterative Deepening Search: depth=1
Arad
Arad
•Next lecturer
Search spaces are usually graphs
Explicit graphs: Implicit graphs:
• e.g. road maps • e.g. problem solving.
• Finite number of states • Graph is usually constructed on
the fly starting from initial state
and applying operators to find
connected states.
• Possibly infinite number of states
Romania with Edge Costs