110
Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana Lazebnik, Percy Liang, Luke Zettlemoyer

Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Embed Size (px)

Citation preview

Page 1: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Search

Tamara BergCS 590-133 Artificial Intelligence

Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana Lazebnik, Percy Liang, Luke Zettlemoyer

Page 2: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Course Information

Instructor: Tamara Berg ([email protected])Course website: http://tamaraberg.com/teaching/Spring_14/

TAs: Shubham Gupta & Rohit Gupta

Office Hours (Tamara): Tuesdays/Thursdays 4:45-5:45pm FB 236 Office Hours (Shubham): Mondays 4-5pm & Friday 3-4pm SN 307 Office Hours (Rohit): Wednesday 4-5pm & Friday 4-5pm SN 312

See website & previous slides for additional important course information.

Page 3: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Announcements for today

• Sign up for the class piazza mailing list here: – piazza.com/unc/spring2014/comp590133

• Reminder: This is a 3 credit course. If you are enrolled for 1 credit, please change to 3 credits.

• HW1 will be released on the course website tonight (Shubham/Rohit will give a short overview at the end of class)

Page 4: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Recall from last class

Page 5: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Agents• An agent is anything that can be viewed as

perceiving its environment through sensors and acting upon that environment through actuators

Page 6: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Rational agents

• For each possible percept sequence, a rational agent should select an action that is expected to maximize its performance measure, given the evidence provided by the percept sequence and the agent’s built-in knowledge

• Performance measure (utility function): An objective criterion for success of an agent's behavior

Page 7: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Types of agentsReflex agent

• Consider how the world IS• Choose action based on

current percept (and maybe memory or a model of the world’s current state)

• Do not consider the future consequences of their actions

Planning agent

• Consider how the world WOULD BE• Decisions based on (hypothesized)

consequences of actions• Must have a model of how the world

evolves in response to actions• Must formulate a goal (test)

Page 8: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Search

• We will consider the problem of designing goal-based agents in fully observable, deterministic, discrete, known environments

Start state

Goal state

Page 9: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Search problem components• Initial state• Actions• Transition model

– What state results fromperforming a given action in a given state?

• Goal state• Path cost

– Assume that it is a sum of nonnegative step costs

• The optimal solution is the sequence of actions that gives the lowest path cost for reaching the goal

Initialstate

Goal state

Page 10: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Example: Romania• On vacation in Romania; currently in Arad• Flight leaves tomorrow from Bucharest

• Initial state– Arad

• Actions– Go from one city to another

• Transition model– If you go from city A to

city B, you end up in city B

• Goal state– Bucharest

• Path cost– Sum of edge costs (total distance

traveled)

Page 11: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

State space• The initial state, actions, and transition model

define the state space of the problem– The set of all states reachable from initial state by any

sequence of actions– Can be represented as a directed graph where the

nodes are states and links between nodes are actions

Page 12: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Vacuum world state space graph

Page 13: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Search• Given:

– Initial state

– Actions

– Transition model

– Goal state

– Path cost

• How do we find the optimal solution?– How about building the state space and then using Dijkstra’s

shortest path algorithm?• Complexity of Dijkstra’s is O(E + V log V), where V is the size of the

state space

• The state space may be huge!

Page 14: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Search: Basic idea

• Let’s begin at the start state and expand it by making a list of all possible successor states

• Maintain a frontier – the set of all leaf nodes available for expansion at any point

• At each step, pick a state from the frontier to expand

• Keep going until you reach a goal state or there are no more states to explore.

• Try to expand as few states as possible

Page 15: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Search tree• “What if” tree of sequences of actions

and outcomes

• The root node corresponds to the starting state

• The children of a node correspond to the successor states of that node’s state

• A path through the tree corresponds to a sequence of actions– A solution is a path ending in a goal state

• Edges are labeled with actions and costs

… … ……

Starting state

Successor state

Action

Goal state

Page 16: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana
Page 17: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana
Page 18: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Tree Search Algorithm Outline

• Initialize the frontier using the start state• While the frontier is not empty– Choose a frontier node to expand according to search strategy

and take it off the frontier– If the node contains the goal state, return solution– Else expand the node and add its children to the frontier

Page 19: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Tree search example

Start: AradGoal: Bucharest

Page 20: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Tree search example

Start: AradGoal: Bucharest

Page 21: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Tree search example

Start: AradGoal: Bucharest

Page 22: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Tree search example

Start: AradGoal: Bucharest

Page 23: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Tree search example

Start: AradGoal: Bucharest

Page 24: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Tree search example

Start: AradGoal: Bucharest

Page 25: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Tree search example

Start: AradGoal: Bucharest

Page 26: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Handling repeated states• Initialize the frontier using the starting state• While the frontier is not empty– Choose a frontier node to expand according to search strategy

and take it off the frontier– If the node contains the goal state, return solution– Else expand the node and add its children to the frontier

• To handle repeated states:– Keep an explored set; which remembers every expanded node– Newly generated nodes already in the explored set or frontier

can be discarded instead of added to the frontier

Page 27: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Search without repeated states

Start: AradGoal: Bucharest

Page 28: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Search without repeated states

Start: AradGoal: Bucharest

Page 29: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Search without repeated states

Start: AradGoal: Bucharest

Page 30: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Search without repeated states

Start: AradGoal: Bucharest

Page 31: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Search without repeated states

Start: AradGoal: Bucharest

Page 32: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Search without repeated states

Start: AradGoal: Bucharest

Page 33: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Search without repeated states

Start: AradGoal: Bucharest

Page 34: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Tree Search Algorithm Outline

• Initialize the frontier using the starting state• While the frontier is not empty– Choose a frontier node to expand according to search strategy

and take it off the frontier– If the node contains the goal state, return solution– Else expand the node and add its children to the frontier

Main question: What should our search strategy be, ie how do we choose which frontier node to expand?

Page 35: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Uninformed search strategies

• A search strategy is defined by picking the order of node expansion

• Uninformed search strategies use only the information available in the problem definition– Breadth-first search– Depth-first search– Iterative deepening search– Uniform-cost search

Page 36: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Informed search strategies

• Idea: give the algorithm “hints” about the desirability of different states – Use an evaluation function to rank nodes and

select the most promising one for expansion

• Greedy best-first search• A* search

Page 37: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Uninformed search

Page 38: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Breadth-first search

• Expand shallowest node in the frontier

Example state space graph for a tiny search

problem

Example from P. Abbeel and D. Klein

Page 39: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Breadth-first search

• Expansion order: (S,d,e,p,b,c,e,h,r,q,a,a, h,r,p,q,f,p,q,f,q,c,G)

Page 40: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Breadth-first search

• Expansion order: (S,d,e,p,b,c,e,h,r,q,a,a, h,r,p,q,f,p,q,f,q,c,G)

Page 41: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Breadth-first search

• Expansion order: (S,d,e,p,b,c,e,h,r,q,a,a, h,r,p,q,f,p,q,f,q,c,G)

Page 42: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Breadth-first search

• Expansion order: (S,d,e,p,b,c,e,h,r,q,a,a, h,r,p,q,f,p,q,f,q,c,G)

Page 43: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Breadth-first search

• Expansion order: (S,d,e,p,b,c,e,h,r,q,a,a, h,r,p,q,f,p,q,f,q,c,G)

Page 44: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Breadth-first search

• Expansion order: (S,d,e,p,b,c,e,h,r,q,a,a, h,r,p,q,f,p,q,f,q,c,G)

Page 45: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Breadth-first search

• Expansion order: (S,d,e,p,b,c,e,h,r,q,a,a, h,r,p,q,f,p,q,f,q,c,G)

Page 46: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Breadth-first search

• Expansion order: (S,d,e,p,b,c,e,h,r,q,a,a, h,r,p,q,f,p,q,f,q,c,G)

Page 47: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Breadth-first search

• Expand shallowest node in the frontier• Implementation: frontier is a FIFO queue

Example state space graph for a tiny search

problem

Example from P. Abbeel and D. Klein

Page 48: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Depth-first search

• Expand deepest node in the frontier

Page 49: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Depth-first search

• Expansion order: (S,d,b,a,c,a,e,h,p,q,q, r,f,c,a,G)

Page 50: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Depth-first search

• Expansion order: (S,d,b,a,c,a,e,h,p,q,q, r,f,c,a,G)

Page 51: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Depth-first search

• Expansion order: (S,d,b,a,c,a,e,h,p,q,q, r,f,c,a,G)

Page 52: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Depth-first search

• Expansion order: (S,d,b,a,c,a,e,h,p,q,q, r,f,c,a,G)

Page 53: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Depth-first search

• Expansion order: (S,d,b,a,c,a,e,h,p,q,q, r,f,c,a,G)

Page 54: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Depth-first search

• Expansion order: (S,d,b,a,c,a,e,h,p,q,q, r,f,c,a,G)

Page 55: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Depth-first search

• Expansion order: (S,d,b,a,c,a,e,h,p,q,q,r,f,c,a,G)

Page 56: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Depth-first search

• Expand deepest unexpanded node• Implementation: frontier is a LIFO queue

Page 57: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

http://xkcd.com/761/

Page 58: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Analysis of search strategies

• Strategies are evaluated along the following criteria:– Completeness

• does it always find a solution if one exists?

– Optimality • does it always find a least-cost solution?

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

– Space complexity• maximum number of nodes in memory

• Time and space complexity are measured in terms of – b: maximum branching factor of the search tree– d: depth of the optimal solution– m: maximum length of any path in the state space (may be infinite)

Page 59: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Properties of breadth-first search

• Complete? Yes (if branching factor b is finite)

• Optimal? Not generally – the shallowest goal node is not necessarily

the optimal oneYes – if all actions have same cost

• Time? Number of nodes in a b-ary tree of depth d: O(bd)(d is the depth of the optimal solution)

• Space? O(bd)

Page 60: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

BFS

Depth Nodes Time Memory

2 110 0.11 ms 107 kilobytes

4 11,110 11 ms 10.6 megabytes

6 10^6 1.1 s 1 gigabyte

8 10^8 2 min 103 gigabytes

10 10^10 3 hrs 10 terabytes

12 10^12 13 days 1 petabyte

14 10^14 3.5 years 99 petabytes

16 10^16 350 years 10 exabytes

Time and Space requirements for BFS with b=10; 1 million nodes/second; 1000 bytes/node

Page 61: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Properties of depth-first search

• Complete?Fails in infinite-depth spaces, spaces with loopsModify to avoid repeated states along path

complete in finite spaces

• Optimal?No – returns the first solution it finds

• Time? May generate all of the O(bm) nodes, m=max depth of any nodeTerrible if m is much larger than d

• Space? O(bm), i.e., linear space!

Page 62: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Iterative deepening search

• Use DFS as a subroutine1. Check the root2. Do a DFS with depth limit 13. If there is no path of length 1, do a DFS search

with depth limit 24. If there is no path of length 2, do a DFS with

depth limit 3.5. And so on…

Page 63: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Iterative deepening search

Page 64: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Iterative deepening search

Page 65: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Iterative deepening search

Page 66: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Iterative deepening search

Page 67: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Properties of iterative deepening search

• Complete?Yes

• Optimal?Not generally – the shallowest goal node is not

necessarily the optimal oneYes – if all actions have same cost

• Time?(d+1)b0 + d b1 + (d-1)b2 + … + bd = O(bd)

• Space?O(bd)

Page 68: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Search with varying step costs

• BFS finds the path with the fewest steps, but does not always find the cheapest path

Page 69: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Uniform-cost search• For each frontier node, save the total cost of

the path from the initial state to that node• Expand the frontier node with the lowest path

cost• Implementation: frontier is a priority queue

ordered by path cost • Equivalent to breadth-first if step costs all equal

Page 70: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Uniform-cost search example

Page 71: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Uniform-cost search example

• Expansion order:(S,p,d,b,e,a,r,f,e,G)

Page 72: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Uniform-cost search example

• Expansion order:(S,p,d,b,e,a,r,f,e,G)

Page 73: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Uniform-cost search example

• Expansion order:(S,p,d,b,e,a,r,f,e,G)

Page 74: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Uniform-cost search example

• Expansion order:(S,p,d,b,e,a,r,f,e,G)

Page 75: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Uniform-cost search example

• Expansion order:(S,p,d,b,e,a,r,f,e,G)

Page 76: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Another example of uniform-cost search

Source: Wikipedia

Page 77: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Properties of uniform-cost search• Complete?

Yes, if step cost is greater than some positive constant ε (gets stuck in infinite loop if there is a path with inifinite sequence of

zero-cost actions)Optimal?Yes – nodes expanded in increasing order of path cost

• Time? Number of nodes with path cost ≤ cost of optimal solution (C*), O(bC*/ ε)This can be greater than O(bd): the search can explore long paths

consisting of small steps before exploring shorter paths consisting of larger steps

• Space? O(bC*/ ε)

Page 78: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Informed search strategies

• Idea: give the algorithm “hints” about the desirability of different states – Use an evaluation function to rank nodes and

select the most promising one for expansion

• Greedy best-first search• A* search

Page 79: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Heuristic function• Heuristic function h(n) estimates the cost of

reaching goal from node n• Example:

Start state

Goal state

Page 80: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Heuristic for the Romania problem

Page 81: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Greedy best-first search

• Expand the node that has the lowest value of the heuristic function h(n)

Page 82: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Greedy best-first search example

Page 83: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Greedy best-first search example

Page 84: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Greedy best-first search example

Page 85: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Greedy best-first search example

Page 86: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Properties of greedy best-first search

• Complete?No – can get stuck in loops

startgoal

Page 87: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Properties of greedy best-first search

• Complete?No – can get stuck in loops

• Optimal? No

Page 88: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Properties of greedy best-first search

• Complete?No – can get stuck in loops

• Optimal? No

• Time? Worst case: O(bm)Can be much better with a good heuristic

• Space?Worst case: O(bm)

Page 89: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

How can we fix the greedy problem?

Page 90: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

A* search

• Idea: avoid expanding paths that are already expensive• The evaluation function f(n) is the estimated total cost

of the path through node n to the goal:

f(n) = g(n) + h(n)

g(n): cost so far to reach n (path cost)h(n): estimated cost from n to goal (heuristic)

Page 91: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

A* search example

Page 92: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

A* search example

Page 93: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

A* search example

Page 94: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

A* search example

Page 95: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

A* search example

Page 96: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

A* search example

Page 97: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Another example

Source: Wikipedia

Page 98: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Uniform cost search vs. A* search

Source: Wikipedia

Page 99: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Admissible heuristics

• An admissible heuristic never overestimates the cost to reach the goal, i.e., it is optimistic

• A heuristic h(n) is admissible if for every node n, h(n) ≤ h*(n), where h*(n) is the true cost to reach the goal state from n

• Is straight line distance admissible? – Yes, straight line distance never overestimates the actual

road distance

Page 100: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Optimality of A*

• Theorem: If h(n) is admissible, A* is optimal• Proof by contradiction:– Suppose A* terminates at goal state n with f(n) = g(n) = C

but there exists another goal state n’ with g(n’) < C– Then there must exist a node n” on the frontier that is on

the optimal path to n’– Because h is admissible, we must have f(n”) ≤ g(n’)– But then, f(n”) < C, so n” should have been expanded first!

Page 101: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Optimality of A*

• A* is optimally efficient – no other tree-based algorithm that uses the same heuristic can expand fewer nodes and still be guaranteed to find the optimal solution– Any algorithm that does not expand all nodes with

f(n) ≤ C* risks missing the optimal solution

Page 102: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Properties of A*

• Complete?Yes – unless there are infinitely many nodes with f(n) ≤ C*

• Optimal?Yes

• Time?Number of nodes for which f(n) ≤ C* (exponential)

• Space?Exponential

Page 103: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Designing heuristic functions• Heuristics for the 8-puzzle

h1(n) = number of misplaced tiles

h2(n) = total Manhattan distance (number of squares from desired location of each tile)

h1(start) = 8

h2(start) = 3+1+2+2+2+3+3+2 = 18

• Are h1 and h2 admissible?

Page 104: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Dominance

• If h1 and h2 are both admissible heuristics and

h2(n) ≥ h1(n) for all n, (both admissible) then h2 dominates h1

• Which one is better for search?– A* search expands every node with f(n) < C* or

h(n) < C* – g(n)– Therefore, A* search with h1 will expand more nodes

Page 105: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Dominance

• Typical search costs for the 8-puzzle (average number of nodes expanded for different solution depths):

• d=12 IDS = 3,644,035 nodesA*(h1) = 227 nodes A*(h2) = 73 nodes

• d=24 IDS ≈ 54,000,000,000 nodes A*(h1) = 39,135 nodes A*(h2) = 1,641 nodes

Page 106: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Heuristics from relaxed problems

• A problem with fewer restrictions on the actions is called a relaxed problem

• The cost of an optimal solution to a relaxed problem is an admissible heuristic for the original problem

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

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

Page 107: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Heuristics from subproblems

• Let h3(n) be the cost of getting a subset of tiles (say, 1,2,3,4) into their correct positions

• Can precompute and save the exact solution cost for every possible subproblem instance – pattern database

Page 108: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Combining heuristics

• Suppose we have a collection of admissible heuristics h1(n), h2(n), …, hm(n), but none of them dominates the others

• How can we combine them?

h(n) = max{h1(n), h2(n), …, hm(n)}

Page 109: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

Additional pointers

• Interactive path finding demo• Variants of A* for path finding on grids

Page 110: Search Tamara Berg CS 590-133 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana

All search strategiesAlgorithm Complete? Optimal? Time

complexitySpace

complexity

BFS

DFS

IDS

UCS

Greedy

A*

No NoWorst case: O(bm)

YesYes

(if heuristic is admissible)

Best case: O(bd)

Number of nodes with g(n)+h(n) ≤ C*

Yes

Yes

No

Yes

If all step costs are equal

If all step costs are equal

Yes

No

O(bd)

O(bm)

O(bd)

O(bd)

O(bm)

O(bd)

Number of nodes with g(n) ≤ C*