A blind search wastes effort on states that are clearly poor. If we can estimate how close a state is to the goal, we can search more wisely.

Heuristics

A heuristic is a rule of thumb that estimates the cost from a state to the goal. It is written h(n). A good heuristic is cheap to compute and close to the true cost. In the 8-puzzle, two common heuristics are the number of tiles out of place and the Manhattan distance, which is the sum of the horizontal and vertical distances of each tile from its goal square. A heuristic is admissible if it never overestimates the true cost, and then algorithms such as A* are guaranteed to find the best path.

Definition: Heuristic. A rule of thumb or estimate, written h(n), of the cost of reaching the goal from state n, used to guide a search towards promising states.

Hill climbing moves to the neighbouring state with the best heuristic value and stops when no neighbour is better. It uses almost no memory, but it can stop at a local maximum, a plateau where all neighbours are equal, or a ridge. Best-first search, or greedy search, keeps a priority queue ordered by h(n) and always expands the node that looks closest to the goal. It is fast, but it is not optimal, since it ignores the cost already spent.

The A* Algorithm

A orders nodes by f(n) = g(n) + h(n), where g(n) is the cost of the path so far and h(n) is the estimated cost to go. It balances the cost spent and the cost expected. With an admissible heuristic, A is complete and optimal. Its weakness is memory, since it keeps all the nodes it has generated. A better heuristic, one that is nearer to the true cost without going over it, expands fewer nodes.

Table 1.4: Heuristic search methods

Method Evaluates by Optimal
Hill climbing h(n) of neighbours No
Best-first (greedy) h(n) No
A* g(n) + h(n) Yes, if h is admissible

Example 1.4: A* on a small graph

Problem: From S to G: edges S-A (cost 1), S-B (cost 4), A-B (cost 2), A-G (cost 6), B-G (cost 3). Heuristic: h(S) = 5, h(A) = 4, h(B) = 2, h(G) = 0. Find the path by A*.

Solution:

  1. Expand S: f(A) = 1 + 4 = 5, f(B) = 4 + 2 = 6. Pick A.
  2. Expand A: B by A costs 1 + 2 = 3, so f(B) = 3 + 2 = 5. G costs 1 + 6 = 7, so f(G) = 7.
  3. Pick B (f = 5), expand: G costs 3 + 3 = 6, so f(G) = 6.
  4. Pick G (f = 6).

Result: The best path is S, A, B, G with cost 6.

import heapq
E = {"S":[("A",1),("B",4)],"A":[("B",2),("G",6)],"B":[("G",3)],"G":[]}
h = {"S":5,"A":4,"B":2,"G":0}
pq = [(h["S"], 0, "S", ["S"])]
while pq:
    f, g, n, p = heapq.heappop(pq)
    if n == "G":
        print(p, g)
        break
    for m, c in E[n]:
        heapq.heappush(pq, (g + c + h[m], g + c, m, p + [m]))

Output

['S', 'A', 'B', 'G'] 6

Common mistakes

  • Using a heuristic that overestimates the cost and then expecting A* to be optimal.
  • Thinking that greedy best-first search always finds the best path.
  • Forgetting that hill climbing can get stuck at a local maximum.

Key takeaways

  • A heuristic h(n) estimates the cost to the goal.
  • A* uses f(n) = g(n) + h(n).
  • An admissible heuristic never overestimates.
  • Hill climbing can get stuck at local maxima, plateaus and ridges.

Practice questions

  1. What is a heuristic?
  2. Write the evaluation function of A*.
  3. What is a local maximum?

Answers to the practice questions

  1. An estimate of the cost from a state to the goal, used to guide the search.
  2. f(n) = g(n) + h(n).
  3. A state better than its neighbours but not the best state overall.

Sources

  • Official BCA syllabus Artificial Intelligence and Expert Systems, course code 25BCA405DS02, Unit I