Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

37
Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1

Transcript of Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

Page 1: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

Computer Science CPSC 322

Lecture

Heuristic Search

(Ch: 3.6, 3.6.1)

Slide 1

Page 2: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

Announcements

• Assignment 1 posted in Connect• Due Monday Feb 2, 2pm

• Assignment 0 will be marked and returned by Wed. class

2

Page 3: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

Lecture Overview

• Recap of Lecture 6

• Best First Search

• A*

• Admissible heuristic

Slide 3

Page 4: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

DFS vs. BFS

How can we achieve an acceptable (linear) space complexity

while maintaining completeness and optimality?

Key Idea: re-compute elements of the frontier rather

than saving them.

Complete Optimal Time Space

DFS NO NO O(bm) O(bm)

BFS YES YES O(bm) O(bm)

????? YES YES O(bm) O(bm)

4

Page 5: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

depth = 1

depth = 2

depth = 3

. . .

Iterative Deepening DFS (IDS) in a Nutshell

• Use DFS to look for solutions at depth 1, then 2, then 3, etc– For depth D, ignore any paths with longer length– Depth-bounded depth-first search

If no goal re-start from scratch and get to depth 2

If no goal re-start from scratch and get to depth 3

If no goal re-start from scratch and get to depth 4

5

Page 6: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

DFS vs. BFS

But what if we have costs associated with search actions (arcs in the search space?

Complete Optimal Time Space

DFS NO NO O(bm) O(bm)

BFS YES YES O(bm) O(bm)

IDS YES YES O(bm) O(bm)

6

Page 7: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

Search with Costs

In this setting we usually want to find the solution that minimizes cost

),cost(,,cost1

10

k

iiik nnnn

Def.: The cost of a path is the sum

of the costs of its arcs

Def.: A search algorithm is optimal if

when it finds a solution, it is the best one:

it has the lowest path cost

Slide 7

Page 8: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

• Lowest-cost-first search finds the path with the lowest cost to a goal node

• At each stage, it selects the path with the lowest cost on the frontier.

• The frontier is implemented as a priority queue ordered by path cost.

Lowest-Cost-First Search (LCFS)

8

Page 9: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

Summary of Uninformed Search

Complete Optimal Time Space

DFS N N O(bm) O(mb)

BFS Y Y(shortest)

O(bm) O(bm)

IDS Y Y(shortest)

O(bm) O(mb)

LCFS Y Costs > 0

Y(Least Cost)

Costs >=0

O(bm) O(bm)

Slide 9

Page 10: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

How to Make Search More Informed?Def.: A search heuristic h(n) is an estimate of the cost of the optimal

(cheapest) path from node n to a goal node.

Estimate: h(n1)

Estimate: h(n2)

Estimate: h(n3)n3

n2

n1

• h can be extended to paths: h(n0,…,nk)=h(nk)

• h(n) should leverage readily obtainable information (easy to compute) about a node. Slide 10

Page 11: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

Example: finding routes

• Possible h(n)? the straight-line (Euclidian) distance between source and goal node (here Bucharest)

11

Page 12: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

Lecture Overview

• Recap of Lecture 6

• Best First Search

• A*

• Admissible heuristic

Slide 12

Page 13: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

• Idea: always choose the path on the frontier with the smallest h value.

• BestFS treats the frontier as a priority queue ordered by h.

• Greedy approach: expand path whose last node seems closest to the goal - chose the solution that is locally the best.

Best First Search (BestFS)

Let’s see how this works in AIspace: in the Search Applet toolbar • select the “Vancouver Neighborhood Graph” problem • set “Search Options -> Search Algorithms” to “Best-First ”.• select “Show Node Heuristics” under “View”• compare number of nodes expanded by BestFS and LCFS

13

Page 14: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

• Complete? See the “misleading heuristics demo” example in AISPACE

• Optimal? Try AISPACE example “ex-best.txt” from schedule page (save it and then load using “load from file” option)

• Time Complexity: O(bm)

- Worst case: has to explore all nodes

• Space Complexity: O(bm)

Analysis of BestFS

Why would one want to use Best Search then? • Because if the heuristics is good if can find the solution very fast.• For another example, see Aispace, Delivery problem graph with C1 linked to

o123 (cost 3.0)

14

Page 15: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

• Always choose the path on the frontier with the smallest h value.• BestFS treats the frontier as a priority queue ordered by h.• Can get to the goal pretty fast if it has a good h but…

It is not complete, nor optimal

• still has time and space worst-case complexity of O(bm)

Best First Search (BestFS)

15

Page 16: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

• Thus, having estimates of the distance to the goal can speed things up a lot, but by itself it can also mislead the search (i.e. Best First Search)

• On the other hand, taking only path costs into account allows LCSF to find the optimal solution, but the search process is still uniformed as far as distance to the goal goes.

What’s Next?

16

Page 17: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

How can we more effectively use h(p) and cost(p)?

Shall we select from the frontier the path p with:

A. Lowest cost(p) – h(p)

B. Highest cost(p) – h(p)

C. Highest cost(p)+h(p) D. Lowest cost(p)+h(p)

17

Page 18: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

How can we more effectively use h(p) and cost(p)?

Shall we select from the frontier the path p with:

A. Lowest cost(p) – h(p)

B. Highest cost(p) – h(p)

C. Highest cost(p)+h(p) D. Lowest cost(p)+h(p)

18

Page 19: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

Lecture Overview

• Recap of Lecture 6

• Best First Search

• A*

• Admissible heuristic

Slide 19

Page 20: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

• A* search takes into account both • the cost of the path to a node c(p) • the heuristic value of that path h(p).

• Let f(p) = c(p) + h(p). • f(p) is an estimate of the cost of a path from the start to a goal

via p.

A* Search

c(p) h(p)

f(p)A* always chooses the path on the frontier with the lowest estimated distance

from the start to a goal node constrained to go via that path.

20

Page 21: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

F-value of ubc kd jb?

A.6 C.10 D. 11B. 9

Computing f-valeues

21

Page 22: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

F-value of ubc kd jb?

C.10

Computing f-valeues

22

Page 23: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

23

Page 24: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

Compare A* and LCFS on the Vancouver graph

• A* search takes into account both • the cost of the path to a node c(p) • the heuristic value of that path h(p).

• Let f(p) = c(p) + h(p). • f(p) is an estimate of the cost of a path from the start to a goal

via p.

A* Search

c(p) h(p)

f(p)A* always chooses the path on the frontier with the lowest estimated distance

from the start to a goal node constrained to go via that path.

24

Page 25: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

A* is complete (finds a solution, if one exists)

and optimal (finds the optimal path to a goal) if

• the branching factor is finite• arc costs are > 0 • h(n) is admissible

Optimality of A*

25

Page 26: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

Admissibility of a heuristic

Def.: Let c(n) denote the cost of the optimal path from node

n to any goal node. A search heuristic h(n) is called admissible if h(n) ≤ c(n) for all nodes n, i.e. if for all nodes it is an underestimate of the cost to any goal.

• Example: is the straight-line distance (SLD) admissible?

26

Page 27: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

Admissibility of a heuristic

Def.: Let c(n) denote the cost of the optimal path from node

n to any goal node. A search heuristic h(n) is called admissible if h(n) ≤ c(n) for all nodes n, i.e. if for all nodes it is an underestimate of the cost to any goal.

• Example: is the straight-line distance admissible?

- The shortest distance between two points is a line.

YES

27

Page 28: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

Admissibility of a heuristicDef.: Let c(n) denote the cost of the optimal path from node n to any

goal node. A search heuristic h(n) is called admissible if h(n) ≤ c(n) for all nodes n, i.e. if for all nodes it is an underestimate of the cost to any goal.

example: the goal is Urzizeni (red box), but all we know is the straight-line distances to Bucharest (green box)

• Possible h(n) = sld(n, Bucharest) + cost(Bucharest, Urzineni)• Admissible?

A. Yes C. It dependsB. No 28

Page 29: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

Admissibility of a heuristic

Def.: Let c(n) denote the cost of the optimal path from node

n to any goal node. A search heuristic h(n) is called admissible if h(n) ≤ c(n) for all nodes n, i.e. if for all nodes it is an underestimate of the cost to any goal.

example: the goal is Urzizeni (red box), but all we know is the straight-line distances to Bucharest (green box)

NO

• Possible h(n) = sld(n, Bucharest) + cost(Bucharest, Urzineni)• Admissible?

29

• Cost of going from Vastul to Urzineni is shorter than this estimate

Page 30: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

Example 2: grid world• Search problem: robot has to find a route from start

to goal location G on a grid with obstacles• Actions: move up, down, left, right from tile to tile• Cost : number of moves• Possible h(n)?

- Manhattan distance (L1 distance) to the goal G: sum of the (absolute) difference of their coordinates

- Admissible?

1 2 3 4 5 6

G

4

3

2

1Slide 30

Page 31: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

Example 2: grid world• Search problem: robot has to find a route from start

to goal location G on a grid with obstacles• Actions: move up, down, left, right from tile to tile• Cost : number of moves• Possible h(n)?

- Manhattan distance (L1 distance) to the goal G: sum of the (absolute) difference of their coordinates

- Admissible? Yes. Manhattan distance is the shortest path between any two tiles of the grid given the actions available and no walls. Including the walls will force the agent to take some extra steps to avoid them

1 2 3 4 5 6

G

4

3

2

1Slide 31

Page 32: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

Heuristic Function for 8-puzzle

An admissible heuristics for the 8-puzzle is?

A. Number of misplaced tiles plus number of correctly place tiles

B. Number of misplaced tiles

C. Number of correctly placed tiles

D. None of the above32

Page 33: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

Example 3: Eight Puzzle• Admissible h(n): Number of Misplaced Tiles: One needs at least

that many moves to get the board in the goal state

Slide 33

Page 34: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

How to Construct an Admissible Heuristic

• Identify relaxed version of the problem: - where one or more constraints have been dropped- problem with fewer restrictions on the actions

• Grid world: the agent can move through walls• Driver: the agent can move straight• 8 puzzle:

- “number of misplaced tiles”:tiles can move everywhere and occupy same spot as others

- “sum of moves between current and goal position”: tiles can occupy same spot as others

Why does this lead to an admissible heuristic?- The problem only gets easier!- Less costly to solve it 34

Page 35: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

How to Construct a Heuristic (cont.)• You should identify constraints which, when dropped, make the

problem easy to solve• important because heuristics are not useful if they're as hard to

solve as the original problem!

This was the case in our examplesRobot: allowing the agent to move through walls. Optimal solution

to this relaxed problem is Manhattan distance

Driver: allowing the agent to move straight. Optimal solution to this relaxed problem is straight-line distance

8puzzle: tiles can move anywhere. Optimal solution to this relaxed problem is number of misplaced tiles

35

Page 36: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

• Apply basic properties of search algorithms: • completeness, optimality

• time and space complexity

• Select the most appropriate search algorithms for specific problems. • Depth-First Search vs. Bredth-First Search vs. Iterative Deepening vs.

Least-Cost-First Search, Best First Search, A*

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

• Construct heuristic functions and discuss their admissibility for specific search problems

Learning Goals for Search(up to today)

Slide 36

Page 37: Computer Science CPSC 322 Lecture Heuristic Search (Ch: 3.6, 3.6.1) Slide 1.

• A*: Ch 6.1• Practice Excercises 3C and 3D

TODO

• Start working on Assignment 1! You can already do - several subquestions in Q1 - Q2 and most of Q3

37