Sub topic 8
Consistent Heuristics and the Triangle Inequality
This chapter assumes you know the A* loop from the implementation chapter, with its heap, g table, parent map, and closed set. It also assumes you know what an admissible heuristic is. Admissibility on its own is not enough for that implementation to be correct. Consistency is the stronger property the closed set relies on.
The definition
A heuristic h is consistent (also called monotone) when, for every edge from n to n' with cost c(n, n'):
h(n) <= c(n, n') + h(n')h(goal) = 0
This is the triangle inequality. The estimate from n can be no larger than the cost of taking one step and then trusting the estimate from where you land. If it were larger, the heuristic would contradict itself: from n it says "far", and one cheap step later it says "close".
What consistency buys you
| Property | Admissible only | Consistent |
|---|---|---|
| `h(n) <= h*(n)` (true remaining cost) | Yes | Yes (implied) |
| `f` never decreases along a path | No | Yes |
| `g(n)` is optimal when `n` is first popped | No | Yes |
| Safe to skip closed nodes forever | No | Yes |
Consistency implies admissibility. To see why, apply the inequality along an optimal path from n to the goal and sum the steps. h(n) is then bounded by the path's total cost plus h(goal) = 0.
Consistency also makes f monotone. For an edge n -> n', f(n') = g(n) + c + h(n') >= g(n) + h(n) = f(n). Because of this, A* pops nodes in nondecreasing order of f, and the first time a node comes off the heap, no cheaper route to it can still be waiting. That guarantee is what makes the line if node in closed: continue correct.
When admissible is not enough
The graph below has an admissible heuristic that breaks the triangle inequality on the edge from B to A.
Every value is admissible. The true remaining costs are 6 for S, 5 for B, and 4 for A. Here is how the trace goes:
- A* pops S. It pushes A with
f = 4and B withf = 6. - It pops A first, closes it with
g = 4, and pushes G withg = 8. - It pops B and finds A at
g = 2. It pushes A again, but A is already closed, so the new entry is skipped. - It pops G and returns cost 8, which misses the optimal cost of 6.
The check below is a standalone function that finds violating edges:
graph = {
'S': [('A', 4), ('B', 1)],
'B': [('A', 1)],
'A': [('G', 4)],
'G': [],
}
h_bad = {'S': 0, 'A': 0, 'B': 5, 'G': 0}
h_good = {'S': 0, 'A': 4, 'B': 5, 'G': 0}
def inconsistent_edges(graph, h, goal):
bad = [(n, m) for n, edges in graph.items()
for m, c in edges if h[n] > c + h[m]]
if h[goal] != 0:
bad.append((goal, goal))
return bad
print(inconsistent_edges(graph, h_bad, 'G')) # [('B', 'A')]
print(inconsistent_edges(graph, h_good, 'G')) # []With the astar function from the implementation chapter defined in the same session, you can reproduce the failure and the fix:
neighbors = lambda n: graph[n]
print(astar('S', 'G', neighbors, lambda n: h_bad[n])[1]) # 8
print(astar('S', 'G', neighbors, lambda n: h_good[n])[1]) # 6Common errors
- Checking only admissibility. Hand-tuned or learned heuristics are often admissible and still inconsistent.
- Forgetting
h(goal) = 0. A nonzero goal estimate breaks the summing argument. - Mismatched movement model. Pairing a heuristic with the wrong move costs can break the inequality. Octile distance on a grid where diagonal moves cost 1 is one example.
- Taking the max of heuristics. The maximum of consistent heuristics is consistent, so this is safe. Averages of admissible heuristics that you then scale up are not.
Exercise
On an undirected graph, edge (X, Y) costs 2, h(X) = 7, and h(Y) = 4. Is h consistent on this edge? Check both directions.
Show answer
X to Y: 7 <= 2 + 4 = 6 fails, so h is inconsistent. Y to X: 4 <= 2 + 7 holds. For an undirected edge, consistency requires |h(X) - h(Y)| <= c, and here 3 > 2.
Exercise
Why does consistency let A* skip a node already in the closed set?
Show answer
With a consistent h, f never decreases along any path, so nodes are popped in nondecreasing f order. When a node is first popped, every other route to it has f at least as large and therefore g at least as large. Its g is already optimal and it never needs to be reopened.