Ant algorithm -> Animal Power

Martin Bernaola
2 min readFeb 11, 2023

--

This algorithm is part of a metaheuristic.

What are metaheuristics?
Basically, it is to find a solution to a combinatorial optimization problem. Does combinatorial optimization sound complex and strange to you? Well, it shouldn’t, a very simple example.

Imagine a simple traveler, who has to visit 3 or more cities and must find the shortest, most optimal path.

Actually, the ant algorithm is one of the many ways to find the shortest path. It is a very easy method.

It is incredible how nature, with its millions of years of evolution, has given humans, and especially computer science a clear and simple method to solve a problem.

A simple explanation

We are an anthill and we want to go looking for food, the easiest way? let’s send them all, some will arrive before and others later to the place of food.
And here is the key to everything, why when we observe an anthill do we always see the ants in a single path?

The ants on their way to the food, leave a trail called PHEROMONES. Something IMPORTANT to detect their shortest path (They follow it because it is in their nature).
So, if you send them all, you will leave traces everywhere, but which one is one that lasts over time (remember that nature also acts, and the air, the water will clean this trace)? The shortest PHEROMONE path is the one that will always have better quality. It will be built by simple NATURE.

Naturally, the ants will always join the path of the strongest Pheromones, which will of course be the shortest.

The idea in the code

You can find the code idea in my repository. Of course, there are thousands of algorithms with better solutions (for example, genetic algorithms). AntShortWay

In the console, you can see how by choosing a number of iterations, we can see the shortest paths (Obviously we must also know how many iterations to do, it is the key) But for that, we have an infinite number of other algorithms that will give us a hand :)

Enjoy :)

--

--

Martin Bernaola

I am an Information Systems Engineer. Venturing into the DevOps world. Welcome and enjoy the "Post Solutions" to make life easier