G51IAIIntroduction to AI Heuristic Searches Instructor: Ho Sooi Hock
Outline Defining Heuristic Search g(n), h(n) Best-First Search Greedy vs. A* Implementation Admissibility and Monotonicity Heuristics Relaxed Problems Informedness Effective Branching Factor
Heuristic Search • Has some domain knowledge beyond the problem definition • Usually more efficient than blind searches • Sometimes known as informed search • Heuristic search works by deciding which is the next best node to expand (there is no guarantee that it is the best node)
Best-First Search • Node selected for expansion based on an evaluation function ( f(n)) • greedy search uses the cost estimate from the current position to the goal, known as heuristic function ( h(n)) • A* search uses a combination of the actual cost to reach the current node and the estimated cost to proceed from the current node to the goal ( g(n) + h(n)) • Compare this with uniform cost search which chooses the lowest cost node thus far ( g(n) )
Heuristic Search - Example Start Finish
Heuristic Search - Example SLD: Straight line distance (as birds fly) between a given city and Bucharest
Greedy Search The 1st node to be selected from the generated nodes is Sibiu because it is closer to Bucharest than either Zerind or Timisoara. h=380 Zerind Oradeo h=374 Fagaras h=366 Arad Sibiu h=176 h=253 Rimnicu Vilcea Timisoara Bucharest h=329 h=0 h=193
Greedy Search Notice that : It finds solution without ever expanding a node that is NOT on the solution path. The solution path is not optimal Arad Sibiu Rimnicu Vilcea Pitesti Bucharest with path cost of (140+80+97+101 = 418) is 32km LESS than Arad Sibiu Fagaras Bucharest (path cost = 140+99+211 = 450)
Greedy Search • It is only concerned with short term gains • It is possible to get stuck in an infinite loop (consider being in Iasi and trying to get to Fagaras) unless mechanism for avoiding repeated states is in place • It is not optimal • It is not complete Time and space complexity is O(Bm); where m is the depth of the search tree
Observations Uniform cost search (UCS) using g(n) is OPTIMAL and COMPLETE UCS is a special case of breath-first search, therefore it is INEFFICIENT Greedy search behaves like depth-first search as it prefers to follow a single path [guided by h(n)] all the way to the goal. It is thus EFFICIENT but it is NOT optimal NOR complete The A* (A Star) search combines the above two strategies to get both advantages, using the evaluation function f(n) = g(n) + h(n)
A* Search • Combines the cost so far and the estimated cost to the goal, i.e. f(n) = g(n) + h(n). This gives an estimated cost of the cheapest solution through n • It can be proved to be optimal and complete providing that the heuristic function is admissible, i.e. h(n) never overestimate the cost to reach the goal
A* Search - Admissibility Is the heuristic SLD ADMISSIBLE? 28km Serdang KL 18km 35km Serdang-KL Kommuter - 28 km Serdang-KL highway - 35 km Straight Line Distance as the birds fly from Serdang to KL - 18 km
A* Search - Example Oradea f = 291+380 = 671 Zerind f = 75+374 = 449 Fagaras f = 239+176 = 415 Sibiu Arad Bucharest f = 140+253 = 393 Rimnicu f = 0+366 = 366 f = 450+0 = 450 f = 220+193 = 413 Craiova Bucharest Timiosara Pitesti f = 366+160 = 526 f = 317+100 = 417 f = 418+0 = 418 f = 118+329 = 447 Craiova f = 455+160 = 615
General Implementation • Implementation is achieved by sorting the nodes based on the evaluation function, f(n) Function BEST-FIRST-SEARCH(problem, EVAL-FN) returns a solution sequence Inputs : problem, a problem Eval-Fn, an evaluation function Queueing-Fn <= a function that orders nodes by EVAL-FN Return GENERAL-SEARCH(problem, Queueing-Fn)
Specific Implementation function GREEDY-SEARCH(problem) returns a solution or failure return BEST-FIRST-SEARCH(problem, h) function A*-SEARCH (problem) returns a solution or failure return BEST-FIRST-SEARCH(problem, g+h)
Admissibility Theorem: If h(n) is admissible, A* using TREE-SEARCH is optimal. Suppose a suboptimal goal node, G2 has been generated and is in the fringe. Let n be an unexpanded node in the fringe such that n is on a shortest path to an optimal goal G. f(G) = g(G)+h(G) = g(G) < g(G2) < g(G2)+h(G2) = f(G2) If h(n) is admissible, then f(n)=g(n)+h(n) ≤ g(G) = g(G)+h(G) = f(G) f(n)≤f(G) <f(G2)and so A* will never select G2 for expansion. Hence A* always return an optimal solution.
Monotonicity (Consistency) Theorem: If h(n) is consistent, A* using GRAPH-SEARCH is optimal A heuristic h(n) is consistent if h(n) ≤ c(n, a, n’) + h(n’) If h(n) is consistent, then the values of f(n) along any path are non-decreasing. g(n’) = g(n) + c(n, a, n’) f(n’) =g(n’)+h(n’) = g(n)+c(n, a, n’) + h(n’) ≥ g(n)+h(n) = f(n) Hence the first goal node selected for expansion must be an optimal solution.
Optimality of A* Search • A* expands nodes in order of increasing f value • Gradually adds "f-contours" of nodes • Contour i has all nodes with f = fi, where fi < fi+1
Monotonicity and Admissibility Any monotonic heuristics is also admissible This argument considers any path in the search space as a sequence of states s1, s2,……sg, where s1 is that start state and sg is the goal. For a sequence of moves in this arbitrarily selected path, monotonicity dictates that: s1 to s2 h(s1) – h(s2) ≤ c(s1, a, s2) s2 to s3 h(s2) – h(s3) ≤ c(s2, a, s3) s3 to s4 h(s3) – h(s4) ≤ c(s3, a, s4) …. …. sg-1 to sg h(sg-1) – h(sg) ≤ c(sg-1, sg) Summing each column and using the monotone property of h(sg) = 0 Path s1 to sg h(s1) ≤ g(sg)
Heuristic Searches - Example Initial State Goal State
Heuristic Searches A* Algorithm Typical solution is about twenty steps Branching factor is approximately three. Therefore a complete search would need to search 320 states. But by keeping track of repeated states we would only need to search 9! (362,880) states But even this is a lot (imagine having all these in memory) Our aim is to develop a heuristic that does not overestimate (it is admissible) so that we can use A* to find the optimal solution
A* AlgorithmPossible Heuristics h1 = the number of tiles that are in the wrong position (=7) h2 = the sum of the distances of the tiles from their goal positions using the Manhattan Distance (=18) Both are admissible but which one is better?
Informedness For two A* heuristics h1 and h2, if h1(n) <= h2(n), for all states n in the search space, we say h2 dominates h1 or heuristic h2 is more informed than h1. Domination translate to efficiency: A* using h2 will never expand more nodes than A* using h1. Hence it is always better to use a heuristic function with higher values, provided it does not over-estimate and that the computation time for the heuristic is not too large
Generating Heuristics with 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
Test From 100 Runs with Varying Solution Depths h2 looks better as fewer nodes are expanded. But why?
Effective Branching Factor • Effective branching factor: average number of branches expanded • h2 has a lower branching factor and so fewer nodes are expanded • Therefore, one way to measure the quality of a heuristic is to find its average branching factor • h2 has a lower EBF and is therefore the better heuristic
Summary • Heuristic search • Characteristics • h(n), g(n) • Best-first-search • Greedy-search • A* • Conditions for Optimality • Heuristics • Informedness & EBF • Read chapter 4 in AIMA book
Acknowledgements Most of the lecture slides are adapted from the same module taught in Nottingham UK by Professor Graham Kendall, Dr. Rong Qu and Dr. Andrew Parker