Heuristics: smart shortcuts in a world of uncertainty

An outfielder (baseball catcher) runs backwards to catch a high ball. We could describe what he is doing as a ballistics problem: initial velocity, launch angle, air resistance, crosswind, and a quick integration to predict where it will land. Except he does none of that. What he does is fix his eyes on the ball and move so that the angle of his gaze stays constant. That is all. If the angle rises, he backs up; if it drops, he moves in. He never works out where the ball is going to land, and he does not even need to know: he gets there anyway.

That is a heuristic, and it is worth understanding why it is not a botched version of the proper calculation. A heuristic is a procedure that reaches a reasonably good solution with limited information, limited time and limited resources. An exact algorithm promises the best possible answer once it has walked the entire space of solutions. A heuristic gives up that promise and takes something that is worth more in the real world: it arrives on time. It is a calculated bet. This will probably work, and it will work before it stops mattering.

Underneath it lies a condition nobody escapes: we are not omniscient. No organism with finite energy and a deadline can afford to weigh every variable before acting. The predator that stops to compute the perfect trajectory of its prey starves; the one that applies “chase whatever runs away” eats, survives, and passes the rule on. Herbert Simon, an economist and one of the founding figures of artificial intelligence, gave this a name back in the nineteen-fifties and eventually won the Nobel in Economics for it. He called it satisficing, a splice of satisfy and suffice: we do not look for the optimal option, we look for the first one that clears the bar of good enough, because carrying on searching has a cost of its own. Rationality is not evaluating everything. It is knowing when to stop.

Two Israeli psychologists, Daniel Kahneman and Amos Tversky, spent the nineteen-seventies cataloguing the shortcuts the human mind actually uses, and the whole field of behavioural economics grew out of it (Kahneman would receive the Nobel in 2002; Tversky had died years earlier). Among them, the availability heuristic (judging how likely something is by how easily an example springs to mind) and anchoring (getting stuck on the first number you hear). We tend to file these as defects, and that is where a misunderstanding creeps in. Biases are not faults in the system: they are the invoice for the shortcut. Availability works beautifully when the things you recall easily are the things that genuinely happen often, which is how it was for almost the entire history of our species. It fails when someone fills your memory with plane crashes on prime-time television. The rule has not turned stupid; the environment it was tuned for has shifted beneath its feet.

Proverbs are exactly this: heuristics, compressed and made portable. “A bird in the hand is worth two in the bush” encodes risk aversion in the face of uncertain gains, something behavioural economics would formalise centuries later. Weather sayings are climate models that work without any meteorology. They are approximate algorithms passed down by word of mouth, and their virtue is that no generation has to redo the calculation from scratch.

In computing, this same logic stopped being a convenience and became the only way out. Many real problems are NP-hard: the number of combinations grows so fast that no computer could walk through them all before the universe cools off. There is no heroism to be had there. Either you prune the search space, or there is no answer.

The most elegant case of that pruning is called A* (pronounced “A star”), a pathfinding algorithm invented in 1968 so that a robot named Shakey, the first machine capable of reasoning about its own movements, could work out how to cross a room without taking forever to think about it. Today it is what your GPS uses to route you and what moves the enemies in almost any video game. The idea is simple: to decide which path to explore before another, it adds two things together, the real cost of the ground already covered and an estimate of the ground still left.

import heapq

def a_star(graph, start, goal, heuristic):
    open_set = [(0, start)]
    real_cost = {start: 0}
    parents = {start: None}

    while open_set:
        _, current = heapq.heappop(open_set)
        if current == goal:
            path = []
            while current:
                path.append(current)
                current = parents[current]
            return path[::-1]

        for neighbour, cost in graph[current].items():
            new_cost = real_cost[current] + cost
            if neighbour not in real_cost or new_cost < real_cost[neighbour]:
                real_cost[neighbour] = new_cost
                priority = new_cost + heuristic(neighbour, goal)
                heapq.heappush(open_set, (priority, neighbour))
                parents[neighbour] = current
    return None

That heuristic function, typically the straight-line distance to the goal, does not compute the route: it guesses at it. And here is the part almost everyone gets wrong. If the estimate never overshoots, meaning it never claims there is less distance left than there really is (what is known as an admissible heuristic), then A* returns the optimal path. Not an acceptable one: the best one. What the hunch buys is not a worse answer in exchange for speed, but the very same answer while exploring a fraction of the graph. And there is a beautiful way to see this: set the heuristic to zero, that is, strip the algorithm of any intuition about which way the goal lies, and A* turns into precisely Dijkstra’s algorithm, the classic method from 1956 that finds the shortest path by expanding in every direction at once, like a spreading oil stain. Dijkstra always gets it right too. It just looks under a great many more stones.

Other methods do pay the classic price. Simulated annealing, genetic algorithms and greedy strategies for the travelling salesman problem hand you solutions that are “good enough” with no guarantee of being the best. And there lies the distinction that actually matters, the one that gets lost when we throw every heuristic into the same bag: some sacrifice optimality, others sacrifice only exhaustiveness. Knowing which one you are holding is the difference between a shortcut and a bodge.

Because heuristics do fail, and they fail in a recognisable way: when the environment stops resembling the one they were tuned for. The negotiator who opens with an absurd figure is exploiting your anchoring. The migrating bird that navigates by the sun gets lost under a storm that lasts a week. The shortcut is not wise, it is merely well fitted to a particular world, and that world can change.

Even so, there is no alternative, and that is the whole point. A heuristic is not a degraded version of perfect reasoning: it is the native way of reasoning for any system, biological or artificial, that has to act against a clock, on a budget, and without seeing everything. Which is to say, any system at all. The opposite of a heuristic is not certainty. It is standing still while the ball comes down.