AI Wiki中文/EN

Heuristic search

启发式搜索

Construct lower-bound heuristics, distinguish admissibility from consistency, and show why greedy search can fail even with a consistent heuristic.

On this pageIntuitionMathematics & methodImplementationLimitationsExercisesResearch connections

An estimate is useful only with the right question

A traveler has two candidate routes. One is already expensive but looks close to the destination; another has cost little so far but looks slightly farther away. Choosing only the estimated remaining distance ignores what has already been paid.

A heuristic h(s)h(s) estimates the remaining cost from state ss to a goal. It can prioritize promising states, but it does not choose a search algorithm by itself. Greedy best-first search ranks by hh alone; A* ranks by g+hg+h, adding the route cost already incurred. CS188, Informed Search

Lower bounds and edge-wise consistency

Let h∗(s)h^*(s) be the true minimum remaining cost, or infinity when no goal is reachable. A nonnegative heuristic is admissible if it never overestimates. It is consistent if it obeys a triangle-like inequality on every directed edge, with zero at goals:

0≤h(s)≤h∗(s),h(s)≤c(s,s′)+h(s′),h(g)=0.0\le h(s)\le h^*(s),\qquad h(s)\le c(s,s')+h(s'),\qquad h(g)=0.

The first inequality defines admissibility; the edge inequality defines consistency. Along any finite route to a goal, adding the consistency inequalities cancels intermediate heuristic values. Therefore a consistent heuristic with zero goal value is admissible. The reverse need not hold. For edges S→A of cost 1 and A→G of cost 1, values h(S)=2,h(A)=0,h(G)=0h(S)=2,h(A)=0,h(G)=0 are admissible but violate consistency on S→A.

A systematic construction solves a relaxed problem: remove constraints so every original route remains possible at no greater cost. Its optimal cost is a lower bound for the original problem. On a four-neighbor unit-cost grid, removing walls gives Manhattan distance:

h(x,y)=∣x−xg∣+∣y−yg∣.h(x,y)=|x-x_g|+|y-y_g|.

Each allowed move changes this value by at most one, proving consistency. Allowing unit-cost diagonal moves breaks that argument: from (0,0)(0,0) to (1,1)(1,1), Manhattan distance is 2 but the true cost is 1. The heuristic’s validity depends on the action and cost model, not the visual appearance of a map. CS188, heuristic design

If two heuristics are admissible, their pointwise maximum is also admissible. Their sum can overcount the same remaining work. For example, adding two identical exact estimates doubles the true cost. Additive heuristics require a justification such as a valid partition of costs.

Check a heuristic and expose a greedy failure

The complete program below uses S→A→G with cost 101 and S→B→G with cost 4. Its heuristic is zero at S, A, and G and 2 at B. The values are both admissible and consistent. Yet greedy search expands A, sees G with estimate zero, and returns cost 101. This failure is about ranking only by remaining cost, not about an invalid heuristic.

The implementation follows the priority-by-hh idea illustrated by AIMA, with a local tie counter and finite-graph duplicate handling. It is an original small teaching implementation. AIMA Python, best-first search

import heapq
import itertools

graph = {'S':[('A',1),('B',2)], 'A':[('G',100)], 'B':[('G',2)], 'G':[]}
h = {'S':0,'A':0,'B':2,'G':0}
exact_remaining = {'S':4,'A':100,'B':2,'G':0}

def consistent(graph, heuristic, goals):
    return (all(heuristic[g] == 0 for g in goals)
            and all(value >= 0 for value in heuristic.values())
            and all(heuristic[u] <= cost+heuristic[v]
                    for u,edges in graph.items() for v,cost in edges))

def greedy_cost(graph, heuristic, start, goal):
    ticket = itertools.count()
    heap = [(heuristic[start], next(ticket), start, 0)]
    seen = {start}
    while heap:
        _,_,u,cost = heapq.heappop(heap)
        if u == goal:
            return cost
        for v,weight in graph.get(u, []):
            if v not in seen:
                seen.add(v)
                heapq.heappush(heap,(heuristic[v],next(ticket),v,cost+weight))
    return None

assert consistent(graph,h,{'G'})
assert all(h[s] <= exact_remaining[s] for s in h)
assert greedy_cost(graph,h,'S','G') == 101
assert exact_remaining['S'] == 2+2 == 4
assert greedy_cost(graph,h,'S','S') == 0
assert greedy_cost(graph,h,'S','absent') is None
small = {'S':[('A',1)],'A':[('G',1)],'G':[]}
assert not consistent(small,{'S':2,'A':0,'G':0},{'G'})

The consistency checker costs O(V+E)O(V+E) for a finite graph. With constant-time heuristic lookup and expected constant-time set operations, this greedy graph search schedules each state at most once, using O(V)O(V) extra storage and O(E+Vlog⁡(V+1))O(E+V\log(V+1)) time. It deliberately does not improve paths to already discovered states: it is a counterexample-producing greedy solver, not UCS or A*.

A useful predictor may lack a search guarantee

An admissible heuristic can still be weak: zero is valid on nonnegative-cost problems but contributes no directional information. A stronger lower bound may save expansions while costing more to compute. Measure both work avoided and heuristic computation.

For A* graph search, consistency supports closing states without reopening them under the standard assumptions. An admissible but inconsistent heuristic can require reopening states when better routes are found. AIMA’s cited best-first graph implementation keeps a closed set; do not infer a general inconsistent-heuristic guarantee merely from accepting a callable heuristic.

A learned distance predictor may be accurate on average while occasionally overestimating. Average prediction error is not an admissibility certificate. Weighted priorities, resource limits, or approximate search can be reasonable choices, but report their actual solution-quality guarantees and conditions.

Validate the estimate against the model

  1. Is Manhattan distance admissible for unit-cost diagonal moves? Answer: not in general; the one-diagonal route is a counterexample.
  2. Can a consistent heuristic make greedy search optimal? Answer: consistency alone cannot. The program returns 101 although a route of cost 4 exists.
  3. If two admissible heuristics return 4 and 7 at a state, what safe combination is available? Answer: their maximum, 7. Their sum, 11, needs a separate justification.
  4. Why does consistency imply admissibility for states with a finite route to a goal? Answer: summing edge inequalities along any such route yields h(s)h(s) no greater than that route’s cost; taking the minimum gives the result.

Heuristics connect modeling and computation

Relaxations, pattern databases, abstractions, and learned estimates trade construction cost against search guidance. They share a practical question: what information about the remaining problem is cheap enough to use repeatedly? Compare against a cost-only solver on small instances, then study expansions, runtime, memory, and returned cost as the instances grow.

Make a prediction, then check

Does consistency alone guarantee optimality for greedy search ordered by h?

Choose one answer

Learning objectives

  • Construct relaxation lower bounds and distinguish admissibility from consistency.
  • Use an executable counterexample to explain how greedy search and A* order states differently.

Prerequisites & connections

Explore related knowledge →

References

  1. CS188 Textbook — Informed Search
    UC Berkeley CS188 course contributors
  2. AIMA Python — search algorithms (commit f104e03, 2026-06-30)
    AIMA Python contributors · 2026

Version1 · Editorial status: Recorded technical checks completed

View technical check record (AI)

Codex · 2026-09-05 · Codex technical check: cited-source comparison, re-derivation and recalculation, bilingual argument comparison, and executed Python assertions; no independent human peer review.

  • Checked hand-derived exact remaining costs, admissibility and consistency; executed the counterexample returning 101 while the optimum is 4.
  • Re-derived telescoping consistency, Manhattan distance under unit four-neighbor moves, the diagonal counterexample, and maximum versus sum combinations.
  • Compared arguments, modeling assumptions, numbers, and exercise answers across languages; structural checks separately compare display formulas, code, references, and main sections. No independent human peer review.

Source locations checked:

Related topics

No matches. Try a synonym or acronym.

0 results shown

Wrap the whole query in double quotes for a phrase match.