Sub topic 15
IDA*: A* with Linear Memory
This chapter assumes you know the A* implementation from earlier chapters, what admissible heuristics are, and how recursive depth-first search works.
Why memory becomes the bottleneck
A stores every generated node in its open heap and its g and parent dictionaries. On a map with a few million cells this is fine. On a puzzle like the 15-puzzle, which has about 10^13 reachable states, A can run out of memory long before it runs out of time.
Iterative deepening A (IDA) keeps the optimality guarantee of A* and uses memory that grows only with the length of the current path. The cost is extra time, because it expands some nodes more than once.
The working model
IDA runs a series of depth-first searches. Each search has a bound* on f = g + h:
- Set the bound to h(start).
- Search depth-first from the start and prune any node whose f is greater than the bound.
- While pruning, record the smallest f that went over the bound.
- If the goal was reached, return the path. If nothing was pruned, no path exists. Otherwise, set the bound to that smallest pruned f and start again from step 2.
Each new bound is the next f-value A would have looked at, so IDA takes in f-contours in the same order A*'s heap would. With an admissible heuristic, the first goal it finds is optimal.
Implementation
The search keeps only path, which is the current branch. Checking nxt in path stops cycles along that branch. There is no closed set.
def ida_star(start, goal, neighbors, h):
path = [start]
def search(g, bound):
node = path[-1]
f = g + h(node)
if f > bound:
return f
if node == goal:
return True
minimum = float('inf')
for nxt, cost in neighbors(node):
if nxt in path:
continue
path.append(nxt)
t = search(g + cost, bound)
if t is True:
return True
minimum = min(minimum, t)
path.pop()
return minimum
bound = h(start)
while True:
t = search(0, bound)
if t is True:
return path
if t == float('inf'):
return None
bound = t
def grid_neighbors(grid):
rows, cols = len(grid), len(grid[0])
def neighbors(cell):
r, c = cell
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nr, nc = r + dr, c + dc
if 0 <= nr < rows and 0 <= nc < cols and grid[nr][nc] == '.':
yield (nr, nc), 1
return neighbors
def manhattan(a, b):
return abs(a[0] - b[0]) + abs(a[1] - b[1])
grid = ["....",
".##.",
"...."]
goal = (2, 3)
path = ida_star((0, 0), goal, grid_neighbors(grid), lambda n: manhattan(n, goal))
print(len(path) - 1) # 5
print(path)Common errors and trade-offs
| Issue | Effect |
|---|---|
| Inadmissible heuristic | The first goal found inside the bound may not be optimal |
| Many distinct f-values (real-valued costs) | The bound goes up by tiny steps, so the number of iterations explodes |
| Graphs with many transpositions (grids) | The same cell is reached by many different paths and searched again each time, because there is no closed set |
| Using a `list` for the cycle check on deep paths | Each check costs O(depth). Keep a parallel `set` if paths get long |
Exercise
During one IDA* iteration with bound 12, the pruned nodes had f-values 14, 13, 17, and 13, and the goal was not reached. What bound does the next iteration use, and why can't it skip straight to 14?
Show answer
The next bound is 13, the smallest pruned f. Skipping to 14 could pass over a goal with f = 13, and the first goal found might then cost more than the optimum. Optimality would be lost.
Exercise
The nxt in path check prevents cycles. Why does IDA still expand far more nodes than A on an open 4-connected grid?
Show answer
The check only looks at the current branch. A cell reached by many different equal-cost routes is searched again on each route, and again in every iteration. A*'s closed set and g-table remove those duplicates.