Heuristic Functions in Artificial Intelligence.
Learn how heuristic functions estimate the remaining cost to reach a goal in AI search problems. Explore Euclidean and Manhattan distance, misplaced tile heuristics, admissibility, consistency, Python implementation, and their role in A* and Greedy Best-First Search.
Heuristic Functions: Guiding Artificial Intelligence Towards Better Solutions
Imagine you are solving a puzzle. You have several possible moves, but you do not want to try every move randomly. Instead, you look at the current arrangement and estimate how far it is from the completed puzzle.
You might count the misplaced pieces or measure how far each piece is from its correct position.
That estimate helps you decide which move to try next.
Artificial Intelligence uses the same idea in search problems. It evaluates possible states and estimates how much work remains before reaching the goal.
This is the role of a heuristic function.
1. What Is a Heuristic Function?
A heuristic function is a function used in Artificial Intelligence to estimate the cost or distance from a current state to a goal state.
It is commonly represented as:
\[
h(n)
\]
Where:
- \(h\) represents the heuristic function.
- \(n\) represents a node or state.
- \(h(n)\) represents the estimated remaining cost from that node to the goal.
For example, in a route-finding problem, a heuristic function might calculate the straight-line distance between the current location and the destination.
The actual road distance may be different, but the estimate provides useful guidance.
A heuristic does not necessarily provide the exact answer. Its purpose is to help the search algorithm make better decisions.
2. Why Are Heuristic Functions Important?
Consider a search problem with hundreds or thousands of possible states.
Without additional guidance, an algorithm may need to explore many of them before finding a solution.
A heuristic function helps reduce unnecessary exploration by estimating which states are more promising.
For example, imagine a robot trying to reach a target in a room.
It can calculate the distance between its current position and the target. If one possible movement brings it closer to the target, that movement may be more promising than another that takes it farther away.
This information can guide the search.
Heuristic functions are especially important in informed search algorithms such as:
- Greedy Best-First Search
- A* Search
3. How Does a Heuristic Function Work?
Let’s consider a simple search tree:
S
/ \
A B
/ \ \
C D G
Suppose G is the goal.
We assign estimated remaining costs to the nodes:
| Node | Heuristic value |
|---|---|
| S | 5 |
| A | 3 |
| B | 1 |
| C | 4 |
| D | 2 |
| G | 0 |
The algorithm can use these estimates to decide which node to explore.
For example:
- A has an estimated remaining cost of 3.
- B has an estimated remaining cost of 1.
If the algorithm uses only heuristic values, it will prefer B.
The goal has a heuristic value of 0 because no additional cost is needed once it has been reached.
4. Types of Heuristic Functions
Heuristic functions are designed according to the problem being solved. There is no single formula that works for every problem.
Let’s understand some common examples.
A. Straight-Line Distance
In route-finding problems, straight-line distance is often used as a heuristic.
It estimates the direct distance between the current location and the destination.
For two points with coordinates \((x_1,y_1)\) and \((x_2,y_2)\), the Euclidean distance is:
\[
h(n)=\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}
\]
This gives the direct distance between the two points.
It may be shorter than the actual road route, but it can provide a useful estimate.
B. Manhattan Distance
Manhattan distance is commonly used in grid-based problems where movement is restricted to horizontal and vertical directions.
It is calculated as:
\[
h(n)=|x_2-x_1|+|y_2-y_1|
\]
Imagine a robot moving through a city grid. It cannot travel diagonally, so it must move along rows and columns.
Manhattan distance estimates the number of horizontal and vertical steps required to reach the target, ignoring obstacles.
C. Misplaced Tile Heuristic
This heuristic is commonly used in the 8-puzzle problem.
It counts how many numbered tiles are not in their correct positions.
For example, if a puzzle has eight numbered tiles and three are misplaced, the heuristic value is:
\[
h(n)=3
\]
A lower value indicates that fewer tiles are misplaced.
D. Number of Remaining Moves
In some puzzles and planning problems, a heuristic can estimate the number of actions still required to reach the goal.
For example, in a simple board game, it might estimate how many moves remain before a particular target configuration is reached.
The estimate depends on the rules and structure of the problem.
5. Admissible Heuristics
One of the most important properties of a heuristic function is admissibility.
A heuristic is called admissible if it never overestimates the actual minimum cost required to reach the goal.
Mathematically:
\[
h(n)\leq h^*(n)
\]
Here:
- \(h(n)\) is the estimated remaining cost.
- \(h^*(n)\) is the true minimum remaining cost.
Suppose the actual minimum cost from a node to the goal is 10.
An admissible heuristic may estimate:
- 5
- 7
- 9
- 10
But it must not estimate 12.
It may underestimate the cost, but it cannot overestimate it.
Why is admissibility important?
Admissibility is important because it helps A* Search find an optimal solution.
With non-negative edge costs, an admissible heuristic supports optimality in A* tree search. For graph search, the implementation must also handle paths and node reopening appropriately when the heuristic is not consistent.
6. Consistent Heuristics
Another important property is consistency, also known as monotonicity.
A heuristic is consistent if its estimated cost satisfies the following condition for every node and its successor:
\[
h(n)\leq c(n,n’)+h(n’)
\]
Where:
- \(h(n)\) is the heuristic value of the current node.
- \(h(n’)\) is the heuristic value of its successor.
- \(c(n,n’)\) is the cost of moving from the current node to the successor.
In simple terms, the estimated cost from the current node should not be greater than the cost of taking one step to a neighbouring node plus the estimated remaining cost from that neighbour.
Consistency helps prevent estimated costs from behaving unexpectedly as the algorithm moves through the search space.
Every consistent heuristic is admissible when the goal has a heuristic value of zero, but an admissible heuristic is not necessarily consistent.
7. Admissible vs Consistent Heuristics
| Feature | Admissible Heuristic | Consistent Heuristic |
|---|---|---|
| Main condition | Never overestimates true minimum remaining cost | Satisfies the consistency inequality |
| Focus | Accuracy of the estimate | Relationship between neighbouring states |
| Relationship | May be inconsistent | Always admissible under the usual goal-zero condition |
| Importance | Supports optimality of A* | Supports reliable graph-search behaviour |
Both properties are important when designing heuristic functions for A*.
8. Combining Heuristics
Sometimes, a problem has more than one useful heuristic.
For example, a puzzle-solving system may use:
- The number of misplaced tiles.
- The total Manhattan distance of the tiles.
If both heuristics are admissible, taking their maximum also gives an admissible heuristic:
\[
h(n)=\max(h_1(n),h_2(n))
\]
This can provide a more informative estimate without overestimating the true remaining cost.
However, simply adding two admissible heuristics does not necessarily preserve admissibility. Their relationship must be considered.
9. Implementing a Heuristic Function in Python
Let’s create a simple Manhattan distance heuristic.
Suppose a robot is at position (2, 3) and its target is at (5, 7).
def manhattan_distance(current, goal):
x1, y1 = current
x2, y2 = goal
return abs(x2 - x1) + abs(y2 - y1)
current_position = (2, 3)
goal_position = (5, 7)
distance = manhattan_distance(
current_position,
goal_position
)
print("Estimated distance:", distance)
Output:
Estimated distance: 7
The calculation is:
\[
|5-2|+|7-3|=3+4=7
\]
The estimated distance is 7 grid steps, assuming horizontal and vertical movement with unit cost and no obstacles.
10. Heuristic Functions in Different Algorithms
Heuristic functions are used differently depending on the search algorithm.
Greedy Best-First Search
Greedy Best-First Search uses:
\[
f(n)=h(n)
\]
It selects the node with the lowest estimated remaining cost.
A* Search
A* uses:
\[
f(n)=g(n)+h(n)
\]
It combines the actual cost already travelled with the estimated remaining cost.
This difference is important.
Greedy Best-First Search focuses on what appears closest to the goal, while A* considers the estimated total cost of the path.
11. Limitations of Heuristic Functions
Although heuristic functions are useful, they also have limitations.
- Estimates may be inaccurate: A heuristic may not represent the actual remaining cost very well.
- Problem-specific design: A useful heuristic for one problem may not work for another.
- Computational cost: A complex heuristic may take significant time to calculate.
- Poor guidance: A weak heuristic may not reduce the number of explored states.
- Optimality conditions: Not every heuristic satisfies admissibility or consistency.
Designing a good heuristic is often an important part of building an effective AI search system.
Conclusion
Heuristic functions allow Artificial Intelligence systems to use additional knowledge when exploring possible solutions.
They estimate how far a current state is from the goal and help search algorithms prioritise promising paths.
Properties such as admissibility and consistency are especially important for algorithms like A* because they determine when optimal solutions can be guaranteed.
In the next article, we will move beyond heuristic search and explore Local Search Algorithms, which solve problems by improving one candidate solution at a time.
Quick quiz
5 questionsOriginally published at farhanpaal.com — https://farhanpaal.com/articles/heuristic-functions-in-artificial-intelligence/
Keep a copy
Read offline as a printout or PDF.
farhanpaal.com