41
Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Embed Size (px)

Citation preview

Page 1: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Computer Science CPSC 322

Lecture 9

(Ch 3.7.1 - 3.7.4, 3.7.6)

Slide 1

Page 2: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Announcements

• Midterm: Friday Feb. 27

- See update schedule on website

Slide 2

Page 3: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Slide 3

Lecture Overview

• Recap of Lecture 8

• Cycle checking, multiple path pruning

• Branch-and-Bound

• Other A* refinements

Page 4: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Apply basic properties of search algorithms:

- completeness, optimality, time and space complexity

Complete Optimal Time Space

DFS N (Y if no cycles)

N O(bm) O(mb)

BFS Y Y O(bm) O(bm)

IDS Y Y O(bm) O(mb)

LCFS(when arc costs

available)

Y Costs > 0

Y Costs >=0

O(bm) O(bm)

Best First(when h available)

N N O(bm) O(bm)

A*(when arc costs > 0 and h admissible)

Y Y O(bm) O(bm)

Recap

4

Page 5: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Recap

• Proved A* optimality under admissibility conditions

• Discussed A* as optimally efficient • No other optimal algorithm is guaranteed to expand fewer

nodes than A*

Slide 5

Page 6: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Slide 6

Lecture Overview

• Recap of Lecture 8

• Cycle checking, multiple path pruning

• Branch-and-Bound

• Other A* refinements

Page 7: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Clarification: state space graph vs search tree

k c

b z

h

ad

fState space graph represents the states in a search problem, and how they are connected by the available operators

Search Tree: Shows how the search space is traversed by a given search algorithm: explicitly “unfolds” the paths that are expanded.

k

c b

zh

a df

If there are no cycles or multiple paths, the two look the same 7

Page 8: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Clarification: state space graph vs search tree

k c

b z

h

ad

f

State space graph

k

c b

zk

a dc

If there are cycles or multiple paths, the two look very different

Search Tree: (first three levels)

h

b

k

c bf

8

Page 9: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Size of state space vs. search treeIf there are cycles or multiple paths, the two look very different

A

B

C

D

A

B

C

B

C CC

D D

• E.g. state space with d states and 2 actions from each state to next• With d + 1 states, search tree has depth d • 2d possible paths through the search space => exponentially larger search

tree!

• With cycles or multiple parents, the search tree can be exponential in the state space

9

Page 10: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Cycle Checking and Multiple Path Pruning

• Cycle checking: good when we want to avoid infinite loops, but also want to find more than one solution, if they exist

• Multiple path pruning: good when we only care about finding one solution• Subsumes cycle checking

10

Page 11: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

• What is the computational cost of cycle checking?

Cycle Checking

• You can prune a path that ends in a node already on the path.

• This pruning cannot remove an optimal solution => cycle check

Good when we want to avoid infinite loops, but also want to

find more than one solution, if they exist

11

Page 12: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Cycle Checking• See how DFS and BFS behave on Cyclic Graph Example in

Aispace, when

Search Options-> Pruning -> Loop detection

is selected• Set N1 to be a normal node so that there is only one start

node. • Check, for each algorithm, what happens during the first

expansion from node 3 to node 2

12

Page 13: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Depth First Search

Since DFS looks at one path at a time,when a node is encountered for the second time (e.g. Node 2 while expanding N0, N2, N5, N3)it is guaranteed to be part of a cycle.

13

Page 14: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Breadth First Search

Since BFS keeps multiple subpaths going, when a node is encountered for the second time, it could be as part of expanding a different path (e.g. Node 2 while expanding N0-.N3). Not necessarily a cycle.

14

Page 15: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Breadth First Search

The cycle for BFS happens when N2 is encountered for the second time while expanding the path N0-.N2-N5-N3 .

15

Page 16: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Computational Cost of Cycle Checking?

D. None of the above

B. Linear time in the path length: before adding a new node to the currently selected path, check that the node is not already part of the path

A. As low as Constant time: (e.g., set a bit to 1 when a node is selected for expansion, and never expand a node with a bit set to 1)

C. It depends on the algorithm

16

Page 17: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Computational Cost of Cycle Checking?

D. None of the above

B. Linear time in the path length: before adding a new node to the currently selected path, check that the node is not already part of the path

A. As low as constant time: (e.g., set a bit to 1 when a node is selected for expansion, and never expand a node with a bit set to 1)

C. It depends on the algorithm

17

Page 18: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

• What is the computational cost of cycle checking?

Cycle Checking

• You can prune a path that ends in a node already on the path.

• This pruning cannot remove an optimal solution => cycle check

• Using depth-first methods, with the graph explicitly stored, this can be done in constant time- Only one path being explored at a time

• Other methods: cost is linear in path length- check each node in the path

18

Page 19: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

• If we only want one path to the solution• Can prune path to a node n that has already been reached via a

previous path- Subsumes cycle check

Multiple Path Pruning

n

19

Page 20: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

• Can see how it works by– Running BFS on the Cyclic Graph Example in CISPACE– See how it handles the multiple paths from N0 to N2– You can erase start node N1 to simplify things

Multiple Path Pruning

n

20

Page 21: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Multiple-Path Pruning & Optimal Solutions

• Problem: what if a subsequent path p2 to n is better (shorter or less costly) than the first path p1 to n, and we want an optimal solution ?

• Can remove all paths from the frontier that use the longer path: these can’t be optimal.

• Can change the initial segment of the paths on the frontier to use the shorter

or• Can prove that this can’t happen for an algorithm?

p1 p1 p1

p2 p2

21

Page 22: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

“ Whenever search algorithm X expands the first path p ending in node n, this is the lowest-cost path from the start node to n (if all costs ≥ 0)”

This is true for

D. None of the above

A. Lowest Cost Search First

C. Both of the above

B. A*

Can prove that search algorithm X always find the optimal path to any node n in the search space first?

22

Page 23: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

“ Whenever search algorithm X expands the first path p ending in node n, this is the lowest-cost path from the start node to n (if all costs ≥ 0)”

This is true for

D. None of the above

A. Lowest Cost Search First

C. Both of the above

B. A*

Can prove that search algorithm X always find the optimal path to any node n in the search space first?

23

Page 24: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

• Which of the following algorithms always find the shortest path to nodes on the frontier first?• Only Least Cost First Search

Counter-example for A*: it expands the upper path first

Special conditions on the heuristic can recover the guarantee of LCFS for A*: the monotone restriction (See P&M text, Section 3.7.2)

2 2

1 1 1 20Start 10 10

0

1 Goal

24

Page 25: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

25

Page 26: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Slide 26

Lecture Overview

• Recap of Lecture 8

• Cycle checking, multiple path pruning

• Branch-and-Bound

• Other A* refinements

Page 27: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Branch-and-Bound Search

• What does allow A* to do better than the other search algorithms we have seen?• Arc cost and h(h) combined in f(n)

• What is the biggest problem with A*?• space

• Possible Solution: • Combine DFS with f

27

Page 28: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Branch-and-Bound SearchOne way to combine DFS with heuristic guidance

• Follows exactly the same search path as depth-first search• But to ensure optimality, it does not stop at the first solution found

• It continues, after recording upper bound on solution cost• upper bound: UB = cost of the best solution found so far• Initialized to or any overestimate of optimal solution cost

• When a path p is selected for expansion:• Compute lower bound LB(p) = f(p) = cost(p) + h(p)

- If LB(p) UB, remove p from frontier without expanding it (pruning)

- Else expand p, adding all of its neighbors to the frontier

28

Page 29: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Example• Arc cost = 1• h(n) = 0 for every n

• UB = ∞

Solution!UB = 5

Before expanding a path p,check its f value f(p):Expand only if f(p) < UB

29

Simplifications for clarity

Page 30: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Example• Arc cost = 1• h(n) = 0 for every n

• UB = 5

Cost = 5Prune! 30

Page 31: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Example• Arc cost = 1• h(n) = 0 for every n

• UB = 5

Cost = 5Prune!

Cost = 5Prune!

Solution!UB =?

31

Page 32: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Example• Arc cost = 1• h(n) = 0 for every n

• UB = 3

Cost = 3Prune!

Cost = 3Prune!Cost = 3Prune!

32

Page 33: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Branch-and-Bound Analysis

• Complete ?

• Optimal:

• Space complexity:

• Time complexity: …….

33

Page 34: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

• Is Branch-and-Bound optimal?

D. Only if there are no cycles

A. YES, with no further conditions

C. Only if h(n) is admissible

B. NO

Branch-and-Bound Analysis

34

Page 35: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

• Is Branch-and-Bound optimal?

D. Only if there are no cycles

A. YES, with no further conditions

C. Only if h(n) is admissible. Otherwise, when checking LB(p) UB, if the answer is yes but h(p) is an overestimate of the actual cost of p, we remove a possibly optomal solution

B. NO

Branch-and-Bound Analysis

35

Page 36: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Branch-and-Bound Analysis

• Complete ? (..even when there are cycles)

B. NO

A. YES

C. It depends

36

Page 37: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Branch-and-Bound Analysis

• Complete ? (..even when there are cycles)

IT DEPENDS on whether we can initialize UB to a finite value, i.e. we have a reliable overestimate of the solution

cost. If we don`t, we need to use ∞, and BBA can be caught in a cycle

37

Page 38: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Branch-and-Bound Analysis

• Complete ? • Same as DFS: can’t handle cycles/infinite graphs. • But complete if initialized with some finite U

• Optimal: YES, if h(n) is admissible

• Space complexity:• Branch & Bound has the same space complexity as DFS• this is a big improvement over A*!

• Time complexity O(bm)

38

Page 39: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Learning Goals for today’s class

• Pruning cycles and multiple paths

• Define/read/write/trace/debug different search algorithms

- In more detail today: Branch-and-Bound

Slide 39

Page 40: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Complete Optimal Time Space

DFS N N O(bm) O(mb)

BFS Y Y O(bm) O(bm)

IDS Y Y O(bm) O(mb)

LCFS(when arc costs available)

Y Costs > 0

Y Costs >=0

O(bm) O(bm)

Best First(when h available)

N N O(bm) O(bm)

A*(when arc costs > 0 and h

admissible )Y Y

O(bm)Optimall

y Efficient

O(bm)

Branch-and-Bound N(Y with

finite initial bound)

YIf h

admissible

O(bm) O(bm)

Search Methods so Far

Slide 40

Page 41: Computer Science CPSC 322 Lecture 9 (Ch 3.7.1 - 3.7.4, 3.7.6) Slide 1

Next class: Fri

Read• 3.6 (Dynamic programming)• 4.1 and 4.2 (Intro to CSP)

• Keep working on assignment-1 !

• Do Practice Exercise 3D

Slide 41