Sub topic 13
Designing Heuristics by Relaxing the Problem
This chapter assumes you already know what an admissible heuristic is, what a consistent heuristic is, and how dominance compares two heuristics. The question here is where good heuristics come from. The most reliable method is to solve a simpler version of the problem exactly and use that solution's cost as the estimate.
What a Relaxed Problem Is
A relaxed problem is the original problem with one or more rules removed. Removing a rule can only add actions. It never takes any away. Every path in the original state space therefore still exists in the relaxed one, and the relaxed space may also contain shortcuts.
This gives the key property. The optimal cost in the relaxed problem is never larger than the optimal cost in the real problem. So the exact solution cost of any relaxation is an admissible heuristic for the original.
It is also consistent. The relaxed cost is a true shortest-path distance in the relaxed graph, and shortest-path distances satisfy the triangle inequality. Every real edge (n, n') is also an edge in the relaxed graph, so h(n) ≤ c(n, n') + h(n') holds.
Example: The 8-Puzzle
In the 8-puzzle, a tile may move into a square only if that square is adjacent to it and empty. Dropping one rule or the other gives two well-known heuristics.
| Rule removed | Relaxed solution cost | Heuristic |
|---|---|---|
| Target must be empty | Each tile walks its own grid distance | Manhattan distance sum |
| Target must be adjacent | Each misplaced tile needs one jump | Misplaced tiles |
| Obstacles (grid pathfinding) | Straight walk on an open grid | Manhattan or octile |
The misplaced-tiles relaxation removes more rules than the Manhattan relaxation, so its solution cost is never higher. Fewer rules give a looser bound.
GOAL = (1, 2, 3, 4, 5, 6, 7, 8, 0) # 0 is the blank
def misplaced(state, goal=GOAL):
return sum(1 for s, g in zip(state, goal) if s != 0 and s != g)
def manhattan_tiles(state, goal=GOAL):
pos = {tile: divmod(i, 3) for i, tile in enumerate(goal)}
total = 0
for i, tile in enumerate(state):
if tile:
r, c = divmod(i, 3)
gr, gc = pos[tile]
total += abs(r - gr) + abs(c - gc)
return total
start = (8, 1, 3, 4, 0, 2, 7, 6, 5)
print(misplaced(start)) # 5
print(manhattan_tiles(start)) # 10Combining Relaxations
If you have several admissible heuristics and none dominates the others, take their maximum:
def combined(*hs):
return lambda n: max(h(n) for h in hs)The maximum of admissible heuristics is still admissible. It is also consistent when each part is consistent, and it dominates every part. The only cost is evaluating all of them at each node.
Common Mistakes
- Adding up independent relaxations. Adding two lower bounds can overestimate when both count the same moves. Use
maxunless you can prove the parts count separate costs. - Relaxing too far. If you drop every rule, you get h = 0, which is admissible but gives A* no guidance.
- Solving the relaxation only approximately. A greedy answer to the relaxed problem can overshoot its true optimum, and then admissibility is lost.
Pattern databases, covered later, take this idea further. They solve a relaxed subproblem exactly ahead of time and store the costs in a table.
Exercise
In grid pathfinding with 4-connected unit-cost moves and walls, which rule do you drop to get the Manhattan heuristic, and why is the result admissible?
Show answer
Drop the rule that walls block movement. On an open grid the cheapest path from a cell to the goal costs exactly |dx| + |dy|. Every real path is also a valid path on the open grid, so the real cost can never be lower. Manhattan distance therefore never overestimates.
Exercise
You have h1 = misplaced tiles and h2 = Manhattan distance for the 8-puzzle. Is h1 + h2 admissible?
Show answer
No. Each misplaced tile is counted once by h1 and at least once more by h2, so the sum can exceed the true cost. For example, a state that is one move from the goal has h1 = 1 and h2 = 1, so h1 + h2 = 2. Use max(h1, h2) instead, which here just equals h2.