Post on 15-Jan-2016
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
Course Information
Instructor: Tamara Berg (tlberg@cs.unc.edu)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.
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)
Recall from last class
Agents• An agent is anything that can be viewed as
perceiving its environment through sensors and acting upon that environment through actuators
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
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)
Search
• We will consider the problem of designing goal-based agents in fully observable, deterministic, discrete, known environments
Start state
Goal state
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
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)
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
Vacuum world state space graph
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!
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
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
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
Tree search example
Start: AradGoal: Bucharest
Tree search example
Start: AradGoal: Bucharest
Tree search example
Start: AradGoal: Bucharest
Tree search example
Start: AradGoal: Bucharest
Tree search example
Start: AradGoal: Bucharest
Tree search example
Start: AradGoal: Bucharest
Tree search example
Start: AradGoal: Bucharest
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
Search without repeated states
Start: AradGoal: Bucharest
Search without repeated states
Start: AradGoal: Bucharest
Search without repeated states
Start: AradGoal: Bucharest
Search without repeated states
Start: AradGoal: Bucharest
Search without repeated states
Start: AradGoal: Bucharest
Search without repeated states
Start: AradGoal: Bucharest
Search without repeated states
Start: AradGoal: Bucharest
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?
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
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
Uninformed search
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
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)
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)
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)
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)
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)
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)
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)
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)
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
Depth-first search
• Expand deepest node in the frontier
Depth-first search
• Expansion order: (S,d,b,a,c,a,e,h,p,q,q, r,f,c,a,G)
Depth-first search
• Expansion order: (S,d,b,a,c,a,e,h,p,q,q, r,f,c,a,G)
Depth-first search
• Expansion order: (S,d,b,a,c,a,e,h,p,q,q, r,f,c,a,G)
Depth-first search
• Expansion order: (S,d,b,a,c,a,e,h,p,q,q, r,f,c,a,G)
Depth-first search
• Expansion order: (S,d,b,a,c,a,e,h,p,q,q, r,f,c,a,G)
Depth-first search
• Expansion order: (S,d,b,a,c,a,e,h,p,q,q, r,f,c,a,G)
Depth-first search
• Expansion order: (S,d,b,a,c,a,e,h,p,q,q,r,f,c,a,G)
Depth-first search
• Expand deepest unexpanded node• Implementation: frontier is a LIFO queue
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)
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)
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
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!
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…
Iterative deepening search
Iterative deepening search
Iterative deepening search
Iterative deepening search
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)
Search with varying step costs
• BFS finds the path with the fewest steps, but does not always find the cheapest path
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
Uniform-cost search example
Uniform-cost search example
• Expansion order:(S,p,d,b,e,a,r,f,e,G)
Uniform-cost search example
• Expansion order:(S,p,d,b,e,a,r,f,e,G)
Uniform-cost search example
• Expansion order:(S,p,d,b,e,a,r,f,e,G)
Uniform-cost search example
• Expansion order:(S,p,d,b,e,a,r,f,e,G)
Uniform-cost search example
• Expansion order:(S,p,d,b,e,a,r,f,e,G)
Another example of uniform-cost search
Source: Wikipedia
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*/ ε)
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
Heuristic function• Heuristic function h(n) estimates the cost of
reaching goal from node n• Example:
Start state
Goal state
Heuristic for the Romania problem
Greedy best-first search
• Expand the node that has the lowest value of the heuristic function h(n)
Greedy best-first search example
Greedy best-first search example
Greedy best-first search example
Greedy best-first search example
Properties of greedy best-first search
• Complete?No – can get stuck in loops
startgoal
Properties of greedy best-first search
• Complete?No – can get stuck in loops
• Optimal? No
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)
How can we fix the greedy problem?
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)
A* search example
A* search example
A* search example
A* search example
A* search example
A* search example
Uniform cost search vs. A* search
Source: Wikipedia
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
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!
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
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
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?
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
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
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
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
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)}
Additional pointers
• Interactive path finding demo• Variants of A* for path finding on grids
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*