360 likes | 528 Views
Chapter 7. A* Pathfinding . 2013/04/18. A* Pathfinding . For most pathfinding problems, A* is the best choice The A* algorithm is efficient, but it can consume quite a few CPU cycles. Straight-line distances to Bucharest Arad 366 Bucharest 0 Craiova 160 Drobeta 242 Eforie 161
E N D
Chapter 7. A* Pathfinding 2013/04/18
A* Pathfinding • For most pathfinding problems, A* is the best choice • The A* algorithm is efficient, but it can consume quite a few CPU cycles.
Straight-line distances to Bucharest Arad 366 Bucharest 0 Craiova 160 Drobeta 242 Eforie 161 Fagaras 176 Giurgiu 77 Hirsova 151 Iasi 226 Lugoj 244 Mehadia 241 Neamt 234 Oradea 380 Pitesti 100 Rimnicu Vilcea 193 Sibiu 253 Timisoara 329 Urziceni 80 Vaslui 199 Zerind 374 Romania with Step Costs in km
Greedy Best-First Search • Greedy best-first search expands the nodes that appears to be closest to the goal • Evaluation function = Heuristic function • f(n) = h(n) = estimated best cost to goal from n • h(n) = 0 if n is a goal • e.g., hSLD(n) = straight-line distance for route-finding function GREEDY-SEARCH( problem ) returns a solution, or failure return BEST-FIRST-SEARCH( problem, h )
Analysis of Greedy Search • Complete? • Yes (in finite space with repeated-state checking) • No (start down an infinite path and never return to try other possibilities) • (e.g., from Iasi to Fagaras) • Susceptible to false starts Isai Neamt (dead end) • No repeated states checking Isai Neamt Isai Neamt (oscillation)
Analysis of Greedy Search (cont.) • Optimal? No • (e.g., from Arad to Bucharest) • Arad → Sibiu → Fagaras → Bucharest • (450 = 140+99+211, is not shortest) • Arad → Sibiu → Rim → Pitesti → Bucharest • (418 = 140+80+97+101) • Time? best: O(d), worst: O(bm) • m: the maximum depth • like depth-first search • a good heuristic can give dramatic improvement • Space? O(bm): keep all nodes in memory
A* Search • Avoid expanding paths that are already expansive • To minimizing the total estimated solution cost • Evaluation function f(n) = g(n) + h(n) • f(n) = estimated cost of the cheapest solution through n • g(n) = path cost so far to reach n • h(n) = estimated cost of the cheapest path from n to goal function A*-SEARCH( problem) returns a solution, or failure return BEST-FIRST-SEARCH( problem, g+h)
Straight-line distances to Bucharest Arad 366 Bucharest 0 Craiova 160 Drobeta 242 Eforie 161 Fagaras 176 Giurgiu 77 Hirsova 151 Iasi 226 Lugoj 244 Mehadia 241 Neamt 234 Oradea 380 Pitesti 100 Rimnicu Vilcea 193 Sibiu 253 Timisoara 329 Urziceni 80 Vaslui 199 Zerind 374 Romania with Step Costs in km (remind)
A* Search Example 140 118 75 140 99 151 80 Arad 366 Bucharest 0 Craiova 160 Drobeta 242 Eforie 161 Fagaras 176 Giurgiu 77 Hirsova 151 Lugoj 244 Mehadia 241 Oradea 380 Pitesti 100 Rimnicu Vilcea 193 Sibiu 253 Timisoara 329 Zerind 374 211 99 145 97 80 80 80 80
Straight-line distances to Bucharest Arad 366 Bucharest 0 Craiova 160 Drobeta 242 Fagaras 176 Lugoj 244 Mehadia 241 Oradea 380 Pitesti 100 Rimnicu Vilcea 193 Sibiu 253 Timisoara 329 Zerind 374 Romania with Step Costs in km 192
Admissible Heuristic • A* search uses an admissible heuristic h(n) • h(n) neveroverestimates the cost to the goal from n • n, h(n) h*(n), where h*(n) is the true cost to reach the goal from n (also, h(n) 0, so h(G) = 0 for any goal G) • e.g., h(n) is not admissible • g(X) + h(X) = 102 • g(Y) + h(Y) = 74 • Optimal path is not found! • e.g., straight-line distance hSLD(n) never overestimates the actual road distance
Admissible Heuristic (cont.) • e.g., 8-puzzle • h1(n) = number of misplaced tiles • h2(n) = total Manhattan distance • i.e., no. of squares from desired location of each tile • h1(S) = • h2(S) = • A* is complete and optimal if h(n) is admissible 8 3+1+2+2+2+3+3+2 = 18
Admissible Heuristics • 8-puzzle • h1(n) = number of misplaced tiles • h2(n) = total Manhattan distance • i.e., no. of squares from desired location of each tile • h1(S) = • h2(S) = 8 3+1+2+2+2+3+3+2 = 18
Effect of Heuristic Accuracy on Performance
7.2 Starting the Search • A* algorithm • Evaluation function f(n) = g(n) + h(n)
7.3 Scoring, Initial values 1+3=4 1+3=4 1+3=4 1+4=5 1+4=5 1+5=6 1+5=6 1+5=6
Examining tile(5,6) 2+3=5 1+3=4 1+3=4 1+3=4 1+4=5 1+4=5 2+4=6 1+5=6 1+5=6 1+5=6
Examining tile(5,5) 2+3=5 1+3=4 1+3=4 1+3=4 1+4=5 1+4=5 2+4=6 1+5=6 1+5=6 1+5=6
Examining tile(5,4) 2+4=6 2+4=6 2+3=5 1+3=4 1+3=4 1+3=4 2+4=6 1+4=5 1+4=5 2+4=5 1+5=6 1+5=6 1+5=6
Examining all tiles with cost 5 2+4=6 2+4=6 2+3=5 1+3=4 1+3=4 1+3=4 2+4=6 1+4=5 1+4=5 2+4=6 1+5=6 2+5=7 1+5=6 1+5=6 2+5=7
Examining all tiles with cost 6 3+4=7 3+3=6 3+5=8 3+5=8 2+4=6 3+5=8 2+4=6 2+3=5 1+3=4 1+3=4 1+3=4 2+4=6 3+5=8 1+4=5 1+4=5 2+4=6 3+5=8 1+5=6 2+5=7 1+5=6 1+5=6 2+5=7 2+6=8 2+6=8 2+6=8 2+6=8
7.5 Terrain Cost • Total Cost from Start = Cost from Start + Terrain Cost • Score = Total Cost from Start + Heuristic
3+3=6 1+3=4 3+3=6 1+4=5 1+4=5 1+5=6 1+5=6 1+5=6
Calculating the lowest-cost path • The lowest-cost path
7.6 Influence Mapping • Influenced by the enemy firing zone