AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# –...

43
CS 4730 AI and Pathfinding CS 4730 – Computer Game Design Some slides courtesy Tiffany Barnes, NCSU

Transcript of AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# –...

Page 1: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

AI  and  Pathfinding  

CS  4730  –  Computer  Game  Design    

Some  slides  courtesy  Tiffany  Barnes,  NCSU  

Page 2: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

AI  Strategies  •  ReacAon  vs.  DeliberaAon  • When  having  the  NPC  make  a  decision,  how  much  thought  goes  into  the  next  move?  

•  How  is  the  AI  different  in:  –  Frozen  Synapse  –  Kingdom  Hearts  –  CivilizaAon  – Halo  

2

Page 3: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

AI  Strategies  •  ReacAon-­‐Based  

–  Fast,  but  limited  capabiliAes  

•  ImplementaAons  –  Finite-­‐State  Machines  –  Rule-­‐Based  Systems  –  Set  PaVern  

3

Page 4: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

AI  Strategies  •  DeliberaAon-­‐Based  

– Much  slower,  but  more  adaptable  

•  ImplementaAons  – A*  /  Dijkstra  –  Roadmaps  – GeneAc  Algorithms  

4

Page 5: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

DeliberaAon  •  Goal  is  to  emphasize  making  the  best  possible  decision  by  searching  across  all  possibiliAes  

•  Thus,  deliberaAon  tends  to  use  various  search  algorithms  across  data  structures  that  contain  the  opAon  space  

5

Page 6: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

“Sense  –  Plan  –  Act”  •  Sense  (or  perceive)  a  sufficiently  complete  model  of  the  world  

•  Plan  by  searching  over  possible  future  situaAons  that  would  result  from  taking  various  acAons  

•  Act  by  execuAng  the  best  course  of  acAon  •  Each  possible  outcome  is  effecAvely  scored  by  a  “metric  of  success”  that  indicates  whether  the  choice  should  be  taken  or  not  

6

Page 7: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

DeliberaAve  vs.  ReacAve  •  These  are  NOT  mutually  exclusive!  •  You  can  have  reacAve  policies  for  immediate  threats    –  Incoming  PC  fire  –  Environmental  destrucAon  

•  And  you  can  have  deliberaAve  policies  for  long-­‐term  planning  –  Build  orders  and  posiAoning  

7

Page 8: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

Core  QuesAons  •  How  do  you  represent  knowledge  about  the  current  task,  environment,  PC,  etc?  

•  How  do  you  find  acAons  that  allow  the  goal  to  be  met?  

8

Page 9: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

Knowledge  RepresentaAon  •  A  decision  tree  is  a  common  way  to  represent  knowledge  

•  The  root  node  is  the  current  state  of  the  game  state  WRT  the  NPC  /  AI  

•  Thus,  deliberaAve  AI  techniques  are  effecAvely  versions  of  search  and  shortest-­‐path  algorithms!  

9

Page 10: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

Tic-­‐Tac-­‐Toe  • What  is  the  decision  tree  for  Tic-­‐Tac-­‐Toe?  

10

Page 11: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

The  Goal  State  •  The  objecAve  of  the  decision  tree  model  is  to  move  from  the  iniAal  game  state  (root  of  the  tree)  to  the  goal  state  of  the  NPC  – What  might  a  goal  state  be?  

• When  searching  through  the  decision  tree  space,  which  path  do  we  take?  

11

Page 12: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

Cost  and  Reward  •  Every  choice  has  a  cost  

– Ammo  – Movement  –  Time  –  Increase  vulnerability  to  aVack  

•  Every  choice  has  a  reward  – Opportunity  to  hit  PC  –  Capture  a  strategic  point  – Gain  new  resources  

12

Page 13: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

Minimax  Algorithm  •  Find  the  path  through  the  decision  tree  that  yields  the  best  outcome  for  one  player,  assuming  the  other  player  always  makes  a  decision  that  would  lead  to  the  best  outcome  for  themselves  

13

Page 14: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

Naïve  Search  Algorithms  •  Breadth-­‐First  Search  

– At  each  depth,  explore  every  opAon  at  the  next  depth  

•  Depth-­‐First  Search  –  Fully  explore  one  possible  path  to  its  “conclusion”,  then  backtrack  to  check  other  opAons  

• What  are  the  problems  with  these  techniques  in  gaming?  

14

Page 15: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

Breadth-­‐First  Search  •  Expand  Root  node  

–  Expand  all  Root  node’s  children  •  Expand  all  Root  node’s  grandchildren  

•  Problem:  Memory  size  

Root Root

Child1 Child2 Root

Child1 Child2

GChild1 GChild2 GChild3 GChild4

Page 16: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

Uniform  Cost  Search  • Modify  Breadth-­‐First  by  expanding  cheapest  nodes  first  

• Minimize  g(n)  cost  of  path  so  far  

Root

Child1 Child2

GChild1 9

GChild2 5

GChild3 3

GChild4 8

Page 17: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

Depth  First  Search  •  Always  expand  the  node  that  is  deepest  in  the  tree  

Root

Child1

GChild1 GChild2

Root

Child1 Root

Child1

GChild1

Page 18: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

Adding  a  HeurisAc  •  Simple  definiAon:  a  heurisAc  is  a  “mental  shortcut”  to  ignore  non-­‐useful  states  to  limit  the  search  space  and  make  the  decision  tree  more  reasonable  

• What  metrics  might  we  use  to  determine  “the  value”  of  a  potenAal  opAon?  

• What  metrics  might  we  use  to  determine  “the  cost”  of  a  potenAal  opAon?  

18

Page 19: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

Adding  a  HeurisAc  •  CreaAng  an  AI  heurisAc  forms  the  basis  of  how  the  NPCs  will  behave  – Will  they  ignore  enemies  that  are  farther  than  X  away?  

– Will  they  avoid  water?  – Will  they  always  move  in  the  straightest  path  to  the  PC?  

19

Page 20: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

Cheaper  Distance  First!  

20

Page 21: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

Greedy  Search  •  Expand  the  node  that  yields  the  minimum  cost    

–  Expand  the  node  that  is  closest  to  target  – Depth  first  – Minimize  the  funcAon  h(n)  the  heurisAc  cost  funcAon  

Page 22: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

Greedy  Search  

22

Page 23: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

Greedy  Search  

23

Page 24: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

Greedy  Search  •  Greedy  gives  us  (ojen)  a  sub-­‐opAmal  path,  but  it’s  really  cheap  to  calculate!  

•  How  can  we  improve  on  this?  •  Add  another  aspect  to  the  funcAon  –  the  cost  of  the  node  +  the  heurisAc  distance  

24

Page 25: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

A*  •  A  best-­‐first  search  (using  heurisAcs)  to  find  the  least-­‐cost  path  from  the  iniAal  state  to  the  goal  state  

•  The  algorithm  follows  the  path  of  lowest  expected  cost,  keeping  a  priority  queue  of  alternate  path  segments  along  the  way  

25

Page 26: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

A*  Search  • Minimize  sum  of  costs  •  g(n)  +  h(n)  

–  Cost  so  far  +  heurisAc  to  goal  •  Guaranteed  to  work  

–  If  h(n)  does  not  overesBmate  cost  •  Examples  

–  Euclidean  distance  

Page 27: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

A*  

27

Page 28: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

A*  

28

Page 29: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

A*  

29

Page 30: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

NavigaAon  Grid  

30

Page 31: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

Pathfinding  in  “real  life”  •  These  algorithms  work  great  when  the  game  is  grid  based  –  Square  grid  – Hex  grid  

•  For  more  “open”  games,  like  FPSs:  –  Path  nodes  are  placed  on  the  map  that  NPCs  can  reach  

– NavigaAon  mesh  layers  are  added  over  the  terrain  – Ojen  done  automaAcally  in  advanced  engines  

31

Page 32: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

NavigaAon  Mesh  •  Instead  of  using  discrete  node  locaAons,  a  node  in  this  instance  is  a  convex  polygon  

•  Every  point  inside  a  valid  polygon  can  be  considered  “fair  game”  to  move  into  

•  NavigaAon  meshes  can  be  auto  generated  by  the  engine,  so  easier  to  manage  than  nodes  

•  Can  also  handle  different  sized  NPCs  by  checking  collisions  

32

Page 33: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

NavigaAon  Mesh  

33

Page 34: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

Groups  •  Groups  stay  together  

– All  units  move  at  same  speed  – All  units  follow  the  same  general  path  – Units  arrive  at  the  same  Ame  

ObstrucAon  Goal  

Page 35: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

Groups  •  Need  a  hierarchical  movement  system  •  Group  structure  

– Manages  its  own  prioriAes  –  Resolves  its  own  collisions  –  Elects  a  commander  that  traces  paths,  etc  

• Commander  can  be  an  explicit  game  feature  

Page 36: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

FormaAons  •  Groups  with  unit  layouts  

–  Layouts  designed  in  advance  •  AddiAonal  States  

–  Forming  –  Formed  –  Broken  

•  Only  formed  formaAons  can  move  

Page 37: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

FormaAons  •  Schedule  arrival  into  posiAon  

–  Start  at  the  middle  and  work  outwards  –  Move  one  unit  at  a  Ame  into  posiAon  –  Pick  the  next  unit  with  

•  Least  collisions  •  Least  distance  

–  Formed  units  have  highest  priority  •  Forming  units  medium  priority  •  Unformed  units  lowest  

Page 38: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

FormaAons  

1 2

3

4

5 6

7

8

9

1 2 3

4 5

6 7 8 9

1 2

3

4

5 6

7

8

9

Not so good…

Better…

Page 39: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

FormaAons:  Wheeling  •  Only  necessary  for  non-­‐symmetric  formaAons  

1 2 3 4 5

1

2

3

4

5

Break  formaAon  here  Stop  moAon  temporarily  

Set  re-­‐formaAon  point  here  

Page 40: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

FormaAons:  Obstacles  

1 2 3 4 5 Scale  formaAon  layout  to    fit  through  gaps  

1 2 3 4 5 1 2 3 4 5

1 2 3 4 5 Subdivide  formaAon    around  small  obstacles  

Page 41: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

FormaAons  •  Adopt  a  hierarchy  of  paths  to  simplify  path-­‐planning  problems  

•  High-­‐level  path  considers  only  large  obstacles  –  Perhaps  at  lower  resoluAon  –  Solves  problem  of  gross  formaAon  movement  –  Paths  around  major  terrain  features  

Page 42: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

AI  That  Learns  •  Imagine  a  player  in  Madden  calls  a  parAcular  play  on  offense  over  and  over  and  over  

•  The  heurisAc  values  for  certain  states  should  change  to  reflect  a  more  opAmal  strategy  

•  Now,  the  adjustment  of  heurisAc  values  represents  long-­‐term  strategy  (to  a  degree)  

42

Page 43: AI#and#Pathfinding# - University of Virginia · CS4730 AIStrategies# • ReacAonTBased# – Fast,#butlimited#capabiliAes# • Implementaons# – FiniteTState#Machines# – RuleTBased#Systems#

CS  4730  

AI  That  Evolves  •  Neural  networks  add  in  a  mutaAon  mechanic  •  Bayesian  networks  can  also  learn  and  add  inferences  

•  How  much  processing  power  can  we  use  for  this?  

•  Is  it  beVer  to  truly  have  a  “learning”  AI,  or  should  we  just  adjust  some  game  parameters?  

43