Top Banner
Solving Problems by Searching CS 486/686: Introduction to Artificial Intelligence 1
40

Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Feb 12, 2018

Download

Documents

buinguyet
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Solving Problems by

Searching

CS 486/686: Introduction to Artificial Intelligence

1

Page 2: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Announcements

• Enrollment: Wait list, Grads

• Projects

• Undergrads: Groups of 3

• Grads: Solo

• Stuck for ideas? Talk to us!

• My Office: DC 2306B (Accessed through AI Lab)

• Slide versions2

Page 3: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Previously on CS 486

• Fully Observable vs Partially

Observable

• Deterministic vs Stochastic

• Episodic vs Dynamic

• Discrete vs Continuous

• Single agent vs Multi agent

3

Page 4: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Introduction

• Search was one of the first topics

studied in AI

- Newell and Simon (1961) General Problem

Solver

• Central component to many AI systems

- Automated reasoning, theorem proving,

robot navigation, scheduling, game

playing,...

4

Page 5: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Search Problems

• Search problem consists of

• a state space

• a successor function (actions, cost)

• a start state

• Goal test

(N, 1.0)

(E, 1.0)

5

Page 6: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Search Problems

• Path: a sequence of states and operators

• May be associated with a cost c

• Solution: a sequence of actions from the start

state to a goal state

• Optimal solution: a path with minimum cost

6

Page 7: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Example: Traveling in Romania

Start

End

•States:

•Initial State:

•Successor Function:

•Goal test:

•Solution:

7

Page 8: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Examples of Search Problems

•States:

•Initial State:

•Successor Function:

•Goal test:

•Solution:

•States:

•Initial State:

•Successor Function:

•Goal test:

•Solution:8

Page 9: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Examples of Search Problems

9

Page 10: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Our Definition Excludes...

Chance

Continuous states

All of the above

Partial

Observability

Adversaries

10

Page 11: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

What is is a state space?

The world state includes every last detail of the environment

A search state keeps only the details needed for planning (abstraction)

• Problem: Pathing

• States: (x,y) location

• Actions: NSEW

• Successor: update location

only

• Goal test: is (x,y)=END

• Problem: Eat-All-Dots

• States: {(x,y), dot booleans}

• Actions: NSEW

• Successor: update location and

possibly a dot boolean

• Goal test: dots all false

Adapted from UC Berkeley’s CS188 Course

11

Page 12: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Representing Search

• State space graph

- Vertices correspond to states

(one vertex for each state)

- Edges correspond to

successors

- Goal test is a set of goal nodes

• We search for a solution by

building a search tree and

traversing it to find a goal

state

S

G

d

b

pq

c

e

h

a

f

r

12

Page 13: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Search Tree

• A search tree:

• Start state is the root of

the tree

• Children are successors

• A plan is a path in the tree.

A solution is a path from

the root to a goal node.

• For most problems we do

not actually generate the

entire tree

S

B

A

G

S

A B

GB G

G13

Page 14: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Quiz

• Given this state graph, how large is the

search tree?

S

B

A

G

14

Page 15: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Expanding Nodes

• Expanding a node

- Applying all legal operators to the state

contained in the node

- Generating nodes for all corresponding

successor states

15

Page 16: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Example: Traveling in Romania

Start

End

16

Page 17: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Generic Search Algorithm

• Initialize with initial state of the problem

• Repeat

- If no candidate nodes can be expanded return failure

- Choose leaf node for expansion, according to search

strategy

- If node contains goal state, return solution

- Otherwise, expand the node. Add resulting nodes to the

tree

17

Page 18: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Generic Search Algorithm

Page 19: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Implementation Details

19

• Need to keep track of nodes to be expanded (frontier)

• Implement using a queue:

0. Insert node for initial state

Page 20: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Implementation Details

20

• Need to keep track of nodes to be expanded (frontier)

• Implement using a queue:

0. Insert node for initial state

• Repeat

1. If queue is empty, return failure

2. Dequeue a node

Page 21: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Implementation Details

21

• Need to keep track of nodes to be expanded (frontier)

• Implement using a queue:

0. Insert node for initial state

• Repeat

1. If queue is empty, return failure

2. Dequeue a node

3. If node contains goal state, return solution

4. Expand node

Page 22: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Implementation Details

22

• Need to keep track of nodes to be expanded (frontier)

• Implement using a queue:

0. Insert node for initial state

• Repeat

1. If queue is empty, return failure

2. Dequeue a node

3. If node contains goal state, return solution

4. Expand node

• Search algorithms differ in their queuing function!

Page 23: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Search Strategies

S

G

d

b

p

q

c

e

h

a

f

r

Adapted from UC Berkeley’s CS188 Course

23

Page 24: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Search Strategies

S

a

b

dp

a

c

e

p

h

f

r

q

q c G

a

qe

p

h

f

r

q

q c G

a 24

Page 25: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Depth-First Search

S

G

d

b

p q

c

e

h

a

f

r

Strategy: Expand deepest node first

Implementation: LIFO stack

25

S

a

b

d p

a

c

e

p

h

f

r

q

q c G

a

qe

p

h

f

r

q

q c G

a

Page 26: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Depth-First Search

S

G

d

b

p q

c

e

h

a

f

r

Strategy: Expand deepest node first

Implementation: LIFO stack

26

S

a

b

d p

a

c

e

p

h

f

r

q

q c G

a

qe

p

h

f

r

q

q c G

a

Page 27: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Key Properties

•Completeness: Is the alg. guaranteed to find a

solution if the solution exists?

•Optimality: Does the alg. find the optimal solution?

•Time complexity

•Space complexity (size of the fringe) …

b1 node

b nodes

b2 nodes

bm nodes

m tiers

b: branching factor

m: maximum depth

d: depth of shallowest goal node

Number of nodes in tree? 1+b+b2+…+bm=O(bm)27

Page 28: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

DFS Properties

•Complete?

•Optimal?

•Time complexity

•Space complexity

b

1 node

b nodes

b2 nodes

bm nodes

m tiers

28

Page 29: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Breadth-First Search

S

G

d

b

p q

c

e

h

a

f

r

Strategy: Expand shallowest node first

Implementation: FIFO queue

29

S

a

b

dp

a

c

e

p

h

f

r

q

q c G

a

qe

p

h

f

r

q

q c G

a

Page 30: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Breadth-First Search

S

G

d

b

p q

c

e

h

a

f

r

Strategy: Expand shallowest node first

Implementation: FIFO queue

30

S

a

b

dp

a

c

e

p

h

f

r

q

q c G

a

qe

p

h

f

r

q

q c G

a

Page 31: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

BFS Properties

•Complete?

•Optimal?

•Time complexity

•Space complexity

b

1 node

b nodes

b2 nodes

bm nodes

m tiers

31

depth d

Page 32: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Quiz: DFS vs BFS

…b

…b

32

Page 33: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Iterative Deepening Search

• Can we combine search methods to take advantage

of DFS space complexity and BFS

completeness/shallow solution advantage?

33

Page 34: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

IDS Properties•Complete?

•Optimal?

•Time complexity

•Space complexity

…b

1 node

b nodes

b2 nodes

bm nodes

m tiers

Wasteful? Most nodes found in

lowest level of search so not too

bad

34

Page 35: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Cost-Sensitive Search

START

GOAL

d

b

pq

c

e

h

a

f

r

2

9 2

81

8

2

3

2

4

4

15

1

32

2

Recall that BFS was only optimal under some conditions

(i.e. we only cared about number of actions taken). What

can we do if actions have different costs? 35

Page 36: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Uniform Cost Search

Strategy: Expand node with lowest path cost first

Implementation: Priority queue

START

GOAL

d

b

pq

c

e

h

a

f

r

2

9 2

81

8

2

3

2

4

4

15

1

32

2

36

Page 37: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

UCS Properties

•Complete?

•Optimal?

•Time complexity

•Space complexity

b

C*/𝜀 tiers

37

Page 38: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Summary

38

Criteria DFS BFS IDS UCS

Optimal No Yes Yes Yes

Complete No Yes Yes Yes

Time O(bm) O(bd) O(bd) O(b+1+floor(C*/ε))

Space O(bm) O(bd) O(bd) O(b+1+floor(C*/ε))

• Assumes no knowledge about the problem

- This makes them general but expensive

Figure 3.21 (Russell & Norvig)

Page 39: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Summary

• These algorithms are basically the

same except for the order in which they

expand nodes

• Basically all priority queues with different

ways to determining priorities

• How successful the search is depends

heavily on your model!

39

Page 40: Solving Problems by Searching - cs.uwaterloo.caakhtsang/cs486/02Searching.pdf · Otherwise, expand the node. Add resulting nodes to the tree 17. Generic Search Algorithm. Implementation

Questions?

• Next class: Informed search

“Can we make use of additional information?”

Why take a bus to London if we are going to Ottawa?

40