State Space Heuristic Search Three Algorithms Backtrack Depth First Breadth First All work if we have well-defined: Goal state Start state State transition rules But could take a long time Heuristic An informed guess that guides search through a state space Can result in a suboptimal solution Blocks World: A Stack of Blocks Start Goal A D B C C B D A Two rules: clear(X) on(X, table) clear(X) ^ clear(Y) on(X,Y) All Three Algorithms will find a solution Partial Look At Search Space A B C D B C DA C BAD BC AD Etc. Heuristic 1 1. 2. For each block that is resting where it should, subtract 1 For each block that is not resting where it should, add 1 Hill Climbing 1. 2. At every level, generate all children Continue down path with lowest score Define three functions: f(n) = g(n) + h(n) Where: h(n) is the heuristic estimate for n--guides the search g(n) is path length from start to current node— ensures that we choose node closest to root when more than 1 have same h value Problem: heuristic is local Given C B A D and CB DA At level n The f(n) of each structure is the same f(n) = g(n) = (1+1-1-1) = g(n) But which is actually better The vertical structure must be undone entirely C B A D B A DC A DCB D C B A For a total of 6 moves ABCD B ACD C B AD But CB C D DA B C AD B A 2 moves Task Design a global heuristic that takes the entire structure into account 1. Subtract 1 for each block that has correct support structure 2. Add 1 for each block in an incorrect support structure f(n) = g(n) + (3+2+1+0) =g(n) + 6 C B A goal D D C B A CB f(n) = g(n) + (1 + 0 -1+ 0) = g(n) DA So the heuristic correctly chose the second structure Leads to a New Algorithm: Best First The Road Not Taken Best first Keeps nodes on open in a priority queue ordered by h(n) so that if it goes down a bad path that at first looks good, it can retry a new path Contains algorithm for generating h(n) Nodes could contain backward pointers so that path back to root can be recovered list best_first(Start) { open = [start], closed = []; while (!open.isEmpty()) { cs = open.serve(); if (cs == goal) return path; generate children of cs; for each child { case: { child is on open; //node has been reached by a shorter path if (g(child) < g(child) on open) g(child on open) = g(child); break; child is on closed; if (g(child < g(child on closed)) { //node has been reached by a shorter path and is more attractive remove state from closed; open.enqueue(child); } break; } default: { f(child) = g(child) + h(child);//child has been examined yet open.enqueue(child); } } closed.enqueue(cs);//all cs’ children have been examined. open.reorder);//reorder queue because case statement may have affected ordering } return([]); //failure State Space of a Hypothetical Search Next Slide Goal: P States with attached evaluations are those generated by best-first States expanded by best-first are indicated in bold Just before evaluating O, closed contains HCBA Just before evaluating O, open contains OPGEFD Admissibility A search algorithm is admissible if it finds the optimal path whenever one exists BF is admissible DF is not admissible Suppose on the previous slide Node S were replaced by a P The optimal path is ACHP But DF discovers ABEKP Algorithm A Uses Best First f(n) = g(n) + h(n) f* f*(n) = g*(n) + h*(n) Where g*(n) is the cost of the shortest path from start to n h*(n) is the cost of the shortest path from n to goal So, f*(n) is the actual cost of the optimal path Consider g*(n) g(n) – actual cost to n g*(n) – shortest path from start to n So g(n) >= g*(n) When g*(n) = g(n), the search has discovered the optimal path to n Consider h*(n) We can’t know it unless exhaustive search is possible and we’ve already searched the state space But we can know sometimes if a given h-1(n) is bounded above by some h-2(n) 8-puzzle Example 283 123 164 -> 8 4 7 5 765 h-1(n) number of tiles not in goal position = 5 (1,2,6,8, B) H-2(n) number of moves required to move them to goal (T1 = 1, T2 = 1, T6 = 1, T8 = 2, TB = 1) =6 So h-1(n) <= h-2(n) h-2(n) cannot exceed h*(n) because each tile has to be moved a certain distance to reach goal no matter what. h-2(n) could equal h*(n) h-1(n) is certainly <= h-2(n) which requires moving each incorrect tile at least as far as h-1(n) So, h-1(n) <= h-2(n) <=h*(n) Leads to a Definition A* If algorithm A uses a heuristic that returns a value h(n) <= h*(n) for all n, then it is called A* Claim All A* algorithms are admissible Suppose: 1. h(n) = 0 and so <= h*(n) Search will be controlled by g(n) If g(n) = 0, search will be random If g(n) is the actual cost to n, f(n) becomes BF because the sole reason for examining a node is its distance from start. We already know that this terminates in an optimal solution 2. h(n) = h*(n) Then the algorithm will go directly to the goal since h*(n) computes the shortest path to the goal Therefore, if our algorithm is between these two extremes, our search will always result in an optimal solution Call h(n) = 0, h’(n) So, for any h such that h’(n) <= h(n) <= h*(n) we will always find an optimal solution The closer our algorithm is to h’(n), the more extraneous nodes we’ll have to examine along the way Informedness For any two A* heuristics, h-a, h-b If h-a(n) <= h-b(n), h-b(n) is more informed. Comparison h-a is BF h-b is the # of tiles out of place Since h-a is 0, h-b is better informed than h-a P. 149 Comparison of two solutions that discover the optimal path to the goal state: 1. BF: h(n) = 0 2. h(n) = number of tiles out of place The better informed solution examines less extraneous information on its path to the goal