Sub topic 7
Admissible Heuristics: Never Overestimate
This chapter assumes you know the A* loop from the earlier chapters: nodes are popped from a priority queue ordered by f(n) = g(n) + h(n). You should also be comfortable with Dijkstra's algorithm and Python dictionaries and heaps. Here we look at the first property that makes a heuristic trustworthy: it must never overestimate.
The Definition
Let h\(n) be the true cost of the cheapest path from node n to a goal. A heuristic h is admissible* if, for every node n:
- h(n) ≤ h\*(n)
- h(n) ≥ 0
These two conditions imply that h(goal) = 0, because h\*(goal) = 0.
An admissible heuristic is an optimist. It can say "the goal is closer than it really is," but it never says "the goal is farther than it really is." Admissibility is one of the two conditions behind A*'s optimality guarantee. The other condition, and the full proof, come in later chapters.
The trivial heuristic h(n) = 0 is always admissible. With it, A turns into uniform-cost search. The perfect heuristic h(n) = h\(n) is also admissible, and with it A* expands only nodes on an optimal path, apart from ties. Every useful heuristic sits between these two.
Why Overestimating Breaks A*
A* stops when it pops a goal. Popping the goal with value f means no other open node had a smaller f. When h never overestimates, each open node's f is a lower bound on the cost of any solution through that node. So "nothing open has a smaller f" means "nothing cheaper is still out there."
An overestimate breaks that reasoning. A node on the optimal path can be given an inflated f. It then sits in the queue while A* pops a worse goal.
Here is what happens with h(S) = 0, h(A) = 4, and h(B) = 10. The true value h\*(B) is 2, so h(B) = 10 overestimates.
- Pop S. Push A with f = 1 + 4 = 5 and B with f = 2 + 10 = 12.
- Pop A (f = 5). Push G with f = 6 + 0 = 6.
- Pop G (f = 6). Return cost 6.
The correct answer is 4. The code below reproduces this. It repeats the A* function from the implementation chapter so that the file runs on its own.
import heapq
from itertools import count
def astar(start, goal, neighbors, h):
tie = count()
open_heap = [(h(start), next(tie), start)]
g = {start: 0}
parent = {start: None}
closed = set()
while open_heap:
_, _, node = heapq.heappop(open_heap)
if node == goal:
path = []
while node is not None:
path.append(node)
node = parent[node]
return path[::-1], g[goal]
if node in closed:
continue
closed.add(node)
for nxt, cost in neighbors(node):
new_g = g[node] + cost
if new_g < g.get(nxt, float('inf')):
g[nxt] = new_g
parent[nxt] = node
heapq.heappush(open_heap, (new_g + h(nxt), next(tie), nxt))
return None, float('inf')
graph = {
'S': [('A', 1), ('B', 2)],
'A': [('G', 5)],
'B': [('G', 2)],
'G': [],
}
neighbors = lambda n: graph[n]
bad_h = {'S': 0, 'A': 4, 'B': 10, 'G': 0}
good_h = {'S': 0, 'A': 5, 'B': 2, 'G': 0}
print(astar('S', 'G', neighbors, bad_h.get)) # (['S', 'A', 'G'], 6)
print(astar('S', 'G', neighbors, good_h.get)) # (['S', 'B', 'G'], 4)With good_h, each value equals the true remaining cost. B gets f = 4, which beats A's f = 6, and the optimal route is found.
Checking Admissibility Empirically
When proving admissibility is hard, or you want to test a heuristic you already proved, you can measure it. Compute h\* exactly by running Dijkstra backward from the goal over reversed edges. Then compare every h(n) against it. This works on test maps small enough to search exhaustively, and it catches bugs that a proof on paper misses.
import heapq
import math
def true_costs_to_goal(nodes, neighbors, goal):
reverse = {n: [] for n in nodes}
for u in nodes:
for v, cost in neighbors(u):
reverse[v].append((u, cost))
dist = {goal: 0.0}
heap = [(0.0, goal)]
while heap:
d, v = heapq.heappop(heap)
if d > dist[v]:
continue
for u, cost in reverse[v]:
nd = d + cost
if nd < dist.get(u, math.inf):
dist[u] = nd
heapq.heappush(heap, (nd, u))
return dist
def admissibility_violations(nodes, neighbors, goal, h, eps=1e-9):
hstar = true_costs_to_goal(nodes, neighbors, goal)
bad = []
for n in nodes:
star = hstar.get(n, math.inf)
if h(n) > star + eps:
bad.append((n, h(n), round(star, 3)))
return bad
def grid8(rows, cols):
def neighbors(cell):
r, c = cell
for dr in (-1, 0, 1):
for dc in (-1, 0, 1):
if dr == 0 and dc == 0:
continue
nr, nc = r + dr, c + dc
if 0 <= nr < rows and 0 <= nc < cols:
yield (nr, nc), math.sqrt(2) if dr and dc else 1.0
return neighbors
cells = [(r, c) for r in range(3) for c in range(3)]
goal = (2, 2)
def manhattan(n):
return abs(n[0] - goal[0]) + abs(n[1] - goal[1])
def octile(n):
dx, dy = abs(n[0] - goal[0]), abs(n[1] - goal[1])
return (dx + dy) + (math.sqrt(2) - 2) * min(dx, dy)
print(admissibility_violations(cells, grid8(3, 3), goal, manhattan))
# [((0, 0), 4, 2.828), ((0, 1), 3, 2.414), ((1, 0), 3, 2.414), ((1, 1), 2, 1.414)]
print(admissibility_violations(cells, grid8(3, 3), goal, octile))
# []The tolerance eps matters. Floating-point sums of √2 can drift by a few units in the last place, and you do not want false alarms from rounding. Unreachable nodes have h\* = ∞, so any finite h is admissible there.
Common Ways Heuristics Overestimate
A heuristic that is admissible for one problem can stop being admissible after a small change to the problem. The usual causes:
| Cause | Example | Fix |
|---|---|---|
| Movement model changed | Manhattan distance on an 8-connected grid: one diagonal step costs √2, but Manhattan charges 2 | Use octile (diagonal cost √2) or Chebyshev (diagonal cost 1) |
| Cheap terrain | Roads cost 0.5 per cell, but h assumes 1 per cell | Multiply h by the minimum per-step cost |
| Units mismatch | Edge costs in seconds, h in meters | Divide distance by the maximum speed |
| Shortcuts in the graph | Portals or teleporters connect distant cells | Use min(direct estimate, distance to nearest portal entry + distance from portal exit to goal) |
| Multiple goals | h measures distance to one fixed goal | Take the minimum over all goals |
| Hand-tuned bonus | Adding a "penalty" constant to discourage some cells | Put penalties in edge costs, not in h |
The terrain and units cases follow the same pattern. A distance heuristic is admissible only if each unit of distance costs at least what the heuristic charges for it. The real requirement is h(n) ≤ (straight-line or grid distance) × (cheapest cost per unit distance).
Combining Admissible Heuristics
Suppose you have several admissible heuristics h₁, …, hₖ. These combinations stay admissible:
- max(h₁, …, hₖ): each one is at most h\*, so the largest is too. This is the standard way to combine heuristics, because it gives the most informed admissible value at each node.
- Any convex combination such as 0.5·h₁ + 0.5·h₂: a weighted average of numbers that are each ≤ h\ is also ≤ h\. It is never larger than the max, so the max is usually the better choice.
- c · h for 0 ≤ c ≤ 1: scaling down keeps admissibility but weakens the guidance.
These combinations can break admissibility:
- h₁ + h₂: the sum overestimates when both heuristics count the same cost. Sums are safe only when the components count disjoint parts of the cost. Pattern databases are built on that idea later in the tutorial.
- c · h for c > 1: this is weighted A*. It gives up exact optimality on purpose, in exchange for a bounded cost ratio, as a later chapter shows.
Admissible Is Not the Same as Good
Admissibility is a correctness property. It says nothing about speed. h = 0 is admissible and gives you Dijkstra, which explores in every direction. Among admissible heuristics, larger values prune more nodes, because A must expand every node whose f is below the optimal cost. A heuristic close to h\ keeps that set small. The chapter on dominance turns this into a precise comparison. The chapter on relaxed problems shows a systematic way to get large admissible values. For now, the rule is to get as close to h\* as you can without going over.
A heuristic also has to be cheap to compute. A* evaluates h at every node it generates. A heuristic that takes as long as a search to compute saves nothing.
Exercise
A road map has edge costs equal to travel time in minutes. The fastest road allows 120 km/h. You propose h(n) = straight-line distance in km from n to the goal. Is h admissible? If not, how do you fix it?
Show answer
It is not admissible in general. At 120 km/h, a car covers 2 km per minute, so a 10 km straight-line gap can take as little as 5 minutes, while h says 10. The fix is to convert to time at the maximum speed: h(n) = distance_km / 2, which gives minutes. No route can beat the straight line driven at top speed, so this never overestimates.
Exercise
On a 4-connected grid with unit step costs, you have h1 = Manhattan distance and h2 = Euclidean distance to the goal. Which of these are admissible: max(h1, h2), h1 + h2, 0.7h1 + 0.3h2, and 1.2*h2?
Show answer
Both h1 and h2 are admissible on this grid, and h1 ≥ h2 everywhere. max(h1, h2) = h1 is admissible. 0.7h1 + 0.3h2 is a convex combination and is admissible. h1 + h2 is not: next to the goal it gives 1 + 1 = 2 when the true cost is 1. 1.2*h2 is not admissible in general: for a cell 5 steps away in a straight line, it gives 6.
Exercise
A grid has two goal cells, G1 and G2, and A* may stop at either one. Your teammate uses h(n) = Manhattan distance to G1. Give a situation where this overestimates, and write an admissible replacement.
Show answer
If n is right next to G2 but far from G1, h(n) is large while the true cost is 1. That is an overestimate, so A* may ignore the nearby goal. Use h(n) = min(manhattan(n, G1), manhattan(n, G2)). Each term is a lower bound on the cost to its own goal, so the minimum is a lower bound on the cost to the nearest goal.