Lecture 05: Heuristic Functions and Optimality
A deep dive into heuristic evaluation, admissibility, consistency, dominance, and generating heuristics from relaxed problems using the 8-puzzle as an example.
In the previous lecture, we established that Informed Search algorithms use a heuristic function to estimate the cheapest path from node to a goal node.
But what makes a good heuristic? How do we mathematically guarantee that our heuristic will lead A* to the optimal solution?
Conditions for Optimality
For A* to be perfectly optimal, the heuristic function must satisfy specific conditions depending on the underlying search algorithm (Tree-Search vs. Graph-Search).
1. Admissibility (Optimistic Heuristics)
An admissible heuristic is one that never overestimates the true cost to reach the goal. If is the true, exact cost to reach the goal from , then:
Because the heuristic is always optimistic, it thinks the goal is closer than it actually is.
- Result: If is admissible, A* using Tree-Search is guaranteed to be optimal.
- Example: Straight-line distance is admissible because a straight line is mathematically the shortest distance between two points; therefore, the straight-line distance can never be greater than the actual road distance.
2. Consistency (Monotonicity)
A heuristic is consistent (or monotonic) if it satisfies the triangle inequality. For every node and each successor node generated by any action :
This means the estimated cost from node to the goal is never greater than the step cost to get to plus the estimated cost from to the goal.
- Result: Consistency ensures that heuristic values decrease monotonically along any path to the goal.
- If is consistent, A* using Graph-Search is optimal without any extra bookkeeping. (Note: Every consistent heuristic is also admissible, but not all admissible heuristics are consistent).
Evaluating Heuristics: The 8-Puzzle
The 8-puzzle consists of a 3x3 grid with 8 numbered tiles and one empty space. The goal is to slide tiles into the empty space until the numbers are in numerical order. To someone who hasn't seen this before, the "actions" in this state space are the movements of the empty tile (Up, Down, Left, or Right).
Here is an example of what the initial search tree looks like when expanding from a start state:
- Average solution length: 22 moves (the shortest path for the specific start state above is 26 actions).
- Branching factor: (as shown in the diagram, the empty tile can move to 2, 3, or 4 positions depending on whether it is in a corner, an edge, or the center).
- Search complexity: A naive search up to depth 22 explores states. Graph search reduces this to reachable distinct states.
Common Admissible Heuristics for the 8-Puzzle
We can formulate two different admissible heuristics for this problem:
- (Misplaced Tiles): The number of tiles that are not in their goal position. For the start state above, . (Admissible because every misplaced tile must move at least once).
- (Manhattan Distance): The sum of the horizontal and vertical distances of all tiles from their goal positions. For the start state above, . (Admissible because each tile must move at least its Manhattan distance, assuming no other tiles are in the way).
The Effect of Heuristic Accuracy: Dominance
One way to characterize the quality of a heuristic is the Effective Branching Factor (). If A* generates nodes to find a solution at depth , then is the branching factor a uniform tree of depth would need to contain nodes: A well-designed heuristic will have a close to 1.
Dominance ()
When evaluating (misplaced) vs (Manhattan) across 1200 random puzzles:
- generated thousands of nodes.
- generated significantly fewer nodes (often an order of magnitude less).
If for all (and both are admissible), we say that dominates .
- is more informed than and is strictly better for searching.
- Implication: A* using will never expand more nodes than A* using . Every node that A* with expands will also be expanded by , but will cause additional useless nodes to be expanded that correctly avoids.
Where do Heuristics come from?
1. Generating heuristics from Relaxed Problems
If we remove restrictions from a problem, the state-space graph of this relaxed problem becomes a super-graph of the original. Because there are more valid edges (shortcuts), the optimal solution to the relaxed problem is always an admissible heuristic for the original problem.
- Original 8-puzzle rule: A tile can move to square B if B is adjacent and B is blank.
- Relaxed rule 1: A tile can move to square B if B is adjacent. (This generates the Manhattan distance ).
- Relaxed rule 2: A tile can move to any square B. (This generates the misplaced tiles ).
2. Pattern Databases
Instead of generating a heuristic for the whole board, we can store the exact solution cost of subproblems. For example, we create a database of the exact number of moves required to get just tiles 1, 2, 3, and 4 into their correct positions. Combining heuristics from multiple pattern databases (by taking their maximum value) can reduce the nodes generated in a 15-puzzle by a factor of 1000 compared to Manhattan distance alone.
3. Learning from Experience
"Experience" means having the agent solve lots of 8-puzzles. Each optimal solution provides training examples mapping a state to its exact cost to the goal. Using Machine Learning techniques (Neural Networks, Decision Trees, Reinforcement Learning), the agent can effectively learn the function dynamically.