Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
Problem solving by search
Tomas Svoboda
Department of Cybernetics, Vision for Robots and Autonomous Systems
March 5, 2018
1 / 31
Outline
I Search problem.
I State space graphs.
I Search trees.
I Strategies, which tree branches to choose?
I Strategy/Algorithm properties?
I Programming instrastructure
2 / 31
Example: Romania
Giurgiu
UrziceniHirsova
Eforie
Neamt
Oradea
Zerind
Arad
Timisoara
Lugoj
Mehadia
DobretaCraiova
Sibiu Fagaras
Pitesti
Vaslui
Iasi
Rimnicu Vilcea
Bucharest
71
75
118
111
70
75
120
151
140
99
80
97
101
211
138
146 85
90
98
142
92
87
86
3 / 31
Example: Romania
Goal:be in Bucharest
Problem formulation:states: position in a city (cities)actions: drive between cities
Solution:Sequence of cities (path)
Giurgiu
UrziceniHirsova
Eforie
Neamt
Oradea
Zerind
Arad
Timisoara
Lugoj
Mehadia
DobretaCraiova
Sibiu Fagaras
Pitesti
Vaslui
Iasi
Rimnicu Vilcea
Bucharest
71
75
118
111
70
75
120
151
140
99
80
97
101
211
138
146 85
90
98
142
92
87
86
4 / 31
Example: The 8-puzzle
2
Start State Goal State
51 3
4 6
7 8
5
1
2
3
4
6
7
8
5
states?actions?solution?cost?
5 / 31
Example: Vacuum cleaner
R
L
S S
S S
R
L
R
L
R
L
S
SS
S
L
L
LL R
R
R
R
states?actions?solution?cost?
6 / 31
A Search Problem
I State space (including Start/Initial state): position, boardconfiguration,
I Actions: drive to, Up, Down, Left . . .
I Transition model: Given state and action return state (and cost)
I Goal test: Are we done?
7 / 31
State Space Graphs
State space graph: a representation of asearch problem
I Nodes are abstracted worldconfigurations
I Arcs represent action results
I Goal test is a set of goal nodes
Each state occurs only once in a state (search)space.
S b
c
d
G
a
8 / 31
Search Trees
Possible futures
Here we startS
a b c
I A “what if” tree of plans and their outcomes
I Start node is the root
I Children are successors
I Nodes show states, but correspond to plans that achieve those states
What does the last item mean, actually?
9 / 31
State Space Graphs vs. Search Trees
S b
c
d
G
a
S
a
S
a b c
b
a
S
d
S G
c
G a b
How big is the search tree?
10 / 31
Search tree for Romania(a) The initial state
(b) After expanding Arad
(c) After expanding Sibiu
Rimnicu Vilcea LugojArad Fagaras Oradea AradArad Oradea
Rimnicu Vilcea Lugoj
ZerindSibiu
Arad Fagaras Oradea
Timisoara
AradArad Oradea
Lugoj AradArad Oradea
Zerind
Arad
Sibiu Timisoara
Arad
Rimnicu Vilcea
Zerind
Arad
Sibiu
Arad Fagaras Oradea
Timisoara
11 / 31
Search elements
(a) The initial state
(b) After expanding Arad
(c) After expanding Sibiu
Rimnicu Vilcea LugojArad Fagaras Oradea AradArad Oradea
Rimnicu Vilcea Lugoj
ZerindSibiu
Arad Fagaras Oradea
Timisoara
AradArad Oradea
Lugoj AradArad Oradea
Zerind
Arad
Sibiu Timisoara
Arad
Rimnicu Vilcea
Zerind
Arad
Sibiu
Arad Fagaras Oradea
Timisoara
I Expand plans - possible ways (tree nodes).
I Manage/Maintan fringe (or frontier) of plans under consideration.
I Expand new nodes wisely(?).
12 / 31
Tree search algorithm
(a) The initial state
(b) After expanding Arad
(c) After expanding Sibiu
Rimnicu Vilcea LugojArad Fagaras Oradea AradArad Oradea
Rimnicu Vilcea Lugoj
ZerindSibiu
Arad Fagaras Oradea
Timisoara
AradArad Oradea
Lugoj AradArad Oradea
Zerind
Arad
Sibiu Timisoara
Arad
Rimnicu Vilcea
Zerind
Arad
Sibiu
Arad Fagaras Oradea
Timisoara
function tree search(problem) return a solution or failureinitialize by using the initial state of the problemloop
if no candidates for expansion then return failureelse choose a leaf node for expansionend ifif the node contains a goal state then return the solutionend ifExpand the node and add the resulting nodes to the tree
end loopend function
13 / 31
Example of a tree search
a
S
b
c
d
e
f
G
S
a
b
e f
G
c d
Which nodes to explore?What are the properties of a strategy/algorithm?
14 / 31
Search (algorithm) properties
I Complete? Guaranteed to find a solution (if exists)?
I Optimal? Guaranteed to find the least cost path?
I Time complexity? How many steps - an operation with a node?
I Space complexity? How many nodes to remember?
How many nodes in a tree? What are tree parameters?
15 / 31
Depth-First Search (DFS)
a
S
b
c
d
e
f
G
S
a
b
e
f
f
e
f
c
f d
G
d
What are the DFS properties?
16 / 31
DFS properties
I Complete?
I Optimal?
I Time complexity?
I Space complexity?
17 / 31
Breadth-First Search (BFS)
a
S
b
c
d
e
f
G
S
a
b e
c
f d
d
G
What are the BFS properties?
18 / 31
BFS properties
I Complete?
I Optimal?
I Time complexity?
I Space complexity?
19 / 31
DFS vs BFS
S
a
b
e
f
f
e
f
c
f d
G
d
S
a
b e
c
f d
d
G
20 / 31
DFS with limited depth, maxdepth=2
Do not follow nodes with depth > maxdepth
a
S
b
c
d
e
f
G
S
a
b
e f
e
f
c
f d
G
d
G
21 / 31
Iterative deepening DFS (ID-DFS)
I Start with maxdepth = 1
I Perform DFS with limited depth. Report success or failure.
I If failure, forget everything, increase maxdepth and repeat DFS
Is it not a terrible waste to forget everything between steps?
22 / 31
Cost sensitive search
a
S
b
c
d
e
f
G
1
3
1
1
2
1
1
1
1
1
1
1.5 S,0
a,1
b,2 e,2
c,1
f,2 d,2
d,1
G,2
I In BFS, DFS, node ±depth was thenode-value.
I How was the depth actuallycomputed?
I How to evaluate nodes with pathcost?
23 / 31
Uniform Cost Search (UCS)
a
S
b
c
d
e
f
G
1
3
1
1
2
1
1
1
1
1
1
1.5 S,0
a,1
b,2
e,3 f,4
e,2.5
c,1
f,2 d,2
G,3
d,3
When to check the goal (and stop) the search? When visiting orexpanding the node?
24 / 31
When to stop, when visiting or expanding?
a
S
b
c
d
e
f
G
1
3
1
1
2
1
1
11.2
0.5
1
1
1
1.5S,0
a,1
b,2
e,3 f,4
e,2.5
f,3
c,1
f,2.2
G,2.7
d,2
G,3
d,3
25 / 31
Example: Graph with costs
Uniform#Cost#Search#
S
a
b
d p
a
c
e
p
h
f
r
q
q c G
a
q e
p
h
f
r
q
q c G
a
Strategy: expand a cheapest node first:
Fringe is a priority queue (priority: cumulative cost) S
G
d
b
p q
c
e
h
a
f
r
3 9 1
16 4 11
5
7 13
8
10 11
17 11
0
6
3 9
1
1
2
8
8 2
15
1
2
Cost contours
2
26 / 31
UCS properties
27 / 31
Programming a Tree Search
28 / 31
Infrastructure for (tree) search algorithms
What should a tree node n now?
I n.state
I n.parent
I n.pathcost
Perhaps we may add something later, if needed . . .
29 / 31
How to organize nodes?
The Python examples are just suggestions, . . .
I A dynamically linked structure (list()).
I Add a node (list.insert(node)).
I Take a node and remove from the structure (node=list.pop()).
I Check the Python modules heapq1 and queue2 for inspiration.
1https://docs.python.org/3.5/library/heapq.html2https://docs.python.org/3.5/library/queue.html
30 / 31
What is the solution?
I We stop when Goal is reached.
I How do we contruct the path?
31 / 31