HW #1 DUE 29/9/2008 Write Java Applet to solve Goats and Cabbage “Missionaries and cannibals” problem with the following search algorithms: Breadth first Iterative deepening Your applet should have graphical interface to illustrate the movements of goats and cabbage across the river. Also, print the following statistics at the end of the search: Maximum depth. Time complexity (time and number of nodes) Space complexity. 1 BLIND SEARCH Depth-first search and breadth-first search are examples of blind (or uninformed) search strategies. Breadth-first search produces an optimal solution (eventually, and if one exists), but it still searches blindly through the state-space. Neither uses any knowledge about the specific domain in question to search through the state-space in a more directed manner. If the search space is big, blind search can simply take too long to be practical, or can significantly limit how deep we're able to look into the space. A search strategy is defined by picking the order of node 2 expansion INFORMED SEARCH - OVERVIEW Heuristic Search Best-First Search Approach Greedy A* Heuristic Functions Local Search and Optimization Hill-climbing Simulated Annealing Local Beam Genetic Algorithms 3 INFORMED SEARCH Relies on additional knowledge about the problem or domain Used to distinguish more promising paths towards a goal frequently expressed through heuristics. may be mislead, depending on the quality of the heuristic In general, performs much better than uninformed search but frequently still exponential in time and space for realistic problems 4 INFORMED SEARCH (CONT.) Find a solution more quickly Find solutions even when there is limited time available Often find a better solution, since more profitable parts of the state-space can be examined, while ignoring the unprofitable parts. A search strategy which is better than another at identifying the most promising branches of a searchspace is said to be more informed. 5 BEST-FIRST SEARCH Relies on an evaluation function f(n) that gives an indication of how useful it would be to expand a node family of search methods with various evaluation functions usually gives an estimate of the distance to the goal often referred to as heuristics in this context The node with the lowest value is expanded first Fringe is ordered by f(n) the name is a little misleading: the node with the lowest value for the evaluation function is not necessarily one that is on an optimal path to a goal function BEST-FIRST-SEARCH(problem, EVAL-FN) returns solution fringe := queue with nodes ordered by EVAL-FN return TREE-SEARCH(problem, fringe) 6 GREEDY BEST-FIRST SEARCH Minimizes the estimated cost to a goal expand the node that seems to be closest to a goal utilizes a heuristic function as evaluation function f(n) = h(n) = estimated cost from the current node to a goal heuristic functions are problem-specific e.g., fSLD(n) = straight-line distance often straight-line distance for route-finding and similar problems often better than depth-first. function GREEDY-SEARCH(problem) returns solution return BEST-FIRST-SEARCH(problem, h) Completeness Time Complexity Space Complexity Optimality no bm bm no b: branching factor, d: depth of the solution, m: maximum depth of the search tree, l: depth limit 7 GREEDY BEST-FIRST SEARCH SNAPSHOT 1 7 2 4 6 3 5 Initial Visited Fringe Current Visible Goal 9 5 6 7 5 6 7 Heuristics 7 8 7 9 5 10 4 11 3 2 12 13 4 14 5 15 6 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Fringe: [13(4), 7(6), 8(7)] + [24(0), 25(1)] 8 ROUTE FINDING 374 366 253 329 9 GREEDY BEST-FIRST SEARCH EXAMPLE 10 GREEDY BEST-FIRST SEARCH EXAMPLE 11 GREEDY BEST-FIRST SEARCH EXAMPLE 12 GREEDY BEST-FIRST SEARCH EXAMPLE So is Arad->Sibiu-> Fagaras->Bucharest optimal? 13 GREEDY SEARCH, EVALUATION Completeness: NO Check on repeated states Minimizing h(n) can result in false starts, e.g. Iasi to Fagaras. GREEDY SEARCH, EVALUATION Cost Max An obvious problem with greedy search is that it doesn't take account of the cost so far, so it isn't optimal, and can wander into dead-ends, like depthfirst search. local minimum = looping 0 Initial State Goal 15 A* SEARCH Combines greedy and uniform-cost search to find the (estimated) cheapest path through the current node f(n) = g(n) + h(n) = path cost + estimated cost to the goal from n heuristics must be admissible h(n) never overestimate the cost to reach the goal very good search method, but with complexity problems function A*-SEARCH(problem) returns solution return BEST-FIRST-SEARCH(problem, g+h) Completeness Time Complexity Space Complexity Optimality yes bd bd yes b: branching factor, d: depth of the solution, m: maximum depth of the search tree, l: depth limit 16 A* SEARCH Evaluation Function: F(n) = g(n) + h(n) Path cost from root to node n Estimated cost of cheapest path from node n to goal 17 A* SNAPSHOT 1 9 9 4 3 11 10 7 2 3 7 2 7 2 4 10 4 6 5 Initial Visited Fringe Current Visible Goal 5 6 13 5 6 7 Edge Cost 2 5 4 4 4 3 11 8 3 7 4 9 7 5 10 2 4 4 11 8 6 3 2 12 5 4 4 13 4 9 9 Heuristics 7 12 3 13 6 14 5 15 2 8 3 6 f-cost 9 10 2 13 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Fringe: [2(4+7), 7(3+4+6), 13(3+2+3+4)] + [24(3+2+4+4+0), 25(3+2+4+3+1)] 18 A* SNAPSHOT WITH ALL F-COSTS 4 3 11 10 7 2 3 7 2 17 5 7 2 11 6 4 Initial Visited Fringe Current Visible Goal 9 1 4 10 5 6 13 5 6 7 Edge Cost 2 20 8 3 24 24 4 21 7 4 5 9 7 29 23 4 14 5 10 2 4 18 4 13 4 11 8 19 6 18 3 11 2 12 5 3 4 12 4 13 3 16 13 6 4 14 2 13 14 13 9 9 18 22 5 6 15 8 3 Heuristics 7 f-cost 9 10 2 25 21 31 25 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 19 A* PROPERTIES The value of f never decreases along any path starting from the initial node This property can be used to draw contours regions where the f-cost is below a certain threshold the better the heuristics h, the narrower the contour around the optimal path 20 A* SNAPSHOT WITH CONTOUR F=11 4 3 11 10 7 2 3 7 2 17 5 7 2 11 6 4 Initial Visited Fringe Current Visible Goal 9 1 4 10 5 6 13 5 6 7 Edge Cost 2 20 8 3 24 24 4 21 7 4 5 9 7 29 23 4 14 5 10 2 4 18 4 13 4 11 8 19 6 18 3 11 2 12 5 3 4 12 4 13 3 16 13 6 4 14 2 13 14 13 9 9 18 22 5 6 15 8 3 Heuristics 7 f-cost 9 10 2 25 21 31 25 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Contour 21 A* SNAPSHOT WITH CONTOUR F=13 4 3 11 10 7 2 3 7 2 17 5 7 2 11 6 4 Initial Visited Fringe Current Visible Goal 9 1 4 10 5 6 13 5 6 7 Edge Cost 2 20 8 3 24 24 4 21 7 4 5 9 7 29 23 4 14 5 10 2 4 18 4 13 4 11 8 19 6 18 3 11 2 12 5 3 4 12 4 13 3 16 13 6 4 2 13 8 7 6 5 4 3 2 1 0 1 16 17 18 19 20 21 22 23 24 25 14 13 14 9 9 18 22 5 6 15 8 3 Heuristics 7 f-cost 9 10 2 25 21 31 25 3 4 5 6 7 27 28 29 30 31 Contour 2 26 22 A* SEARCH EXAMPLE 23 A* SEARCH EXAMPLE 24 A* SEARCH EXAMPLE 25 A* SEARCH EXAMPLE 26 A* SEARCH EXAMPLE 27 A* SEARCH EXAMPLE 28 A* SEARCH A* expands nodes in increasing f value Gradually adds f-contours of nodes 29 OPTIMALITY OF A* A* will find the optimal solution A* is optimally efficient the first solution found is the optimal one no other algorithm is guaranteed to expand fewer nodes than A* A* is not always “the best” algorithm optimality refers to the expansion of nodes other criteria might be more relevant it generates and keeps all nodes in memory improved in variations of A* 30 COMPLEXITY OF A* The number of nodes within the goal contour search space is still exponential with respect to the length of the solution better than other algorithms, but still problematic Frequently, space complexity is more severe than time complexity A* keeps all generated nodes in memory 31 MEMORY-BOUNDED SEARCH Search algorithms that try to conserve memory Most are modifications of A* iterative deepening A* (IDA*) simplified memory-bounded A* (SMA*) recursive best-first search (RBFS) 32 ITERATIVE DEEPENING A* (IDA*) Explores paths within a given contour (f-cost limit) in a depth-first manner this saves memory space because depth-first keeps only the current path in memory but it results in repeated computation of earlier contours since it doesn’t remember its history was the “best” search algorithm for many practical problems for some time does have problems with difficult domains 33 SIMPLIFIED MEMORY-BOUNDED A* (SMA*) Uses all available memory for the search drops nodes from the queue when it runs out of space those with the highest f-costs If there is a tie (equal f-values) we first delete the oldest nodes first. complete if there is enough memory for the shortest solution path often better than A* and IDA* but some problems are still too tough trade-off between time and space requirements 34 HEURISTICS FOR SEARCHING For many tasks, a good heuristic is the key to finding a solution prune the search space move towards the goal 37 HEURISTIC FUNCTIONS 8-PUZZLE Example: 8-Puzzle Average solution cost for a random puzzle is 22 moves Branching factor is about 3 Empty tile in the middle -> four moves Empty tile on the edge -> three moves Empty tile in corner -> two moves 322 is approx 3.1e10 Get rid of repeated states 181440 distinct states 38 HEURISTIC FUNCTIONS 8-PUZZLE h1=the number of misplaced tiles h2=the sum of the Manhattan distances of the tiles from their goal positions h1 = 7 h2 = 4+0+3+3+1+0+2+1+2 = 16 39 HEURISTIC FUNCTIONS 8-PUZZLE h1=0 h2=0 h1=2 h1=2 h2=8 h2=2 40