Download presentation
Presentation is loading. Please wait.
Published byMagnus Williams Modified over 8 years ago
1
1 Artificial Intelligence in Games Week 5 Steve Rabin steve.rabin@gmail.com www.aiwisdom.com/uw
2
2 Questions from Last Week Pathfinding search spaces? Pathfinding search algorithms? Pathfinding terms?
3
3 Pop Quiz 1. If a pathfinding search algorithm is "complete", what does it guarantee? 2. Name 2 search space representations that represent the walkable surface. 3. Name the 3 steering rules of flocking. 4. Name 2 additional rules that benefit flocking. 5. Bonus Question: In flocking, how is the leader chosen?
4
4 Project 2: A* Pathfinding (due week 6, port due week 7) Absolute requirement: Draw the open list of quads as one color, the closed list of quads as another color, draw the final path, and have Tiny follow the path Due week 6 using special base code (80% of project 2 grade) (20%) Draw one step of A* each frame (use toggle) (20%) A path is always found if it exists (completeness) (15%) The path is optimal with a heuristic weight of 1.0 (15%) The path is optimal with a heuristic weight of 0.0, and the search grows in a circular pattern (5%) The path uses diagonals and doesn't run into corners (5%) Calculate heuristic using both Euclidean and cardinal/intercardinal methods (use toggle) (10%) Rubberband final path where possible (use toggle) (10%) Smooth using a Catmull-Rom spline (use toggle) (-10%) If it crashes at any time Due week 7 using your own code (20% of project 2 grade) (100%) Port A* to your own engine
5
5 Project 2: A* Pathfinding Extra Credit (extra 20%) Implement Floyd-Warshall algorithm (in addition to A*). Must offer button on-screen to toggle on/off.
6
6 Project 2: A* Pathfinding Watching the search Escape after each time through search loop Use quads to mark search Open list Closed list Draw final path with line segments Waypoint to waypoint
7
7 Project 2: A* Pathfinding START GOAL
8
8 Project 2: Without Diagonals (Wrong Way) START GOAL
9
9 Project 2: With Diagonals (Wrong Way) START GOAL
10
10 Project 2: With Diagonals (Right Way) START GOAL
11
11 Project 2: Before Simple (Conservative) Rubberbanding START GOAL
12
12 Project 2: Look at Node Triplets; Eliminate Middle if Area is Clear START GOAL
13
13 Project 2: After Simple (Conservative) Rubberbanding START GOAL
14
14 Project 2: Look at Node Triplets; Eliminate Middle if Area is Clear START GOAL
15
15 Project 2: Look at Node Triplets Eliminate Middle if Area is Clear START GOAL
16
16 Project 2: Look at Node Triplets; Eliminate Middle if Area is Clear START GOAL
17
17 Project 2: After Rubberbanding START GOAL
18
18 Heuristic Cost Guess the distance to the goal Don't over estimate (to be admissible) Best heuristic cost algorithm? In the general case? On a grid? What about node-to-node cost?
19
19 Heuristic Calculation Options Manhattan distance xDiff + yDiff = 9 + 3 = 12
20
20 Heuristic Calculation Options Chebyshev distance (Queen's distance) max(xDiff, yDiff) = max(9,3) = 9
21
21 Heuristic Calculation Options Euclidean sqrt(xDiff 2 + yDiff 2 ) = sqrt(9 2 +3 2 ) = 9.48
22
22 Heuristic Calculation Options Octile min(xDiff,yDiff) * sqrt(2) + max(xDiff,yDiff) – min(xDiff,yDiff) = min(9,3) * 1.41 + max(9,3) – min(9,3) = 10.24
23
23 Pathfinding Clarification Chebyshev distance = 9 Manhattan distance = 12 Euclidean distance = 9.48 Octile = 10.24
24
24 Quick Review from Last Week
25
25 Search Space Representations
26
26 Search Space Representations
27
27 Search Space Representations
28
28 Search Space Representations
29
29 Search Space Representations
30
30 Pathfinding Search Algorithms
31
31 Pathfinding Search Terminology Search Space – connected graph to be explored Node – the individual locations of the search space Start Node – the place where the search will begin Goal Node – the goal location of the search Completeness – If a path exists, the search will find it Heuristic – a function, h(n), that returns an estimate of the cheapest cost from a Node to the Goal Node Admissible – if h(n) never overestimates the cost of getting to the Goal Node
32
32 Open and Closed Lists Concept of Open and Closed Lists Open List: Promising nodes not yet explored Closed List: Nodes already explored
33
33 General Structure of Search Algorithms 1. Push Start Node onto the Open List 2. While Open List not empty a. Pop a node from the Open List b. If node is Goal Node, then path found c. Find “successor” neighboring nodes and add them to the Open List d. Place node on the Closed List
34
34 Pathfinding Search Algorithms from Last Week Random Bounce Random Trace Breadth-First Best-First Dijkstra A*
35
35 A* Combines Best-First and Dijkstra Given cost and Heuristic cost Open List sorted by: Final cost = Given cost + (Heuristic cost * weight) f(x) = g(x) + (h(x) * weight) Consider what different weights do like 0, 1, and 2
36
36 A* Algorithm 1. Push Start Node onto the Open List 2. Pop best node off Open List (call it B) a. If B is the Goal Node, then path found b. If Open List is empty, then no path possible c. Find all neighboring nodes – for each node (call it C): I. Compute its cost (f, g, h) II. If C is not on the Open or Closed list, put it on Open List. III. If C is on the Closed List, but this new one is cheaper, then take it off the Closed List and put this new cheaper one on Open List. IV. If C is on the Open List, but this new one is cheaper, then take it off the Open List and put this new cheaper one on Open List. d. Place B on the Closed List (we’re done with it) e. If taken too much time this frame, abort search for now and resume next frame f. Go to step 2
37
37 Additional Search Algorithms Depth-First Iterative Deepening Depth-First Iterative Deepening A* (IDA*)
38
38 Depth-First Strategy Pick a depth Explore all paths at that depth Benefits Complete Optimal path found Low memory requirement Improvements?
39
39 Iterative Deepening Depth-First Strategy Explore all paths at a particular min depth If path found, return path Increase depth by 1 If depth > max depth, no path Repeat
40
40 Iterative Deepening A* Proposed by Richard Korf (1985) Key insight Use f(x) as limit (instead of depth) Strategy Explore all paths at a particular min cost If path found, return path Increase cost by 1 If cost > max cost, no path Repeat
41
41 Pathfinding Aesthetics
42
42 Aesthetic Optimizations Maximize responsiveness How? Make paths more direct Fewer wiggles Smoothed paths Physics-based At movement time – problems? On waypoints before movement (demo) Splines Catmull-Rom Spline (Benefits? Problems?) Bezier Spline (Benefits? Problems?)
43
43 Catmull-Rom Spline C1 continuity (not C2) Passes through all control points http://www.cse.unsw.edu.au/~lambert/splines/CatmullRom.html http://www.ibiblio.org/e-notes/Splines/Cardinal.htm
44
44 Catmull-Rom to Create In-Between Waypoints output_point = point_1 * (-0.5*u*u*u + u*u – 0.5*u) + point_2 * (1.5*u*u*u + -2.5*u*u + 1.0) + point_3 * (-1.5*u*u*u + 2.0*u*u + 0.5*u) + point_4 * (0.5*u*u*u – 0.5*u*u); Points are waypoints in 3D u in range [0,1] Consider u=0 and u=1 Creates new waypoints between point_2 and point_3
45
45 Catmull-Rom to Create In-Between Waypoints (2) //u = 0.0 output_point = point_2; //u = 0.25 output_point = point_1 * -0.0703125 + point_2 * 0.8671875 + point_3 * 0.2265625 + point_4 * -0.0234375; //u = 0.5 output_point = point_1 * -0.0625 + point_2 * 0.5625 + point_3 * 0.5625 + point_4 * -0.0625; //u = 0.75 output_point = point_1 * -0.0234375 + point_2 * 0.226563 + point_3 * 0.8671875 + point_4 * -0.0703125; //u = 1.0 output_point = point_3;
46
46 Catmull-Rom in DirectX D3DXVec2CatmullRom(D3DXVECTOR2* pOut, CONST D3DXVECTOR2* pV1, CONST D3DXVECTOR2* pV2, CONST D3DXVECTOR2* pV3, CONST D3DXVECTOR2* pV4, FLOAT s) D3DXVec3CatmullRom(D3DXVECTOR3* pOut, CONST D3DXVECTOR3* pV1, CONST D3DXVECTOR3* pV2, CONST D3DXVECTOR3* pV3, CONST D3DXVECTOR3* pV4, FLOAT s)
47
47 Catmull-Rom to Create In-Between Waypoints (3) What do you do at the ends?
48
48 Project 2: After Rubberbanding START GOAL
49
49 Project 2: Make Nodes Regularly Spaced START GOAL
50
50 Project 2: Apply Catmull-Rom Spline START GOAL
51
51 Project 2: Apply Catmull-Rom Spline START GOAL
52
52 Project 2: Apply Catmull-Rom Spline START GOAL
53
53 Project 2: Apply Catmull-Rom Spline START GOAL
54
54 Smoothing with Bezier Splines
55
55 Smoothing with Bezier Splines (2) P 0 = Current position P 1 = P 0 + (Normalize(current dir) * K) P 2 = P 3 – (Normalize(P next – P 3 ) * K) P 3 = Next interface
56
56 A* Speed Optimizations Let's list strategies/techniques!
57
57 A* Speed Optimizations: Hints Let's list strategies/techniques! Search fewer nodes Avoiding search when overkill Avoiding search when it won't be successful Solving only part of the problem Memory optimization Data structure optimizations
58
58 A* Speed Optimizations Search space representation Bad ones? Good ones? Open list data structure Pathfind less Pool nodes Purposely overestimate the heuristic cost Hierarchical pathfinding
59
59 Open List Sorting Unsorted Linked List Pop: O(n) Push: O(1) Update: O(n) Priority Queue (binary tree) Pop: O(log n) Push: O(log n) Update: O(n) Fibonacci Heap Pop: O(log n) *worst case O(n) Push: O(1) Update: O(n) ???
60
60 A* Algorithm: Priority Queue class PriorityQueue { public: std::vector heap; }; bool IsPriorityQueueEmpty( PriorityQueue& pqueue ) { return( pqueue.heap.empty() ); } class NodeTotalGreater { public: //This is required for STL to sort the priority queue //(it’s entered as an argument in the STL heap functions) bool operator()( Node * first, Node * second ) const { return( first->total > second->total ); } };
61
61 A* Algorithm: PopPriorityQueue Node* PopPriorityQueue( PriorityQueue& pqueue ) { //Total time = O(log n) //Get the node at the front - it has the lowest total cost Node * node = pqueue.heap.front(); //pop_heap will move the node at the front to position N and //then sort the heap to make positions 1 through N-1 correct //(STL makes no assumptions about your data and doesn't want //to change the size of the container.) std::pop_heap( pqueue.heap.begin(), pqueue.heap.end(), NodeTotalGreater() ); //pop_back() will remove the last element from the heap //(now the heap is sorted for positions 1 through N) pqueue.heap.pop_back(); return( node ); }
62
62 A* Algorithm: PushPriorityQueue void PushPriorityQueue( PriorityQueue& pqueue, Node* node ) { //Total time = O(log n) //Pushes the node onto the back of the vector //(the heap is now unsorted) pqueue.heap.push_back( node ); //Sorts the new element into the heap std::push_heap( pqueue.heap.begin(), pqueue.heap.end(), NodeTotalGreater() ); }
63
63 A* Algorithm: UpdateNodeOnPriorityQueue void UpdateNodeOnPriorityQueue( PriorityQueue& pqueue, Node* node ) { //Total time = O(n+log n) //Loop through the heap and find the node to be updated std::vector ::iterator i; for( i=pqueue.heap.begin(); i!=pqueue.heap.end(); i++ ) { if( (*i)->location == node->location ) { //Found node - resort from this position in the heap //(since its total value was changed before this //function was called) std::push_heap( pqueue.heap.begin(), i+1, NodeTotalGreater() ); return; }
64
64 Overestimating Heuristic Cost A* combines Best-First and Dijkstra Given cost and Heuristic cost Final cost = Given cost + (Heuristic cost * weight) f(x) = g(x) + (h(x) * weight) Consider what different weights do like 0, 1, and 2 Demo
65
65 Cluster Heuristic From book (p235) Cluster A Cluster C Cluster B A B C A B C 1329 7 13 29 7 x x x 13 14 9 10 7 12 13 11 15 7 8 12 10 11 8 8 6 6 7 Precomputed Table
66
66 Hierarchical Pathfinding Whiteboard Demo
67
67 Other Ideas Bidirectional pathfinding When is it a good idea? Return partial paths if run out of time 50 NPCs want paths simultaneously What’s your strategy? Global optimization? http://mrl.nyu.edu/~perlin/experiments/path/ http://mrl.nyu.edu/~perlin/experiments/path/ Cache failed start-end pairs Avoid A* altogether Test straight line path
68
68 Precompute All Paths How would you store it???
69
69 Floyd-Warshall All-Pairs Shortest Path Pre-compute ALL paths Just look up answer in a table!
70
70 Floyd-Warshall All-Pairs Shortest Path Table A 4 ABCDE A B C D E B C D E 2 10 3 4 5
71
71 Floyd-Warshall All-Pairs Shortest Path O(n 3 ) int path[][]; //initialize to starting costs void FloydWarshall() { for (k = 1 to n) { for each (i,j) in (1..n) { path[i][j] = min ( path[i][j], path[i][k] + path[k][j] ); }
72
72 Floyd-Warshall All-Pairs Shortest Path Table A 4 ABCDE A0 B0 C0 D0 E0 B C D E 2 10 3 4 5
73
73 Floyd-Warshall All-Pairs Shortest Path Table A 4 ABCDE A24 B3 C410 D3 4 E5 B C D E 2 3 4 5
74
74 Floyd-Warshall All-Pairs Shortest Path Table A 4 ABCDE A024 B03 C4010 D3 04 E50 B C D E 2 3 4 5
75
75 Floyd-Warshall All-Pairs Shortest Path Table A 4 ABCDE A024∞∞ B∞0∞3∞ C4∞010∞ D∞3 04 E∞∞5∞0 B C D E 2 3 4 5
76
76 Floyd-Warshall All-Pairs Shortest Path Table A 4 ABCDE A024∞∞ B∞0∞3∞ B C D E 2 10 3 4 5 for (k = 1 to n) { for each (i,j) in (1..n) { path[i][j] = min (path[i][j], path[i][k] + path[k][j]); }
77
77 Floyd-Warshall All-Pairs Shortest Path Table A 4 ABCDE A024∞∞ B∞0∞3∞ B C D E 2 10 3 4 5 for (k = 1 to n) { for each (i,j) in (1..n) { //example i=A, j=D, k=B path[i][j] = min (path[i][j], path[i][k] + path[k][j]); }
78
78 Floyd-Warshall All-Pairs Shortest Path Table A 4 ABCDE A0245∞ B∞0∞3∞ B C D E 2 10 3 4 5 for (k = 1 to n) { for each (i,j) in (1..n) { //example i=A, j=D, k=B path[i][j] = min (path[i][j], path[i][k] + path[k][j]); }
79
79 Floyd-Warshall All-Pairs Shortest Path Table A 4 ABCDE A02459 B1601237 C460913 D 3904 E9115140 B C D E 2 10 3 4 5
80
80 Floyd-Warshall (Represents ideal heuristic) A 4 ABCDE A02459 B1601237 C460913 D 3904 E9115140 B C D E 2 10 3 4 5
81
81 Floyd-Warshall Inspired Next Node Look-up Table A 4 ABCDE AABCBB BDBDDD CAACAA DEBEDE ECCCCE B C D E 2 10 3 4 5
82
82 Floyd-Warshall Inspired Next Node Look-up Table Speed of search? Cost to compute? What's the downside during runtime? Which search space representations does it work on?
83
83 Company of Heroes GDC 2007 Presentation
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.