Simple Ant updated
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
ACKNOWLEDGMENT
This model is originated from Chapter One of the book "Introduction to Agent-Based Modeling: Modeling Natural, Social and Engineered Complex Systems with NetLogo", by Uri Wilensky & William Rand.
- Wilensky, U. & Rand, W. (2015). Introduction to Agent-Based Modeling: Modeling Natural, Social and Engineered Complex Systems with NetLogo. Cambridge, MA. MIT Press.
I have further modified and heavily commented the model for learning NetLogo and Complexity concepts. I have made both Chinese and English vdieo tutorials on how to code up this model and analyse what this model generate. You are welcome to follow the tutorials while playing the model. https://github.com/EmbraceLife/shendusuipian/issues/50
WHAT IS IT?
In this model, 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.
HOW IT WORKS
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.
HOW TO USE IT
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.
If you want to change the number of ants, move the POPULATION slider before pressing SETUP.
THINGS TO NOTICE
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.
EXTENDING THE MODEL
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 model, the ants always "know" where the nest is: when they want to go back to the nest, they just turn towards the center of the world (using facexy 0 0
). Real ants use a variety of different approaches to find their way back to the nest. Try to implement some alternative strategies.
The ants only respond to chemical levels between 0.05 and 2. The lower limit is used so the ants aren't infinitely sensitive. Try removing the upper limit. What happens? Why?
In the uphill-chemical
procedure, the ant "follows the gradient" of the chemical. That is, it "sniffs" in three directions, then turns in the direction where the chemical is strongest. You might want to try variants of the uphill-chemical
procedure, changing the number and placement of "ant sniffs."
NETLOGO FEATURES
The built-in diffuse
primitive lets us diffuse the chemical easily without complicated code.
The primitive patch-right-and-ahead
is used to make the ants smell in different directions without actually turning.
RELATED MODELS
This model is a slight modification of the Ants models in the Biology section of the NetLogo models library.
CREDITS AND REFERENCES
This model is a simplified version of:
- Wilensky, U. (1997). NetLogo Ants model. http://ccl.northwestern.edu/netlogo/models/Ants. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.
HOW TO CITE
This model is part of the textbook, “Introduction to Agent-Based Modeling: Modeling Natural, Social and Engineered Complex Systems with NetLogo.”
If you mention this model or the NetLogo software in a publication, we ask that you include the citations below.
For the model itself:
- Wilensky, U. (1997). NetLogo Ants Simple model. http://ccl.northwestern.edu/netlogo/models/AntsSimple. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.
Please cite the NetLogo software as:
- Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.
Please cite the textbook as:
- Wilensky, U. & Rand, W. (2015). Introduction to Agent-Based Modeling: Modeling Natural, Social and Engineered Complex Systems with NetLogo. Cambridge, MA. MIT Press.
COPYRIGHT AND LICENSE
Copyright 1997 Uri Wilensky.
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.
This model was created 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.
Comments and Questions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Original Code provided by Uri Wilensky;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Wilensky, U. (1997). NetLogo Ants Simple model. http://ccl.northwestern.edu/netlogo/models/AntsSimple. ;; Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;further modified and heavily commented by 深度碎片;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; links to Chinese and English videos can be found from https://github.com/EmbraceLife/shendusuipian/issues/50 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; problem ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; 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. ;; define patch properties ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; patches-own [ ;; patches as land, has 2 properties pheromone ;; amount of pheromone food ;; amount of food ] turtles-own [ ;; turle or ant has one property carrying-food? ;; true or false ] globals [ count-a-pile ;; count the original number of food in one pile of food max-pheromone ;; maximum number of pheromone at any piece of land finish-time ;; a constant to make model stop at a long time later ] ;; How to experiment on properties and globals ;; clear-all, create-turtles 1 [set size 5 set color green ] ;; ask one-of patches [ set psize 5 set pcolor blue ] ;;;;;;;;;;;;;;;;;;;;;;;; ;;; Setup procedures ;;; ;;;;;;;;;;;;;;;;;;;;;;;; to setup ;; setup the world clear-all ;; wipe out the world first set-default-shape turtles "bug" ;; make all turtles' shape to be "bug" setup-patches ;; setup the world for ants set finish-time 1000000 ;; a constant to make model stop at a long time latera constant to make model stop at a long time later reset-ticks ;; put clock back to 0 ; import-world "ant.csv" ;; bring a saved world into setup to continue iterating end ;;;;;;;;;;;;;;;;;;;;; ;;; Go procedures ;;; ;;;;;;;;;;;;;;;;;;;;; to go ;; make iterations if count turtles < population [ create-ant ] ;; 1. create as much as ants as you want ask turtles [ ;; 2. ask each and every ant to move ;; perform ant's simple actions recolor ;; differentiate ants for carrying-food or not with color ] diffuse pheromone (diffusion-rate / 100) ;; 3. define how pheromone diffuse itself ask patches [ ;; 4. define how pheromone evaporate itself set pheromone pheromone * (100 - evaporation-rate) / 100 ;; decreasing by evaporation-rate if pheromone < 0.05 [ set pheromone 0 ] ;; if < 0.05, disappear ] recolor-patches ;; 5. paint patches of pheromone with scale-color set max-pheromone max [ pheromone ] of patches ;; find out the max pheromone on any patch in the world (stats) ;; if food is gone, if pherome is gone, and if stop-switch = true, then track the finish-time and put off stop-switch if sum [ food ] of patches = 0 and sum [ pheromone ] of patches = 0 and auto-stop? [ ; export-world "ant.csv" ;; to save the world agents data set finish-time ticks set auto-stop? false ] ;; to make sure it only happen once if ticks > finish-time + 100 [ stop ] ;; after both food and pheromone is gone, let model run 100 ticks, then stop the simulation ;; tick stop: stop model at specified ticks if stop-ticks? and ticks = user-ticks [ stop ] tick ;; update the iteration clock or ticks, without it we can't see updating in graphics end ;; for experiment ;;;;;;;;;;;;;;;;;;; to setup-behavior ;; setup the world for other experiment clear-all ;; wipe out the world first set-default-shape turtles "bug" ;; make all turtles' shape to be "bug" setup-patches ;; setup the world for ants setup-large-food ;; overwrite to create large food area reset-ticks ;; put clock back to 0 end to setup-large-food ;; paint 3 food areas ;; setup a food source on the right to the center ask patch (0.6 * max-pxcor) (0.6 * max-pycor) [ ;; find a patch of land at (0.6 x 35, 0.6 x 35) coordinates; make it same distance to second food pile make-food-source 20 cyan ;; paint this patch surround area with cyan ] ;; setup a food source on the lower-left ask patch (-0.6 * max-pxcor) (-0.6 * max-pycor) [ ;; find a patch of land at (-0.6 x 35, -0.6 x 35), try observer: show max-pxcor make-food-source 20 sky ;; paint this patch surrounding area with sky color ] ;; setup a food source on the upper-left ask patch (-0.8 * max-pxcor) (0.8 * max-pycor) [ ;; find a patch of land at (-0.8 x 35, 0.8 x 35), try observer: show max-pxcor make-food-source 20 blue ;; paint this patch surround area with blue ] ;; setup a food source on the lower-left ask patch (0.8 * max-pxcor) (-0.8 * max-pycor) [ ;; find a patch of land at (-0.8 x 35, 0.8 x 35), try observer: show max-pxcor make-food-source 20 gray ;; paint this patch surround area with blue ] end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to setup-patches ;; setup the world for ants setup-nest ;; build nest for ants setup-food ;; build food piles for ants recolor-patches ;; paint non-food, non-nest area, paint the pheromone to form trail end to setup-nest ;; paint nest with violet color ask patches with [ nest? ] [ ;; ask all patches whose nest? is true set pcolor violet ;; set this patch of land to violet color ] end to setup-food ;; paint 3 food areas ;; setup a food source on the right to the center ask patch (0.6 * max-pxcor) (0.6 * max-pycor) [ ;; find a patch of land at (0.6 x 35, 0.6 x 35) coordinates; make it same distance to second food pile make-food-source 5 cyan ;; paint this patch surround area with cyan ] ;; setup a food source on the lower-left ask patch (-0.6 * max-pxcor) (-0.6 * max-pycor) [ ;; find a patch of land at (-0.6 x 35, -0.6 x 35), try observer: show max-pxcor make-food-source 5 sky ;; paint this patch surrounding area with sky color ] ;; setup a food source on the upper-left ask patch (-0.8 * max-pxcor) (0.8 * max-pycor) [ ;; find a patch of land at (-0.8 x 35, 0.8 x 35), try observer: show max-pxcor make-food-source 5 blue ;; paint this patch surround area with blue ] end to make-food-source [ food-area food-source-color ] ;; build a food pile based on the given patch and paint it with specified color ;; by the way, this is a function with a user input ask patches with [ distance myself < food-area ] [ ;; based on current patch, ask its surrounding neighbor patches within radius 5 unit distance set food 2 ;; set the patches' food property to be 2 set pcolor food-source-color ;; set the patches' color to be specified color ] set count-a-pile count patches with [ distance myself < food-area ] ;; count the number of patches forming a pile of food end to recolor-patches ;; paint pheromones or trails of the patches of land which have food nor nest ask patches with [ food = 0 and not nest? ] [ ;; ask all patches which have neither food nor nest set pcolor scale-color green pheromone 0.1 5 ;; paint patches with scale-color on green depend on pheromone level (low value 0.1 = dark-green, high value 5 = light-green) ] end to move ;; 'move' contains all major ant behaviors if not carrying-food? [ look-for-food ] ;; 1. look for food, if not carrying food if carrying-food? [ move-towards-nest ] ;; 2. move towards nest, if carrying food wander ;; 3. both actions require ant to wander ;; 4. make the pen-down ant to draw its trail and action state ;; 4.1 paint for look-for-food and wander ask turtles with [pen-mode = "down"] [ ;; ask the turtle with pen down if not carrying-food? [ set label "look-for-food and wander" ;; if the turtle does not carry food, label it with "look-for-food" set label-color blue ;; and give blue as label color set color blue ;; paint the ant blue with its pen ] ] ;; 4.2 paint for move-towards-nest and wander ask turtles with [pen-mode = "down"] [ ;; ask the turtle with pen down if carrying-food? [set label "move-towards-nest and wander" ;; if the ant carries food, label ant with "move-towards-nest" set label-color yellow ;; and give yellow as label color set color yellow ;; paint the ant yellow with its pen ] ] ;; 4.3 paint for found food ask turtles with [pen-mode = "down"] [ ;; ask the turtle with pen down, if food > 0 [set label "Food, Food!" ;; if the ant found food, label ant with "Food, Food !" set label-color magenta ;; and give yellow as label color set color magenta ;; paint the ant yellow with its pen ] ] end to create-ant ;; create an ant create-turtles 1 [ ;; create a turtle set size 2 ;; make it big so easier to see set carrying-food? false ;; set ant property as not carrying food mode ; ;; testify this function ; fd random 10 ;; fd a random number between 1 and 10 ; set label who ;; Note the number of ants created ] end to move-towards-nest ;; How ants move back to nest ifelse nest? [ ;; under ask turtles mode, check each ant's `nest?` true or false set carrying-food? false ;; if at nest, drop food (set property 'carrying-food' to be false) rt 180 ;; and head out again is turn 180 degree ] [ set pheromone pheromone + 60 ;; if not at nest yet, increase the current patch's pheromone by 60 in value, leaving pheromone for tracking facexy 0 0 ;; turn towards the nest, which is at the center ] end to look-for-food ;; How ants look for food ifelse food > 0 [ ;; under ask turtles mode, we can access current turtle/ant's patch and its properties ;; if current patch's food is more than 0, meaning we found food on this patch/location set carrying-food? true ;; pick up food, set current turtle's carrying-food property to be true set food food - 1 ;; reduce current patch's food property by value 1 rt 180 ;; make the current ant turn around by 180 to the right ] [ uphill-pheromone ;; if no food at this patch, this ant go face towards the direction where the pheromone smell is strongest ] end to uphill-pheromone ;; sniff left and right, and face towards where the strongest smell is if pheromone < pheromone-sensitivity [ ;; if current patch's pheromone is less than 2 (very weak);; what if uplift the upper limit from 2 too 200 let scent-ahead pheromone-scent-at-angle 0 ;; assign the current heading direction-next patch's pheromone to local variable 'scent-ahead' let scent-right pheromone-scent-at-angle 45 ;; assign local variable 'scent-right' with the pheromone value of the patch ;; which is 1 patch away with direction of current heading turn right 45 degree let scent-left pheromone-scent-at-angle -45 ;; assign local variable 'scent-left' with the pheromone value of the patch ;; which is 1 patch away with direction of current heading turn left 45 degree ;; ant only look for stronger pheromone in the area ahead of itself with 90 degree variation if (scent-right > scent-ahead) or (scent-left > scent-ahead) [ ;; if pheromone on the right is more than that ahead or that on the left is more than that ahead ifelse scent-right > scent-left ;; and if pheromone on the right is more than that on the left [ rt 45 ] ;; turn right 45 degree [ lt 45 ] ;; otherwise, turn left 45 degree ] ;; this is for test-look-for-food only ; type "left " print precision scent-left 2 ; type "ahead " print precision scent-ahead 2 ; type "right " print precision scent-right 2 ] end ;; for experiment ;;;;;;;;;;;;;;;;;;; to test-look-for-food ;; This is for verify function look-for-food and uphill-pheromone ask patches with [ pxcor > 0 and pxcor < 30 and pycor < -10 and pycor > -30 ] [ set pheromone pxcor / 15 set pcolor scale-color green pheromone 0 2] create-turtles 1 [ set size 5 set color sky set xcor 0 set ycor -20 set heading 90] ask patches with [ pxcor = 5 and pycor = -20 ] [ set pheromone pheromone - 1 set pcolor blue] ask patches with [ pxcor = 6 and pycor = -19 ] [ set pheromone pheromone - 1 set pcolor red] end to go-test-look-for-food ;; This is for verify function look-for-food and uphill-pheromone ask turtles [ look-for-food fd 1] end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to wander ;; make ant wander around rt random 40 ;; turn right randomly with maximum value at 40 degrees lt random 40 ;; turn left randomly with maximum value at 40 degrees ;; given ant can sniff where the stronger pheromone is, wonder makes the ant direction vary from -40 to 40 degree (user defined variation) if not can-move? 1 [ rt 180 ] ;; if ant can not move ahead by 1 unit distance, then turn right 180 degrees fd 1 ;; move forward by 1 unit distance end to recolor ;; use color differentiate ants for carrying-food or not ifelse carrying-food? ;; if the current ant has food on it [ set color orange + 1 ] ;; set its color to be orange + 1 [ set color red ] ;; otherwise make it color red end to-report pheromone-scent-at-angle [ angle ] ;; report the value of pheromone of a patch on certain direction and distance let p patch-right-and-ahead angle 1 ;; create a local variable p, assign a patch to it, such patch is about `angle` to the right and 1 patch distance away if p = nobody [ report 0 ] ;; if this patch does not exist, report value 0 report [ pheromone ] of p ;; if this patch exist, report this patch's pheromone value end to-report nest? ;; report a property like value for all patches or turtles ;; if the distance between the turtle and (0,0) is smaller than 5, report true; otherwise false ;; if the distance between the standing ant and (0,0) is smaller than 5, report true; otherwise false report distancexy 0 0 < 5 end ; Copyright 1997 Uri Wilensky. ; See Info tab for full copyright and license.
There is only one version of this model, created almost 7 years ago by 深度碎片 Kenny.
Attached files
File | Type | Description | Last updated | |
---|---|---|---|---|
Simple Ant updated.png | preview | Preview for 'Simple Ant updated' | almost 7 years ago, by 深度碎片 Kenny | Download |
This model does not have any ancestors.
This model does not have any descendants.