Ant Colony food-finding

Prepared by: Eric Lloyd

Introduction
Algorithm Description
Results
Usefulness/Applicability
Links to Code/Project

Back to main page

Introduction

The behavior exhibited by colonies ants provides an excellent example of emergent behavior. Individually, each ant is relatively simple and "stupid", yet when they gather into an entire colony they are able to build nests, find food, and-most importantly-adapt to a constantly changing environment. All of these characteristics make the behavior of ant colonies an intruiging problem to study.

In particular, I examined the food foraging behavior of ants. When searching for food in the real world, ants leave a trace of pheremone behind, which other ants can then detect and choose to follow. Each ant will, in general, follow a path that has the highest consentration of pheremone, as this means that a large number of ants have taken this path. In this way, ants are able to follow paths to items like food without actually being aware of the location of their goal.

Algorithm Description

To implement this ant food-foraging algorithm using StarLogo, I had to think about what programming an 'ant' would need, and what programming the 'world' would need. These needs were mapped to the turtle and observer code, respectively. In general, these are the steps a single ant would need to take:

check the spot we're standing on
    if it's food and we don't have food, pick it up and turn around
    if it's the nest and we have food, drop it and turn around

Find the direction with the highest scent and turn in that direction
wiggle to introduce randomness
drop some level of chemical
move forward one square
repeat
This simple algorithm creates several questions, however. How does the ant determine the direction with the highest scent? How does the ant drop chemicals? Does the value remain the same or change based on some factor? Should only a single chemical be dropped? How does this effect performance? To answer these questions, I took several approaches (the results of these different approaches are documented in the results section.

For the levels of chemicals, I initially made an ant drop more chemical when it was carrying food that when it wasn't. This meant that when ants were initally randomly searching for food, they wouldn't be as likely to create strong paths that wouldn't lead to food. Here is the code:


ifelse hasFood? [setchemlevel chemlevel + .1]
          [setchemlevel chemlevel + .2]
 

To determine the best direction to move, I used a simple function that works similar to gradient ascent. Since the "world" of the ants is divided into patches, which can be thought of as a simple grid system, each ant has a possibility of 8 squares for it's next move. In one version of the program, I had the ants check the chemical levels of the three squares ahead of them. The ants would look at the chemlevel of each square, and would turn in the direction of the highest level of chemical.


to find-best-path
  setahead next-chemlevel
  lt 45
  setleft next-chemlevel
  rt 90
  setright next-chemlevel
  lt 45
  
  if (right > ahead) and (right > left) 
      [rt 45 stop]

  if (left > ahead) and (left > right) 
      [lt 45]     

end

to next-chemlevel
  ;;return the chemlevel variable at the spot
  ;;  we would be at if we moved fd 1
  output chemlevel-at dx dy
end

In another version of the program, I allowed the ants to check the square directly to their left and right, thinking that since they would be checking more neighbors, they would more accurately follow a path. As you can see in the Results section, this wasn't the case.

Finally, I made the simplest version of the code contain a single chemical that the ants would use to both find their way to food and back from food. In general, this meant that ants needed to tend to move in a forward direction so they wouldn't get confused between attempting to reach the nest and attempting to reach food. Another possibility was to use two separate chemicals, one for a path to food, and one for returning to the nest. I learned that in the wild, an ant's nest gives off a specific "scent" that the ants can "smell" when they are foraging. The scent becomes stronger as the ants approach the nest, and in this way they can tell if they are heading towards the nest.

For example, here is a sample space, where the blue circle represents the nest, the purple circles represent the food, and the red color is the distribution of nest-scent. The darking a sqaure is, the less nest scent it has on it.

By adding the "nest-scent" idea, the algorithm changed slightly. Instead of always dropping chemicals (pherenomes) and always following pherenomes, ants needed to check two different chemicals. When they were simply foraging for food, they would leave no chemical, but would follow the path with the highest pheremone level. When the ants had found food, they would begin drop phernome, but now search for the path with the highest nest-scent value, since that path should move them closer to the nest.

For the 'world' code, the only important code segment to the algorithm is the section that deals with evaporation. This allows for the pheremone levels to decrease over time, which in turn allows unfruitful paths to be abandonded. This is an important section of the algorithm. With an environment containing several locations with food, for example, when one food source is depleted, eventually ants should abandon the path to this spot in favor of more fruitful paths.
In my implementation, each patch (grid square) had a chemlevel--a floating point number that represents the pherenome level on that particular square. Here is the StarLogo code that reduces the amount of pherenome by a constant factor after a set amount of time.


if not (chemlevel = 0) 
        [setchemlevel chemlevel * evap-rate / 100] 
if (chemlevel < .1) [setchemlevel 0]

Where evap-rate is the percent of the chemical that remains. ie, chemlevel = 70 means that every time this function runs, the chemlevel with be reduced by 30%.

Results

The results of this program provide interesting insight into emergent behavior. For all of my different cases, the basic state space for the ants was set up as such, with the nest in the center and three piles of food at varying distances.

Each square that held food was initialized to hold a random "number" of food pieces between 0 and 10. Once the food on a spot was depleted, it would turn black and function a just another spot of ground.

Using the simplest algorithm was really very successful. All the ants were sent searching at one time, which filled the screen with the green pheremone trails. Ants that found food quickly (which tended to happen more with the closer food source) would immediately turn around and follow their scent back to the nest, reinforcing their path with it. As more ants would return to the nest, more would choose to follow this path, until the food was depleted. At that point, paths to the other food sources became more popular. (note that in the following pictures, the lighter green a grid square is, the more pheremone it has on it)

     

There were, however, some unprofitable paths that were maintained. Since each ant had no idea where the nest was, all it could do is follow the strongest path in the general direction it was moving. If it happened to cross a path with similar pherenome strength the the path it was on, and if this path didn't cause to much of a direction change, an ant could choose either path--Even if one path led directly to the nest and one led completely out of the way.

When each ant was allowed to check five directions, the results became much worse. Eventually, every single ant became stuck in a cycle that would never lead to food.

        

This phenomenon occured because when ants were allowed to examine the squares immediately to their left and right as choices for their next move, it became possible (and not only possible, but likely) that the ants would tend to oscillate between two adjacant squares, increasing chemical levels and causing any ants passing by to become caught in the loop.

The most successful project (by successful I mean the project where there were the fewest number of false paths created and the least amount of time was taken to consume all the food) was the implementation that used the nest-scent. In this example, the ants didn't create a large number of paths during the initial foraging. Instead, the first paths were created only after ants had found any food. Also, since the nest scent always increased as it approached the nest, from any point on the map, an ant could find it's way back to the nest without having to search. This made the paths to the food extremely simple, as shown below:

While this did give better results, I don't believe it is as useful for a real algorithm. In many cases, agents are not going to be able to know exactly where one of the goals is from their current position. The agent won't be able to always know that increasing nest scent always leads to a nest, for example, so by making the agent more complex (ie, two different chemicals) you also run the risk of making it much less adaptable.

Usefulness/Applicability

This very interesting result from the ant foraging algorithm has a large practical application in the technilogical world. The most prominent use of this algrotihm can be found with packet routing over the internet. The decision of what route to send a packet along the internet invovles find the quickest path to the destination. This is acheived in a similar manner to the ants find food above. Each packet leaves an amount of a "chemical" behind as it travels a route over the internet. Packets then behave like ants in that they decide which path to take next based on the "chemical" level at each route choice. This algorithm is very powerful because it is extremely adaptable--It the network throuput changes, a new route will become more popular, and thus the packets will be routed around the slow network node without having to explicitly send them there.

This algorithm is a specific instance of the classic problem of the travelling salesperson. Using ant food foraging, this problem is correctly solved a large amount of the time. Again, the real power of algorithms based on emergent behavior is their adaptability to unforeseen situations. Say, for example, one of the paths in a TSP map has become unavailable (ie, construction, etc), but this condition isn't known until someone attempts to take that path. A classic algorithm may incorrectly route the saleman through the construction, while ants would have changed their behavior pattern as soon as the problem arose.

References
http://news.bbc.co.uk/1/hi/sci/tech/1537645.stm
http://ai-depot.com/Essay/SocialInsects-Ants.html
http://www.ieee-icnp.org/2004/papers/1-4.pdf

Links to Code/Project
To see my projects in action on the web via a java applet, click on one of the following:

Basic food/nest finding algorithm - single chemical,
checking only three forward squares for possible movement

View online

Basic food/nest finding algorithm - single chemical,
checking both forward and side squares for movement

View online
More complicated food/nest finding algorithm - separate
nest/food chemicals, checking only three forward squares for
possible movement
View online

If that doesn't work, you can download the StarLogo package from their homepage at http://education.mit.edu/starlogo/ and my .slogo project files from below

Basic food/nest finding algorithm - single chemical,
checking only three forward squares for possible movement

Right-click and select 'Save as'
ERICants.slogo

Basic food/nest finding algorithm - single chemical,
checking both forward and side squares for movement

Right-click and select 'Save as'
ERICants-check5directions.slogo
More complicated food/nest finding algorithm - separate
nest/food chemicals, checking only three forward squares for
possible movement
Right-click and select 'Save as'
ERICants-nestscent.slogo

 

The source code for each project is divided into two separate files, one containing the code for the turtles, one containing the code for the observer

Basic food/nest finding algorithm - single chemical,
checking only three forward squares for possible movement

turtles source code
observer source code

Basic food/nest finding algorithm - single chemical,
checking both forward and side squares for movement

turtles source code
observer source code

More complicated food/nest finding algorithm - separate
nest/food chemicals, checking only three forward squares for
possible movement

turtles source code
observer source code