Strategies for State Space Search (no corresponding text chapter)

Download Report

Transcript Strategies for State Space Search (no corresponding text chapter)

Strategies for
State Space Search
(no corresponding text chapter)
Wanted
●
An algorithm and associated data structure(s) that
can:
1) Solve an arbitrary 8-puzzle problem (or problem like
it),
2) Do it in a reasonable amount of time using a
reasonable amount of memory, and
3) Be guaranteed to find an optimal solution, that is, one
with a minimum number of moves.
An 8-puzzle Problem Solution
283
164
7 5
up
283
1 4
765
up
2 3
184
765
left
23
184
765
down
123
84
765
right
123
8 4
765
Notes on the Problem Solution
●
●
The solution is a sequence of states, beginning
with the start state and ending with the final state
The solution is not unique
➢
●
●
●
For example, there is another solution corresponding
to right-left-up-up-left-down-right
No state occurs twice in the solution (no cycles)
The solution is optimal, that is, there is no shorter
solution
The solution must be searched for in a space of
many possibilities
Space of Possible States
283
164
7 5
283
164
75
283
64
175
83
264
175
1 3
824
765
283
1 4
765
283
14
765
2 3
184
765
283
164
75
283
16
754
283
14
765
283
6 4
175
23
184
765
234
1 8
765
23
184
765
283
1 6
754
283
674
1 5
123
84
765
234
18
765
283
16
754
2 3
186
754
123
8 4
765
123
864
7 5
123
784
65
23
186
754
28
163
754
283
156
7 4
23
186
754
2 8
163
754
Notes on the State Space
●
●
●
A line connecting two states si and sj means that
there is a move that can turn si into sj and vice
versa
A complete space would show every possible 8puzzle state (maximum 9! = 362,880)
A search for a solution requires navigating the
space making decisions about which way to go at
each state
Solution Within The State Space
283
164
7 5
283
164
75
283
64
175
83
264
175
1 3
824
765
283
1 4
765
283
14
765
2 3
184
765
283
164
75
283
16
754
283
14
765
283
6 4
175
23
184
765
234
1 8
765
23
184
765
283
1 6
754
283
674
1 5
123
84
765
234
18
765
283
16
754
2 3
186
754
123
8 4
765
123
864
7 5
123
784
65
23
186
754
28
163
754
283
156
7 4
23
186
754
2 8
163
754
Tree of Possible Solutions
283
164
7 5
up
left
283
164
75
up
283
64
175
right
283
1 4
765
right
283
164
7 5
up
2 3
184
765
left
23
184
765
down
123
84
765
right
123
8 4
765
283
164
75
up
283
16
754
left
283
164
7 5
Notes on the Search Tree
●
A tree is a way of visualizing a state space search
●
Some states appear more than once in the tree
–
●
●
For example, the sequences of actions left-right and
right-left both result in the same state
Some states introduce a cycle, or a loop within
the solution path
The branching factor b is the average number of
actions possible at a state
Size of the Search Tree
●
●
●
●
●
Without restrictions on cyclic solutions, the tree is
infinite
With these restrictions, the tree is still very large,
O(bd), where d is the tree's depth
If b = 3 and d = 5, then bd = 243
If b = 3 and d = 30, then bd =
205,891,132,094,649
To solve an arbitrary 8-puzzle problem, the tree
search algorithm will have to be "intelligent''
What is Needed
●
A general search operation that could be coded to
work on problems of various types
●
An intelligent search operation that is
guaranteed to find the shortest solution to a
problem
●
We will develop such an operation with the
help of a queue
An Example Search Space
1
2
3
6
7
8
13
19
20
4
14
21
Goal
State
9
10
15
16
17
22
23
24
25
26
5
11
12
18
27
28
The Role of the Queue
Suppose we start with an empty queue:
Add the start state as a node in the search space:
1
Now do the following as a loop:
- Remove a node from the queue
- If the node holds the final state, quit
- Otherwise, compute a collection of nodes, each
of which holds a legal next state (expand the node)
- Add these nodes (the children) to the queue
- Continue the loop
Trace of the Queue
Start with an empty queue:
Add the start state as a node:
1
Remove node, check it, and add its children (expand it):
2 3 4 5
Repeat:
3 4 5 6 7
(suppose you don't allow the
node you just came from)
Queue Trace (cont'd)
Repeat:
4 5 6 7 8
Again:
5 6 7 8 9 10 11
Eventually...:
21 21 22 23 24 28
Remove 21 and terminate, because 21 is the final state.
Full Trace of the Queue
1
2 3 4 5
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9 10 11
6 7 8 9 10 11 12
7 8 9 10 11 12
8 9 10 11 121314
9 10 11 121314
10 11 1213141516
11 121314151617
121314151617
131415161718
1415161718192021
1516171819202121
16171819202121
171819202121222324
1819202121222324
1920212122232428
20212122232428
212122232428
An Example Search Space
1
2
3
6
7
8
13
19
20
4
14
21
Goal
State
9
10
15
16
17
22
23
24
25
26
5
11
12
18
27
28
Using a Queue (cont'd)
Order in which nodes are removed:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21
Suppose we remembered the parent that each
child came from. Then the solution path discovered is:
1, 2, 7, 13, 21
This is a breadth-first search of the state space.
It is guaranteed to find the shortest solution path.
Why Breadth-First Search (BFS) is
Optimal
●
Because it uses a queue, BFS examines all nodes
at depth k before it examines any nodes at depth
k+1, for any k
●
Suppose the shortest path has the final state at
depth d of the state space
●
BFS will examine all nodes at depth d before it
examines any deeper nodes
●
Therefore BFS will find the shortest path first
Breadth-First State Space Search
●
Uses a queue to remember the nodes that are
waiting to be expanded
●
Will find the shortest solution first, if one exists
●
Memory required (worst case) is proportional to
the search tree size: O(bd) -- exponential
●
Time required (worst case) is proportional to the
search tree size: O(bd) -- exponential
Using a Stack To Search
A queue is a First-In First-Out (FIFO) data structure.
All items enter the queue at the tail and are removed
from the head:
2 3 4 5
head
tail
A stack is a Last-In First-Out (LIFO) data structure.
All items are added to and removed from the head.
Therefore, the tail can be ignored:
2 3 4 5
head
An Example Search Space
1
2
3
6
7
8
13
19
20
4
14
21
Goal
State
9
10
15
16
17
22
23
24
25
26
5
11
12
18
27
28
Using a Stack To Search
After adding the children of the start state:
2 3 4 5
Now remove a node (5). Is it final? No. Then compute
its children (i.e., expand it) and add them:
2 3 4 12
Repeat:
2 3 4 18
Again:
2 3 4 28
Using a Stack (cont'd)
Now we remove 28 and find it has no children.
So add nothing:
2 3 4
Remove 4, check if it is final, and add its children:
2 3 9 10 11
Eventually:
2 3 15 22 21
and the search ends, because 21 is removed.
A Full Trace of Stack
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
4 5
4 12
4 18
4 28
4
9 10 11
9 10
9 17
9
15 16
15 22 23 24
15 22 23 25 26 27
15 22 23 25 26
15 22 23 25
15 22 23
15 22
15 22 21
Using a Stack (cont'd)
Following the steps, you find that the order in which
nodes are removed from the stack is:
1, 5, 12, 18, 28, 4, 11, 10, 17, 9, 16, 24, 27, 26, 25,
23, 22, 21
If you remembered the parent each child descended
from, the solution path you would have discovered is:
1, 4, 9, 16, 22, 21
This is a depth-first search of the state space.
Note that it is not guaranteed to find the shortest
solution. However, it is very memory-efficient.
Depth-First State Space Search
●
Uses a stack to remember the nodes that are
waiting to be expanded
●
May not find the shortest solution first
●
Can get caught in depth-first runaway if cyclic
solution paths are not detected (see next)
●
Memory required for the stack is the search depth
(d) times the number of siblings at each depth
level (b): O(bd) -- linear
●
Time required (worst case) is proportional to the
search tree size: O(bd) -- exponential
Dealing with Depth-First Runaway
●
283
164
7 5
A cyclic solution path:
283
1 4
765
283
164
7 5
283
14
765
283
164
75
283
714
65
283
714
6 5
283
64
175
283
7 4
615
283
6 4
175
283
74
615
283
674
1 5
283
674
15
Dealing with Depth-First Runaway
(cont'd)
●
Circularity Check: Do not allow the creation of
a node that has itself as an ancestor
➢
➢
➢
➢
●
Done at expansion time
Requires a search up as far as the root (through a
node's parent)
Fairly expensive: O(d)
Greatly reduces number of useless nodes added to
queue
Although breadth-first search is optimal and thus
does not get caught in depth-first runaway, it still
benefits from a circularity check by reducing
number of nodes on queue.
The Node Class
●
Need to implement the circularity check, and also
reconstruct solution path at end of search
●
Don't want to change State interface
●
Solution: wrap a Node class around State
object
●
Associations:
●
➢
The State this node represents
➢
The parent Node that this node descended from (null
if root)
Operations
Node Expansion Algorithm
expand(node) returns a collection of nodes
{
MoveList mlist = get move list from problem
children = a new collection
for each move in mlist {
State newState = apply move to node's state
if ((newState is not null) and
(newState survives circularity check))
Node newNode = new node for newState with
node as its parent
add newNode to children
}
}
return children
}
General Search Algorithm
●
Suppose waiting is an already constructed
queue or stack, root is the root node of the
state space, and problem is an already
constructed problem.
search(root) returns Node or null {
waiting.add(root)
while (waiting is not empty) {
node = waiting.remove()
state = node.getState()
if (problem.success(state))
return node
else {
children = expand(node)
for each child in children
waiting.add(child)
}
}
return null
}
Reconstructing the Solution Path
●
Recall that as nodes are created in the state space,
they know which parent they descended from
●
Upon expansion, the parent should be recorded in
the child
●
So when the final state is found, the path back to
the root (start state) can be found (see next slide)
Solution Paths
1
2
4
Path found by
7
breadth-first search
9
13
16
21
Goal
State
22
Path found by
depth-first search
Some BreadthFirst Results
Problem
Water Jug
Bridge
8-Puzzle 1
8-Puzzle 2
8-Puzzle 3
8-Puzzle 4
8-Puzzle 5
Optimal Solution Adds &
Solution Found
Removes Largest Size Size at End
4
4
521
272
271
5
5
1,305
704
703
5
5
97
28
27
10
10
1,859
552
551
18
18
148,123
42,252
42,251
20
20
555,076
137,911
137,910
24
Heap overflow
Time
(sec)
0.010
0.030
0.003
0.028
0.760
2.560
Some DepthFirst Results
Optimal Solution
Problem Solution Found
Water Jug
4
11
Bridge
5
5
8-Puzzle 1
5
13441
8-Puzzle 2
10
72512
Adds &
Time
Removes Largest Size Size at End (sec)
51
28
27
0.003
629868
37
10
2.475
37513
10018
10017
11.949
202175
50364
50363
416.164