Sub topic 16
Pattern Databases for Puzzle Search
This chapter assumes you know A and the astar function from "Implementing A with a Binary Heap", and that you know how relaxed problems produce admissible heuristics. Manhattan distance works well on sliding-tile puzzles, but it treats each tile as if the other tiles were not there. A pattern database (PDB) keeps some of those interactions. It does this by solving a smaller version of the puzzle exactly and storing every answer in a table.
The Idea: Abstract, Solve, Store
A pattern is a subset of the tiles. To abstract a state, you keep the pattern tiles and the blank, and you replace every other tile with an indistinguishable "don't care" marker. Many real states collapse into one abstract state.
Every real solution maps to an abstract solution of the same length. That makes the exact abstract distance a relaxation, so it never overestimates. It is also consistent, because one real move changes the abstract distance by at most 1.
A PDB is built once, offline, by running breadth-first search backward from the abstract goal. During search, the heuristic becomes a single dictionary lookup.
Building a PDB for the 8-Puzzle
from collections import deque
GOAL = (1, 2, 3, 4, 5, 6, 7, 8, 0)
def blank_moves(i):
r, c = divmod(i, 3)
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nr, nc = r + dr, c + dc
if 0 <= nr < 3 and 0 <= nc < 3:
yield nr * 3 + nc
def abstract(state, pattern):
return tuple(t if t == 0 or t in pattern else -1 for t in state)
def build_pdb(pattern):
start = abstract(GOAL, pattern)
dist = {start: 0}
queue = deque([start])
while queue:
s = queue.popleft()
b = s.index(0)
for j in blank_moves(b):
t = list(s)
t[b], t[j] = t[j], t[b]
t = tuple(t)
if t not in dist:
dist[t] = dist[s] + 1
queue.append(t)
return dist
P1, P2 = {1, 2, 3, 4}, {5, 6, 7, 8}
pdb1, pdb2 = build_pdb(P1), build_pdb(P2)
print(len(pdb1)) # 15120
def pdb_h(state):
return max(pdb1[abstract(state, P1)], pdb2[abstract(state, P2)])
def puzzle_neighbors(state):
b = state.index(0)
for j in blank_moves(b):
t = list(state)
t[b], t[j] = t[j], t[b]
yield tuple(t), 1
start = (1, 2, 3, 4, 5, 6, 0, 7, 8)
print(pdb_h(start)) # 2
path, cost = astar(start, GOAL, puzzle_neighbors, pdb_h)
print(cost) # 2Sliding moves are reversible, so a BFS outward from the goal gives each abstract state's distance to the goal.
Combining Databases
Taking the max of several PDBs is always admissible. Summing them is only admissible if two conditions hold. The patterns must be disjoint, and each PDB must count only the moves of its own pattern tiles. These are called additive PDBs. The code above counts every blank move, so its tables must be combined with max, not +.
| Combination | Requirement | Strength |
|---|---|---|
| `max` of PDBs | none | at least as strong as the best single PDB |
| sum of PDBs | disjoint patterns, count only pattern-tile moves | usually much stronger |
| `max(pdb, manhattan)` | none | never worse than either |
Trade-offs
- Memory: table size grows with the number of placements of the pattern tiles. A larger pattern gives a better heuristic, but the table grows quickly.
- Build time: you pay for the build once, and it pays off when you solve many instances that share the same goal.
- Goal dependence: the table is tied to one goal state. If the goal changes, you have to rebuild it.
Exercise
You build two PDBs for the 8-puzzle with disjoint patterns {1,2,3,4} and {5,6,7,8}, using the build_pdb function above. A teammate sets h = pdb1 + pdb2. Is this admissible? What change would make the sum safe?
Show answer
It is not guaranteed admissible. Each table counts every blank move, including moves of tiles outside its pattern, so the same real move can be counted in both tables. To make the sum safe, give zero cost to moves of non-pattern tiles when building each table, so each PDB counts only its own tiles' moves. With 0/1 costs, the BFS must use a deque that puts zero-cost successors at the front, or Dijkstra's algorithm.