strategies for state space search (no corresponding text chapter)

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

Post on 19-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Strategies for State Space Search (no corresponding text chapter)

Strategies forState Space Search

(no corresponding text chapter)

Page 2: 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.

Page 3: Strategies for State Space Search (no corresponding text chapter)

An 8-puzzle Problem Solution2 8 31 6 47 5

2 8 31 47 6 5

2 31 8 47 6 5

2 31 8 47 6 5

1 2 38 4

7 6 5

1 2 38 47 6 5

up

up

left

down

right

Page 4: Strategies for State Space Search (no corresponding text chapter)

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

Page 5: Strategies for State Space Search (no corresponding text chapter)

Space of Possible States2 8 31 6 47 5

2 8 31 47 6 5

2 31 8 47 6 5

2 8 31 6 4

7 5

2 8 31 6 47 5

2 8 36 4

1 7 5

2 8 31 67 5 4

8 32 6 41 7 5

2 8 36 41 7 5

2 8 31 4

7 6 5

2 8 31 47 6 5

2 31 8 47 6 5

2 31 8 47 6 5

1 2 38 4

7 6 5

1 2 38 47 6 5

1 2 37 8 4

6 5

1 38 2 47 6 5

1 2 38 6 47 5

2 8 31 67 5 4

2 81 6 37 5 4

2 3 41 87 6 5

2 8 31 6

7 5 4

2 8 31 5 67 4

2 31 8 67 5 4

2 8 36 7 41 5

2 3 41 87 6 5

2 81 6 37 5 4

2 31 8 67 5 4

2 31 8 67 5 4

Page 6: Strategies for State Space Search (no corresponding text chapter)

Notes on the State Space

● A line connecting two states si and s

j means that

there is a move that can turn si into s

j and vice

versa● A complete space would show every possible 8-

puzzle state (maximum 9! = 362,880)● A search for a solution requires navigating the

space making decisions about which way to go at each state

Page 7: Strategies for State Space Search (no corresponding text chapter)

Solution Within The State Space2 8 31 6 47 5

2 8 31 47 6 5

2 31 8 47 6 5

2 8 31 6 4

7 5

2 8 31 6 47 5

2 8 36 4

1 7 5

2 8 31 67 5 4

8 32 6 41 7 5

2 8 36 41 7 5

2 8 31 4

7 6 5

2 8 31 47 6 5

2 31 8 47 6 5

2 31 8 47 6 5

1 2 38 4

7 6 5

1 2 38 47 6 5

1 2 37 8 4

6 5

1 38 2 47 6 5

1 2 38 6 47 5

2 8 31 67 5 4

2 81 6 37 5 4

2 3 41 87 6 5

2 8 31 6

7 5 4

2 8 31 5 67 4

2 31 8 67 5 4

2 8 36 7 41 5

2 3 41 87 6 5

2 81 6 37 5 4

2 31 8 67 5 4

2 31 8 67 5 4

Page 8: Strategies for State Space Search (no corresponding text chapter)

Tree of Possible Solutions2 8 31 6 47 5

2 8 31 47 6 5

2 31 8 47 6 5

2 31 8 47 6 5

1 2 38 4

7 6 5

1 2 38 47 6 5

up

up

left

down

right

2 8 31 6 4

7 5

2 8 31 6 47 5

left right

2 8 36 4

1 7 5

2 8 31 6 47 5

up right2 8 31 67 5 4

2 8 31 6 47 5

up left

Page 9: Strategies for State Space Search (no corresponding text chapter)

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

Page 10: Strategies for State Space Search (no corresponding text chapter)

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

Page 11: Strategies for State Space Search (no corresponding text chapter)

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

Page 12: Strategies for State Space Search (no corresponding text chapter)

An Example Search Space

1

3 4 52

6 7 8 9 1110 12

1413

19 2120

16 1715

23 2422

26 2725

18

28

GoalState

Page 13: Strategies for State Space Search (no corresponding text chapter)

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

Page 14: Strategies for State Space Search (no corresponding text chapter)

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)

Page 15: Strategies for State Space Search (no corresponding text chapter)

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.

Page 16: Strategies for State Space Search (no corresponding text chapter)

Full Trace of the Queue

12 3 4 53 4 5 6 74 5 6 7 85 6 7 8 9 116 7 8 9 10 12

1314

1617

202121

2324

1011

7 8 9 10 12118 9 10 1211

1314

9 10 121113

14

10 1211 151613

14

1211 15171613

14

12 15171613

14

15 181716

14

15 18192021171615 1819

2120211716 1819212021171819 22

23242120211819 22232421202119 22 28

2324212021 22 2823242121 22 28

Page 17: Strategies for State Space Search (no corresponding text chapter)

An Example Search Space

1

3 4 52

6 7 8 9 1110 12

1413

19 2120

16 1715

23 2422

26 2725

18

28

GoalState

Page 18: Strategies for State Space Search (no corresponding text chapter)

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

Page 19: Strategies for State Space Search (no corresponding text chapter)

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

Page 20: Strategies for State Space Search (no corresponding text chapter)

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

Page 21: Strategies for State Space Search (no corresponding text chapter)

Using a Stack To Search

2 3 4 5

A queue is a First-In First-Out (FIFO) data structure.All items enter the queue at the tail and are removedfrom the head:

head

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 tail

Page 22: Strategies for State Space Search (no corresponding text chapter)

An Example Search Space

1

3 4 52

6 7 8 9 1110 12

1413

19 2120

16 1715

23 2422

26 2725

18

28

GoalState

Page 23: Strategies for State Space Search (no corresponding text chapter)

Using a Stack To Search

2 3 4 5

After adding the children of the start state:

Now remove a node (5). Is it final? No. Then computeits children (i.e., expand it) and add them:

2 3 4 12

Repeat:

2 3 4 18Again:

2 3 4 28

Page 24: Strategies for State Space Search (no corresponding text chapter)

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.

Page 25: Strategies for State Space Search (no corresponding text chapter)

A Full Trace of Stack

12 3 4 52 3 4 122 3 4 182 3 4 282 3 42 3 9 10 112 3 9 102 3 9 172 3 92 3 15 162 3 15 22 23 242 3 15 22 23 25 26 272 3 15 22 23 25 262 3 15 22 23 252 3 15 22 232 3 15 222 3 15 22 21

Page 26: Strategies for State Space Search (no corresponding text chapter)

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 descendedfrom, 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.

Page 27: Strategies for State Space Search (no corresponding text chapter)

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

Page 28: Strategies for State Space Search (no corresponding text chapter)

Dealing with Depth-First Runaway● A cyclic solution path:

2 8 31 6 47 5 2 8 3

1 47 6 5 2 8 3

1 47 6 5 2 8 3

7 1 46 5 2 8 3

7 1 46 5 2 8 3

7 46 1 5 2 8 3

7 46 1 5 2 8 3

6 7 41 5

2 8 36 7 41 5

2 8 36 41 7 5

2 8 36 4

1 7 52 8 31 6 4

7 5

2 8 31 6 47 5

Page 29: Strategies for State Space Search (no corresponding text chapter)

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.

Page 30: Strategies for State Space Search (no corresponding text chapter)

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

➢ Accessors for the state and parent

Page 31: Strategies for State Space Search (no corresponding text chapter)

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}

Page 32: Strategies for State Space Search (no corresponding text chapter)

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}

Page 33: Strategies for State Space Search (no corresponding text chapter)

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)

Page 34: Strategies for State Space Search (no corresponding text chapter)

Solution Paths

1

42

7 9

13

21

16

22

GoalState

Path found bydepth-first search

Path found bybreadth-first search

Page 35: Strategies for State Space Search (no corresponding text chapter)

Some BreadthFirst Results

Optimal Solution Adds & TimeProblem Solution Found Removes Largest Size Size at End (sec)Water Jug 4 4 521 272 271 0.010Bridge 5 5 1,305 704 703 0.0308-Puzzle 1 5 5 97 28 27 0.0038-Puzzle 2 10 10 1,859 552 551 0.0288-Puzzle 3 18 18 148,123 42,252 42,251 0.7608-Puzzle 4 20 20 555,076 137,911 137,910 2.5608-Puzzle 5 24 Heap overflow

Page 36: Strategies for State Space Search (no corresponding text chapter)

Some DepthFirst Results

Optimal Solution Adds & TimeProblem Solution Found Removes Largest Size Size at End (sec)Water Jug 4 11 51 28 27 0.003Bridge 5 5 629868 37 10 2.4758-Puzzle 1 5 13441 37513 10018 10017 11.9498-Puzzle 2 10 72512 202175 50364 50363 416.164