Sub topic 3
Choosing a Heuristic for Grid Maps
This chapter assumes you already know the astar function from the previous chapter, and what admissible and consistent heuristics are.
Matching the Heuristic to the Movement Model
A grid heuristic should equal the cost of the cheapest move sequence on an empty grid. Then it is exact when no obstacles are in the way, and it never overestimates when they are.
| Movement | Diagonal cost | Best heuristic |
|---|---|---|
| 4-connected | not allowed | Manhattan |
| 8-connected | √2 | Octile |
| 8-connected | 1 | Chebyshev |
| Any of these | any | Euclidean (admissible, weaker) |
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)
for h in (manhattan, euclidean, chebyshev, octile):
print(h.__name__, round(h((0, 0), (3, 1)), 3))Dominance and Nodes Expanded
Suppose h₂(n) ≥ h₁(n) for every node n, and both heuristics are admissible. Then h₂ dominates h₁, and A using h₂ expands no more nodes than A using h₁, apart from nodes that tie on f. You can measure the difference with the effective branching factor b\, which solves N + 1 = 1 + b\ + b\² + … + b\ᵈ, where N is the number of nodes expanded and d is the solution depth. A b\* closer to 1 means a better-informed heuristic.
Tie-Breaking
On open grids, many nodes share the same f value. If you break ties toward the smaller h (in other words, the larger g), A* keeps extending the deepest path instead of widening the search. To do this, push (f, h, counter, node) onto the heap.
Relaxed Problems
To get an admissible heuristic, remove a constraint from the problem and solve the easier version exactly. The relaxed problem's optimal cost can never be higher than the real one. For the 8-puzzle, letting tiles slide through each other gives Manhattan distance. Letting tiles teleport gives the misplaced-tiles count, which Manhattan distance dominates.
Weighted A*
Weighted A* ranks nodes by f = g + w·h with w ≥ 1. It usually expands far fewer nodes, and the cost of the path it returns is at most w times the optimal cost.
IDA*
IDA (iterative deepening A) runs depth-first searches cut off at an f bound. After each pass it raises the bound to the smallest f value that exceeded it. Memory grows only with the path length, but nodes get expanded again on every pass, which is costly on grids with many paths into the same cell.
Pattern Databases
A pattern database stores the exact solution cost for a subset of puzzle tiles, computed by searching backward from the goal. During search, the heuristic is a table lookup. If the databases count only moves of their own tiles and cover disjoint tile sets, you can add their values together and the sum stays admissible.
Exercise
A robot moves on an 8-connected grid where diagonal steps cost 1. Is octile distance admissible here, and is it the best choice?
Show answer
No. Octile charges √2 for each diagonal step, but a diagonal step really costs 1, so octile can overestimate. Chebyshev distance is the correct heuristic for this movement model.