ICS-381 Principles of Artificial Intelligence Week 5 Informed Search Algorithms Chapter 4 Dr.Tarek Helmy El-Basuny Dr. Tarek Helmy, ICS-KFUPM 1 Last Time: Summary Searching Strategies Evaluating criteria Uninformed Search Strategies: There is no information about the path cost. 1. Breadth-First Search 2. Uniform-Cost Search 3. Depth-First Search 4. Depth-Limited Search 5. Iterative Deepening Search 6. BI-directional Search Dr. Tarek Helmy, ICS-KFUPM 2 Assignment # 2: Programming Java implementation of Uninformed Search algorithms [1- 5]. Your code should be error free and runable. 1. Breadth-First Search 2. Uniform-Cost Search 3. Depth-First Search 4. Depth-Limited Search 5. Iterative Deepening Search Maximum Grade is 2.5 Points/100. Submission through WebCT Due date is: midnight on Wed. November 7th. Dr. Tarek Helmy, ICS-KFUPM 3 Why: Heuristic/informed Search Algorithms With blind/uninformed search: BFS, DFS, Uniform Cost, DLDF, DIDF:  No notion concept of the “right direction”  Can only recognize goal once it’s achieved  Too resource (both time and space) demanding for real complex problems. Instead we turn to “heuristic/informed search” methods, which don’t search the whole search space, but focus on promising states.  We have rough/estimated idea of how good various states are, and use this knowledge to guide our search. Heuristic searches estimate the cost to the goal from its current position.  h(n) = estimated minimal path cost from n to a goal state.  If n is goal then h(n)=0 Implementation is achieved by sorting the nodes based on the evaluation function; h(n). Our goal is not to minimize the distance from the current head of our path to the goal, we want to minimize the overall path cost to the goal! We kept looking at nodes closer and closer to the goal, but were accumulating costs as we got further from the initial state (g(n)). Dr. Tarek Helmy, ICS-KFUPM 4 Topics to be Covered in this Chapter Informed/heuristic search strategies: There is an information about the path cost.  Best-First Search Greedy Best-First Search A* Search  Hill-Climbing Search  Simulated Annealing Search  Local Beam Search  Genetic Algorithms Dr. Tarek Helmy, ICS-KFUPM 5 Heuristic/Informed Search • A Heuristic is a function that, when applied to a state, returns a number that tells us approximately how far the state is from the goal state. • Note we said “approximately”. Heuristics might underestimate or overestimate the merit of a state. • Heuristics that only underestimate are very desirable, and are called admissible. Dr. Tarek Helmy, ICS-KFUPM 6 Admissible Heuristics 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. An admissible heuristic never overestimates the cost to reach the goal, i.e., will make the algorithm optimal. Theorem: If h(n) is admissible, A* using TREE-SEARCH is optimal Root n g(n) : cost of path h(n) : Heuristic (expected) minimum cost to goal h*(n): true minimum cost to goal Goal Dr. Tarek Helmy, ICS-KFUPM 7 Dominance Heuristics If h2(n) ≥ h1(n) for all n (both admissible) then h2 dominates h1 h2 is better for search: it is guaranteed to expand less nodes. Typical search costs = average number of nodes expanded, i.e. Start h4 h1 Dr. Tarek Helmy, ICS-KFUPM h3 Goal h2 8 Example: Heuristics for 8-Puzzle I Current State • h(n) is the number of misplaced tiles (not including the blank) Goal State 1 4 7 2 5 1 4 7 2 5 8 3 6 8 3 6 • In this case, only “8” is misplaced, so the heuristic function evaluates to 1. • The heuristic is telling us, that I think a solution might be available in just 1 more move. Notation: h(n) Dr. Tarek Helmy, ICS-KFUPM 11 22 33 44 55 66 77 8 8 h(current state) = 1 9 N N N N N Y N N Example: Heuristics for 8-Puzzle II Current State • The Manhattan Distance (not including the blank) Goal State • • 3 4 7 1 4 7 2 5 1 2 5 8 8 6 3 3 2 spaces 8 3 6 In this case, only the “3”, “8” and “1” tiles are misplaced, by 2, 3, and 3 squares respectively, so the heuristic function evaluates to 8. 3 spaces 8 1 3 spaces 1 In other words, the heuristic is telling us, that it thinks a solution is available in just 8 more moves. Notation: h(n) Dr. Tarek Helmy, ICS-KFUPM Total 8 h(current state) = 8 10 Best First Search General approach of informed search: It is an improved combination of breadthfirst and depth-first algorithms.  Best-first search: node is selected for expansion based on an evaluation function f(n) that measures distance to the goal. Chooses the best node from the queue to continue the search regardless of the node's actual position in the problem graph. 1. The main steps of this algorithm are as follow: 1. Add the initial node (starting point) to the queue 2. Compare the front node to the goal state. If they match then the solution is found. 3. If they do not match then expand the front node by adding all the nodes from its links to a queue. 4. If all nodes in the queue are expanded then the goal state is not found (e.g. there is no solution). Stop. 5. Apply the heuristic function to evaluate and reorder the nodes in the queue. 6. Go to step 2 Special cases:  Greedy best first search  A* search Dr. Tarek Helmy, ICS-KFUPM 11 Best First Search Algorithm • • • • • Open list of nodes reached but not yet expanded Closed list of nodes that have been expanded Choose lowest cost node on Open list Add it to Closed, add its successors to Open Stop when Goal is first removed from Open start put s in OPEN, compute h(s) yes Fail OPEN empty ? Remove the node of OPEN whose h(s) value is smallest and put it in CLOSE (call it n) n = goal ? yes Success Expand n. calculate h(n) of successor Put successors to OPEN pointers back to n Dr. Tarek Helmy, ICS-KFUPM 12 Route Finding 374 366 253 329 Dr. Tarek Helmy, ICS-KFUPM 13 Greedy Best First Search Expands the node that appears Romania with step costs in km to be the closest one to the goal Idea: Use an evaluation function f(n) = h(n) Usually it is very difficult to compute the exact distance from the goal state. Therefore, a heuristic function is used to estimate of cost from n to goal.  e.g., hSLD(n) = straight-line distance from n to Bucharest. Attempts to predict how close the end of a path is to a solution. Paths which are judged to be closer to a solution are extended first. Dr. Tarek Helmy, ICS-KFUPM 14 Example: Greedy Best-First Search Dr. Tarek Helmy, ICS-KFUPM 15 Example: Greedy Best-First Search The first expansion step produces:  Sibiu, Timisoara and Zerind Greedy best-first will select Sibiu. Dr. Tarek Helmy, ICS-KFUPM 16 Example: Greedy Best-First Search If Sibiu is expanded we get:  Arad, Fagaras, Oradea and Rimnicu Vilcea Greedy best-first search will select: Fagaras Dr. Tarek Helmy, ICS-KFUPM 17 Example: Greedy Best-First Search If Fagaras is expanded we get:  Sibiu and Bucharest Goal reached !!  Yet not optimal (see Arad, Sibiu, Rimnicu Vilcea, Pitesti) Dr. Tarek Helmy, ICS-KFUPM 18 Greedy/Best First Search Algorithm Implementation Use a priority queue, where nodes with best scores are taken off the queue first. While queue not empty and not found do:  Remove the BEST node N from queue.  If N is a goal state, then found = TRUE.  Find all the successor nodes of N, assign them a score, and put them on the queue. Dr. Tarek Helmy, ICS-KFUPM 19 ICS-381 Principles of Artificial Intelligence Week 5 Informed Search Algorithms Chapter 4 Dr.Tarek Helmy El-Basuny Dr. Tarek Helmy, ICS-KFUPM 20 Last Time We presented:  Why do we need Informed/heuristic search strategies?  A heuristic function will estimate the cost to the goal from the current node to the goal node.  Characteristics of the heuristic function (Admissible, dominance). Greedy Best-First Search We are going to present Greedy Best-First Search Example (8-Puzzle) A* Searching algorithm Improving the performance of A* Dr. Tarek Helmy, ICS-KFUPM 21 8-Puzzle Example: Greedy Best-First Search f(N) = h(N) = number of misplaced tiles (not including the blank) 3 5 3 4 3 4 Initial State 2 4 2 3 1 3 0 4 5 Dr. Tarek Helmy, ICS-KFUPM Goal 4 22 8-Puzzle Example: Greedy Best-First Search f(N) = h(N) = Σ distances of tiles to goal 6 5 Initial State 2 5 2 4 1 3 0 4 6 Dr. Tarek Helmy, ICS-KFUPM Goal 5 23 Evaluating: Greedy Best First Search Algorithm Complete:  No , can get stuck in loops,  Yes, in finite space with repeated-state checking Time: O(bm), but a good heuristic can give dramatic improvement  m : the maximum depth of the search space  b: # of branches at each node Space: O(bm) -- keeps all nodes in memory Optimal: No Dr. Tarek Helmy, ICS-KFUPM 24 A* Search Start node A* search is similar to Greedy best-first search with the n0 difference that: A* search includes in its evaluation function the cost from the g(n) start node to the current node, in addition to the estimated cost from the current node to the goal. Evaluation function: f(n) = g(n) + h(n), where: Current Node n  g(n): Cost so far to reach n (Uniform cost search minimizes it)  h(n): Estimated cost to goal from n (best-first search minimizes it) h(n)  f(n): Estimated total cost of path from the starting node n0 through n to the goal (best estimated cost for complete solution) Using current cost and estimated cost give this algorithm completeness (if the solution exists, it will be found) and optimality (it will find the best solution). Goal All these advantages made A* algorithm the most popular search algorithm in AI applications. Dr. Tarek Helmy, ICS-KFUPM 25 8-Puzzle Example: A* Search f(n) = g(n) + h(n): with h(n) = number of misplaced tiles (not including the blank) 3+3 1+5 2+3 3+4 5+2 0+4 3+2 1+3 4+1 2+3 5+0 3+4 1+5 Dr. Tarek Helmy, ICS-KFUPM 2+4 26 Romania Map Example: A* Search Romania with step costs in km Dr. Tarek Helmy, ICS-KFUPM 27 A* Search Example Expand Arrad and determine f(n) for each node  f(Sibiu)=g(Arad, Sibiu) + h(Sibiu)=140+253=393  f(Timisoara)=g(Arad, Timisoara) + h(Timisoara)=118+329=447  f(Zerind)=g(Arad, Zerind) + h(Zerind)=75+374=449 Best choice is Sibiu Dr. Tarek Helmy, ICS-KFUPM 28 A* Search Example Expand Rimnicu Vilcea and determine f(n) for each node  f(Craiova)=g(Rimnicu Vilcea, Craiova)+h(Craiova)=360+160=526  f(Pitesti)=g(Rimnicu Vilcea, Pitesti)+h(Pitesti)=317+100=417  f(Sibiu)=g(Rimnicu Vilcea,Sibiu)+h(Sibiu)=300+253=553 Best choice is Pitesti Dr. Tarek Helmy, ICS-KFUPM 29 A* Search Example Expand Fagaras and determine f(n) for each node  f(Sibiu)=g(Fagaras, Sibiu)+h(Sibiu)=338+253=591  f(Bucharest)=g(Fagaras,Bucharest)+h(Bucharest)=450+0=450 Dr. Tarek Helmy, ICS-KFUPM 30 A* Search Example Expand Sibiu and determine f(n) for each node  f(Arad)=g(Sibiu,Arad)+h(Arad)=280+366=646  f(Fagaras)=g(Sibiu,Fagaras)+h(Fagaras)=239+179=415  f(Oradea)=g(Sibiu,Oradea)+h(Oradea)=291+380=671  f(Rimnicu Vilcea)=g(Sibiu,Rimnicu Vilcea)+h(Rimnicu Vilcea)=220+192=413 Best choice is Rimnicu Vilcea Dr. Tarek Helmy, ICS-KFUPM 31 A* Search Example Expand Fagaras and determine f(n) for each node  f(Sibiu)=g(Fagaras, Sibiu)+h(Sibiu)=338+253=591  f(Bucharest)=g(Fagaras,Bucharest)+h(Bucharest)=450+0=450 Dr. Tarek Helmy, ICS-KFUPM 32 A* Search Example Expand Pitesti and determine f(n) for each node  f(Bucharest)=g(Pitesti,Bucharest)+h(Bucharest)=418+0=418 Best choice is Bucharest !!!  Optimal solution (only if h(n) is admissible)  Note values along optimal path !! Dr. Tarek Helmy, ICS-KFUPM 33 A* Search Properties Complete: Yes (unless there are infinitely many nodes with f ≤ f(g) )  Terminates to produce best solution Yes Complexity  Time: Depends on h, can be exponential  Memory: O(bm), stores all nodes Optimal:  Optimality of A* (proof very important) Dr. Tarek Helmy, ICS-KFUPM 34 Optimality of A* (proof) Suppose some suboptimal goal 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 G1. start n G2 G1 Suboptimal Goal if f(n) < f(G2) then A* will prefer n over G2 f(G) = g(G) since h(G) = 0 f(G2) = g(G2) since h(G2) = 0 g(G2) > g(G1) since G2 is suboptimal f(G2) > f(G1) from above Dr. Tarek Helmy, ICS-KFUPM 35 Optimality of A* (proof) Suppose some suboptimal goal 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 G1. start n G2 G1 Suboptimal Goal f(G2) > f(G1) copied from last slide h(n) ≤ h*(n) since h is admissible (under-estimate) g(n) + h(n) ≤ g(n) + h*(n) from above f(n) ≤ f(G1) since g(n)+h(n)=f(n) & g(n)+h*(n)=f(G) f(n) < f(G2) means it is the optimal solution. Dr. Tarek Helmy, ICS-KFUPM 36 Optimality of A* A* expands nodes in order of increasing f value Gradually adds "f-contours" of nodes Contour i contains all nodes with f≤fi where fi < fi+1 Dr. Tarek Helmy, ICS-KFUPM 37 f Contour Function UCS f Contour: not optimal A* f Contour: optimal Start Goal Start Goal Goal Start Greedy’s BFS f Contour: not optimal Dr. Tarek Helmy, ICS-KFUPM 38 Consistent Heuristics A heuristic is consistent if for every node n, every successor n' of n generated by any action a, then we have h(n) ≤ c(n,a,n') + h(n'). This is a form of triangular inequality: If h is consistent, we have f(n') = g(n') + h(n') = g(n) + c(n,a,n') + h(n') ≥ g(n) + h(n) = f(n) f(n’) ≥ f(n) When a heuristic is consistent, the values of f(n) along any path are non-decreasing. keeps all checked nodes in memory Theorem: to avoid repeated states If h(n) is consistent, A* using GRAPH-SEARCH is optimal Consistent heuristics are admissible. Not all admissible heuristics are consistent. Dr. Tarek Helmy, ICS-KFUPM 39 Pruning The Search Tree In A* search, if h is too big (means dominates another h), it will prevent the node (and its successors, grand-successors, etc.) from ever being expanded. This is called “pruning” (like removing branches from a tree) Pruning the tree reduces the search complexity exponential.  Only if a good heuristic is available Dr. Tarek Helmy, ICS-KFUPM 40 The End!! Thank you Any Questions? Dr. Tarek Helmy, ICS-KFUPM 41