# A Java Primer of Ant Colony Algorithms

**Posted:**July 20, 2015

**Filed under:**Uncategorized |

**Tags:**algorithms, ant colony, artificial intelligence, java, optimization Leave a comment

**The Ants in the Garden**

Ants always find a way: I used to found them on the garden of my house marching on ordered lines while carrying food to their nest. And they were indeed ordered: all the ants seemed to be following some paths that my eyes weren’t able to see. Later, I found these paths weren’t only invisible: They were also optimal. Which is kind of surprising since ants have very limited sight and a very small brain.

The colony mechanism is as simple as it is surprising. When the ants traverse a terrain looking for food, they drop a chemical called pheromone. This means that the ants that by chance have found shorter paths have also deposited more pheromone on them. Because the path are shorter -i.e optimal- they can traverse it more times in the same amount of time.

Pheromone is special for Ants for two reasons: They have senses to detect it and they feel attracted to it. This translate that Ants will have a tendency to traverse paths that have stronger pheromone trails on them. The technical term for this is stigmergy, and is the mechanism that allows this amazing behavior:

- All Ants of the colony go after a food. They traverse randomly the garden looking for a way to reach it.
- A very lucky ant gets there first, before any other fellow ant. He grabs the food, and returns it to the nest.
- New ants are assigned the mission to get food to the nest. These new ants found the pheromone deposited in 2, so they follow that lead.
- While the Ant in 2 keeps going from the Food to the Nest, the other unlucky ants are still trying to find their way.
- Now the path discovered in 2 is very pheromone intensive, because is being traversed several times by several ants. Even the Ants that take inefficient paths in 1 are now taken the path discovered in 2 since is so attractive now (i.e pheromone intensive).
- Now all the colony follows the optimal path.

That means that the Colony won’t starve, and that the system has converged to optimality.

**The Ants in Code**

But this is a Programming article, not a Biology one. On 1992, Marco Dorigo applied the strategy of the Ants to the solution of optimization problems. There are problems in Computer Science where the search space is so vast that it is impossible to traverse it completely to find the optimal solution. So, what Dorigo proposed was to build Software-Ants that will traverse this search space and look for an optimal solution, like the real ants do. This artificial ants don’t guarantee us that they will find the best solution, but they will find as a solution that is good-enough for our needs. The algorithms of this kind are called heuristics.

The optimization problems that can be solved in this fashion are vast: I can mention now the Travelling Salesman Problem, the Knapsack Problem and even Image Processing tasks like Image Segmentation and Clustering. And since Dorigo’s proposal on 1992 several algorithms have appeared that follow the principle of using Artificial Ants, like Ant System, Ant Colony System, Max-Min Ant System and many others. These algorithms have being grouped in a meta-heuristic which is now known as Ant Colony Optimization (ACO).

**The Ants in Java**

So, with Ant Colony Optimization you can solve several problems, using several algorithms. In order to save myself some lines of code, I have developed a little framework that allows an easy implementation of ACO algorithms in Java, called Isula (available for free here). I will use some snippets from Isula to demonstrate how to implement Ant Algorithms in Java.

This is a portion of the *solveProblem()* method of the AcoProblemSolver class:

while (iteration < numberOfIterations) { antColony.clearAntSolutions(); antColony.buildSolutions(environment, configurationProvider); applyDaemonActions(DaemonActionType.AFTER_ITERATION_CONSTRUCTION); updateBestSolution(environment); iteration++; }

That’s a snippet from an Isula-based Java Program available here.

This is the main loop of any Ant Colony Algorithm. We need to establish a stop condition for our program, so we define a parameter to set the maximum number of iterations our Ants will be traversing the problem space, which in the ACO domain is usually a graph. On each iteration, these things happen:

- Our Ants prepare themselves to build a solution: This can be a route, a cluster or any other entity which our problem requires us to find.
- The colony will traverse the problem graph and every ant of them will built a solution. Some of them will be good, some of them will be bad.
- There are some global actions that can be required in our algorithm. For example, we can rank our ants and only allow some of them to deposit pheromone. Those procedures are called Daemon Actions in ACO terminology.
- We store the best solution built so far. When we reach the maximum number of iterations, that will be the output of our program.

Now let’s take a deeper look into how the colony builds solutions. This is a snippet from the buildSolution() method of the AntColony class:

for (Ant<C, E> ant : hive) { while (!ant.isSolutionReady(environment)) { ant.selectNextNode(environment, configurationProvider); } ant.doAfterSolutionIsReady(environment, configurationProvider); }

Some things to note there:

- The
*AntColony*class administer a number of ants which are stored in the hive attribute. It depends on your algorithm characteristics or a parameter how many ants you want to use. - Each ant of the colony will add components to their solution until it is considered finished. For example, if we’re tyring to cluster an image we will continue adding pixels to the cluster until we have covered every pixel in our image.
- The selection of the next component is stochastic: the ant will choose according some probabilities. One key factor in their decision is the amount of pheromone related to a solution component, but several others will be considered depending on the algorithm you are using.
- Once an Ant has finished its solution, some things might happen. You can improve the generated solution with a local search procedure, for example, or you can start depositing pheromone. Once again, that depends on the nature of your algorithm.

And, the more important class if you are working on an Ant algorithm is the Ant itself. Here’s a snippet of the Ant class taken from Isula:

public abstract class Ant<C, E extends Environment> { private int currentIndex = 0; private List<AntPolicy<C, E>> policies = new ArrayList<AntPolicy<C, E>>(); private C[] solution; private List<C> visitedComponents = new ArrayList<C>();

Notice this:

- This is parametrized class. The class Parameter C represents a component in our solution: In the image clustering example, this class would represent a pixel and the assigned cluster.
- The solution is an array of these components. This Java Ant controls how the solution is built by keeping the current component index in the array.
- The Ant behavior is also determined by the algorithm you are using or proposing. For example, the specific rule for selecting a component vary greatly from algorithm to algorithm. This specific ant behavior is modeled as an AntPolicy in the Isula Framework.

**The Ants in Action**

Let’s put our colony to work in order to solve a specific optimization problem. The Flow Shop Scheduling optimization problem tries to find an optimal ordering of a number of jobs, given each job needs to be processed on some machines, requiring a specific amount of time on each machine depending on the job nature.

We will try to find an ordering of jobs -permutation in combinatorial terms- so we finish processing all the jobs in the minimum amount of time. Depending on the number of jobs and machines, solving this problem precisely can take considerable time, so we rely on our Isula-based ants to find a good-enough solution:

FlowShopProblemSolver problemSolver; ProblemConfiguration configurationProvider = new ProblemConfiguration(); problemSolver = new FlowShopProblemSolver(graph, configurationProvider); configurationProvider.setEnvironment(problemSolver.getEnvironment()); problemSolver.addDaemonActions( new StartPheromoneMatrixForMaxMin<Integer, FlowShopEnvironment>(), new FlowShopUpdatePheromoneMatrix()); problemSolver.getAntColony().addAntPolicies( new PseudoRandomNodeSelection<Integer, FlowShopEnvironment>(), new ApplyLocalSearch()); problemSolver.solveProblem(); showSolution(graph, problemSolver);

That’s a snippet from an Isula-based Java Program available here , which is an adaptation of the algorithm proposed by Thomas Stutzle in the paper “An ant approach to the flow shop problem”. Take a look at the following:

- The
*FlowShopProblemSolver*is an extension of the*AcoProblemSolver*class we reviewed before, with some minor modifications to suit the Flow-Shop problem. We call the*solveProblem*() method in order to trigger the generation of the solution. - The paper proposes an adaptation of the well-known algorithm Max-Min Ant System, so we are reusing some code already available for that algorithm, like the Daemon Action for initializing the pheromone matrix. The Update Pheromone Matrix policy was also reused but some adaptations were required.
- A very popular node-selection rule was proposed by Dorigo in its Ant Colony System algorithm. Stutzle used the same in its paper, so we can reuse it directly from the implementation available in Isula code.
- The author proposes a Local Search procedure after an Ant finishes building its solution. We developed a custom Ant Policy for that task.

We will try our algorithm with a problem instance of 20 jobs and 5 machines (dataset available here). Here’s a visual representation of the solution found:

I hope this article helps you get a feeling on what you can accomplish through Ant Colony algorithms, and how to do it in the Java Programming language. I also invite you to take a look at the Isula Framework and try to use the code already available. I’d like to finish with some Java Projects that use ACO algorithms to solve optimization problems:

- The Flow Shop Scheduling Problem, using Max-Min Ant System
- Image Binary Thresholding, with Ant System
- Image Clustering using Max-Min Ant System