Pathfinding to everywhere

Publication date: 30 August 2012
Originally published, in a shorter version, 2012 in Atomic: Maximum Power Computing
Last modified 25-Sep-2012.

 

The glory of mathematics is that it's completely abstract, by definition, but can apply to a very large number of real-world situations. The classic kid's Algebra Objection - "when am I ever going to use this?" - can have some surprising answers.

Take pathfinding, for instance.

Pathfinding is part of "graph theory", the mathematics of connected objects, which is about as broadly applicable as the name suggests. Any things that are connected physically, logically or even in some cases figuratively, with paths between them that can be traversed in some way, is amenable to graph-theory analysis.

In computer games, pathfinding is what lets an entity in a game - a unit in an RTS...

... or a monster in an RPG - get from one place to another, without being guided through every navigational choice by the player.

Pathfinding in 3D action games frequently just cheats. Since it's easy to break down most 3D-game maps into relatively few chunks each of which has relatively few paths to other locations, invisible pathfinding rails, known as "navigation meshes" or "navmeshes", can be baked into each map. So even if you've got Serious Sam monster-hordes, they'll all be able to get where they're meant to with no need for CPU-intensive thought. In open maps like the countryside in a sandbox game and the numerous open arenas in the Serious Sam series...

...entities can usually just run in a straight line to wherever they want to be (like, close to the player, in order to try to kill you), and turn only if they strike a cliff or tree.

This simplification of pathfinding and other AI functions is what made, for instance, the enemy soldiers in Half-Life seem so clever. Put a bunch of those soldiers in a small room with no behaviour hints and briefly show them Gordon Freeman through a door, and they'll immediately erase any perception of intelligence by declaring a festival of friendly fire that the monsters in Doom would consider stupid.

(Pathfinding algorithms can also be used in reverse, as it were, to create features in randomised game maps - mazes, rivers and so forth.)

RTS games have to be smarter about pathfinding, because there can be large numbers of units trying to get from A to B...

...and they're probably not allowed to walk through each other. Getting each unit to dynamically figure out where bottlenecks are and whether they should wait their turn to go through or take a longer route - which could easily be useless, suicidal or both - creates a combinatorial explosion of pathing decisions which can easily take up way too much CPU power, even on a cutting-edge PC.

Pathing techniques that can get a bunch of robot warriors through a narrow mountain pass can be used for other things, as well.

(Heck, Frozen Synapse...

Art-gallery problem

...even looks like a computational-geometry simulation.)

An unexciting but very important application for pathfinding algorithms is computer network routing, on anything from a home LAN (where routing is generally trivially simple) to the whole Internet (where it really, really isn't).

It's normal for there to be several routing steps just between you, a home Internet user, and your ISP's servers. The billions of nodes on the entire Internet connect in ever-shifting patterns, as particular routes get clogged or broken. An enormous amount of routing hardware works collaboratively to connect as much of the Internet as possible via the fastest routes possible.

Simple pathfinding algorithms are also used in any paint program that has a flood fill, or its more modern descendant the magic-wand selection tool, both of which can be thought of as "pathfinding to anywhere", starting wherever you clicked.

Flood fill affects all contiguous pixels the same colour as the one you clicked on, flowing up and then down from the selected pixel (or down and then up, or occasionally both ways at once). The magic wand does the same thing, but with a configurable allowance for how different pixel-colour can be and still be selected. This looked pretty impressive when I first watched it happen in Opal Paint on the Amiga in 1993, but modern computers are so fast that you can't see it any more.

Road navigation is a problem somewhere between flood-fill and Internet routing in complexity, because the graph of roads and towns stays pretty much static. But it has some of the real-world problems and complications of network routing, and can have more disastrous results for end-users who find themselves in a two-hour traffic jam, or directed up a dirt road and over a cliff that their GPS didn't know about.

A lot of the extra complexity comes from the fact that real-world networks usually don't have paths between nodes that're all the same, so relatively simple shortest-path calculations aren't useful. Real-world networks are, instead, usually "flow networks", in which connections between nodes have limitations in the amount and direction of traffic they can handle. Flow-network pathing can be used in a game to let an army plan to avoid a traffic jam before it actually happens, or by municipal planners to lay out traffic lights and roadworks to minimise real traffic jams, or for lots of other applications. Water and sewage transport, electricity grids, meteorology, and plenty of applications in biology and zoology as well.

Even today, computer simulation of complex analogue systems like this can be very challenging. Game programmers can't just say "OK, every unit evaluates every possible way it can get from A to B, negotiates with all of the other units to avoid jams, and then heads off". Doing that, if you've got more than a very few units, is about as easy as forecasting the weather two weeks in advance. If two friendly armies are ordered to follow paths that intersect, you need some very crafty maths to prevent a ridiculous traffic jam, or a computer-choking combinatorial explosion of calculations. Or both.

But we've actually been computer-simulating very complex systems, like the forces and control inputs affecting a car, or the behaviour of national economies, since well before the invention of the transistor. The trick is to make an analogue computer, not a digital one. If you're modelling something that behaves like a fluid flowing through various tubes and constrictions and valves and pumps and reservoirs, just build an analogous collection of plumbing, put some water in it, and start computing!

The most famous "water computers" were the Monetary National Income Analogue Computers (MONIACs; they were also rather wonderfully known as "Financephalographs"), invented in 1949.

MONIAC computer
(Image source: Flickr user psd)

They were meant to be educational simulators, but turned out to be at least as good at predicting economic events as human economists, not that that's saying much. More practically, the baffling maze of hydraulic hardware in an automatic gearbox is, in essence, an analogue computer running a gear-selection and clutch-control program dictated by its structure.

Sometimes the theory of finding paths through graphs of nodes applies obviously, as in network routing and GPS navigation. But it's lurking in loads of other areas. Blood circulation. Shifts in animal populations. What people buy, and what sellers charge. And, yes, the sometimes-sensible, sometimes-idiotic movements of Mammoth tanks and Belltower mercenaries.

So, in this case, the Algebra Objection can be answered: "Is ten times a day enough for you?"

Other columns

Learning to love depreciation

Overclockers: Get in early!

Stuff I Hate

Why Macs annoy me

USB: It's worth what you pay

"Great product! Doesn't work!"

The virus I want to see

Lies, damned lies and marketing

Unconventional wisdom

How not to e-mail me

Dan's Quick Guide to Memory Effect, You Idiots

Your computer is not alive

What's the point of robot pets?

Learning from spam

Why it doesn't matter whether censorware works

The price of power

The CPU Cooler Snap Judgement Guide

Avoiding electrocution

Video memory mysteries

New ways to be wrong

Clearing the VR hurdles

Not So Super

Do you have a license for that Athlon?

Cool bananas

Getting rid of the disks

LCDs, CRTs, and geese

Filling up the laptop

IMAX computing

Digital couch potatoes, arise!

Invisible miracles

Those darn wires

Wossit cost, then?

PFC decoded

Cheap high-res TV: Forget it.

V-Pr0n

Dan Squints At The Future, Again

The programmable matter revolution

Sounding better

Reality Plus™!

I want my Tidy-Bot!

Less go, more show

In search of stupidity

It's SnitchCam time!

Power struggle

Speakers versus headphones

Getting paid to play

Hurdles on the upgrade path

Hatin' on lithium ion

Wanted: Cheap giant bit barrel

The screen you'll be using tomorrow

Cool gadget. Ten bucks.

Open Sesame!

Absolutely accurate predictions

The truth about everything

Burr walnut computing

Nothing new behind the lens

Do it yourself. Almost.

The quest for physicality

Tool time

Pretty PCs - the quest continues

The USB drive time bomb

Closer to quietness

Stuff You Should Want

The modular car

Dumb smart houses

Enough already with the megapixels

Inching toward the NAS of our dreams

Older than dirt

The Synthetics are coming

Pr0nBack!

Game Over is nigh

The Embarrassingly Easy Case Mod

Dumb then, smart now

Fuel cells - are we there yet?

A PC full of magnets

Knowledge is weakness

One Laptop Per Me

The Land of Wind, Ghosts and Minimised Windows

Things that change, things that don't

Water power

Great interface disasters

Doughnut-shaped universes

Grease and hard drive change

Save me!

Impossible antenna, only $50!

I'm ready for my upgrade

The Great Apathetic Revolution

Protect the Wi-Fi wilderness!

Wi-Fi pirate radio

The benign botnet

Meet the new DRM, same as the old DRM

Your laptop is lying to you

Welcome to super-surveillance

Lemon-fresh power supplies

A>B>C>A!

Internet washing machines, and magic rip-off boxes

GPGPU and the Law of New Features

Are you going to believe me, or your lying eyes?

We're all prisoners of game theory

I think I'm turning cyborg-ese, I really think so

Half an ounce of electrons

Next stop, clay tablets

A bold new computer metaphor

Won't someone PLEASE think of the hard drives?!

Alternate history

From aerial torpedoes to RoboCars

How fast is a hard drive? How long is a piece of string?

"In tonight's episode of Fallout 4..."

How hot is too hot?

Nerd Skill Number One

What'll be free next?

Out: Hot rods. In: Robots.

500 gig per second, if we don't get a flat

No spaceship? No sale.

The shifting goalposts of AI

Steal This Education

Next stop: Hardware piracy

A hundred years of EULAs

The triumph of niceness

The daily grind

Speed kings

Alt-tCRASH

Game crazy

Five trillion bits flying in loose formation

Cannibalise the corpses!

One-note NPCs

Big Brother is watching you play

Have you wasted enough time today?

The newt hits! You die...

Stuck in the foothills

A modest censorship proposal

In Praise of the Fisheye

Filenames.WTF

The death of the manual

Of magic lanterns, and MMORPGs

When you have eliminated the impossible...

Welcome to dream-land

Welcome to my museum

Stomp, don't sprint!

Grinding myself down

Pathfinding to everywhere

A deadly mouse trap

If it looks random, it probably isn't

Identical voices and phantom swords

Boing!

Socialised entertainment

Warfare. Aliens. Car crashes. ENTERTAINMENT!

On the h4xx0ring of p4sswordZ

Seeing past the normal

Science versus SoftRAM

Righteous bits

Random... ish... numbers

I get letters

Money for nothing



Give Dan some money!
(and no-one gets hurt)