Presentation is loading. Please wait.

Presentation is loading. Please wait.

Search (cont) & CSP Intro Tamara Berg CS 560 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore,

Similar presentations


Presentation on theme: "Search (cont) & CSP Intro Tamara Berg CS 560 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore,"— Presentation transcript:

1 Search (cont) & CSP Intro Tamara Berg CS 560 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore, Svetlana Lazebnik, Percy Liang, Luke Zettlemoyer

2 Reminders HW1 is due Sept 10 – This will take a significant amount of time. Start now! – Anyone still looking for a group?

3 Recall from last class

4 Uninformed search

5 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)

6 Depth-first search Expansion order: (S,d,b,a,c,a,e,h,p,q, q,r,f,c,a,G)

7 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 one Yes – if all actions have same cost Time? Number of nodes in a b-ary tree of depth d: O(b d ) (d is the depth of the optimal solution) Space? O(bd)O(bd)

8 Properties of depth-first search Complete? Fails in infinite-depth spaces, spaces with loops Modify 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(b m ) nodes, m=max depth of any node Terrible if m is much larger than d Space? O(bm), i.e., linear space!

9 Iterative deepening search Use DFS as a subroutine 1.Check the root 2.Do a DFS with depth limit 1 3.If there is no path of length 1, do a DFS search with depth limit 2 4.If there is no path of length 2, do a DFS with depth limit 3. 5.And so on…

10 Iterative deepening search

11

12

13

14 Properties of iterative deepening search Complete? Yes, when b is finite Optimal? Not generally – the shallowest goal node is not necessarily the optimal one Yes – if all actions have same cost Time? (d+1)b 0 + d b 1 + (d-1)b 2 + … + b d = O(b d ) Space? O(bd)

15 Search with varying step costs BFS finds the path with the fewest steps, but does not always find the cheapest path

16 Search with varying step costs BFS finds the path with the fewest steps, but does not always find the cheapest path

17 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

18 Uniform-cost search example

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

20 Uniform-cost search example Expansion order: (S,p,d,b,e,a,r,f,e,G)

21 Uniform-cost search example Expansion order: (S,p,d,b,e,a,r,f,e,G)

22 Uniform-cost search example Expansion order: (S,p,d,b,e,a,r,f,e,G)

23 Uniform-cost search example Expansion order: (S,p,d,b,e,a,r,f,e,G)

24 Another example of uniform-cost search Source: WikipediaWikipedia

25 Optimality of uniform-cost search Graph separation property: every path from the initial state to an unexplored state has to pass through a state on the frontier – Proved inductively Optimality of UCS: proof by contradiction – Suppose UCS terminates at goal state n with path cost 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’ – But because g(n”) ≤ g(n’) < g(n), n” should have been expanded first!

26 Properties of uniform-cost search Complete? Yes, if step costs are 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(b C*/ ε ) This can be greater than O(b d ): the search can explore long paths consisting of small steps before exploring shorter paths consisting of larger steps Space? O(b C*/ ε )

27 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

28 Heuristic function Heuristic function h(n) estimates the cost of reaching goal from node n Example: Start state Goal state

29 Heuristic for the Romania problem

30 Greedy best-first search Expand the node that has the lowest value of the heuristic function h(n)

31 Greedy best-first search example

32

33

34

35 Properties of greedy best-first search Complete? No – can get stuck in loops (with explored set complete in finite spaces) start goal

36

37 Properties of greedy best-first search Complete? No – can get stuck in loops(with explored set complete in finite spaces) Optimal? No

38 Properties of greedy best-first search Complete? No – can get stuck in loops(with explored set complete in finite spaces) Optimal? No Time? Space?

39 How can we fix the greedy problem?

40 Properties of greedy best-first search Complete? No – can get stuck in loops(with explored set complete in finite spaces) Optimal? No Time? Worst case: O(b m ) Can be much better with a good heuristic Space? Worst case: O(b m )

41 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)

42 A * search example

43

44

45

46

47

48 Another example Source: WikipediaWikipedia

49 Uniform cost search vs. A* search Source: WikipediaWikipedia

50 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

51 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!

52 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

53 Properties of A* Complete? Yes – unless there are infinitely many nodes with f(n) ≤ C* Optimal? Yes Time? Number of nodes for which g(n)+h(n) ≤ C* Space? Number of nodes for which g(n)+h(n) ≤ C*

54 Designing heuristic functions Heuristics for the 8-puzzle h 1 (n) = number of misplaced tiles h 2 (n) = total Manhattan distance (number of squares from desired location of each tile) h 1 (start) = 8 h 2 (start) = 3+1+2+2+2+3+3+2 = 18 Are h 1 and h 2 admissible?

55 Dominance If h 1 and h 2 are both admissible heuristics and h 2 (n) ≥ h 1 (n) for all n, (both admissible) then h 2 dominates h 1 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 h 1 will expand more nodes

56 Dominance Typical search costs for the 8-puzzle (average number of nodes expanded for different solution depths): d=12IDS = 3,644,035 nodes A * (h 1 ) = 227 nodes A * (h 2 ) = 73 nodes d=24 IDS ≈ 54,000,000,000 nodes A * (h 1 ) = 39,135 nodes A * (h 2 ) = 1,641 nodes

57 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 h 1 (n) gives the shortest solution If the rules are relaxed so that a tile can move to any adjacent square, then h 2 (n) gives the shortest solution

58 Heuristics from subproblems Let h 3 (n) be the cost of getting a subset of tiles (say, 1,2,3,4) into their correct positions Can pre-compute and save the exact solution cost for every possible sub-problem instance – pattern database

59 Combining heuristics Suppose we have a collection of admissible heuristics h 1 (n), h 2 (n), …, h m (n), but none of them dominates the others How can we combine them? h(n) = max{h 1 (n), h 2 (n), …, h m (n)}

60 Weighted A* search Idea: speed up search at the expense of optimality Take an admissible heuristic, “inflate” it by a multiple α > 1, and then perform A* search as usual Fewer nodes tend to get expanded, but the resulting solution may be suboptimal (its cost will be at most α times the cost of the optimal solution)

61 Example of weighted A* search Heuristic: 5 * Euclidean distance from goal Source: WikipediaWikipedia

62 Example of weighted A* search Heuristic: 5 * Euclidean distance from goal Source: WikipediaWikipedia Compare: Exact A*

63 A* Solving Tile Puzzle

64 A* Playing Mario Mario Playing A* search

65

66

67

68 All search strategies AlgorithmComplete?Optimal? Time complexity Space complexity BFS DFS IDS UCS Greedy A* No Worst case: O(b m ) Yes Yes (if heuristic is admissible) Best case: O(bd) Number of nodes with g(n)+h(n) ≤ C* Yes No Yes If all step costs are equal Yes No O(b d ) O(b m ) O(b d ) O(bm) O(bd) Number of nodes with g(n) ≤ C*

69 Additional pointers Interactive path finding demo Variants of A* for path finding on grids

70 Intro to CSPs

71 Constraint Satisfaction Problems (Chapter 6)

72 What is search for? Assumptions: single agent, deterministic, fully observable, discrete environment Search for planning – The path to the goal is the important thing – Paths have various costs, depths Search for assignment – Assign values to variables while respecting certain constraints – The goal (complete, consistent assignment) is the important thing

73 Constraint satisfaction problems (CSPs) Definition: – X i is a set of variables {X 1,… X n } – D i is a set of domains {D 1,... D n } one for each variable – C is a set of constraints that specify allowable combinations of values – Solution is a complete, consistent assignment

74 Example: Map Coloring Variables: WA, NT, Q, NSW, V, SA, T Domains: {red, green, blue} Constraints: adjacent regions must have different colors e.g., WA ≠ NT, or (WA, NT) in {(red, green), (red, blue), (green, red), (green, blue), (blue, red), (blue, green)}

75 Example: Map Coloring Solutions are complete and consistent assignments, e.g., WA = red, NT = green, Q = red, NSW = green, V = red, SA = blue, T = green

76 Example: n-queens problem Put n queens on an n × n board with no two queens in the same row, column, or diagonal

77 Example: N-Queens Variables: X ij Domains: {0, 1} Constraints:  i,j X ij = N (X ij, X ik )  {(0, 0), (0, 1), (1, 0)} (X ij, X kj )  {(0, 0), (0, 1), (1, 0)} (X ij, X i+k, j+k )  {(0, 0), (0, 1), (1, 0)} (X ij, X i+k, j–k )  {(0, 0), (0, 1), (1, 0)} X ij

78 N-Queens: Alternative formulation Variables: Q i Domains: {1, …, N} Constraints:  i, j non-threatening (Q i, Q j ) Q2Q2 Q1Q1 Q3Q3 Q4Q4

79 Example: Cryptarithmetic Variables: T, W, O, F, U, R X 1, X 2 Domains: {0, 1, 2, …, 9} Constraints: O + O = R + 10 * X 1 W + W + X 1 = U + 10 * X 2 T + T + X 2 = O + 10 * F Alldiff(T, W, O, F, U, R) T ≠ 0, F ≠ 0 X 2 X 1

80 Example: Sudoku Variables: X ij Domains: {1, 2, …, 9} Constraints: Alldiff(X ij in the same unit) X ij


Download ppt "Search (cont) & CSP Intro Tamara Berg CS 560 Artificial Intelligence Many slides throughout the course adapted from Dan Klein, Stuart Russell, Andrew Moore,"

Similar presentations


Ads by Google