55
DIT411/TIN175, Artificial Intelligence Chapter 3: Classical search algorithms CHAPTER 3: CLASSICAL SEARCH CHAPTER 3: CLASSICAL SEARCH ALGORITHMS ALGORITHMS DIT411/TIN175, Artificial Intelligence Peter Ljunglöf 19 January, 2018 1

CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

  • Upload
    lethuan

  • View
    225

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

DIT411/TIN175, Artificial Intelligence Chapter 3: Classical search algorithms

CHAPTER 3: CLASSICAL SEARCHCHAPTER 3: CLASSICAL SEARCHALGORITHMSALGORITHMS

DIT411/TIN175, Artificial Intelligence

Peter Ljunglöf

19 January, 2018

1

Page 2: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

DEADLINE FOR FORMING GROUPSDEADLINE FOR FORMING GROUPS

Today is the deadline for forming groups.

if you have any problems, please talk to me in the breake.g., if you cannot contact one of your group membersor if you don’t have a group yet

Don’t forget to create a Github team and clone the Shrdlite repository

then email me with information about your group

Today we will decide your exact supervision time and your supervisor

2

Page 3: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

TABLE OF CONTENTSTABLE OF CONTENTSIntroduction (R&N 3.1–3.3)

Graphs and searchingExample problemsA generic searching algorithm

Uninformed search (R&N 3.4)Depth-first searchBreadth-first searchUniform-cost searchUniform-cost search

Heuristic search (R&N 3.5–3.6)Greedy best-first searchA* searchAdmissible and consistent heuristics

3

Page 4: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

INTRODUCTION (R&N 3.1–3.3)INTRODUCTION (R&N 3.1–3.3)GRAPHS AND SEARCHINGGRAPHS AND SEARCHING

EXAMPLE PROBLEMSEXAMPLE PROBLEMS

A GENERIC SEARCHING ALGORITHMA GENERIC SEARCHING ALGORITHM

4

Page 5: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

GRAPHS AND SEARCHINGGRAPHS AND SEARCHING

O�en we are not given an algorithm to solve a problem, but only a specification of a solution — we have to search for it.

A typical problem is when the agent is in one state, it has a set of deterministic actions it can carry out, and wants to get to a goal state.

Many AI problems can be abstracted into the problem of finding a path in a directed graph.

O�en there is more than one way to represent a problem as a graph.

5

Page 6: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

STATE-SPACE SEARCH: COMPLEXITY DIMENSIONSSTATE-SPACE SEARCH: COMPLEXITY DIMENSIONS

Observable? fullyDeterministic? deterministicEpisodic? episodicStatic? staticDiscrete? discreteN:o of agents single

Most complex problems (partly observable, stochastic, sequential) usualy have components using state-space search.

6

Page 7: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

DIRECTED GRAPHSDIRECTED GRAPHS

A graph consists of a set of nodes and a set of ordered pairs of nodes, called arcs or edges.

Node is a neighbor of if there is an arc from to . That is, if .

A path is a sequence of nodes such that .

The length of path is .

A solution is a path from a start node to a goal node, given a set of start nodes and goal nodes.

(Russel & Norvig sometimes call the graph nodes states).

N A

n2 n1 n1 n2

( , ) ∈ An1 n2

( , ,… , )n0 n1 nk ( , ) ∈ Ani−1 ni

( , ,… , )n0 n1 nk k

7

Page 8: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

EXAMPLE: TRAVEL IN ROMANIAEXAMPLE: TRAVEL IN ROMANIAWe want to drive from Arad to Bucharest in Romania

8

Page 9: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

EXAMPLE: GRID GAMEEXAMPLE: GRID GAMEGrid game: Rob needs to collect coins ,

without running out of fuel, and end up at location (1,1):

What is a good representation of the search states and the goal?

, , ,C1 C2 C3 C4

9

Page 10: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

EXAMPLE: VACUUM-CLEANING AGENTEXAMPLE: VACUUM-CLEANING AGENT

States [room A dirty?, room B dirty?, robot location]Initial state any stateActions le�, right, suck, do-nothingGoal test [false, false, –]

10

Page 11: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

EXAMPLE: THE 8-PUZZLEEXAMPLE: THE 8-PUZZLE

States a 3 x 3 matrix of integersInitial state any stateActions move the blank space: le�, right, up, downGoal test equal to the goal state

11

Page 12: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

EXAMPLE: THE 8-QUEENS PROBLEMEXAMPLE: THE 8-QUEENS PROBLEM

States any arrangement of 0 to 8 queens on the boardInitial state no queens on the boardActions add a queen to any empty squareGoal test 8 queens on the board, none attacked

This gives us possible paths to explore!64 × 63 ×⋯ × 57 ≈ 1.8 × 1014

12

Page 13: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

EXAMPLE: THE 8-QUEENS PROBLEM (ALTERNATIVE)EXAMPLE: THE 8-QUEENS PROBLEM (ALTERNATIVE)

States one queen per column in le�most columns, none attackedInitial state no queens on the boardActions add a queen to a square in the le�most empty column, make sure that no queen is attackedGoal test 8 queens on the board, none attacked

Using this formulation, we have only 2,057 paths!

13

Page 14: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

EXAMPLE: KNUTH’S CONJECTUREEXAMPLE: KNUTH’S CONJECTUREDonald Knuth conjectured that all positive integers can be obtained by starting with

the number 4 and applying some combination of the factorial, square root, and floor.

States algebraic numbers Initial state 4Actions apply factorial, square root, or floor operationGoal test a given positive integer (e.g., 5)

= 5

⎣⎢⎢ (4!)!‾ ‾‾‾√‾ ‾‾‾‾‾‾√

‾ ‾‾‾‾‾‾‾‾√‾ ‾‾‾‾‾‾‾‾‾‾‾

√‾ ‾‾‾‾‾‾‾‾‾‾‾‾‾

⎦⎥⎥

(1, 2.5, 9, , 1.23 ⋅ , ,…)2‾√ 10456 2‾√‾ ‾‾√

14

Page 15: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

EXAMPLE: ROBOTIC ASSEMBLYEXAMPLE: ROBOTIC ASSEMBLY

States real-valued coordinates of robot joint angles parts of the object to be assembledActions continuous motions of robot jointsGoal test complete assembly of the object

15

Page 16: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

HOW DO WE SEARCH IN A GRAPH?HOW DO WE SEARCH IN A GRAPH?

A generic search algorithm:

Given a graph, start nodes, and a goal description, incrementally explore paths from the start nodes.

Maintain a frontier of nodes that are to be explored.

As search proceeds, the frontier expands into the unexplored nodes until a goal node is encountered.

The way in which the frontier is expanded defines the search strategy.

16

Page 17: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

ILLUSTRATION OF GENERIC SEARCHILLUSTRATION OF GENERIC SEARCH

17

Page 18: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

A GENERIC TREE SEARCH ALGORITHMA GENERIC TREE SEARCH ALGORITHMTree search: Don’t check if nodes are visited multiple times

 function Search(graph, initialState, goalState):

initialise frontier using the initialState while frontier is not empty:

select and remove node from frontierif node.state is a goalState then return node for each child in ExpandChildNodes(node, graph):

add child to frontier return failure

18

Page 19: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

USING TREE SEARCH ON A GRAPHUSING TREE SEARCH ON A GRAPH

explored nodes might be revisitedfrontier nodes might be duplicated

19

Page 20: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

TURNING TREE SEARCH INTO GRAPH SEARCHTURNING TREE SEARCH INTO GRAPH SEARCHGraph search: Keep track of visited nodes

 function Search(graph, initialState, goalState):

initialise frontier using the initialStateinitialise exploredSet to the empty setwhile frontier is not empty:

select and remove node from frontierif node.state is a goalState then return nodeadd node to exploredSetfor each child in ExpandChildNodes(node, graph):

add child to frontier if child is not in frontier or exploredSetreturn failure

20

Page 21: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

TREE SEARCH VS. GRAPH SEARCHTREE SEARCH VS. GRAPH SEARCH

Tree search

Pro: uses less memoryCon: might visit the same node several times

Graph search

Pro: only visits nodes at most onceCon: uses more memory

Note: The pseudocode in these slides (and the course book) is not the only possible! E.g., Wikipedia uses a different variant.

21

Page 22: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

GRAPH NODES VS. SEARCH NODESGRAPH NODES VS. SEARCH NODESSearch nodes are not the same as graph nodes!Search nodes should contain more information:

the corresponding graph node (called state in R&N)the total path cost from the start nodethe estimated (heuristic) cost to the goalenough information to be able to calculate the final path

 procedure ExpandChildNodes(parent, graph):

for each (action, child, edgecost) in graph.successors(parent.state):yield new SearchNode(child,

            …total cost so far…,            …estimated cost to goal…,            …information for calculating final path…)

22

Page 23: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

UNINFORMED SEARCH (R&N 3.4)UNINFORMED SEARCH (R&N 3.4)DEPTH-FIRST SEARCHDEPTH-FIRST SEARCH

BREADTH-FIRST SEARCHBREADTH-FIRST SEARCH

UNIFORM-COST SEARCHUNIFORM-COST SEARCH

23

Page 24: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

QUESTION TIME: DEPTH-FIRST SEARCHQUESTION TIME: DEPTH-FIRST SEARCHWhich shaded goal will a depth-first search find first?

24

Page 25: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

QUESTION TIME: BREADTH-FIRST SEARCHQUESTION TIME: BREADTH-FIRST SEARCHWhich shaded goal will a breadth-first search find first?

25

Page 26: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

DEPTH-FIRST SEARCHDEPTH-FIRST SEARCH

Depth-first search treats the frontier as a stack.

It always selects one of the last elements added to the frontier.

If the list of nodes on the frontier is , then:

is selected (and removed).Nodes that extend are added to the front of the stack (in front of ).

is only selected when all nodes from have been explored.

[ , , ,…]p1 p2 p3

p1

p1 p2

p2 p1

26

Page 27: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

ILLUSTRATIVE GRAPH: DEPTH-FIRST SEARCHILLUSTRATIVE GRAPH: DEPTH-FIRST SEARCH

27

Page 28: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

COMPLEXITY OF DEPTH-FIRST SEARCHCOMPLEXITY OF DEPTH-FIRST SEARCH

Does DFS guarantee to find the path with fewest arcs?

What happens on infinite graphs or on graphs with cycles if there is a solution?

What is the time complexity as a function of the path length?

What is the space complexity as a function of the path length?

How does the goal affect the search?

28

Page 29: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

BREADTH-FIRST SEARCHBREADTH-FIRST SEARCH

Breadth-first search treats the frontier as a queue.

It always selects one of the earliest elements added to the frontier.

If the list of paths on the frontier is , then:

is selected (and removed).Its neighbors are added to the end of the queue, a�er .

is selected next.

[ , ,… , ]p1 p2 pr

p1

pr

p2

29

Page 30: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

ILLUSTRATIVE GRAPH: BREADTH-FIRST SEARCHILLUSTRATIVE GRAPH: BREADTH-FIRST SEARCH

30

Page 31: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

COMPLEXITY OF BREADTH-FIRST SEARCHCOMPLEXITY OF BREADTH-FIRST SEARCH

Does BFS guarantee to find the path with fewest arcs?

What happens on infinite graphs or on graphs with cycles if there is a solution?

What is the time complexity as a function of the path length?

What is the space complexity as a function of the path length?

How does the goal affect the search?

31

Page 32: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

UNIFORM-COST SEARCHUNIFORM-COST SEARCHWeighted graphs:

Sometimes there are costs associated with arcs. The cost of a path is the sum of the costs of its arcs.

An optimal solution is one with minimum cost.

Uniform-cost search (aka Lowest-cost-first search):Uniform-cost search selects a path on the frontier with the lowest cost.The frontier is a priority queue ordered by path cost.It finds a least-cost path to a goal node — i.e., uniform-cost search is optimalWhen arc costs are equal breadth-first search.

cost( ,… , ) = ( , )n0 nk ∑i=1

k

∣∣ ni−1 ni ∣∣

32

Page 33: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

HEURISTIC SEARCH (R&N 3.5–3.6)HEURISTIC SEARCH (R&N 3.5–3.6)GREEDY BEST-FIRST SEARCHGREEDY BEST-FIRST SEARCH

A* SEARCHA* SEARCH

ADMISSIBLE AND CONSISTENT HEURISTICSADMISSIBLE AND CONSISTENT HEURISTICS

33

Page 34: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

HEURISTIC SEARCHHEURISTIC SEARCHPrevious methods don’t use the goal to select a path to explore.

Main idea: don’t ignore the goal when selecting paths.

O�en there is extra knowledge that can guide the search: heuristics.

is an estimate of the cost of the shortest path from node to a goal node.

needs to be efficient to compute.

is an underestimate if there is no path from to a goal with cost less than .

An admissible heuristic is a nonnegative underestimating heuristic function:

h(n) n

h(n)

h(n) n

h(n)

0 ≤ h(n) ≤ cost(best path from n to goal)

34

Page 35: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

EXAMPLE HEURISTIC FUNCTIONSEXAMPLE HEURISTIC FUNCTIONS

Here are some example heuristic functions:

If the nodes are points on a Euclidean plane and the cost is the distance, can be the straight-line distance (SLD) from n to the closest goal.

If the nodes are locations and cost is time, we can use the distance to a goal divided by the maximum speed, (or the average speed, , which makes it non-admissible).

If the graph is a 2D grid maze, then we can use the Manhattan distance.

If the goal is to collect all of the coins and not run out of fuel, we can use an estimate of how many steps it will take to collect the coins and return to goal position, without caring about the fuel consumption.

A heuristic function can be found by solving a simpler (less constrained) version of the problem.

h(n)

h(n) = d(n)/vmax

h(n) = d(n)/vavg

35

Page 36: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

EXAMPLE HEURISTIC: ROMANIA DISTANCESEXAMPLE HEURISTIC: ROMANIA DISTANCES

36

Page 37: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

GREEDY BEST-FIRST SEARCHGREEDY BEST-FIRST SEARCH

Main idea: select the path whose end is closest to a goal according to the heuristic function.

Best-first search selects a path on the frontier with minimal -value.

It treats the frontier as a priority queue ordered by .

h

h

37

Page 38: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

GREEDY SEARCH EXAMPLE: ROMANIAGREEDY SEARCH EXAMPLE: ROMANIA

This is not the shortest path!

38

Page 39: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

GREEDY SEARCH IS NOT OPTIMALGREEDY SEARCH IS NOT OPTIMALGreedy search returns the path: Arad–Sibiu–Fagaras–Bucharest (450km)The optimal path is: Arad–Sibiu–Rimnicu–Pitesti–Bucharest (418km)

39

Page 40: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

BEST-FIRST SEARCH AND INFINITE LOOPSBEST-FIRST SEARCH AND INFINITE LOOPS

Best-first search might fall into an infinite loop!

40

Page 41: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

COMPLEXITY OF BEST-FIRST SEARCHCOMPLEXITY OF BEST-FIRST SEARCH

Does best-first search guarantee to find the path with fewest arcs?

What happens on infinite graphs or on graphs with cycles if there is a solution?

What is the time complexity as a function of the path length?

What is the space complexity as a function of the path length?

How does the goal affect the search?

41

Page 42: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

A* SEARCHA* SEARCH

A* search uses both path cost and heuristic values.

is the cost of path .

estimates the cost from the end node of to a goal.

, estimates the total path cost of going from the start node, via path to a goal:

cost(p) p

h(p) p

f (p) = cost(p) + h(p)

p

nstart  − →−−−path p

cost(p)

   goal− →−−−−estimate

h(p)

f (p)

42

Page 43: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

A* SEARCHA* SEARCH

A* is a mix of uniform-cost search and best-first search.

It treats the frontier as a priority queue ordered by .

It always selects the node on the frontier with the lowest estimated distance from the start to a goal node constrained to go via that node.

f (p)

43

Page 44: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

COMPLEXITY OF A* SEARCHCOMPLEXITY OF A* SEARCH

Does A* search guarantee to find the path with fewest arcs?

What happens on infinite graphs or on graphs with cycles if there is a solution?

What is the time complexity as a function of the path length?

What is the space complexity as a function of the path length?

How does the goal affect the search?

44

Page 45: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

A* SEARCH EXAMPLE: ROMANIAA* SEARCH EXAMPLE: ROMANIA

A* guarantees that this is the shortest path!

Note that we didn’t stop when we added Bucharest to the frontier. Instead, we stopped when we removed Bucharest from the frontier!

45

Page 46: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

A* SEARCH IS OPTIMALA* SEARCH IS OPTIMALThe optimal path is: Arad–Sibiu–Rimnicu–Pitesti–Bucharest (418km)

46

Page 47: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

A* ALWAYS FINDS A SOLUTIONA* ALWAYS FINDS A SOLUTION

A* will always find a solution if there is one, because:

The frontier always contains the initial part of a path to a goal, before that goal is selected.

A* halts, because the costs of the paths on the frontier keeps increasing, and will eventually exceed any finite number.

47

Page 48: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

ADMISSIBILITY (OPTIMALITY) OF ADMISSIBILITY (OPTIMALITY) OF A*A*

If there is a solution, A* always finds an optimal one first, provided that:

the branching factor is finite,

arc costs are bounded above zero (i.e., there is some such that all of the arc costs are greater than ), and

is nonnegative and an underestimate of the cost of the shortest path from to a goal node.

These requirements ensure that keeps increasing.

ϵ > 0

ϵ

h(n)

n

f

48

Page 49: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

WHY IS A* OPTIMAL?WHY IS A* OPTIMAL?

The values in A* are increasing, therefore:

first A* expands all nodes with

then A* expands all nodes with

finally A* expands all nodes with

A* will not expand any nodes with , where is the cost of an optimal solution.

(Note: all this assumes that the heuristics is admissible)

f

f (n) < C

f (n) = C

f (n) > C

f (n) > C∗C∗

49

Page 50: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

ILLUSTRATION: WHY IS A* OPTIMAL?ILLUSTRATION: WHY IS A* OPTIMAL?

A* gradually adds “ -contours” of nodes (cf. BFS adds layers)

Contour has all nodes with , where

f

i f = fi <fi fi+1

50

Page 51: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

QUESTION TIME: HEURISTICS FOR THE 8 PUZZLEQUESTION TIME: HEURISTICS FOR THE 8 PUZZLE = number of misplaced tiles = total Manhattan distance

(i.e., no. of squares from desired location of each tile)

 =  8  =  3+1+2+2+2+3+3+2 = 18

(n)h1

(n)h2

(StartState)h1

(StartState)h2

51

Page 52: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

DOMINATING HEURISTICSDOMINATING HEURISTICS

If (admissible) for all , then dominates and is better for search.

Typical search costs (for 8-puzzle):

depth = 14 DFS  ≈  3,000,000 nodes A*( ) = 539 nodes A*( ) = 113 nodes

depth = 24 DFS  ≈  54,000,000,000 nodes A*( ) = 39,135 nodes A*( ) = 1,641 nodes

Given any admissible heuristics , , the maximum heuristics is also admissible and dominates both:

(n) ≥ (n)h2 h1 n

h2 h1

h1

h2

h1

h2

ha hb h(n)

h(n) = max( (n), (n))ha hb

52

Page 53: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

HEURISTICS FROM A RELAXED PROBLEMHEURISTICS FROM A RELAXED PROBLEM

Admissible heuristics can be derived from the exact solution cost of a relaxed problem:

If the rules of the 8-puzzle are relaxed so that a tile can move anywhere, then gives the shortest solution

If the rules are relaxed so that a tile can move to any adjacent square, then gives the shortest solution

Key point: the optimal solution cost of a relaxed problem is never greater than the optimal solution cost of the real problem

(n)h1

(n)h2

53

Page 54: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

SUMMARY OF TREE SEARCH STRATEGIESSUMMARY OF TREE SEARCH STRATEGIESSearch strategy

Frontierselection

Halts ifsolution?

Halts if nosolution?

Spaceusage

Depth first Last node added No No LinearBreadth first First node added Yes No Exp

Best first Global min No No Exp

Uniform cost Minimal Yes No Exp

A* Minimal Yes No Exp 

Halts if: If there is a path to a goal, it can find one, even on infinite graphs.Halts if no: Even if there is no solution, it will halt on a finite graph (with cycles).Space: Space complexity as a function of the length of the current path.

h(p)

cost(p)

f (p)

54

Page 55: CHAPTER 3: CLASSICAL SEARCH ALGORITHMSchalmersgu-ai-course.github.io/AI-lecture-slides/lecture2.pdf · DIT411/TIN175, Ar tificial Intelligence Chapter 3: Classical search algorithms

EXAMPLE DEMOEXAMPLE DEMO

Here is an example demo of several different search algorithms, including A*. And you can play with different heuristics:

Note that this demo is tailor-made for planar grids, which is a special case of all possible search graphs.

(e.g., the Shrdlite graph will not be a planar grid)

http://qiao.github.io/PathFinding.js/visual/

55