Sub topic 10
Choosing a Heuristic for Grid Maps
This chapter assumes you know what an admissible heuristic is (one that never overestimates the remaining cost) and have the astar function from the implementation chapter. On grid maps, the right heuristic depends almost entirely on how the agent is allowed to move and what each move costs.
Match the Heuristic to the Movement Model
A good grid heuristic computes the exact shortest path cost on an empty grid with the same movement rules. That makes it admissible, because obstacles can only make the real path longer. It is also as large as it can be without overestimating.
| Movement model | Step costs | Best heuristic | Formula (dx, dy = absolute differences) |
|---|---|---|---|
| 4-connected | 1 per orthogonal step | Manhattan | dx + dy |
| 8-connected | 1 orthogonal, √2 diagonal | Octile | dx + dy + (√2 − 2)·min(dx, dy) |
| 8-connected | 1 for any step | Chebyshev | max(dx, dy) |
| Any-angle | Straight-line length | Euclidean | √(dx² + dy²) |
Euclidean distance is admissible for every model in the table, because no path can be shorter than a straight line. On a 4-connected grid it is weaker than Manhattan, so A* expands more nodes than it needs to.
Comparing the Values
For the same pair of cells, the heuristics give quite different estimates:
import math
def manhattan(a, b):
return abs(a[0] - b[0]) + abs(a[1] - b[1])
def euclidean(a, b):
return math.hypot(a[0] - b[0], a[1] - b[1])
def chebyshev(a, b):
return max(abs(a[0] - b[0]), abs(a[1] - b[1]))
def octile(a, b):
dx, dy = abs(a[0] - b[0]), abs(a[1] - b[1])
return (dx + dy) + (math.sqrt(2) - 2) * min(dx, dy)
a, b = (0, 0), (3, 5)
for h in (manhattan, octile, euclidean, chebyshev):
print(f"{h.__name__:10s} {h(a, b):.3f}")
# manhattan 8.000
# octile 6.243
# euclidean 5.831
# chebyshev 5.000On an 8-connected grid with √2 diagonals, the true empty-grid cost from (0, 0) to (3, 5) is 3 diagonal steps plus 2 straight steps, which is 3√2 + 2 ≈ 6.243. Octile gives exactly that. Euclidean and Chebyshev give less, so they are admissible but less informed. Manhattan gives 8, which is too high.
Common Errors
- Unit mismatch. If each step costs 10 (a common trick to avoid floats), scale the heuristic by 10 as well. A heuristic in different units from g is either too weak or inadmissible.
- Terrain costs. If some cells cost more than 1, base the heuristic on the cheapest step cost. Multiply the formula by the minimum cost per step, not the average.
- Corner cutting. If diagonal moves through blocked corners are forbidden, octile stays admissible. The rule only removes moves, and removing moves never shortens a path.
Exercise
An agent moves on an 8-connected grid where diagonal steps cost √2. Compute the Manhattan and octile values from (0, 0) to (2, 2) and the true empty-grid cost. Which heuristic is admissible?
Show answer
The true cost is two diagonal steps, 2√2 ≈ 2.828. Octile gives 4 + (√2 − 2)·2 ≈ 2.828, which is exact and therefore admissible. Manhattan gives 4, which overestimates, so it is not admissible for this movement model.
Exercise
On a 4-connected grid with unit costs, why might you still choose Manhattan over Euclidean, given that both are admissible?
Show answer
Manhattan is always greater than or equal to Euclidean, and it equals the true cost on an empty grid. The larger admissible estimate guides A* more tightly toward the goal, so it expands fewer nodes and still returns an optimal path.