Heuristic Search Generate and Test Hill Climbing – Simple Hill Climbing – Steepest Ascend Hill Climbing – Simulated Annealing Best First Search – – – – A* IDA* Beam Search AO* Hill Climbing Problems: – local maxima problem – plateau problem – Ridge goal Ridge goal plateau Simulated Annealing T = initial temperature x = initial guess v = Energy(x) Repeat while T > final temperature – Repeat n times X’ Move(x) V’ = Energy(x’) If v’ < v then accept new x [ x x’ ] Else accept new x with probability exp(-(v’ – v)/T) – T = 0.95T /* for example */ At high temperature, most moves accepted At low temperature, only moves that improve energy are accepted Best first Search BestFirstSearch(state space =<S,P,I,G,W>,f) Open {<f(I),I>} Closed while Open do <wx,x> ExtractMin(Open) if Goal(x, ) then return x Insert(x,Closed) for y Child(x ,) do if yClosed then Insert(<f(y),y>,Open) return fail Open is implemented as a priority queue Best First Search Example: The Eight-puzzle The task is to transform the initial puzzle 2 8 3 1 6 4 7 5 into the goal state 1 2 8 7 3 4 6 5 by shifting numbers up, down, left or right. Best-First (Heuristic) Search 4 5 3 3 3 1 6 4 7 8 3 2 1 6 4 1 7 5 8 3 2 1 4 1 6 5 7 7 3 3 8 3 2 8 3 2 1 4 7 1 4 7 6 5 6 5 8 8 2 2 3 2 4 7 5 8 3 2 8 3 4 1 6 4 7 5 3 2 8 8 4 1 4 6 5 7 6 6 5 to the goal 3 2 1 4 7 6 5 to further unnecessary expansions 5 4 3 5 Best First Search - A* <S,P,I,G,W> - state space h*(x) - a minimum path weight from x to the goal state - heuristic estimate of h*(x) h(x) g*(x) g(x) f*(x) = g*(x) + h*(x) f(x) = g(x) + h(x) - a minimum path weight from I to x - estimate of g*(x) (i.e. minimal weight found as far Search strategies - A* A*Search(state space =<S,P,I,G,W>,h) Open {<h(I),0,I>} Closed while Open do <fx,gx,x> ExtractMin(Open) [minimum for fx] if Goal(x, ) then return x Insert(<fx,gx,x>,Closed) for y Child(x ,) do gy = gx + W(x,y) fy = gy + h(y) if there is no <f,g,y>Closed with ffy then Insert(< fy,gy,y>,Open) [replace existing <f,g,y>, if present] return fail Search strategies - A* Best-First Search (A*) 0+4 1+5 3+3 8 3 1 6 4 7 2 8 3 2 1 6 4 1 7 5 8 3 2 1 4 1 6 5 7 2 2+3 2 7 1+3 2+3 8 3 2 8 3 2 1 4 7 1 4 7 6 5 6 5 2 3 3+4 goal ! 1 8 5+0 7 3+2 3 2 8 3 4 1 6 4 7 5 3 2 8 8 4 1 4 6 5 7 6 2 3 2 3 1 8 4 1 8 4 7 6 5 7 6 5 1 2 3 1 2 3 8 4 7 8 4 6 5 6 5 7 4 6 5 4+1 5 7 8 6 1+5 5 2+4 3+4 5+2 3 5 Admissible search Definition An algorithm is admissible if it is guaranteed to return an optimal solution (with minimal possible path weight from the start state to a goal state) whenever a solution exists. Admissibility What must be true about h for A* to find optimal path? X h=100 1 h=0 2 73 Y h=1 1 h=0 Admissibility What must be true about h for A* to find optimal path? A* finds optimal path if h is admissible; h is admissible when it never overestimates. X h=100 1 h=0 2 73 Y h=1 1 h=0 Admissibility What must be true about h for A* to find optimal path? A* finds optimal path if h is admissible; h is admissible when it never overestimates. In this example, h is not admissible. X h=100 1 2 73 Y h=1 1 h=0 h=0 g(X)+h(X) = 102 g(Y)+h(Y) = 74 Optimal path is not found! Admissibility of a Heuristic Function Definition A heuristic function h is said to be admissible if 0 h(n) h*(n) for all nS. Proof of Optimality of A* Start Let G be an optimal goal state with path cost f* G2 be a sub optimal goal state with path cost g(G2) > f* Suppose n is a leaf node on the optimal path to G n G Because h is admissible, f* f(n) Because n has not been chosen for expansion, f(n) f(G2) Therefore f* f(G2) Because G2 is a goal state, h(G2) = 0, and thus f(G2) = g(G2) Therefore f* g(G2) which contradicts our assumption G2 Extreme Cases If we set h=0, then A* is Breadth First search; h=0 is admissible heuristic (when all costs are non-negative). If g=0 for all nodes, A* is similar to Steepest Ascend Hill Climbing If both g and h =0, it can be as dfs or bfs depending on the structure of Open How to chose a heuristic? Original problem P A set of constraints P is complex Use cost of a best solution path from n in P' as h(n) for P Admissibility: h* h cost of best solution in P >= cost of best solution in P' Relaxed problem P' removing one or more constraints P' becomes simpler Solution space of P Solution space of P' How to chose a heuristic - 8-puzzle Example: 8-puzzle – Constraints: to move from cell A to cell B cond1: there is a tile on A cond2: cell B is empty cond3: A and B are adjacent (horizontally or vertically) – Removing cond2: h2 (sum of Manhattan distances of all misplaced tiles) – Removing cond2 and cond3: h1 (# of misplaced tiles) Here h2 h1 How to chose a heuristic - 8-puzzle h1(start) = 7 h2(start) = 18 Performance comparison Search Cost (nodes) d 2 4 6 8 10 12 14 16 18 20 22 24 IDS 10 112 680 6384 47127 3644035 - A* (h1) 6 13 20 39 93 227 539 1301 3056 7276 18094 39135 Effective Branching Factor A* (h2) 6 12 18 25 39 73 113 211 363 676 1219 1641 IDS A* (h1) A* (h2) 2.45 2.87 2.73 2.80 2.79 2.78 - 1.79 1.48 1.34 1.33 1.38 1.42 1.44 1.45 1.46 1.47 1.48 1.48 1.79 1.45 1.30 1.24 1.22 1.24 1.23 1.25 1.26 1.27 1.28 1.26 Data are averaged over 100 instances of 8-puzzle, for varous solution lengths. Note: there are still better heuristics for the 8-puzzle. Comparing heuristic functions Bad estimates of the remaining distance can cause extra work! Given two algorithms A1 and A2 with admissible heuristics h1 and h2 < h*(n), if h1(n) < h2(n) for all non-goal nodes n, then A1 expands at least as many nodes as A2 h2 is said to be more informed than h1 or, A2 dominates A1 Relaxing optimality requirements A* algorithm Theorem If h is admissible, then A* algorithm is -admissible, i.e. it finds a path with a cost at most (1+ )C*. Note hF does not need to be admissible Relaxing optimality requirements Weighted evaluation function fw(n) = (1–w)g(n) + w h(n) w=0 w = 1/2 w=1 - Breadth First - A* - Best First * IDA : Iterative deepening * A To reduce the memory requirements at the expense of some additional computation time, combine uninformed iterative deepening search with A* Use an f-cost limit instead of a depth limit Iterative Deepening A* In the first iteration, we determine a “f-cost limit” f’(n0) = g’(n0) + h’(n0) = h’(n0), where n0 is the start node. We expand nodes using the depth-first algorithm and backtrack whenever f’(n) for an expanded node n exceeds the cut-off value. If this search does not succeed, determine the lowest f’value among the nodes that were visited but not expanded. Use this f’-value as the new limit value and do another depth-first search. Repeat this procedure until a goal node is found. IDA* Algorithm - Top level function IDA*(problem) returns solution root := Make-Node(Initial-State[problem]) f-limit := f-Cost(root) loop do solution, f-limit := DFS-Contour(root, f-limit) if solution is not null, then return solution if f-limit = infinity, then return failure end IDA* contour expansion function DFS-Countour(node,f-limit) returns solution sequence and new f-limit if f-Cost[node] > f-limit then return (null, f-Cost[node] ) if Goal-Test[problem](State[node]) then return (node,f-limit) for each node s in Successor(node) do solution, new-f := DFS-Contour(s, f-limit) if solution is not null, then return (solution, f-limit) next-f := Min(next-f, new-f); end return (null, next-f) IDA* on a graph example S 11.9 D 12.4 A 13.4 D 16.9 B 13.7 14.0 C 19.9 E A 19.4 E 12.9 16.9 19.7 17.7 13.0 E B B F 14.0 20.0 21.7 17.0 21.0 24.9 25.4 19.0 13.0 D F B F C E A C G 0.0 G 4.0 0.0 C G 3.0 F 0.0 G IDA* trace Level 0: IDA(S, 11.9) Level 1: IDA(S, 11.9) Level 2: IDA(S, 12.4) 11.9 IDA(A, 11.9) 13.4 IDA(D, 11.9) 12.4 IDA(A, 12.4) 13.4 IDA(D, 12.9) IDA(A, 12.9) 19.4 IDA(E, 12.9) 12.9 Level 3: IDA(S, 12.9) IDA(F, 12.9) 13.0 IDA*: Iteration 1, Limit = 4 0+4 1+5 8 3 1 6 4 7 2 8 3 2 1 6 4 1 7 5 8 3 2 1 4 1 6 5 7 2 2+3 2 7 1+3 2+3 7 5 8 3 4 6 5 3 2 8 8 4 1 4 6 5 7 6 2+4 3 5 IDA*: Iteration 2, Limit = 5 0+4 1+5 3+3 8 3 1 6 4 7 5 2 8 3 2 1 6 4 1 7 5 8 3 2 1 4 1 8 4 6 5 7 6 5 2 3 1 8 4 7 6 5 1 2 3 8 4 6 5 2 2+3 2 7 1+3 2+3 8 3 2 8 3 2 1 4 7 1 4 7 6 5 6 5 2 3 3+4 goal ! 1 8 5+0 7 3+2 7 4 6 5 4+1 7 8 3 4 6 5 3