54
1 Brian Williams, Fall 10 1 Problem Solving as State Space Search Brian C. Williams 16.410-13 Sept 13 th , 2010 Slides adapted from: 6.034 Tomas Lozano Perez, Russell and Norvig AIMA Brian Williams, Fall 10 2 Assignments • Remember: Problem Set #1: Java warm up Out last Wednesday, Due this Wednesday, September 15 th • Reading: Today: Solving problems through search [AIMA] Ch. 3.1-4 Wednesday: Asymptotic Analysis Lecture 2 Notes of 6.046J; Recurrences, Lecture 12 Notes of 6.042J.

16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

Embed Size (px)

Citation preview

Page 1: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

1

Brian Williams, Fall 10 1

Problem Solving as State Space Search

Brian C. Williams 16.410-13 Sept 13th, 2010

Slides adapted from: 6.034 Tomas Lozano Perez, Russell and Norvig AIMA

Brian Williams, Fall 10 2

Assignments

•  Remember: Problem Set #1: Java warm up

Out last Wednesday, Due this Wednesday, September 15th

•  Reading: –  Today: Solving problems through search [AIMA] Ch. 3.1-4 –  Wednesday: Asymptotic Analysis Lecture 2 Notes of 6.046J;

Recurrences, Lecture 12 Notes of 6.042J.

Page 2: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

2

Brian Williams, Fall 10 3

Recap - Course Objectives 1.  Understand the major types of agents and

architectures: –  goal-directed vs utility-based –  deliberative vs reactive –  model-based vs model-free

2.  Learn the modeling and algorithmic building blocks for creating agents:

–  Model problem in an appropriate formal representation.

–  Specify, analyze, apply and implement reasoning algorithms to solve the problem formulations.

Brian Williams, Fall 10 4

Plan

Execute Monitor & Diagnosis

Locate in World

Plan Routes

Map

Maneuver and Track

Mission Goals

Recap – Agent Architectures

Functions: Robust, coordinated operation + mobility

It Begins with State Space Search!

Page 3: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

3

Brian Williams, Fall 10 5

Problem Solving as State Space Search

•  Problem Formulation (Modeling) – Problem solving as state space search

•  Formal Representation – Graphs and search trees

•  Reasoning Algorithms – Depth and breadth-first search

Brian Williams, Fall 10 6

Most Agent Building Block Implementations Use Search

Robust Operations: •  Activity Planning •  Diagnosis •  Repair •  Scheduling •  Resource Allocation

Mobility: •  Path Planning •  Localization •  Map Building •  Control Trajectory

Design

Page 4: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

4

Brian Williams, Fall 10 7

Example: Outpost

Logistics Planning

Brian Williams, Fall 10 8

Early AI: What are the universal problem solving methods?

Astronaut

Goose

Grain

Fox

Rover

Can the astronaut get its supplies

safely across a Lunar crevasse?

•� Astronaut + 1 item

allowed in the rover.

•� Goose alone eats Grain

•� Fox alone eats Goose

Simple Trivial

Image produced for NASA by John Frassanito and Associates.

Page 5: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

5

Brian Williams, Fall 10 9

Problem Solving as State Space Search

•  Formulate Goal –  State

•  Astronaut, Fox, Goose & Grain below crevasse.

•  Formulate Problem –  States

•  Astronaut, Fox, Goose & Grain above or below the crevasse.

–  Operators •  Move: Astronaut drives rover and 1 or 0 items

to other side of crevasse. –  Initial State

•  Astronaut, Fox, Goose & Grain above crevasse.

•  Generate Solution –  Sequence of Operators (or States)

•  Move(goose,astronaut), Move(astronaut), . . .

Brian Williams, Fall 10 10

Astronaut Goose Grain Fox

Astronaut Goose Grain Fox

Grain Fox

Astronaut Goose

Goose Grain

Astronaut Fox

Goose Fox

Astronaut Grain

Goose Grain Fox

Astronaut

Page 6: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

6

Brian Williams, Fall 10 11

Astronaut Goose Grain Fox

Grain Fox

Astronaut Goose

Astronaut Goose Grain Fox

Goose Fox

Astronaut Grain

Astronaut Grain Fox

Goose

Astronaut Goose

Grain Fox

Goose Grain

Astronaut Fox

Astronaut Goose Fox

Grain

Grain

Astronaut Goose Fox

Fox

Astronaut Goose Grain

Goose

Astronaut Fox

Grain

Goose Grain Fox

Astronaut

Astronaut Grain

Goose Fox

Astronaut Fox

Goose Grain

Astronaut

Goose Grain Fox

Astronaut Goose Grain

Fox

Brian Williams, Fall 10 12

Astronaut Goose Grain Fox

Grain Fox

Astronaut Goose

Astronaut Grain Fox

Goose

Goose

Astronaut Fox

Grain

Astronaut Goose

Grain Fox

Astronaut Goose Grain Fox

Grain

Astronaut Goose Fox

Fox

Astronaut Goose Grain

Goose Fox

Astronaut Grain

Goose Grain

Astronaut Fox

Astronaut Goose Fox

Grain

Astronaut Grain

Goose Fox

Astronaut Fox

Goose Grain

Goose Grain Fox

Astronaut

Astronaut

Goose Grain Fox

Astronaut Goose Grain

Fox

Page 7: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

7

Brian Williams, Fall 10 14

•  Swaggert & Lovell assemble emergency rig for Apollo 13 lithium hydroxide unit.

Languages for Expressing States and Operators for Complex Tasks

Image source: NASA.

Brian Williams, Fall 10 13

Formulation Example: 8-Puzzle

•  States: •  Operators: •  Initial and

Goal States:

5 4

6 1

7 3

8

2

1 2

8

3

7 6

4

5

Start Goal

integer location for each tile AND …?? move empty square up, down, left, right as shown above

Page 8: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

8

Brian Williams, Fall 10 15

Example: STRIPS Planning Language

Initial state: (and (hose a) �

(clamp b)� (hydroxide-unit c) � (on-table a) � (on-table b)

(on-table c) � (clear a) � (clear b) � (clear c) � (empty arm))

goal (partial state): (and (connected a b) �

(connected b c)))

precondition: (and (clear hose) (on-table hose)

(empty arm))

effect: (and (not (clear hose)) (not (on-table hose)) (not (empty arm)) (holding arm hose)))

Operators

16

Problem: Find a Route from home to MIT

States? Operators?, Initial and Goal State?

© MapQuest, Navigation Technologies. All rights reserved. This content is excluded from our Creative Commons license. For more information, see http://ocw.mit.edu/fairuse.

pickup hose

Page 9: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

9

Brian Williams, Fall 09 17

Problem: Compensating for Error Online

1 2 3

8 4

7 6 5

•  Policy, π(v) → e, dictates how to act in all states. •  Policy π corresponds to a shortest path tree from all vertices to the destination.

18

How do we Map Path Planning to State Space Search?

Start position

Goal position

Vehicle translates, but no rotation

Page 10: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

10

19

1. Create Configuration Space

Idea: Transform to equivalent problem of navigating a point.

Start position

Goal position

Vehicle translates, but no rotation

20

2. Map From Continuous Problem to Graph Search: Create Visibility Graph Start

position

Goal position

Page 11: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

11

21

Start position

Goal position

2. Map From Continuous Problem to Graph Search: Create Visibility Graph

22

3. Find Shortest Path (e.g., A*) Start

position

Goal position

Page 12: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

12

23

Start position

Goal position

Resulting Solution

24

A Visibility Graph is a Kind of Roadmap

Start position

Goal position

What are the strengths / weaknesses of roadmaps? What are some other types of roadmaps?

Page 13: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

13

25

Voronoi Diagrams Lines equidistant from CSpace obstacles

Brian Williams, Fall 03 26

Roadmaps: Approximate Fixed Cell

Page 14: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

14

Brian Williams, Fall 03 27

Roadmaps: Approximate Fixed Cell

Brian Williams, Fall 03 28

Roadmaps: Approximate Variable Cell

Page 15: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

15

Brian Williams, Fall 03 29

Roadmaps: Exact Cell Decomposition

Brian Williams, Fall 03 30

How do we handle large state spaces?

Start position

Goal position

RRT, Ta

RRT, Tb

Rapid exploring Random Trees

Page 16: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

16

Brian Williams, Fall 10 31

Problem Solving as State Space Search

•  Problem Formulation (Modeling) – Problem solving as state space search

•  Formal Representation – Graphs and search trees

•  Reasoning Algorithms – Depth and breadth-first search

Brian Williams, Fall 10 32

Problem Formulation: A Graph Operator

Edge

State

Vertex

Directed Graph (one-way streets)

Undirected Graph (two-way streets)

neighbors (adjacent)

Tail Vertex of Edge

Head Vertex of Edge

h t

Page 17: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

17

Brian Williams, Fall 10 33

Problem Formulation: A Graph

In Degree (2)

Out Degree (1)

Directed Graph (one-way streets)

b

Undirected Graph (two-way streets)

b Degree (3)

Brian Williams, Fall 10 34

Problem Formulation: A Graph

Directed Graph (one-way streets)

a c

d e

b

Undirected Graph (two-way streets)

a c

e d

b

Connected graph Path between all vertices.

Complete graph All vertices are adjacent.

Sub graph Subset of vertices edges between vertices in Subset

Clique A complete subgraph (All vertices are adjacent).

Strongly connected graph Directed path between all vertices.

Page 18: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

18

Brian Williams, Fall 10 35

Specifying a Graph: G = <V, E>

a c

d e

b Vertices V = {a, b, c, d, e}

Edges E = {<a, b>, <a, c>, <b, e>, <c, b>, <c,d>, <e, d>}

Notation: <a, b, … n> an ordered list of elements a, b … {a, b, … n} an unordered set of distinguished elements.

Brian Williams, Fall 10 36

Examples of Graphs

San Fran

Boston

LA Dallas

Wash DC

Roadmap

A B C

A B

C

A B

C

A

B

C

Put C on B

Put C on A

Put B on C

Put C on A

A

B

C Put A on C

Planning Actions

(graph of possible states of the world)

Page 19: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

19

Brian Williams, Fall 10 37

Formalizing State Space Search

C

S

B

G A

D

Input: A search problem S = <g, S, G> where •  graph g = <V, E>, •  start vertex S in V, and •  goal vertex G in V.

Output: A simple path P = <S, v2, … G> in g from S to G.

Brian Williams, Fall 10 38

Simple Paths of Graph g = <V, E> C

S

B

G A

D

A simple path is a path that has no cycles. A cycle is a subpath where start = end (i.e., repeated vertices).

A (directed) path P of graph g is a sequence of vertices <v1, … vn> in V such that each successive pair <vi,vi+1> is a (directed) edge in E

start end

<S, A, D, C>

Page 20: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

20

Brian Williams, Fall 10 39

A Problem Solver Searches through all Simple Paths

C

S

B

G A

D

S

D

A

C G

Brian Williams, Fall 10 40

A Search Tree Denotes All Simple Paths

C

S

B

G A

D

S

D

B A

C G

C G

D

C G

Enumeration is: •  Complete •  Systematic •  Sound

Page 21: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

21

Brian Williams, Fall 10 41

Search Trees

A tree T is a directed graph, such that •  there exists exactly one undirected path between any pair of vertices. • In degree of each vertex is 1

think of a tree as a “family” tree

Brian Williams, Fall 10 42

Search Trees

Root

Branch (Edge)

Node (Vertex)

Leaf

think of a tree as a “family” tree

Page 22: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

22

Brian Williams, Fall 10 43

Search Trees

Parent

Child

Siblings

think of a tree as a “family” tree

Brian Williams, Fall 10 44

Search Trees

Ancestors

think of a tree as a “family” tree

Page 23: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

23

Brian Williams, Fall 10 45

Search Trees

Descendants

think of a tree as a “family” tree

Brian Williams, Fall 10 46

Problem Solving as State Space Search

•  Problem Formulation (Modeling) – Problem solving as state space search

•  Formal Representation – Graphs and search trees

•  Reasoning Algorithms – Depth and breadth-first search

Page 24: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

24

Brian Williams, Fall 10 47

Classes of Search Blind Depth-First Systematic exploration of whole tree

(uninformed) Breadth-First until the goal is found.

Iterative-Deepening

Heuristic Hill-Climbing Use heuristic measure of goodness

(informed) Best-First of a node.

Beam

Optimal A* Use path “length” measure. Find

(informed) Branch&Bound “shortest” path.

Brian Williams, Fall 10 48

Depth First Search (DFS)

S

D

B A

C G

C G

D

C G

Local Rule: After visiting node… •  Visit its children before its siblings •  Visit its children left to right

1

2

3

4 5

6

7

8

9 10

11

S

A

D

C G

C

B

D

C G

G

Page 25: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

25

Brian Williams, Fall 10 49

Breadth First Search (BFS)

S

D

B A

C G

C G

D

C G

Local Rule: After visiting node… •  Visit its siblings, before its children •  Visit its children left to right

1

2

4

8 9

5

3

6

10 11

7

S

A

D

C G

C

B

D

C G

G

Brian Williams, Fall 10 50

Elements of Algorithm Design Algorithm Description: (Today)

–  stylized pseudo code, sufficient to analyze and implement the algorithm (implementation next Wednesday).

Algorithm Analysis: (Wednesday & Monday) •  Time complexity:

–  how long does it take to find a solution? •  Space complexity:

–  how much memory does it need to perform search?

•  Soundness: –  when a solution is returned, is it guaranteed to be correct?

•  Completeness: –  is the algorithm guaranteed to find a solution when there is one?

Page 26: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

26

Brian Williams, Fall 10 51

Problem Solving as State Space Search

•  Problem Formulation (Modeling) •  Formal Representation •  Reasoning Algorithms

– A generic search algorithm description – Depth-first search example – Handling cycles – Breadth-first search example

Brian Williams, Fall 10 52

Solve <g = <V, E>, S, G> using State Space Search

Search States: •  All simple paths <S, …v> in g starting at S Initial State: •  <S> Operator: •  Extend a path <S, … v> to <S, … v, u>

for each <v, u> in E –  call u a child of v

Goal: •  A simple path <S, …, G> in g

Page 27: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

27

Brian Williams, Fall 10 53

Solve <g = <V, E>, S, G> using State Space Search

How do we maintain the search state? •  An ordering on partial paths yet to be expanded

(called a queue Q).

How do we perform search? •  Repeatedly:

1.  Select next partial path from Q. 2.  Expand it. 3.  Add expansions to Q.

•  Terminate when goal G is found.

S

D

B A

C G

C G

D

C G

Brian Williams, Fall 10 54

Simple Search Algorithm: Preliminaries

•  A partial path from S to D is listed in reverse order, •  e.g., <D, A, S>

•  The head of a partial path is its most recent visited node, •  e.g., D.

•  The Q is a list of partial paths, •  e.g. (<D, A, S>, <C, A, S> …>.

S

D

B A

C G

C G

D

C G

Page 28: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

28

Brian Williams, Fall 10 55

Simple Search Algorithm Let Q be a list of partial paths, S be the Start node and G be the Goal node.

1.   Initialize Q with partial path <S> 2.   If Q is empty, fail. Else, pick a partial path N from Q 3.   If head(N) = G, return N (goal reached!) 4.   Else:

a)   Remove N from Q b)  Find all children of head(N) and

create a one-step extension of N to each child c)   Add all extended paths to Q d)  Go to step 2

Brian Williams, Fall 10 56

Problem Solving as State Space Search

•  Problem Formulation (Modeling) •  Formal Representation •  Reasoning Algorithms

– A generic search algorithm description – Depth-first search example – Handling cycles – Breadth-first search example

Page 29: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

29

Brian Williams, Fall 10 57

Depth First Search (DFS)

S

D

B A

C G

C G

D

C G

1

2

3

4 5

6

7

8

9 10

11

S

A

D

C G

C

B

D

C G

G

Assume we remove the first element of Q,

Where to Q do we add the path extensions?

Idea: After visiting node •  Visit its children left to right (or top to bottom) •  Visit its children before its siblings

Brian Williams, Fall 10 58

Simple Search Algorithm Let Q be a list of partial paths, S be the start node and G be the Goal node.

1.   Initialize Q with partial path <S> 2.   If Q is empty, fail. Else, pick a partial path N from Q 3.   If head(N) = G, return N (goal reached!) 4.   Else:

a)   Remove N from Q b)  Find all children of head(N) and

create a one-step extension of N to each child c)   Add all extended paths to Q d)  Go to step 2

Page 30: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

30

Brian Williams, Fall 10 59

Depth-First Pick first element of Q; Add path extensions to front of Q

C

S

B

G A

D

Q

1 (S) 2 3 4 5

1

Brian Williams, Fall 10 60

Simple Search Algorithm Let Q be a list of partial paths, S be the Start node and G be the Goal node.

1.   Initialize Q with partial path <S> 2.   If Q is empty, fail. Else, pick a partial path N from Q 3.   If head(N) = G, return N (goal reached!) 4.   Else:

a)   Remove N from Q b)  Find all children of head(N) and

create a one-step extension of N to each child c)   Add all extended paths to Q d)  Go to step 2

Page 31: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

31

Brian Williams, Fall 10 61

C

S

B

G A

D

Q

1 (S) 2 3 4 5

1

Depth-First Pick first element of Q; Add path extensions to front of Q

Brian Williams, Fall 10 62

C

S

B

G A

D

Q

1 (S) 2 (A S) 3 4 5

1

Added paths in blue

Depth-First Pick first element of Q; Add path extensions to front of Q

Page 32: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

32

Brian Williams, Fall 10 63

C

S

B

G A

D

Q

1 (S) 2 (A S) (B S) 3 4 5

1

Added paths in blue

Depth-First Pick first element of Q; Add path extensions to front of Q

Brian Williams, Fall 10 64

Simple Search Algorithm Let Q be a list of partial paths, Let S be the start node and Let G be the Goal node.

1.   Initialize Q with partial path <S> 2.   If Q is empty, fail. Else, pick a partial path N from Q 3.   If head(N) = G, return N (goal reached!) 4.   Else:

a)   Remove N from Q b)  Find all children of head(N) and

create a one-step extension of N to each child c)   Add all extended paths to Q d)  Go to step 2

Page 33: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

33

Brian Williams, Fall 10 65

C

S

B

G A

D

Q

1 (S) 2 (A S) (B S) 3 4 5

1

Added paths in blue

2

Depth-First Pick first element of Q; Add path extensions to front of Q

Brian Williams, Fall 10 66

C

S

B

G A

D

Q

1 (S) 2 (A S) (B S) 3 (C A S) (D A S) (B S) 4 5

1

2

Depth-First Pick first element of Q; Add path extensions to front of Q

Added paths in blue

Page 34: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

34

Brian Williams, Fall 10 67

C

S

B

G A

D

Q

1 (S) 2 (A S) (B S) 3 (C A S) (D A S) (B S) 4 5

1

2

3

Depth-First Pick first element of Q; Add path extensions to front of Q

Added paths in blue

Brian Williams, Fall 10 68

C

S

B

G A

D

Q

1 (S) 2 (A S) (B S) 3 (C A S) (D A S) (B S) 4 (D A S) (B S) 5

1

2

3

Depth-First Pick first element of Q; Add path extensions to front of Q

Added paths in blue

Page 35: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

35

Brian Williams, Fall 10 69

C

S

B

G A

D

Q

1 (S) 2 (A S) (B S) 3 (C A S) (D A S) (B S) 4 (D A S) (B S) 5

1

2

3

4

Depth-First Pick first element of Q; Add path extensions to front of Q

Added paths in blue

Brian Williams, Fall 10 70

C

S

B

G A

D

Q

1 (S) 2 (A S) (B S) 3 (C A S) (D A S) (B S) 4 (D A S) (B S)

5 (C D A S)(G D A S) (B S)

1

2

3

4

Depth-First Pick first element of Q; Add path extensions to front of Q

Page 36: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

36

Brian Williams, Fall 10 71

C

S

B

G A

D

Q

1 (S) 2 (A S) (B S) 3 (C A S) (D A S) (B S) 4 (D A S) (B S)

5 (C D A S)(G D A S) (B S)

1

2

3

4

Depth-First Pick first element of Q; Add path extensions to front of Q

Brian Williams, Fall 10 72

C

S

B

G A

D

Q

1 (S) 2 (A S) (B S) 3 (C A S) (D A S) (B S) 4 (D A S) (B S)

5 (C D A S)(G D A S) (B S)

6 (G D A S)(B S)

1

2

3

4

Depth-First Pick first element of Q; Add path extensions to front of Q

Page 37: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

37

Brian Williams, Fall 10 73

C

S

B

G A

D

Q

1 (S) 2 (A S) (B S) 3 (C A S) (D A S) (B S) 4 (D A S) (B S)

5 (C D A S)(G D A S) (B S)

6 (G D A S)(B S)

1

2

3

4

Depth-First Pick first element of Q; Add path extensions to front of Q

Brian Williams, Fall 10 74

Simple Search Algorithm Let Q be a list of partial paths, Let S be the start node and Let G be the Goal node.

1.   Initialize Q with partial path <S> 2.   If Q is empty, fail. Else, pick a partial path N from Q 3.   If head(N) = G, return N (goal reached!) 4.   Else:

a)   Remove N from Q b)  Find all children of head(N) and

create a one-step extension of N to each child c)   Add all extended paths to Q d)  Go to step 2

Page 38: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

38

Brian Williams, Fall 10 75

Problem Solving as State Space Search

•  Problem Formulation (Modeling) •  Formal Representation •  Reasoning Algorithms

– A generic search algorithm description – Depth-first search example – Handling cycles – Breadth-first search example

Brian Williams, Fall 10 76

C

S

B

G A

D

Issue: Starting at S and moving top to bottom, will depth-first search ever reach G?

Page 39: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

39

Brian Williams, Fall 10 77

C

S

B

G A

D

Q

1 (S) 2 (A S) (B S) 3 (C A S) (D A S) (B S) 4 (D A S) (B S)

5 (C D A S)(G D A S) (B S)

6 (G D A S)(B S)

1

2

3

4

Depth-First

•  C visited multiple times •  Multiple paths to C, D & G

How much wasted effort can be incurred in the worst case?

Effort can be wasted in more mild cases

Brian Williams, Fall 10 78

How Do We Avoid Repeat Visits? Idea:

•  Keep track of nodes already visited.

•  Do not place expanded path on Q if head is a visited node.

Does this maintain correctness?

•  Any goal reachable from a node that was visited a second time would be reachable from that node the first time.

Does this always improve efficiency?

•  Visits only a subset of the original paths, such that each node appears at most once at the head of a visited path.

Page 40: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

40

Brian Williams, Fall 10 79

How Do We Modify The Simple Search Algorithm?

Let Q be a list of partial paths, Let S be the Start node and Let G be the Goal node.

1.   Initialize Q with partial path <S> as only entry; 2.   If Q is empty, fail. Else, pick some partial path N from Q 3.   If head(N) = G, return N (goal reached!) 4.   Else

a)   Remove N from Q b)  Find all children of head(N) and

create a one-step extension of N to each child c)   Add to Q all the extended paths d)  Go to step 2

Brian Williams, Fall 10 80

Simple Search Algorithm Let Q be a list of partial paths, Let S be the start node and Let G be the Goal node.

1.   Initialize Q with partial path <S> as only entry; set Visited = {} 2.   If Q is empty, fail. Else, pick some partial path N from Q 3.   If head(N) = G, return N (goal reached!) 4.   Else

a)   Remove N from Q b)  Find all children of head(N) not in Visited and

create a one-step extension of N to each child c)   Add to Q all the extended paths d)  Add children of head(N) to Visited e)   Go to step 2

Page 41: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

41

Brian Williams, Fall 10 81

Testing for the Goal •  This algorithm stops (in step 3) when head(N) = G.

•  We could have performed this test in step 6 as each extended path is added to Q. This would catch termination earlier and be perfectly correct for all the searches we have covered so far.

•  However, performing the test in step 6 will be incorrect for the optimal search algorithms that we look at later. We have chosen to leave the test in step 3 to maintain uniformity with these future searches.

Brian Williams, Fall 10 82

Problem Solving as State Space Search

•  Problem Formulation (modeling) •  Formal Representation •  Reasoning Algorithms

– A generic search algorithm description – Depth-first search example – Handling cycles – Breadth-first search example

Page 42: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

42

Brian Williams, Fall 10 83

Breadth First Search (BFS)

S

D

B A

C G

C G

D

C G

1

2

4

8 9

5

3

6

10 11

7

S

A

D

C G

C

B

D

C G

G

Idea: After visiting node •  Visit its children left to right •  Visit its siblings, before its children

Assume we remove the first element of Q,

Where to Q do we add the path extensions?

Brian Williams, Fall 10 84

Breadth-First with Visited List Pick first element of Q; Add path extensions to end of Q

C

S

B

G A

D

Q Visited

1 (S) S 2 3 4 5 6

1

Page 43: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

43

Brian Williams, Fall 10 85

Breadth-First with Visited List Pick first element of Q; Add path extensions to end of Q

C

S

B

G A

D

Q Visited

1 (S) S 2 3 4 5 6

1

Brian Williams, Fall 10 86

Breadth-First with Visited List Pick first element of Q; Add path extensions to end of Q

C

S

B

G A

D

Q Visited

1 (S) S 2 (A S) (B S) A,B,S 3 4 5 6

1

Page 44: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

44

Brian Williams, Fall 10 87

Breadth-First with Visited List Pick first element of Q; Add path extensions to end of Q

C

S

B

G A

D

Q Visited

1 (S) S 2 (A S) (B S) A,B,S 3 4 5 6

1

2

Brian Williams, Fall 10 88

Breadth-First with Visited List Pick first element of Q; Add path extensions to end of Q

C

S

B

G A

D

Q Visited

1 (S) S 2 (A S) (B S) A,B,S 3 (B S) (C A S) (D A S) C,D,B,A,S 4 5 6

1

2

Page 45: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

45

Brian Williams, Fall 10 89

Breadth-First with Visited List Pick first element of Q; Add path extensions to end of Q

C

S

B

G A

D

Q Visited

1 (S) S 2 (A S) (B S) A,B,S 3 (B S) (C A S) (D A S) C,D,B,A,S 4 5 6

1

2

3

Brian Williams, Fall 10 90

Breadth-First with Visited List Pick first element of Q; Add path extensions to end of Q

C

S

B

G A

D

Q Visited

1 (S) S 2 (A S) (B S) A,B,S 3 (B S) (C A S) (D A S) C,D,B,A,S 4 (C A S) (D A S) (G B S)* G,C,D,B,A,S 5 6

1

2

3

* We could stop here, when the first path to the goal is generated.

Page 46: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

46

Brian Williams, Fall 10 91

Breadth-First with Visited List Pick first element of Q; Add path extensions to end of Q

C

S

B

G A

D

Q Visited

1 (S) S 2 (A S) (B S) A,B,S 3 (B S) (C A S) (D A S) C,D,B,A,S 4 (C A S) (D A S) (G B S)* G,C,D,B,A,S 5 6

1

2

3

4

* We could stop here, when the first path to the goal is generated.

Brian Williams, Fall 10 92

Breadth-First with Visited List Pick first element of Q; Add path extensions to end of Q

C

S

B

G A

D

Q Visited

1 (S) S 2 (A S) (B S) A,B,S 3 (B S) (C A S) (D A S) C,D,B,A,S 4 (C A S) (D A S) (G B S)* G,C,D,B,A,S 5 (D A S) (G B S) G,C,D,B,A,S 6

1

2

3

4

5

Page 47: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

47

Brian Williams, Fall 10 93

Breadth-First with Visited List Pick first element of Q; Add path extensions to end of Q

C

S

B

G A

D

Q Visited

1 (S) S 2 (A S) (B S) A,B,S 3 (B S) (C A S) (D A S) C,D,B,A,S 4 (C A S) (D A S) (G B S)* G,C,D,B,A,S 5 (D A S) (G B S) G,C,D,B,A,S 6 (G B S) G,C,D,B,A,S

1

2

3

4

5

6

Brian Williams, Fall 10 94

Breadth-First with Visited List Pick first element of Q; Add path extensions to end of Q

C

S

B

G A

D

Q Visited

1 (S) S 2 (A S) (B S) A,B,S 3 (B S) (C A S) (D A S) C,D,B,A,S 4 (C A S) (D A S) (G B S)* G,C,D,B,A,S 5 (D A S) (G B S) G,C,D,B,A,S 6 (G B S) G,C,D,B,A,S

1

2

3

4

5

6

Page 48: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

48

Brian Williams, Fall 10 95

Depth-first with Visited List Pick first element of Q; Add path extensions to front of Q

C

S

B

G A

D

Q Visited

1 (S) S 2 (A S) (B S) A, B, S 3 (C A S) (D A S) (B S) C,D,B,A,S 4 (D A S) (B S) C,D,B,A,S 5 (G D A S) (B S) G,C,D,B,A,S

1

2

3

4

5

Brian Williams, Fall 10 96

Depth First Search (DFS) S

D

B A

C G

C G

D

C G

S

D

B A

C G

C G

D

C G

Breadth First Search (BFS)

For each search type, where do we place the children on the queue?

Depth-first:

Add path extensions to front of Q

Pick first element of Q

Breadth-first:

Add path extensions to back of Q

Pick first element of Q

Page 49: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

49

Brian Williams, Fall 10 97

What You Should Know •  Most problem solving tasks may be

formulated as state space search. •  State space search is formalized using

graphs, simple paths, search trees, and pseudo code.

•  Depth-first and breadth-first search are framed, among others, as instances of a generic search strategy.

•  Cycle detection is required to achieve efficiency and completeness.

Brian Williams, Fall 10 98

Appendix

Page 50: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

50

Brian Williams, Fall 10 99

Breadth-First (without Visited list) Pick first element of Q; Add path extensions to end of Q

C

S

B

G A

D

Q

1 (S) 2 3 4 5 6 7

1

Brian Williams, Fall 10 100

Breadth-First (without Visited list) Pick first element of Q; Add path extensions to end of Q

C

S

B

G A

D

Q

1 (S) 2 (A S) (B S) 3 4 5 6 7

1

2

Added paths in blue

Page 51: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

51

Brian Williams, Fall 10 101

Breadth-First (without Visited list) Pick first element of Q; Add path extensions to end of Q

C

S

B

G A

D

Q

1 (S) 2 (A S) (B S) 3 (B S) (C A S) (D A S) 4 5 6 7

1

2

3

Added paths in blue

Brian Williams, Fall 10 102

Breadth-First (without Visited list) Pick first element of Q; Add path extensions to end of Q

C

S

B

G A

D

Q

1 (S) 2 (A S) (B S) 3 (B S) (C A S) (D A S) 4 (C A S) (D A S) (D B S) (G B S)* 5 6 7

1

2

3

4

Added paths in blue Revisited nodes in pink * We could have stopped here, when the first path to the goal was generated.

Page 52: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

52

Brian Williams, Fall 10 103

Breadth-First (without Visited list) Pick first element of Q; Add path extensions to end of Q

C

S

B

G A

D

Q

1 (S) 2 (A S) (B S) 3 (B S) (C A S) (D A S) 4 (C A S) (D A S) (D B S) (G B S)* 5 (D A S) (D B S) (G B S) 6 7

1

2

3

4

5

Brian Williams, Fall 10 104

Breadth-First (without Visited list) Pick first element of Q; Add path extensions to end of Q

C

S

B

G A

D

Q

1 (S) 2 (A S) (B S) 3 (B S) (C A S) (D A S) 4 (C A S) (D A S) (D B S) (G B S)* 5 (D A S) (D B S) (G B S) 6 (D B S) (G B S) (C D A S) (G D A S) 7

1

2

3

4

5 6

Page 53: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

53

Brian Williams, Fall 10 105

Breadth-First (without Visited list) Pick first element of Q; Add path extensions to end of Q

C

S

B

G A

D

Q

1 (S) 2 (A S) (B S) 3 (B S) (C A S) (D A S) 4 (C A S) (D A S) (D B S) (G B S)* 5 (D A S) (D B S) (G B S) 6 (D B S) (G B S) (C D A S) (G D A S) 7 (G B S) (C D A S) (G D A S)(C D B S)(G D B S)

1

2

3

4

5 6

7

Page 54: 16.410 Lecture 02: Problem Solving as State Space Search · PDF file– Problem solving as state space search • Formal Representation – Graphs and search trees • Reasoning Algorithms

MIT OpenCourseWarehttp://ocw.mit.edu

16.410 / 16.413 Principles of Autonomy and Decision MakingFall 2010

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.