A* Pathfinding

among other things

Pathfinding

....................
.S..................
....................
..................G.
....................
  • Common application: AI in games
  • Consider:
    • 2D normalized grid
    • A starting position (S)
    • A goal position (G)
  • Find a path with the lowest cost to get from S to G
    • (Could be more than one! but any will do)
....................
.S..................
.x..................
.xxxxxxxxxxxxxxxxxG.
....................

How It Works

  • A* is a best-first search -- a heuristic
    • It goes step-by-step and looks at its immediate options
      • e.g. in the grid example, from the starting point:
        • North 1 step
        • East 1 step
        • South 1 step
        • West 1 step
    • It evaluates each step and assigns them value
      • This value is some combination of 1) cost and 2) how much closer it approaches its goal

In Math Terms

For current node n:

f(n) = g(n) + h(n)

total cost = cost of path so far + n's estimated cost

  • Aiming for the smallest value of f(n)
  • Need to define h(n) depending on problem
  • In grid example:
    • g(n) = total cost of previous nodes travelled
    • h(n) = let's say, the Cartesian distance to goal
      • L-shaped distance, not straight line
      • Could be wrong -- could have obstacles or higher cost nodes. But it's a good estimate!

Iterating Through

  • We have a list of open nodes -- starts with our immediate choices, and will get bigger
  • We have a list of closed nodes -- nodes we already considered. No need to redo work.
  • We choose a node to visit based on its f(n). Whichever open node has the smallest f(n) is the one we visit next.
  • When we visit an open node, we add its neighboring nodes to the open list and close the node we're visiting.

Iterating Through

  • At the start of the grid example, we have four open nodes
    • North, East, South, West -- all immediately from the center
    • All of these nodes have g(n) = 0 since this is the start
    • North and West have h(n) = 20
    • South and East have h(n) = 18
    • Therefore, we'll visit either one of South or East first
.O..................
OSO.................
.O..................
..................G.
....................

Iterating Through

  • The choice between ties is largely arbitrary, so let's choose East for now
    • (don't use rand(), though -- be sane and do something like choosing the one with the smaller pointer address if you have to)
  • Visit East, add its neighbors to the open list, close East, and reconsider our new options
    • And so on until we get to the goal
.OO.................
OSXO................
.OO.................
..................G.
....................

Animated

"Pathfinding"

  • Okay, really, it's graph traversal
    • Technically a subset of Dijkstra's algorithm!
  • But one of its most common applications is pathfinding in games
    • Other applications include robotics or even parsing natural language (i.e. computers forming sentences and communicating in plain English or other languages)

"Pathfinding"

Don't constrain yourself to this example:

....................
.S..................
....................
..................G.
....................

Think of it more like this, in more general terms:

and so on

start

north

east

south

west

Mario AI

  • Inputs are a bitmask of combinations of inputs
  • Up Down Left Right Jump Speed
    • 000000
    • 000001
    • etc.
  • goal is just x-axis on the far-right
  • g(n) is number of frames or x-axis -- either's plausible
  • h(n) is estimated distance to goal
    • obstacles and damage have either high or infinite cost

GOAP

  • Pronounced like "soap"
  • Goal Oriented Action Programming
  • Subset of problems solved with A*, but with the intent to tackle strategy instead of navigating some kind of physical space
  • Team that worked on F.E.A.R. has a lot of literature on why they took this approach

Why GOAP?

  • Team's previous games implemented strategy AI via a finite state machine
  • Lots of if-then style coding, making minor variations on enemies took a lot of design and programming time
  • Ended up with a lot of unmaintainable, duplicated code

What GOAP does instead

  • GOAP decouples the details of the FSM logic and lets the AI figure it out on their own in real-time
  • An actor has a list of actions -- things they can do in-game that change the world state
  • An actor has a desired goal state -- actions change state

How this changed things

  • Programmers made one thing and handed it to designers
    • Instead of needing to update a massive codebase due to desired designer updates
  • Enemy designing process changed from tedious maintenance to just slapping together a few actions and a goal state onto an enemy
    • Able to churn out higher quantity of enemies with much more variety

Decoupling!

Questions?

astar

By tdhoward

astar

A* pathfinding

  • 627