Ant Colony

No preview image

1 collaborator

Default-person Tina Conis (Author)


(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 5.0.3 • Viewed 889 times • Downloaded 47 times • Run 0 times
Download the 'Ant Colony' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


This is an extention "Ants", a model included in the default model library of NetLogo.

In this project, a colony of ants forages for food. Though each ant follows a set of simple rules, the colony as a whole acts in a sophisticated way.


When an ant finds a piece of food, it carries the food back to the nest, dropping a chemical as it moves. When other ants "sniff" the chemical, they follow the chemical toward the food. As more ants carry food to the nest, they reinforce the chemical trail.


Click the SETUP button to set up the ant nest (in violet, at center) and three piles of food. Click the GO button to start the simulation. The chemical is shown in a green-to-white gradient.

The EVAPORATION-RATE slider controls the evaporation rate of the chemical. The DIFFUSION-RATE slider controls the diffusion rate of the chemical. There is an on-off PLOT? switch. Turning off the plotting lets the model run faster.

If you want to change the number of ants, move the POPULATION slider before pressing SETUP.

The original sliders were teaked a little to add functionality.

You can click the screen to add food.

AMOUNT-OF-WIGGLE is a parameter that controls how much the ants divate from a straight path. It randomly chooses a number from a normal distribution and tells the ant to step slightly in that direction. The slider adjusts the standard deviation. If this slider is set to 0, the ants will walk in straight lines. If it is set higher than 100, the ants pretty much act like Brownian motion. Optimal wiggle is about 20.

LoS is an abbreviation for Line of Sight. This parameter tells the ant how far to the left and right it should �sniff�. Optimal value is about 40. Anything under 15 and over 150 seems to be dysfunctional.

CHEM-LOW-THRESHOLD tells the ant to ignore scents that are below this threshold. Setting this number to 0 lets the ants smell infinitely small amounts of pheromone. The optimal value for this seems to be dependent on the food source, amount of ants, evaporation-rate and diffusion-rate.

CHEM-UPPER-THRESHOLD tells the ants to ignore the difference in scent for pheromone over this threshold. The optimal value for this seems to be dependent on the food source and the amount of ants.

RETURN-WIGGLE is a switch that when on lets ants with food wiggle as much as ants without food. When this is switched off, ants returning with food walk in a straight line towards the nest.


The ant colony generally exploits the food source in order, starting with the food closest to the nest, and finishing with the food most distant from the nest. It is more difficult for the ants to form a stable trail to the more distant food, since the chemical trail has more time to evaporate and diffuse before being reinforced.

Once the colony finishes collecting the closest food, the chemical trail to that food naturally disappears, freeing up ants to help collect the other food sources. The more distant food sources require a larger "critical number" of ants to form a stable trail.

The consumption of the food is shown in a plot. The line colors in the plot match the colors of the food piles.


Try different placements for the food sources. What happens if two food sources are equidistant from the nest? When that happens in the real world, ant colonies typically exploit one source then the other (not at the same time).

In this project, the ants use a "trick" to find their way back to the nest: they follow the "nest scent." Real ants use a variety of different approaches to find their way back to the nest. Try to implement some alternative strategies.


Copyright 1997 Uri Wilensky. All rights reserved.

Permission to use, modify or redistribute this model is hereby granted, provided that both of the following requirements are followed:

a) this copyright notice is included.

b) this model will not be redistributed for profit without permission from Uri Wilensky. Contact Uri Wilensky for appropriate licenses for redistribution for profit.

This model was created as part of the project: CONNECTED MATHEMATICS: MAKING SENSE OF COMPLEX PHENOMENA THROUGH BUILDING OBJECT-BASED PARALLEL MODELS (OBPML). The project gratefully acknowledges the support of the National Science Foundation (Applications of Advanced Technologies Program) -- grant numbers RED #9552950 and REC #9632612.

This model was developed at the MIT Media Lab using CM StarLogo. See Resnick, M. (1994) "Turtles, Termites and Traffic Jams: Explorations in Massively Parallel Microworlds." Cambridge, MA: MIT Press. Adapted to StarLogoT, 1997, as part of the Connected Mathematics Project.

This model was converted to NetLogo as part of the projects: PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN CLASSROOMS and/or INTEGRATED SIMULATION AND MODELING ENVIRONMENT. The project gratefully acknowledges the support of the National Science Foundation (REPP & ROLE programs) -- grant numbers REC #9814682 and REC-0126227. Converted from StarLogoT to NetLogo, 1998.

Comments and Questions

Some feedback on your model

Hi Tina, First off - I think this is a neat idea for solving the traveling salesman problem and starting from the existing ants model is a great way to start, but I'll need a little more info from you to understand exactly how you want this to work. Can you explain to me your overall idea of how you wan to use the ant logic to help with the traveling salesman problem? Using the ant model, you have a way for ants to locate the food sources, and leave trails so others can find them. If you think of the food sources as 'homes' for the traveling salesmen - what is the next step for finding the best (or at least a good) route for all the homes? One quick comment about the model as it exists: In the setup method - the crt command creates turtles, but what you is ants, so you can replace 'crt' (which is short for create-turtles) with 'create-ants', which will create ants for your model. Send along your response to my above questions and I can give more directed feedback. Happy coding!

Posted over 4 years ago

Summary of my model

I haven't worked out my model completely yet, but I have an idea of how I want it to work. I want to start off with each food source being the nest for several ants but I don't know how to write the code to make that happen. I want the ants to travel from their original food sources to all of the other food sources and back, traveling to each food source (other than their nest), one exactly once. After that, they should travel around to each of the other food-sources, based on the pheromone trails, but not go back to their nests until the end of that cycle. This should repeat until a majority of the ants are following the same path. I want to be able to keep track of the paths and the path lengths somehow. Thank you for your advice

Posted over 4 years ago

Some thoughts on how to design your model

Hi Tina, I think your description makes sense to me. Here is what I am thinking for how you could about implementing this. 1 - each ant will need to know how many stops it needs to make in total, as well as keep track of where it has been and where it needs to go. 2 - after an ant leaves a food source, it leaves a phermone trail specific to that food source (so an ant leaving food source 2 leaves a different trail than an ant leaving food source 3). 3 - if the ant has not hit all the food sources yet, it wanders until it finds a phermone trail leading to a source it hasn't yet been to - so if the ant finds the pheramone trail for food source 3 but has already been there, it keeps walking; if it his the pheramone trail for food soruce 5 and hasn't visited it yet, it will then follow that trail. 4 - the ant continues wondering until it finds the last food source then returns back to the nest. How does all that sound? Does that make sense? Let me know and happy coding!

Posted over 4 years ago

That sounds great

Hi, That all makes sense. I like your idea of having different pheromone trails for each of the food sources. I was also thinking that (at least after the first cycle) the ants would determine which path to take depending on the strength of the pheromone trails. I'm fairly new to coding so I'm not exactly sure how I would go about writing the code for all of this. If you could give me any tips on that, that would be great. Thank you!

Posted over 4 years ago

Click to Run Model

breed [food-source
breed [ants]
ants-own [original_x_location 
patches-own [
  chemical             ;; amount of chemical on this patch
  ground               ;; color of ground                 ;; amount of food on this patch (1 or 2)
  food                ;; true on nest patches, false elsewhere
  nest-scent]           ;; number that is higher closer to the nest

;;; Setup procedures ;;;

to setup
  ;; (for this model to work with NetLogo's new plotting features,
  ;; __clear-all-and-reset-ticks should be replaced with clear-all at
  ;; the beginning of your setup procedure and reset-ticks at the end
  ;; of the procedure.)
  set-default-shape turtles "ant"  ;; makes them look like ants
  resize-world -40 40 -40 40
  ;;resize-world -30 30 -30 30

;;; Go procedures ;;;

to setup-food-sources
   set-default-shape food-source "circle"
  create-food-source number-of-food-sources [setxy random-xcor random-ycor] 
  ask food-source [set food number-of-food-sources]
  ask food-source [hatch-ants 1]
    ask ants[set size 3         ;; easier to see
    set color red 
    set original_x_location xcor
    set original_y_location ycor]    ;; red = not carrying food

to go  ;; forever button
  ask ants
  [  look-for-food         ;; not carrying food? look for it
     return-to-nest        ;; carrying food? take it back to nest
    fd 1 ]
  diffuse chemical (diffusion-rate / 100)
  ask patches
  [ set chemical chemical * (100 - evaporation-rate) / 100  ;; slowly evaporate chemical

to return-to-nest  ;; turtle procedure
  ask ants [setxy original_x_location original_y_location]
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; get from original code
   set chemical chemical + 60  ;; drop some chemical
    uphill-nest-scent        ;; head toward the greatest value of nest-scent

to look-for-food  ;; turtle procedure
  if not can-move? 1 [ rt 180 ] 
  if xcor = original_x_location and ycor = original_y_location [fd 2] ;;;;;;;;;;;;;;;;;;;;;;;;;;;get ants to ignore food on original food-source
  if food > 0
  [         ;; pick up food
    set food food - 1        ;; and reduce the food source
    rt 180                   ;; and turn around
    stop ]
  ;; go in the direction where the chemical smell is strongest
  if (chemical >= Chem-Low-Threshold) and (chemical < Chem-Upper-Threshold)
  [ uphill-chemical ]

;; sniff forward, left, and right, and go where the strongest smell is

to uphill-chemical  ;; turtle procedure
  let scent-ahead chemical-scent-at-angle   0
  let scent-right chemical-scent-at-angle  LoS
  let scent-left  chemical-scent-at-angle  (LoS * -1)
  if (scent-right > scent-ahead) or (scent-left > scent-ahead)
  [ ifelse scent-right > scent-left
    [ rt LoS ]
    [ lt LoS ] ]

;; sniff left and right, and go where the strongest smell is

to uphill-nest-scent  ;; turtle procedure
  let scent-ahead nest-scent-at-angle   0
  let scent-right nest-scent-at-angle  LoS
  let scent-left  nest-scent-at-angle  (LoS * -1)
  if (scent-right > scent-ahead) or (scent-left > scent-ahead)
  [ ifelse scent-right > scent-left
    [ rt LoS ]
    [ lt LoS ] ]

to wiggle  ;; turtle procedure
  rt random-normal 0 amount-of-wiggle
  ;; lt random-normal 0 amount-of-wiggle
  ;; if not can-move? 1 [ rt 180 ]

to-report nest-scent-at-angle [angle]
  let p patch-right-and-ahead angle 1
  if p = nobody [ report 0 ]
  report [nest-scent] of p

to-report chemical-scent-at-angle [angle]
  let p patch-right-and-ahead angle 1
  if p = nobody [ report 0 ]
  report [chemical] of p

;; Credit to 1997 Uri Wilensky for frame of program

There are 2 versions of this model.

Uploaded by When Description Download
Tina Conis over 4 years ago updated model Download this version
Tina Conis over 4 years ago Initial upload Download this version

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.