1 Artificial Intelligence in Games Week 5 Steve Rabin

Slides:



Advertisements
Similar presentations
Heuristics, and what to do if you dont know what to do Carl Hultquist.
Advertisements

Review: Search problem formulation
Problem solving with graph search
AI Pathfinding Representing the Search Space
An Introduction to Artificial Intelligence
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Problem Solving Agents A problem solving agent is one which decides what actions and states to consider in completing a goal Examples: Finding the shortest.
CSE 380 – Computer Game Programming Pathfinding AI
CS171 Introduction to Computer Science II Graphs Strike Back.
PATHFINDING WITH A* Presented by Joseph Siefers February 19 th, 2008.
Search in AI.
CPSC 322 Introduction to Artificial Intelligence October 27, 2004.
Review: Search problem formulation
1 Using Search in Problem Solving Part II. 2 Basic Concepts Basic concepts: Initial state Goal/Target state Intermediate states Path from the initial.
Informed Search (no corresponding text chapter). Recall: Wanted " An algorithm and associated data structure(s) that can: 1) Solve an arbitrary 8-puzzle.
Games with Chance Other Search Algorithms CPSC 315 – Programming Studio Spring 2008 Project 2, Lecture 3 Adapted from slides of Yoonsuck Choe.
MAE 552 – Heuristic Optimization Lecture 27 April 3, 2002
Using Search in Problem Solving
Blind Search-Part 2 Ref: Chapter 2. Search Trees The search for a solution can be described by a tree - each node represents one state. The path from.
CS 188: Artificial Intelligence Spring 2006 Lecture 2: Queue-Based Search 8/31/2006 Dan Klein – UC Berkeley Many slides over the course adapted from either.
Heuristics CSE 473 University of Washington. © Daniel S. Weld Topics Agency Problem Spaces SearchKnowledge Representation Planning PerceptionNLPMulti-agentRobotics.
Chapter 5.4 Artificial Intelligence: Pathfinding.
State-Space Searches. 2 State spaces A state space consists of –A (possibly infinite) set of states The start state represents the initial problem Each.
State-Space Searches.
Informed Search Idea: be smart about what paths to try.
Chapter 5.4 Artificial Intelligence: Pathfinding.
Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.
Vilalta&Eick: Informed Search Informed Search and Exploration Search Strategies Heuristic Functions Local Search Algorithms Vilalta&Eick: Informed Search.
1 Game AI Path Finding. A Common Situation of Game AI A Common Situation of Game AI Path Planning Path Planning –From a start position to a destination.
Charles Lin. Graph Representation Graph Representation DFS DFS BFS BFS Dijkstra Dijkstra A* Search A* Search Bellman-Ford Bellman-Ford Floyd-Warshall.
GRAPHS
Introduction to search Chapter 3. Why study search? §Search is a basis for all AI l search proposed as the basis of intelligence l inference l all learning.
P ROBLEM Write an algorithm that calculates the most efficient route between two points as quickly as possible.
Informed search algorithms Chapter 4. Outline Best-first search Greedy best-first search A * search Heuristics.
State-Space Searches. 2 State spaces A state space consists of A (possibly infinite) set of states The start state represents the initial problem Each.
1 Artificial Intelligence in Games Week 6 Steve Rabin TA: Chi-Hao Kuo TA: Allan.
Informed search strategies Idea: give the algorithm “hints” about the desirability of different states – Use an evaluation function to rank nodes and select.
Informed Search Strategies Lecture # 8 & 9. Outline 2 Best-first search Greedy best-first search A * search Heuristics.
1 Artificial Intelligence in Games Week 5 Steve Rabin TA: Chi-Hao Kuo TA: Allan.
Administration CI meetings resume next week, as usual Some TAs in labs, plus Dr. Betz –Go to your lab as usual –If your TA is not there, ask for help from.
For Monday Read chapter 4, section 1 No homework..
Review: Tree search Initialize the frontier using the starting state While the frontier is not empty – Choose a frontier node to expand according to search.
Lecture 3: Uninformed Search
Search CPSC 386 Artificial Intelligence Ellen Walker Hiram College.
Advanced Artificial Intelligence Lecture 2: Search.
Artificial Intelligence for Games Informed Search (2) Patrick Olivier
Search (continued) CPSC 386 Artificial Intelligence Ellen Walker Hiram College.
REFERENCE: “ARTIFICIAL INTELLIGENCE FOR GAMES”, IAN MILLINGTON. A* (A-STAR)
Slides by: Eric Ringger, adapted from slides by Stuart Russell of UC Berkeley. CS 312: Algorithm Design & Analysis Lecture #36: Best-first State- space.
1 CO Games Development 1 Week 8 A* Gareth Bellaby.
Graph Search II GAM 376 Robin Burke. Outline Homework #3 Graph search review DFS, BFS A* search Iterative beam search IA* search Search in turn-based.
CSCE 552 Spring 2010 AI (III) By Jijun Tang. A* Pathfinding Directed search algorithm used for finding an optimal path through the game world Used knowledge.
CSCE 552 Fall 2012 AI By Jijun Tang. Homework 3 List of AI techniques in games you have played; Select one game and discuss how AI enhances its game play.
A* Reference: “Artificial Intelligence for Games”, Ian Millington.
Chapter 3 Solving problems by searching. Search We will consider the problem of designing goal-based agents in observable, deterministic, discrete, known.
Review: Tree search Initialize the frontier using the starting state
Chapter 5.4 Artificial Intelligence: Pathfinding
Heuristic Search Introduction to Artificial Intelligence
Lesson Objectives Aims Understand the following “standard algorithms”:
Artificial Intelligence Problem solving by searching CSC 361
Heuristics Local Search
Heuristics Local Search
Informed Search Idea: be smart about what paths to try.
State-Space Searches.
State-Space Searches.
The Rich/Knight Implementation
State-Space Searches.
Informed Search Idea: be smart about what paths to try.
The Rich/Knight Implementation
Presentation transcript:

1 Artificial Intelligence in Games Week 5 Steve Rabin

2 Questions from Last Week Pathfinding search spaces? Pathfinding search algorithms? Pathfinding terms?

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 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 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 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 Project 2: A* Pathfinding START GOAL

8 Project 2: Without Diagonals (Wrong Way) START GOAL

9 Project 2: With Diagonals (Wrong Way) START GOAL

10 Project 2: With Diagonals (Right Way) START GOAL

11 Project 2: Before Simple (Conservative) Rubberbanding START GOAL

12 Project 2: Look at Node Triplets; Eliminate Middle if Area is Clear START GOAL

13 Project 2: After Simple (Conservative) Rubberbanding START GOAL

14 Project 2: Look at Node Triplets; Eliminate Middle if Area is Clear START GOAL

15 Project 2: Look at Node Triplets Eliminate Middle if Area is Clear START GOAL

16 Project 2: Look at Node Triplets; Eliminate Middle if Area is Clear START GOAL

17 Project 2: After Rubberbanding START GOAL

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 Heuristic Calculation Options Manhattan distance xDiff + yDiff = = 12

20 Heuristic Calculation Options Chebyshev distance (Queen's distance) max(xDiff, yDiff) = max(9,3) = 9

21 Heuristic Calculation Options Euclidean sqrt(xDiff 2 + yDiff 2 ) = sqrt( ) = 9.48

22 Heuristic Calculation Options Octile min(xDiff,yDiff) * sqrt(2) + max(xDiff,yDiff) – min(xDiff,yDiff) = min(9,3) * max(9,3) – min(9,3) = 10.24

23 Pathfinding Clarification Chebyshev distance = 9 Manhattan distance = 12 Euclidean distance = 9.48 Octile = 10.24

24 Quick Review from Last Week

25 Search Space Representations

26 Search Space Representations

27 Search Space Representations

28 Search Space Representations

29 Search Space Representations

30 Pathfinding Search Algorithms

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 Open and Closed Lists Concept of Open and Closed Lists Open List: Promising nodes not yet explored Closed List: Nodes already explored

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 Pathfinding Search Algorithms from Last Week Random Bounce Random Trace Breadth-First Best-First Dijkstra A*

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 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 Additional Search Algorithms Depth-First Iterative Deepening Depth-First Iterative Deepening A* (IDA*)

38 Depth-First Strategy Pick a depth Explore all paths at that depth Benefits Complete Optimal path found Low memory requirement Improvements?

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 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 Pathfinding Aesthetics

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 Catmull-Rom Spline C1 continuity (not C2) Passes through all control points

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 *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 Catmull-Rom to Create In-Between Waypoints (2) //u = 0.0 output_point = point_2; //u = 0.25 output_point = point_1 * point_2 * point_3 * point_4 * ; //u = 0.5 output_point = point_1 * point_2 * point_3 * point_4 * ; //u = 0.75 output_point = point_1 * point_2 * point_3 * point_4 * ; //u = 1.0 output_point = point_3;

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 Catmull-Rom to Create In-Between Waypoints (3) What do you do at the ends?

48 Project 2: After Rubberbanding START GOAL

49 Project 2: Make Nodes Regularly Spaced START GOAL

50 Project 2: Apply Catmull-Rom Spline START GOAL

51 Project 2: Apply Catmull-Rom Spline START GOAL

52 Project 2: Apply Catmull-Rom Spline START GOAL

53 Project 2: Apply Catmull-Rom Spline START GOAL

54 Smoothing with Bezier Splines

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 A* Speed Optimizations Let's list strategies/techniques!

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 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 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 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 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 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 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 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 Cluster Heuristic From book (p235) Cluster A Cluster C Cluster B A B C A B C x x x Precomputed Table

66 Hierarchical Pathfinding Whiteboard Demo

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? Cache failed start-end pairs Avoid A* altogether Test straight line path

68 Precompute All Paths How would you store it???

69 Floyd-Warshall All-Pairs Shortest Path Pre-compute ALL paths Just look up answer in a table!

70 Floyd-Warshall All-Pairs Shortest Path Table A 4 ABCDE A B C D E B C D E

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 Floyd-Warshall All-Pairs Shortest Path Table A 4 ABCDE A0 B0 C0 D0 E0 B C D E

73 Floyd-Warshall All-Pairs Shortest Path Table A 4 ABCDE A24 B3 C410 D3 4 E5 B C D E

74 Floyd-Warshall All-Pairs Shortest Path Table A 4 ABCDE A024 B03 C4010 D3 04 E50 B C D E

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

76 Floyd-Warshall All-Pairs Shortest Path Table A 4 ABCDE A024∞∞ B∞0∞3∞ B C D E 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 Floyd-Warshall All-Pairs Shortest Path Table A 4 ABCDE A024∞∞ B∞0∞3∞ B C D E 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 Floyd-Warshall All-Pairs Shortest Path Table A 4 ABCDE A0245∞ B∞0∞3∞ B C D E 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 Floyd-Warshall All-Pairs Shortest Path Table A 4 ABCDE A02459 B C D 3904 E B C D E

80 Floyd-Warshall (Represents ideal heuristic) A 4 ABCDE A02459 B C D 3904 E B C D E

81 Floyd-Warshall Inspired Next Node Look-up Table A 4 ABCDE AABCBB BDBDDD CAACAA DEBEDE ECCCCE B C D E

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 Company of Heroes GDC 2007 Presentation