Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as...

87
Problem Solving as State Space Search Brian C.Williams 16.410-13 Sep 14 th , 2004 Slides adapted from: 6.034 Tomas Lozano Perez, Russell and Norvig AIMA Brian Williams, Spring 04 1

Transcript of Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as...

Page 1: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Problem Solving as State Space Search

Brian C.Williams16.410-13Sep 14th, 2004

Slides adapted from:6.034 Tomas Lozano Perez,Russell and Norvig AIMA Brian Williams, Spring 04 1

Page 2: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Assignments

• Remember: Problem Set #1: Simple Scheme and Search due Monday, September 20th, 2003.

• Reading: – Solving problems by searching: AIMA Ch. 3

Brian Williams, Spring 04 2

Page 3: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Brian Williams, Spring 04 3

Complex missions must carefully:

• Plan complex sequences of actions

• Schedule actions

• Allocate tight resources

• Monitor and diagnose behavior

• Repair or reconfigure hardware.

Most AI problems, like these, may be formulated as state space search.

Page 4: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Outline

• Problem Formulation– Problem solving as state space search

• Mathematical Model– Graphs and search trees

• Reasoning Algorithms– Depth and breadth-first search

Brian Williams, Spring 04 4

Page 5: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

AstronautGooseGrainFox

Rover

Can the astronaut get its supplies safely across the Martian canal?

• Astronaut + 1 item allowed in the rover.

• Goose alone eats Grain• Fox alone eats Goose

Early AI: What are the universal problem solving methods?

TrivialSimpleBrian Williams, Spring 04 5

Page 6: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Problem Solving as State Space Search

• Formulate Goal– State

• Astronaut, Fox, Goose & Grain across river

• Formulate Problem– States

• Location of Astronaut, Fox, Goose & Grain at top or bottom river bank

– Operators• Astronaut drives rover and 1 or 0 items

to other bank.

• Generate Solution– Sequence of Operators (or States)

• Move(goose,astronaut), Move(astronaut), . . .Brian Williams, Spring 04 6

Page 7: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Brian Williams, Spring 04 7

AstronautGooseGrainFox

GrainFox

AstronautGoose

GooseGrain

AstronautFox

GooseFox

AstronautGrain

GooseGrainFox

Astronaut

AstronautGooseGrainFox

Page 8: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Brian Williams, Spring 04 8

AstronautGooseGrainFox

GrainFox

AstronautGoose

AstronautGooseGrainFox

GooseFox

AstronautGrain

AstronautGrainFox

Goose

AstronautGoose

GrainFox

GooseGrain

AstronautFox

AstronautGooseFox

Grain

Grain

AstronautGooseFox

Fox

AstronautGooseGrain

Goose

AstronautFox

Grain

GooseGrainFox

Astronaut

AstronautGrain

GooseFox

AstronautFox

GooseGrain

Astronaut

GooseGrainFox

AstronautGooseGrain

Fox

Page 9: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Brian Williams, Spring 04 9

AstronautGooseGrainFox

GrainFox

AstronautGoose

AstronautGrainFox

Goose

Goose

AstronautFox

Grain

AstronautGoose

GrainFox

AstronautGooseGrainFox

Grain

AstronautGooseFox

Fox

AstronautGooseGrain

GooseFox

AstronautGrain

GooseGrain

AstronautFox

AstronautGooseFox

Grain

AstronautGrain

GooseFox

AstronautFox

GooseGrain

GooseGrainFox

Astronaut

Astronaut

GooseGrainFox

AstronautGooseGrain

Fox

Page 10: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Example: 8-Puzzle

5 4

6 1

7 3

8

2

1 2

8

3

7 6

4

5

Start Goal

• States: • Operators: • Goal Test:

integer location for each tile AND …move empty square up, down, left, rightgoal state as given

Brian Williams, Spring 04 10

Page 11: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Example: Planning Discrete Actions

courtesy of NASA

• Swaggert & Lovell work on Apollo 13 emergency rig lithium hydroxide unit. – Assembly

• Mattingly works in ground simulator to identify new sequence handling severe power limitations.– Planning & Resource Allocation

• Mattingly identifies novel reconfiguration, exploiting LEM batteries for power.– Reconfiguration and Repair

Brian Williams, Spring 04 11

Page 12: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Planning as State Space Search:STRIPS Operator Representation

Effects specify how to change the set of assertions.

Initial state:(and (hose a)

(clamp b)(hydroxide-unit c) (on-table a) (on-table b) (clear a) (clear b) (clear c) (arm-empty))

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

(attached b a)))

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

(empty arm))

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

(holding arm hose)))

pickup hose

Note: strips doesn’t

allow derived effects;

you must be complete!}

Available actions Strips operators

Brian Williams, Spring 04 12

Page 13: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

STRIPS Action Assumptions

• Atomic time.

• Agent is omniscient (no sensing necessary).

• Agent is sole cause of change.

• Actions have deterministic effects.

• No indirect effects.

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

(empty arm))

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

(holding arm hose)))

pickup hose

Brian Williams, Spring 04 13

Page 14: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

STRIPS Action Schemata• Instead of defining:

pickup-hose and pickup-clamp and …

• Define a schema (with variables ?v):

Brian Williams, Spring 04 14

(:operator pick-up:parameters ((hose ?ob1)):precondition (and (clear ?ob1)

(on-table ?ob1) (empty arm))

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

Page 15: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Outline

• Problem Formulation– Problem solving as state space search

• Mathematical Model– Graphs and search trees

• Reasoning Algorithms– Depth and breadth-first search

Brian Williams, Spring 04 15

Page 16: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Problem Formulation: A GraphState

Node(vertex)

Operator

Link(edge)

de

Brian Williams, Spring 04 16

DirectedGraph(one-way streets)

neighbors(adjacent)

Incident to edge

Start Vertex of Edge

End Vertex of Edge

UndirectedGraph(two-way streets)

Page 17: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Problem Formulation: A Graph

In Degree (2) Degree (1)

bOut Degree (1)

b

UndirectedGraph(two-way streets)

DirectedGraph(one-way streets)

Brian Williams, Spring 04 17

Page 18: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Problem Formulation: A GraphStrongly connected graph

Directed path between all vertices.Connected graph

Path between all vertices.

Complete graphAll vertices are adjacent.

ac

ed

b

ac

de

b

UndirectedGraph(two-way streets)

Sub graphSubset of verticesedges between vertices in Subset

CliqueA complete subgraphAll vertices are adjacent.

DirectedGraph(one-way streets)

Brian Williams, Spring 04 18

Page 19: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Examples of Graphs

San Fran

Boston

LA Dallas

Wash DC

Roadmap

Brian Williams, Spring 04 19

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

CPut A on C

Planning Actions

(graph of possible states of the world)

Page 20: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

A Graph

SFO

Bos

LA Dallas

Wash DC

Airline Routes

Brian Williams, Spring 04 20

A Graph G is represented as a pair <V,E>, where:

• V is a set of vertices {v1 …}

• E is a set of (directed) edges {e1, …}

An edge is a pair <v1, v2> of vertices, where

• v2 is the head of the edge,

•and v1 is the tail of the edge

< {Bos, SFO, LA, Dallas, Wash DC}

{<SFO, Bos>,

<SFO, LA>

<LA, Dallas>

<Dallas, Wash DC>

. . .} >

Page 21: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

A Solution is a State Sequence:Problem Solving Searches Paths

S

D

A

C

C

S

B

GA

D start

end

A path is a sequence of edges (or vertices)<S, A, D, C>

Brian Williams, Spring 04 21

Simple path has no repeated vertices.

For a cycle, start = end.

Page 22: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

A Solution is a State Sequence:Problem Solving Searches Paths

S

D

A

C G

C

S

B

GA

D

Represent searched paths using a tree.

Brian Williams, Spring 04 22

Page 23: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

A Solution is a State Sequence:Problem Solving Searches Paths

S

D

BA

C G

C G

D

C G

C

S

B

GA

D

Represent searched paths using a tree.

Brian Williams, Spring 04 23

Page 24: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Search Trees

Root

Branch(Edge)

Node(vertex)

Brian Williams, Spring 04 24

Page 25: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Search Trees

Parent(Ancestor)

Child(Descendant)

Siblings

Brian Williams, Spring 04 25

Page 26: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Search Trees

Ancestors

Descendants

Brian Williams, Spring 04 26

Page 27: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Outline

• Problem Formulation– Problem solving as state space search

• Mathematical Model– Graphs and search trees

• Reasoning Algorithms– Depth and breadth-first search

Brian Williams, Spring 04 27

Page 28: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Classes of SearchBlind Depth-First Systematic exploration of whole tree

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

Iterative-Deepening

Heuristic Hill-Climbing Uses heuristic measure of goodness

(informed) Best-First of a node,e.g. estimated distance to.

Beam goal.

Optimal Branch&Bound Uses path “length” measure. Finds

(informed) A* “shortest” path. A* also uses heuristic

Brian Williams, Spring 04 28

Page 29: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Classes of SearchBlind Depth-First Systematic exploration of whole tree

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

Iterative-Deepening

Brian Williams, Spring 04 29

Page 30: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Depth First Search (DFS)Idea: After visiting node•Visit children, then siblings•Visit siblings left to right

S

D

BA

C G

C G

D

C G

1

2

3

5

6

7

8 11

Brian Williams, Spring 04 30

4 9 10

S

A

D

C G

C

B

D

C G

G

Page 31: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Breadth First Search (BFS)Idea: After visiting node• Visit siblings, then children• Visit relatives left to right (top to bottom)

S

D

BA

C G

C G

D

C G

1

2

4

9

5

3

6

10 11

7

S

A

D

C G

C

B

D

C G

G

8

Brian Williams, Spring 04 31

Page 32: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Elements of Algorithm DesignDescription: (today)

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

Analysis: (next Wednesday)• 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?

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

• Space complexity: – how much memory does it need to perform search?

Brian Williams, Spring 04 32

Page 33: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Outline

• Problem Formulation: State space search• Model: Graphs and search trees• Reasoning Algorithms: DFS and BFS

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

Brian Williams, Spring 04 33

Page 34: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Simple Search AlgorithmGoing Meta: Search as State Space SearchHow do we maintain the search state?

• A set of partial paths explored thus far.

• An ordering on which partial path to expand next • called a queue Q.

How do we perform search? • Repeatedly:

• Select next partial path from Q. • Expand it.• Add expansions to Q.

• Terminate when goal found.

State Space

S

D

BA

C G

C G

D

C G

Operator

Goal-TestBrian Williams, Spring 04 34

Page 35: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Simple Search Algorithm

Brian Williams, Spring 04 35

• S denotes the start node• G denotes the goal node.• A partial path is a path from S to some node D,

• e.g., (D A S)

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

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

S

D

BA

C G

C G

D

C G

Page 36: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Brian Williams, Spring 04 36

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

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

create all one-step extensions of N to each child.c) Add all extended paths to Qd) Go to step 2.

Page 37: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Outline

• Problem Formulation: State space search• Model: Graphs and search trees• Reasoning Algorithms: DFS and BFS

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

Brian Williams, Spring 04 37

Page 38: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Depth First Search (DFS)Idea: • Visit children, then siblings• Visit siblings left to right, (top to bottom).

S

D

BA

C G

C G

D

C G

1

2

3

5

6

7

8 11

Brian Williams, Spring 04 38

4 9 10

S

A

D

C G

C

B

D

C G

G

Assuming that we pick the first element of Q,Then where do we add path extensions to the Q?

Page 39: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Brian Williams, Spring 04 39

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

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

create all the one-step extensions of N to each child.c) Add all extended paths to Qd) Go to step 2.

Page 40: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Brian Williams, Spring 04 40

C

S

B

GA

D

Q

1 (S)2345

1

Page 41: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Brian Williams, Spring 04 41

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

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

create all the one-step extensions of N to each child.c) Add all extended paths to Qd) Go to step 2.

Page 42: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Q

1 (S)2345

C

S

B

GA

D1

Brian Williams, Spring 04 42

Page 43: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Q

1 (S)2345

(A S)

C

S

B

GA

D1

Added paths in blue

Brian Williams, Spring 04 43

Page 44: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Q

1 (S)2345

(A S) (B S)

C

S

B

GA

D1

Added paths in blue

Brian Williams, Spring 04 44

Page 45: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Brian Williams, Spring 04 45

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

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

create all the one-step extensions of N to each child.c) Add all extended paths to Qd) Go to step 2.

Page 46: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Q

1 (S)2345

(A S) (B S)

C

S

B

GA

D1

2

Added paths in blue

Brian Williams, Spring 04 46

Page 47: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Q

1 (S)2345

(A S) (B S)(C A S) (D A S) (B S)

C

S

B

GA

D

Brian Williams, Spring 04 47

1

2

Added paths in blue

Page 48: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Q

1 (S)2345

(A S) (B S)(C A S) (D A S) (B S)

C

S

B

GA

D1

2

Added paths in blue

Brian Williams, Spring 04 48

Page 49: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Brian Williams, Spring 04 49

C

S

B

GA

D1

2

3Q

1 (S)2345

(A S) (B S)(C A S) (D A S) (B S)

Added paths in blue

Page 50: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Brian Williams, Spring 04 50

C

S

B

GA

D1

2

3Q

1 (S)2345

(A S) (B S)(C A S) (D A S) (B S)(D A S) (B S)

Added paths in blue

Page 51: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

C

S

B

GA

D

Q

1 (S)2345

(A S) (B S)(C A S) (D A S) (B S)(D A S) (B S)

1

2

3

4

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

Added paths in blue

Brian Williams, Spring 04 51

Page 52: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

C

S

B

GA

D

Q

1 (S)234

5

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

1

2

3

4

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

Brian Williams, Spring 04 52

Page 53: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Brian Williams, Spring 04 53

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

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

create all the one-step extensions of N to each child.c) Add all extended paths to Qd) Go to step 2.

Page 54: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

C

S

B

GA

D

Q

1 (S)234

5

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

1

2

3

4

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

Brian Williams, Spring 04 54

Page 55: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

C

S

B

GA

D

Q

6 (G D A S)(B S)

1 (S)234

5

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

1

2

3

4

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

Brian Williams, Spring 04 55

Page 56: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

C

S

B

GA

D

Q

6 (G D A S)(B S)

1 (S)234

5

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

1

2

3

4

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

Brian Williams, Spring 04 56

Page 57: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Outline

• Problem Formulation: State space search• Model: Graphs and search trees• Reasoning Algorithms: DFS and BFS

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

Brian Williams, Spring 04 57

Page 58: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

C

S

B

GA

D

Brian Williams, Spring 04 58

Page 59: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

C

S

B

GA

D

Q

6 (G D A S)(B S)

1 (S)234

5

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

1

2

3

4

Depth-FirstEffort can be wasted in more mild cases

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

How much wasted effort can be incurred in the worst case?Brian Williams, Spring 04 59

Page 60: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

How Do We Avoid Repeat Visits?

Brian Williams, Spring 04 60

Idea:

• Keep track of nodes already visited.

• Do not place visited nodes on Q.

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 it always improve efficiency?

• Visits only a subset of the original paths, suc thateach node appears at most once at the head of a path in Q.

Page 61: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

How Do We ModifySimple 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 Q3. If head(N) = G, return N (goal reached!)4. Else

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

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

Brian Williams, Spring 04 61

Page 62: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Brian Williams, Spring 04 62

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 Q3. If head(N) = G, return N (goal reached!)4. Else

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

create all the one-step extensions of N to each child.c) Add to Q all the extended paths; d) Add children of head(N) to Visitede) Go to step 2.

Page 63: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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 searches we look at later. We have chosen to leave the test in step 3 to maintain uniformity with these future searches.

Brian Williams, Spring 04 63

Page 64: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Outline

• Problem Formulation: State space search• Model: Graphs and search trees• Reasoning Algorithms: DFS and BFS

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

Brian Williams, Spring 04 64

Page 65: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Breadth First Search (BFS)Idea: • Visit siblings before their children• Visit relatives left to right

S

D

BA

C G

C G

D

C G

1

2

4

9

5

3

6

10 11

7

S

A

D

C G

C

B

D

C G

G

8

Assuming that we pick the first element of Q,Then where do we add path extensions to the Q?

Brian Williams, Spring 04 65

Page 66: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Breadth-FirstPick first element of Q; Add path extensions to end of Q

Q Visited

6

1 (S) S2345

C

S

B

GA

D1

Brian Williams, Spring 04 66

Page 67: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Breadth-FirstPick first element of Q; Add path extensions to end of Q

Q Visited

6

1 (S) S2345

C

S

B

GA

D1

Brian Williams, Spring 04 67

Page 68: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Breadth-FirstPick first element of Q; Add path extensions to end of Q

Q Visited

6

1 (S) S2345

(A S) (B S) A,B,S

C

S

B

GA

D1

Brian Williams, Spring 04 68

Page 69: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Breadth-FirstPick first element of Q; Add path extensions to end of Q

Q Visited

6

1 (S) S2345

(A S) (B S) A,B,S

C

S

B

GA

D1

2

Brian Williams, Spring 04 69

Page 70: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Breadth-FirstPick first element of Q; Add path extensions to end of Q

Q Visited

6

1 (S) S2345

(A S) (B S) A,B,S(B S) (C A S) (D A S) C,D,B,A,S

C

S

B

GA

D1

2

Brian Williams, Spring 04 70

Page 71: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Breadth-FirstPick first element of Q; Add path extensions to end of Q

Q Visited

6

1 (S) S2345

(A S) (B S) A,B,S(B S) (C A S) (D A S) C,D,B,A,S

C

S

B

GA

D1

2

3

Brian Williams, Spring 04 71

Page 72: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Breadth-FirstPick first element of Q; Add path extensions to end of Q

Q Visited

6

1 (S) S2345

(A S) (B S) A,B,S(B S) (C A S) (D A S) C,D,B,A,S(C A S) (D A S) (G B S)* G,C,D,B,A,S

C

S

B

GA

D1

2

3

* We could stop here, when the first path to the goal is generated.Brian Williams, Spring 04 72

Page 73: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Breadth-FirstPick first element of Q; Add path extensions to end of Q

Q Visited

6

1 (S) S2345

(A S) (B S) A,B,S(B S) (C A S) (D A S) C,D,B,A,S(C A S) (D A S) (G B S)* G,C,D,B,A,S

C

S

B

GA

D1

2

3

4

* We could stop here, when the first path to the goal is generated.Brian Williams, Spring 04 73

Page 74: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Breadth-FirstPick first element of Q; Add path extensions to end of Q

C

S

B

GA

D

Q Visited

6

1 (S) S2345

(A S) (B S) A,B,S(B S) (C A S) (D A S) C,D,B,A,S(C A S) (D A S) (G B S)* G,C,D,B,A,S(D A S) (G B S) G,C,D,B,A,S

1

2

3

4

5

Brian Williams, Spring 04 74

Page 75: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Breadth-FirstPick first element of Q; Add path extensions to end of Q

C

S

B

GA

D

Q Visited

6 (G B S) G,C,D,B,A,S

1 (S) S2345

(A S) (B S) A,B,S(B S) (C A S) (D A S) C,D,B,A,S(C A S) (D A S) (G B S)* G,C,D,B,A,S(D A S) (G B S) G,C,D,B,A,S

1

2

3

4

5

6

Brian Williams, Spring 04 75

Page 76: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Breadth-FirstPick first element of Q; Add path extensions to end of Q

C

S

B

GA

D

Q Visited

6 (G B S) G,C,D,B,A,S

1 (S) S2345

(A S) (B S) A,B,S(B S) (C A S) (D A S) C,D,B,A,S(C A S) (D A S) (G B S)* G,C,D,B,A,S(D A S) (G B S) G,C,D,B,A,S

1

2

3

4

5

6

Brian Williams, Spring 04 76

Page 77: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Depth-first with visited listPick first element of Q; Add path extensions to front of Q

C

S

B

GA

D

Q Visited

1 (S) S2345

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

1

2

3

4

5

Brian Williams, Spring 04 77

Page 78: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Depth First Search (DFS)S

D

BA

C G

C G

D

C G

Depth-first:

Add path extensions to front of Q

Pick first element of Q

S

D

BA

C G

C G

D

C G

Breadth First Search (BFS)Breadth-first:

Add path extensions to back of Q

Pick first element of Q

For each search type, where do we place the children on the queue?Brian Williams, Spring 04 78

Page 79: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

What You Should Know• Most problem solving tasks may be

formulated as state space search.• Mathematical representations for search are

graphs and search trees.• Depth-first and breadth-first search may be

framed, among others, as instances of a generic search strategy.

• Cycle detection is required to achieve efficiency and completeness.

Brian Williams, Spring 04 79

Page 80: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

Appendix

Brian Williams, Spring 04 80

Page 81: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Q

67

1 (S)2345

C

S

B

GA

D1

Brian Williams, Spring 04 81

Page 82: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Q

67

1 (S)2345

(A S) (B S)

C

S

B

GA

D1

2

Added paths in blue

Brian Williams, Spring 04 82

Page 83: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Q

67

1 (S)2345

(A S) (B S)(B S) (C A S) (D A S)

C

S

B

GA

D1

2

3

Added paths in blue

Brian Williams, Spring 04 83

Page 84: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Q

67

1 (S)2345

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

C

S

B

GA

D1

2

3

4

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

Brian Williams, Spring 04 84

Page 85: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Q

67

1 (S)2345

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

C

S

B

GA

D1

2

3

4

5

Brian Williams, Spring 04 85

Page 86: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Q

6 (D B S) (G B S) (C D A S) (G D A S)7

1 (S)2345

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

C

S

B

GA

D1

2

3

4

56

Brian Williams, Spring 04 86

Page 87: Problem Solving as State Space Search · PDF fileEarly AI: What are the ... Problem Solving as State Space Search • Formulate Goal –State • Astronaut, Fox, Goose & Grain across

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

Q

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)

1 (S)2345

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

C

S

B

GA

D1

2

3

4

56

7

Brian Williams, Spring 04 87