Game Programmig Big-Yu Che Natioal Taiwa Uiversity
Search o Blid search Breadth-First Search Depth-First Search o Heuristic search A* o Adversary search MiMax 2
Itroductio to Search o o Usig tree diagram (usually) to describe a search problem Search starts o Goal o Node i Goal ode g Successors o c 1, c 2, c 3 Depth o d = 0, 1, 2, Search problem Iput o o Descriptio of the iitial ad goal odes A procedure that produces the successors of a arbitrary ode Output o A legal sequece of odes startig with the iitial ode ad edig with the goal ode. i c 1 c 2 c 3 d = 1 g d = 2 d = 3 d = 4 3
Search Examples i Traditioal AI o Game playig Chess Backgammo o Fidig a path to goal The towers of Haoi Slidig tile puzzles o 8 puzzles o Simply fidig a goal -quees 4
Search Algorithm 1. Set L to be a list of the iitial odes. At ay give poit i time, L is a list of odes that have ot yet bee examied. 2. If L is empty, failed. Otherwise, pick a ode from L. 3. If is the goal ode, stop ad retur it ad the path from the iitial ode to. 4. Otherwise, remove from L ad add to L all of s childre, labelig each with its path from the iitial ode. 5. Retur to Step 2. 5
Depth-First Search o Always explorig the child of the most recetly expaded ode o Termial odes beig examied from left to right o If the ode has o childre, the procedure backs up a miimum amout before choosig aother ode to examie. 4 3 1 2 8 9 5 6 7 10 g 11 d = 1 d = 2 d = 3 d = 4 6
Depth-First Search o We stop the search whe we select the goal ode g. o Depth-first search ca be implemeted by pushig the childre of a give ode oto the frot of the list L i Step 4. of Search Algorithm. o Ad always choosig the first ode o L as the oe to expad. 7
Depth-First Search Algorithm 1. Set L to be a list of the iitial odes. 2. If L is empty, failed. Otherwise, pick a ode from L. 3. If is the goal ode, stop ad retur it ad the path from the iitial ode to. 4. Otherwise, remove from L ad add to the frot of L all of s childre, labelig each with its path from the iitial ode. 5. Retur to Step 2. 8
Breadth-First Search o The tree examied from top to dow, so every ode at depth d is examied before ay ode at depth d + 1. o We ca implemet breadth-first search by addig the ew odes to the ed of the 5 list L. 1 2 3 4 6 7 8 d = 1 d = 2 d = 3 9 10 11 g 12 d = 4 9
Breadth-First Search Algorithm 1. Set L to be a list of the iitial odes. 2. If L is empty, failed. Otherwise, pick a ode from L. 3. If is the goal ode, stop ad retur it ad the path from the iitial ode to. 4. Otherwise, remove from L ad add to the ed of L all of s childre, labelig each with its path from the iitial ode. 5. Retur to Step 2. 10
Heuristic Search o Neither depth-first or breadth-first search o Explorig the tree i aythig resemblig a optimal order. o Miimizig the cost to solve the problem. 1 3 2 d = 1 d = 2 d = 3 4 g d = 4 11
Heuristic Search o Whe we pickig a ode from the list L i Step 2. of Search Algorithm, what we will do is to remove steadily from the root ode toward the goal by always selectig a ode that is as close to the goal as possible. Estimated by distace ad miimizig the cost? o A*! 12
Adversary Search o Assumptios Two-perso games i which the players alterate moves. They are games of perfect iformatio, where the kowledge available to each player is the same. o Examples : Tic-tac-toe Checkers Chess Go Othello Backgammo o Imperfect iformatio Pokers Bridge 13
MiMax a max ply Nodes with the maximizer to move are square; odes with the miimizer to move are circles -1 e b c d f 1 g -1 h i -1 j k 1 l m o 1-1 1 1 mi max mi max mi Maximizer to achieve the Outcome of 1; miimizer to Achieve the outcome of -1 p q r -1 1-1 14
MiMax a 1 max -1 b c 1 d -1 mi -1-1 e f g 1 1 1 h i -1 j k 1 l m o 1-1 1 1 max mi max mi The maximizer wis! p q r -1 1-1 15
MiMax Idea 1. Expad the etire tree below. 2. Evaluate the termial odes as wis for the miimizer or maximizer. 3. Select a ulabelled ode all of whose childre have bee assiged values. If there is o such ode, retur the value assiged to the ode. 4. If the selected ode is oe at which the miimizer moves, assig it a value that is the miimum of the values of its childre. If it is a maximizig ode, assig it a value that is the maximum of the childre s values. Retur to Step 3. 16
MiMax Algorithm 1. Set L = { }, the uexpaded odes i the tree. 2. Let x be the 1st ode o L. If x = ad there is a value assiged to it, retur this value. 3. If x has bee assiged a value v x, let p be the paret of x ad v p the value curretly assiged to p. If p is a miimizig ode, set v p = mi(v p, v x ). If p is a maximizig ode, set v p = max(v p, v x ). Remove x from L ad retur to Step 2. 4. If x has ot bee assiged a value ad is a termial ode, assig it the value 1 or -1 depedig o whether it is a wi for the maximizer or miimizer respectively. Assig x the value 0 if the positio is a draw. Leave x o L ad retur to Step 2. 5. Otherwise, set v x to be if x is a maximizig ode ad + if x is a miimizig ode. Add the childre of x to the frot of L ad retur to Step 2. 17
MiMax o Some issues Draw Estimated value e() o e() = 1 : the ode is a wi for maximizer o e() = -1 : the ode is a wi for miimizer o e() = 0 : that is a draw o e() = -1 ~ 1 : the others Whe to decide stop the tree expadig further? 18
MiMax Algorithm 1. Set L = { }, the uexpaded odes i the tree. 2. Let x be the 1st ode o L. If x = ad there is a value assiged to it, retur this value. 3. If x has bee assiged a value v x, let p be the paret of x ad v p the value curretly assiged to p. If p is a miimizig ode, set v p = mi(v p, v x ). If p is a maximizig ode, set v p = max(v p, v x ). Remove x from L ad retur to Step 2. 4. If x has ot bee assiged a value ad is a termial ode, assig it the value 1 or -1 depedig o whether it is a wi for the maximizer or miimizer respectively. Assig x the value 0 if the positio is a draw. Leave x o L ad retur to Step 2. 5. Otherwise, set v x to be if x is a maximizig ode ad + if x is a miimizig ode. Add the childre of x to the frot of L ad retur to Step 2. 19
MiMax Algorithm (fial) 1. Set L = { }, the uexpaded odes i the tree. 2. Let x be the 1st ode o L. If x = ad there is a value assiged to it, retur this value. 3. If x has bee assiged a value v x, let p be the paret of x ad v p the value curretly assiged to p. If p is a miimizig ode, set v p = mi(v p, v x ). If p is a maximizig ode, set v p = max(v p, v x ). Remove x from L ad retur to Step 2. 4. If x has ot bee assiged a value ad either x is a termial ode or we have decided ot to expad the tree further, compute its value usig the evaluatio fuctio. Leave x o L ad retur to Step 2. 5. Otherwise, set v x to be if x is a maximizig ode ad + if x is a miimizig ode. Add the childre of x to the frot of L ad retur to Step 2. 20
Itroductio to Path Fidig o A commo situatio of game AI o Path plaig From start positio to the goal o Most popular techique A* (A Star) o 1968 o A search algorithm o Favorite teachig example : 15-pizzule o Algorithm that searches i a state space for the least costly path from start state to a goal state by examiig the eighborig states 21
Dijkstra vs. A* Without obstacle With obstacle Dijkstra A* 22
Dijkstra vs. A* o Dijkstra: compute the optimal solutio o Diskstra: search space much larger tha A* o A*: simple o A*: fast o A*: good result o A*: employ heuristic estimate to elimiate may paths with high costs -> speedup process to compute satisfactory shortest paths 23
A*: cost fuctios o Goal: compute a path from a start poit S to a goal poit G o Cost at poit : f() = g() + h() o g(): distace from the start poit S to the curret poit o h(): estimated distace from the curret poit to the goal poit G o f(): curret estimated cost for poit 24
A*: cost fuctios o o o o o o o The role of h() A major cost evaluatio fuctio of A* Guide the performace of A* d(): the actual distace betwee S ad G h() = 0 : A* is equivalet to Dijkstra algorithm h() <= d () : guaratee to compute the shortest path; the lower the value h(), the more ode A* expads h() = d () : follow the best path; ever expad aythig else; difficult to compute h() i this way! h() > d() : ot guaratee to compute a best path; but very fast h() >> g() : h() domiates -> A* becomes the Best First Search 25
A* Algorithm o Add START to OPEN list o while OPEN ot empty o get ode from OPEN that has the lowest f() o if is GOAL the retur path o move to CLOSED o for each ' = CaMove(, directio) o g(') = g() + 1 o calculate h(') o if ' i OPEN list ad ew ' is ot better, cotiue o if ' i CLOSED list ad ew ' is ot better, cotiue o remove ay ' from OPEN ad CLOSED o add as 's paret o add ' to OPEN o ed for o ed while o if we get to here, the there is No Solutio 26
Example Straight-lie distace to Bucharest Arad 366 Oradea Bucharest 0 71 Neamt Craiova 160 Dobreta 242 Zerid 87 Eforie 161 151 Fagaras 178 Iasi Arad Giurgiu 77 Hirsova 151 140 Sibiu 92 Fagaras 118 99 Iasi 226 80 Vaslui Lugoj 244 Rimicu Vilcea Mehadia 241 Neamt 234 Timisoara 111 97 211 142 Lugoj Pitesti Oradea 380 70 Hirsova Pitesti 98 146 98 Mehadia 85 Rimicu Vilcea 193 101 Urzicei Sibiu 253 138 86 Bucharest Timisoara 329 Dobreta 90 Urzicei 80 120 Craiova Eforie Vaslui 199 Giurgiu Zerid 374 30
A* Example Straight-lie distace to Bucharest Oradea 71 Neamt Zerid 87 151 Iasi Arad 0+366 140 Sibiu 92 Fagaras 118 99 80 Vaslui Rimicu Vilcea Timisoara 111 97 211 142 Lugoj Pitesti 70 Hirsova 146 98 Mehadia 85 101 Urzicei 138 86 Bucharest Dobreta 90 120 Craiova Eforie Giurgiu Arad 366 Bucharest 0 Craiova 160 Dobreta 242 Eforie 161 Fagaras 178 Giurgiu 77 Hirsova 151 Iasi 226 Lugoj 244 Mehadia 241 Neamt 234 Oradea 380 Pitesti 98 Rimicu Vilcea 193 Sibiu 253 Timisoara 329 Urzicei 80 Vaslui 199 Zerid 374 31
A* Example Straight-lie distace to Bucharest Oradea 71 Neamt +374 Zerid 87 151 Iasi Arad 140+253 0+366 140 Sibiu 92 Fagaras 118 99 80 Vaslui Rimicu Vilcea Timisoara 111 97 211 142 118+329 Lugoj Pitesti 70 Hirsova 146 98 Mehadia 85 101 Urzicei 138 86 Bucharest Dobreta 90 120 Craiova Eforie Giurgiu Arad 366 Bucharest 0 Craiova 160 Dobreta 242 Eforie 161 Fagaras 178 Giurgiu 77 Hirsova 151 Iasi 226 Lugoj 244 Mehadia 241 Neamt 234 Oradea 380 Pitesti 98 Rimicu Vilcea 193 Sibiu 253 Timisoara 329 Urzicei 80 Vaslui 199 Zerid 374 32
A* Example Straight-lie distace to Bucharest Oradea 71 Neamt +374 Zerid 87 151 Iasi Arad 140+253 0+366 140 Sibiu 92 Fagaras 118 99 80 Vaslui Rimicu Vilcea Timisoara 111 97 211 142 118+329 Lugoj Pitesti 70 Hirsova 146 98 Mehadia 85 101 Urzicei 138 86 Bucharest Dobreta 90 120 Craiova Eforie Giurgiu Arad 366 Bucharest 0 Craiova 160 Dobreta 242 Eforie 161 Fagaras 178 Giurgiu 77 Hirsova 151 Iasi 226 Lugoj 244 Mehadia 241 Neamt 234 Oradea 380 Pitesti 98 Rimicu Vilcea 193 Sibiu 253 Timisoara 329 Urzicei 80 Vaslui 199 Zerid 374 33
A* Example Straight-lie distace to Bucharest Oradea 291+380 71 Neamt +374 Zerid 87 151 Iasi Arad 140+253 0+366 Sibiu 239+176 140 92 Fagaras 118 99 80 Vaslui 220+193 Rimicu Vilcea Timisoara 111 97 211 142 118+329 Lugoj Pitesti 70 Hirsova 146 98 Mehadia 85 101 Urzicei 138 86 Bucharest Dobreta 90 120 Craiova Eforie Giurgiu Arad 366 Bucharest 0 Craiova 160 Dobreta 242 Eforie 161 Fagaras 178 Giurgiu 77 Hirsova 151 Iasi 226 Lugoj 244 Mehadia 241 Neamt 234 Oradea 380 Pitesti 98 Rimicu Vilcea 193 Sibiu 253 Timisoara 329 Urzicei 80 Vaslui 199 Zerid 374 34
A* Example Straight-lie distace to Bucharest Oradea 291+380 71 Neamt +374 Zerid 87 151 Iasi Arad 140+253 0+366 Sibiu 239+176 140 92 Fagaras 118 99 80 Vaslui 220+193 Rimicu Vilcea Timisoara 111 97 211 142 118+329 Lugoj Pitesti 70 Hirsova 146 98 Mehadia 85 101 Urzicei 138 86 Bucharest Dobreta 90 120 Craiova Eforie Giurgiu Arad 366 Bucharest 0 Craiova 160 Dobreta 242 Eforie 161 Fagaras 178 Giurgiu 77 Hirsova 151 Iasi 226 Lugoj 244 Mehadia 241 Neamt 234 Oradea 380 Pitesti 98 Rimicu Vilcea 193 Sibiu 253 Timisoara 329 Urzicei 80 Vaslui 199 Zerid 374 35
A* Example Straight-lie distace to Bucharest Oradea 291+380 71 Neamt +374 Zerid 87 151 Iasi Arad 140+253 0+366 Sibiu 239+176 140 92 Fagaras 118 99 220+193 80 Vaslui Rimicu Vilcea Timisoara 111 97 211 142 118+329 Lugoj Pitesti 70 Hirsova 146 317+100 98 Mehadia 85 101 Urzicei 138 86 Bucharest Dobreta 90 120 Craiova Eforie 366+160 Giurgiu Arad 366 Bucharest 0 Craiova 160 Dobreta 242 Eforie 161 Fagaras 178 Giurgiu 77 Hirsova 151 Iasi 226 Lugoj 244 Mehadia 241 Neamt 234 Oradea 380 Pitesti 98 Rimicu Vilcea 193 Sibiu 253 Timisoara 329 Urzicei 80 Vaslui 199 Zerid 374 36
A* Example Straight-lie distace to Bucharest Oradea 291+380 71 Neamt +374 Zerid 87 151 Iasi Arad 140+253 0+366 Sibiu 239+176 140 92 Fagaras 118 99 220+193 80 Vaslui Rimicu Vilcea Timisoara 111 97 211 142 118+329 Lugoj Pitesti 70 Hirsova 146 317+100 98 Mehadia 85 101 Urzicei 138 86 Bucharest Dobreta 90 120 Craiova Eforie 366+160 Giurgiu Arad 366 Bucharest 0 Craiova 160 Dobreta 242 Eforie 161 Fagaras 178 Giurgiu 77 Hirsova 151 Iasi 226 Lugoj 244 Mehadia 241 Neamt 234 Oradea 380 Pitesti 98 Rimicu Vilcea 193 Sibiu 253 Timisoara 329 Urzicei 80 Vaslui 199 Zerid 374 37
A* Example Straight-lie distace to Bucharest Oradea 291+380 71 Neamt +374 Zerid 87 151 Iasi Arad 140+253 0+366 Sibiu 239+176 140 92 Fagaras 118 99 220+193 80 Vaslui Rimicu Vilcea Timisoara 111 97 211 142 118+329 Lugoj Pitesti 70 Hirsova 146 317+100 98 Mehadia 85 101 Urzicei 138 86 Bucharest Dobreta 450+0 90 120 Craiova Eforie 366+160 Giurgiu Arad 366 Bucharest 0 Craiova 160 Dobreta 242 Eforie 161 Fagaras 178 Giurgiu 77 Hirsova 151 Iasi 226 Lugoj 244 Mehadia 241 Neamt 234 Oradea 380 Pitesti 98 Rimicu Vilcea 193 Sibiu 253 Timisoara 329 Urzicei 80 Vaslui 199 Zerid 374 38
A* Example Straight-lie distace to Bucharest Oradea 291+380 71 Neamt +374 Zerid 87 151 Iasi Arad 140+253 0+366 Sibiu 239+176 140 92 Fagaras 118 99 220+193 80 Vaslui Rimicu Vilcea Timisoara 111 97 211 142 118+329 Lugoj Pitesti 70 Hirsova 146 317+100 98 Mehadia 85 101 Urzicei 138 86 Bucharest Dobreta 450+0 90 120 Craiova Eforie 366+160 Giurgiu Arad 366 Bucharest 0 Craiova 160 Dobreta 242 Eforie 161 Fagaras 178 Giurgiu 77 Hirsova 151 Iasi 226 Lugoj 244 Mehadia 241 Neamt 234 Oradea 380 Pitesti 98 Rimicu Vilcea 193 Sibiu 253 Timisoara 329 Urzicei 80 Vaslui 199 Zerid 374 39
A* Example Straight-lie distace to Bucharest Oradea 291+380 71 Neamt +374 Zerid 87 151 Iasi Arad 140+253 0+366 Sibiu 239+176 140 92 Fagaras 118 99 220+193 80 Vaslui Rimicu Vilcea Timisoara 111 97 211 142 118+329 Lugoj Pitesti 70 Hirsova 146 317+100 98 Mehadia 85 101 Urzicei 138 86 Bucharest Dobreta 450+0 90 120 Craiova 418+0 Eforie 366+160 Giurgiu Arad 366 Bucharest 0 Craiova 160 Dobreta 242 Eforie 161 Fagaras 178 Giurgiu 77 Hirsova 151 Iasi 226 Lugoj 244 Mehadia 241 Neamt 234 Oradea 380 Pitesti 98 Rimicu Vilcea 193 Sibiu 253 Timisoara 329 Urzicei 80 Vaslui 199 Zerid 374 40
A* Example Straight-lie distace to Bucharest Oradea 291+380 71 Neamt +374 Zerid 87 151 Iasi Arad 140+253 0+366 Sibiu 239+176 140 92 Fagaras 118 99 220+193 80 Vaslui Rimicu Vilcea Timisoara 111 97 211 142 118+329 Lugoj Pitesti 70 Hirsova 146 317+100 98 Mehadia 85 101 Urzicei 138 86 Bucharest Dobreta 450+0 90 120 Craiova 418+0 Eforie 366+160 Giurgiu Arad 366 Bucharest 0 Craiova 160 Dobreta 242 Eforie 161 Fagaras 178 Giurgiu 77 Hirsova 151 Iasi 226 Lugoj 244 Mehadia 241 Neamt 234 Oradea 380 Pitesti 98 Rimicu Vilcea 193 Sibiu 253 Timisoara 329 Urzicei 80 Vaslui 199 Zerid 374 41
A* Algorithm o State Locatio Neighborig states o Search space Related to terrai format Grids Triagles or Covex Polygos Poits of Visibility o Cost estimate o Path Typical A* path Straight path Smooth path o Hierarchical path fidig 42
Search Space & Neighborig States o Rectagular Grid Use grid ceter o Quadtree Use grid ceter o Triagles or Covex Polygos Use edge midpoit Use triagle ceter Triagles Rectagular Grid Quadtree 43
Search Space & Neighborig States o Poits of Visibility (POV) o Geeralized cyliders Use itersectios Poits of Visibility Geeralized Cyliders 44
Cost Estimate o Cost fuctio CostFromStart CostToGoal o Miimum cost Distace traveled Time of traveled Movemet poits expeded Fuel cosumed Pealties for passig through udesired area Bouses for passig through desired area o Estimate To goal distace 45
Result Path Typical A* Path Straight Path Smooth Path (Catmull-Rom Splie) 46
Hierarchical Path Fidig o Break the terrai for path fidig to several oes hierarchically Room-to-Room 3D layered terrai Terrai LOD o Pros Speedup the search Solve the problem of layered path fidig 48
Path Fidig Challeges o Movig Goal Do you eed to fid path each frame? o Movig Obstacles Predictio Scheme o Complexity of the Terrai Hierarchical path fidig o Good Path 49