Heuristics
Reading time15 minIn brief
Article summary
In this article, you will learn about heuristics. Such algorithm help approximating the solution to a problem that is too complex to be solved exactly.
Main takeaways
-
A heuristic allows to find approximate solutions to a problem.
-
It is a compromise between quality of the found solution and needed computation time.
-
It encodes an intuition on how to approximate a solution to a complex problem.
-
Sometimes, heuristics can find optimal solutions.
Article contents
1 — Video
Please check the video below. You can also read the transcripts if you prefer.
To go further
2 — Heuristics and optimal solutions
As stated before, the use of heuristics may lead to finding a correct solution to a problem, but not necessarily the optimal one. This is the case for instance of the greedy approach, that iteratively choses locally optimal solutions to estimate a global optimum.
Still, using a heuristic does not always make a solution non-optimal. Recall for instance the bruteforce algorithm with backtracking studied previously.
The efficiency of backtracking strongly depends on the order in which branches of the tree of solutions are explored. In the previous session, you probably have chosen to visit remaining vertices in the meta-graph as they come in your meta-graph structure like this:
def solve_tsp (graph, source):
...
for vertex in graph.get_neighbors(current_vertex):
if vertex not in current_visited_vertices:
# Explore that branch
...
However, using a heuristic to encode an intuition on which neighbor should be explored first may be a good idea, as – if it is a good heuristic – it could lead to exploration of short paths quickly, thus leading to pruning numerous branches.
The following partial code is a possible solution, in which we propose to use a min-heap, that will contain all neighbors, with an associated value corresponding to the score given by the heuristic. Since it is a min-heap, the lower the score, the better the quality of the neighbor:
def heuristic (neighbor, ...):
# Return a score for the neighbor to evaluate
# In this example, the lower the better, but you could adapt the code to change that
...
return XXX
def evaluate_neighbors (neighbors, ...):
# Here, we use our heuristic to give a score to all candidate neighbors
# Then, we use these scores to fill a min-heap
scores = []
for neighbor in neighbors:
score = heuristic(neighbor, ...)
heapq.heappush(scores, (score, neighbor))
return scores
def solve_tsp (graph, source) :
...
non_visited_neighbors = [neighbor for neighbor in graph.get_neighbors(current_vertex) if neighbor not in explored_vertices]
scores = evaluate_neighbors(non_visited_neighbors, ...)
while len(scores) > 0 :
best_score, best_neighbor = heapq.heappop(scores)
# Explore that branch in priority
...
You can for instance try to choose as a heuristic to return a score which is the distance between the vertex being explored and the candidate neighbor, i.e., the weight in the meta-graph.
3 — Meta-heuristics
The meta prefix indicates that the word it is associated with is set to a higher level of abstraction. In this case, a meta-heuristic is a heuristic that will itself be used to find heuristics to solve a problem.
Meta-heuristics make it possible to abstract the problem of finding the right heuristics, by considering heuristics as a solution to an optimization problem, whose objective is to maximize precision on the problem studied, while limiting complexity. Thus, a meta-heuristic is an algorithm that will go through the space of possible heuristics, and choose one that will be judged more optimal than the others according to certain criteria (hence the heuristic part in the word).
Meta-heuristics completely ignore the problem studied, considering that the heuristics sought is just a function that must be optimized to minimize a criterion. Thus, the same meta-heuristic can be used to produce heuristics that respond to a very large number of problems.
Some of the most well-known meta-heuristics are ant colonies, genetic algorithms, simulated annealing, etc.
To go beyond
-
Ant colonies aim at reproducing the behavior of ants to find a shortest path in a graph.
-
The Traveling Salesman Problem: When Good Enough Beats Perfect.
A video on the TSP and a few heuristics to approximate its solution.
-
Wikipedia page on meta-heuristics.
If you want to learn more about those.