Simple Ant updated

Simple Ant updated preview image

1 collaborator

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 6.0.4 • Viewed 124 times • Downloaded 9 times • Run 0 times
Download the 'Simple Ant updated' modelDownload this modelEmbed this model

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


Info tab cannot be displayed because of an encoding error

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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 over 5 years ago by 深度碎片 Kenny.

Attached files

File Type Description Last updated
Simple Ant updated.png preview Preview for 'Simple Ant updated' over 5 years ago, by 深度碎片 Kenny Download

This model does not have any ancestors.

This model does not have any descendants.