Follow Trail NetLogo Model

Produced for the book series "Exercises for Artificial Intelligence";

Author: W. J. Teahan; Publisher: Ventus Publishing Aps, Denmark.

powered by NetLogo

view/download model file: Follow-Trail.nlogo

WHAT IS IT?

This NetLogo model allows the user to test out various trail following behaviours for ants. It is an extension of the Santa Fe Trail model. The Santa Fe Ant Trail was devised by John Koza in order to test the performance of evolutionary algorithms. This model allows the user to load various trails from data files, or draw them directly, then various behaviours can be loaded into the input box and modified for testing on the various trails.


HOW IT WORKS

One or more turtle agents are created as the ants at random locations. Their actions are determined by the code in the Ant-Actions input box of the Interface. This box is editable - the user can modify the code as she or he wishes by pressing the Change box. The code is then executed by the ant each tick when the Go button is pressed.

Various motor actions for the ants are provided in the model for use in defining their behaviour and are explained as follows:
- move: This moves the ant forward one step.
- turn-left: This turns the ant left by 90 degrees.
- turn-right: This turns the ant right by 90 degrees.
- wiggle: The ant changes its direction randomly as determined by the wiggle-angle slider. (The amount of the change in direction will be a random number from 0 to the wiggle-angle value).
- wiggle-maybe: The ant performs a wiggle, but only with probability 1 / wiggle-maybe-ratio.

Various sensing and/or sensory-motor actions for the ant are provided in the model for use and are explained as follows:
- food-ahead: This reports whether there is food directly ahead of the ant.
- food-on-my-left: This reports whether there is food on the left of the ant.
- food-on-my-right: This reports whether there is food on the right of the ant.
- food-ALR: This reports the action needed to be performed by the ant if there is food nearby. If there is none, then the default action is to move ahead.
- food-direction: This reports the direction of food if there is any nearby. If there is none, then the default direction is 0 degrees (straight ahead).


HOW TO USE IT

Choose which behaviour you want to be executed by the ant using the which-behaviour chooser. This will then be loaded into the Ant-Actions input box when the Setup button is pressed. Then to run the model, press the Go button.

You can draw you own parts of the trail using the Draw Trail button, or erase parts of the trail using the Erase Trail button.


THE INTERFACE

The buttons in the Interface are defined as follows:

- Setup: This will clear the environment and load the trail specified by the which-trail slider into it, and create no-of-ants-to-add ant turtle agents at random locations. It will also load the code for the ant's behaviour (as specified by the which-behaviour chooser) into the Ant-Actions input box. This can then be edited by the user if they wish.

- Go: This will run the simulation.

- Create New Ants: This will add new ants into the environment, the amount being that specified by the no-of-ants-to-add slider.

- Erase All Ant Paths: This will erase all the ant paths.

- Draw Trail: This allows the user to draw their own trail for testing out different environments.

- Erase Trail: This allows the user to erase any existing parts of the trail.

The chooser, monitor, input box, sliders and switches are defined as follows:

- which-behaviour: This specifies the behaviour that gets loaded into the Ant-Actions input box and subsequently executed when the Go button is pressed. The behaviours are defined as follows:
"Koza": This is Koza's evolved behaviour for the Santa Fe Trail.
"Koza Modified": This is Koza's evolved behaviour for the Santa Fe Trail but modified to remove unnecessary steps.
"Santa Fe Optimum": This is the optimum behaviour for the Santa Fe Trail (it follows the trail exactly).
"Look ALR 1": The ant looks ahead for the trail, then left, then right (no peripheral vision); it turns in the direction of food that was first found, otherwise it moves forward.
"Look ALR 2": The ant looks ahead for the trail; then left; then right (using peripheral vision). Then it moves in the direction of food.
"Look ALR 3": This is similar to "Look ALR 2" but uses simpler code.
"Look ALR 4": This is similar to "Look ALR 2" but uses even simpler code.
"Look ALR 5": This is the same as for "Look ALR 2" but the ant wiggles if the trail is not found.
"Look ALR 6": This is the same as for "Look ALR 5" but wiggle-maybe is used to get the ant out of any loops it may be stuck in as it follows the path (e.g. such as a small square loop at trail corners).

- which-trail: This specifies the trail that is drawn in the environment. The trails are defined as follows:
"None": This will not draw any trail into the environment at Setup. User-drawn trails can be added using the Draw Trail button and erased using the Erase Trail button.
"Circle": This draws the trail as a filled in circle in the centre of the environment if the circle-at-centre? switch is set to On, and centred at the origin (top left) otherwise. (This will cause the circle to be wrapped across the four corners of the environment.) The dimensions of the circle are specified using the size-of-circle slider.
"Cross": This will draw the trail as a cross in the centre of the environment.
"Ring": This will draw the trail as a circle in the environment as for the "Circle" trail above, but the centre of the circle will be hollow i.e. it will end up as a ring shape. The width of the ring is specified using the ring-width slider.
"Santa Fe": This is the Santa Fe Ant Trail as for the Santa Fe Trail model.
"Spiral": This will draw the trail as a square spiral in the centre of the environment.
"Square": This will draw the trail as a square towards the outer part of the environment.

- Ant-Actions: These are the actions the ant performs each tick of the simulation.

- no-of-ants-to-add: This specifies the number of ants that are initially created at setup, and subsequently when the Create New Ants button is pressed.

- wiggle-angle: This specifies the random angle directional change if the ants employ the use of the wiggle or wiggle-maybe motor action in their behaviour.

- wiggle-maybe-ratio: This defines the probability that the ant performs a wiggle randomly if the wiggle-maybe action is included in their behaviour. The probability is set as 1 / wiggle-maybe-ratio that a wiggle will occur.

- size-of-circle: This is the size of the circle when the which-trail chooser is set to "Circle".

- ring-width: This is the width of the ring when the which-trail chooser is set to "Ring".

- Show-Paths?: If this is set to On, then the path that each ant takes will be drawn in the environment.

- circle-at-centre?: If set to On, and which-trail is set to "Circle" or "Ring", this will draw the circle or ring trail at the centre of the environment; otherwise, it will draw it at the origin (top left corner).

- random-heading?: If set to On, this will result in the ants having a random heading when they are created rather than heading towards the origin (top left corner).

- Number of Ants: This is the number of ants currently in the environment.

- Proportion of ants on the trail: This is the proportion of ants on the trail compared to the total number of ants


THINGS TO NOTICE

Notice the incredible variety of situations that arise in the simulation despite the relative simplicity of both the behaviours and the trails, with many emergent phenomena occurring as a result of the interaction of the ants with the environment.

Notice that many of the ants end up getting stuck in a tight square loop, often at trail corners. There are two additional behaviours added to this model from the Santa Fe Trail model ("Look ALR 5" and "Look ALR 6") that include some random 'wiggling' to provide the ants with a random element to their behaviour. However, notice that the ants' ability to follow the trails for the "Look ALR 6" behaviour appears to be slightly compromised as a result compared to the other similar "Look ALR"-type behaviours that do not include the addition of the wiggle-maybe action prior to the final move action.

Notice that the Santa Fe Optimum behaviour does not perform any sensing actions - there are only the motor actions needed to follow the Santa Fe Trail exactly. This is the minimum number of motor steps needed to follow the trail (hence why this is called the Santa Fe Optimum behaviour). However, this behaviour is too specific - it is only useful for following the Santa Fe Ant Trail and nothing else. For example, if you set the which-behaviour slider to "Santa Fe Optimum" and the which-trail slider to "Santa Fe", you will notice that unless the ant is located at the start of the trail and pointing in the right direction at setup, it will not follow the trail at all. (The paths of all the ants do however create an interesting pattern).

Notice what happens when you select the Circle trail, set the number of ants to maximum, and turn off the Show-Paths? switch. The ants seem to spin around the circle. At the beginning of the simulation, they also head en masse to four different quadrants of the circle if the random-heading? switch is set to Off.

Notice what happens when you turn the random-heading? switch to Off. The ants for most behaviours will head in clumps to a common point (the origin, or top left corner) at the beginning of the simulation before they acquire the trail. This can lead to some interesting effects for some of the trails. Try repeatedly adding more ants into the environment when you are testing this out.

Notice which behaviours in relation to which trails lead to higher proportions of ants on the trail (as measured by the Proportion of ants on the trail monitor).


THINGS TO TRY

Try out all the different behaviours on the different trails to see which do well at keeping to the trail. Which trails cause problems for the agents to follow?

Try out different values for the sliders to see what affect this has on the ants' abilities to follow the trails. Try out with a small number of ants and then with a large number of ants (by changing the value of the no-of-ants-to-add slider). Turn the Show-Paths? switch on and off to see the simulation with and without the paths drawn. Try adding more and more agents by repeatedly pressing the Create New Ants button. (You will need to turn the Show-Paths? switch to Off when you do this, otherwise the ants' paths will quickly swamp the visualisation).

Try writing your own behaviours or modify an existing one using the Change button in the Ant-Actions input box. (For example, remove the last 'move' command from Koza's behaviour to see what happens). Make sure that you press the Setup button first before doing this to clear any existing paths. How well do your behaviours perform?

Try drawing a one-patch wide cross inside of the ring trail when the which-behaviour slider has been set to "Look ALR 5" after the simulation has been running for a while so that the Proportion of ants on the trail is at 1 (i.e. 100% of the ants are on the ring). Why do the ants fail to follow the inside cross? Next try increasing the width of the cross to two patches or more. What happens now and why?


EXTENDING THE MODEL

Try modifying the behaviour to include collision detection so that the ants avoid other ants heading their way. Also add in a crowd following behaviour where the ants head in the direction of the crowd when there are a lot of other ants heading the same way.


NETLOGO FEATURES

Note the use of the run command to run the commands in the Ant-Actions code.


RELATED MODELS

See the Santa Fe Trail model.


CREDITS AND REFERENCES

This model was created by William John Teahan and Loukas Georgiou.

To refer to this model in publications, please use:

Follow Trail NetLogo model.
Teahan, W. J. (2010). Exercises for Artificial Intelligence. Ventus Publishing Aps


PROCEDURES

; Follow Trail model.
; 
; The ant turtle agents try to follow a trail or trails.
;
; This model is based on the Santa Fe model written by Loukas Georgiou and Bill Teahan.
;
; Copyright 2010 William John Teahan and Loukas Georgiou. All Rights Reserved.

breed
[ ants ant ]          ;; These are the turtle agents that wander around trying to find and follow the trail or trails

globals
[
  init-actions        ;; Actions have been initialised (checked and modified in the first loop of "go"
  actions             ;; The Ant-actions to be executed in "go"
  colour-of-trail     ;; This is the colour used to draw the trail
  colour-of-grass     ;; This is the colour to draw everything that is not the trail
]

;;;;;;;;;;;;;;;;;;;;;;;;
;;; Setup procedures ;;;
;;;;;;;;;;;;;;;;;;;;;;;;

to setup-ant

  set size 1.5
  set color (random 14) * 10 + 5
  if (color = colour-of-trail or color = colour-of-grass)
    [ set color black ]
  setxy random-xcor random-ycor
  if (not random-heading?)
    [ facexy 0 0 ]
  ifelse Show-Paths?
    [ pen-down ]
    [ pen-up ] 
end
  
to setup

  clear-all  
  setup-global-settings
  setup-santa-fe-trail
  execute-behaviour
  
  create-ants no-of-ants-to-add [ setup-ant ]
  
end

to setup-global-settings
; This initialises the global variables and settings.

  set-default-shape turtles "bug"
  set Ant-Actions " "
  set init-actions false
  set actions ""
  set colour-of-trail 34 ; light brown
  set colour-of-grass 68 ; light green

end


to setup-santa-fe-trail
; This procedure sets up the Sante Fe Trail in the environment by
; reading it in from a file.

  ask patches
  [
    set pcolor colour-of-grass ; the colour of the background
  ]

  let data ""
  let char ""
  let x -1
  let y -1  

  ifelse (which-trail = "Circle") or (which-trail = "Ring")
    [ ; draw the circle or ring using the in-radius command
      let cx 0
      let cy 0
      if circle-at-centre?
        [ set cx 16 set cy 16 ]
      ask patch cx cy
      [ ask patches in-radius size-of-circle
        [ set pcolor colour-of-trail ]
      ]
      if (which-trail = "Ring")
        [
          ask patch cx cy
          [ ask patches in-radius (size-of-circle - ring-width)
            [ set pcolor colour-of-grass ]
          ]
        ]
    ]
    [ ; load from a file
      if (which-trail = "None")
        [ file-open "Follow-Trail-None.dat" ]    
      if (which-trail = "Cross")
        [ file-open "Follow-Trail-Cross.dat" ]    
      if (which-trail = "Santa Fe")
        [ file-open "Follow-Trail-Santa-Fe.dat" ]
      if (which-trail = "Spiral")
        [ file-open "Follow-Trail-Spiral.dat" ]    
      if (which-trail = "Square")
        [ file-open "Follow-Trail-Square.dat" ]    
     
      while [not file-at-end?]
      [
        set data file-read-line
        set y y + 1
        set x 0
        
        while [x < 32]
        [    
          set char item x data      
          if char = "1" [ask patch x (y * -1) [set pcolor colour-of-trail]] ; colour the trail in brown
          set x x + 1      
        ]    
      ]  
      file-close
    ]
end

to-report behaviour-filename
; Returns the filename of the program code to execute depending on the value of the slider which-behaviour.

  let behaviours
  [
    "Koza"              ; Koza's evolved behaviour for the Santa Fe Trail
    "Koza Modified"     ; Koza's evolved behaviour for the Santa Fe Trail but modified to remove unnecessary steps
    "Santa Fe Optimum"  ; The optimum behaviour for the Santa Fe Trail
    "Look ALR 1"        ; Look ahead for trail, then left, then right (no peripheral vision); turn in the direction of food
                        ; that was first found, otherwise move forward 
    "Look ALR 2"        ; Look ahead for trail; then left; then right (using peripheral vision). Then move in direction of food.
    "Look ALR 3"        ; Similar to "Look ALR 2" but simpler code
    "Look ALR 4"        ; Similar to "Look ALR 2" but even simpler code
    "Look ALR 5"        ; Same as for "Look ALR 2" but wiggle if trail not found
    "Look ALR 6"        ; Same as for "Look ALR 5" but wiggle-seldomly to get the ant out of a loop it may be stuck in as it
                        ; follows the path (e.g. such as small square loop at trail corners)
  ]
  let program-filenames
  [
    "Follow-Trail-Program-Koza.ctr"
    "Follow-Trail-Program-Koza-Modified.ctr"
    "Follow-Trail-Program-Santa-Fe-Optimum.ctr"
    "Follow-Trail-Program-Look-ALR-1.ctr"
    "Follow-Trail-Program-Look-ALR-2.ctr"
    "Follow-Trail-Program-Look-ALR-3.ctr"
    "Follow-Trail-Program-Look-ALR-4.ctr"
    "Follow-Trail-Program-Look-ALR-5.ctr"
    "Follow-Trail-Program-Look-ALR-6.ctr"
  ]
  
  ifelse (which-behaviour = "Any of the above")
    [ report one-of program-filenames ]
    [
      let p position which-behaviour behaviours
      report item p program-filenames
    ]
end 

to execute-behaviour
; This performs the actions as defined 

  let act ""
  set Ant-Actions ""
  file-open behaviour-filename
  while [not file-at-end?]
  [
    set act word act word file-read-line "\n"
  ]  
  file-close
  set Ant-Actions act
end

;;;;;;;;;;;;;;;;;;;;
;;; Go procedure ;;;
;;;;;;;;;;;;;;;;;;;;

to go  
  if not init-actions
  [
    let act Ant-Actions
    set act remove " " act
    if empty? act [stop]

    set actions (word "ask turtles [\n " Ant-Actions " \n]") 
  ]
  
  set init-actions true

  ifelse (Show-Paths?)
    [ ask turtles [ pen-down ]]
    [ ask turtles [ pen-up ]]
    
  run actions 

  tick
end

;;;;;;;;;;;;;;;;;;;;;;
;;; ANT OPERATIONS ;;;
;;;;;;;;;;;;;;;;;;;;;;

to move
; The ant performs the motor step of moving forward.

  fd 1
end

to turn-left
; The ant performs the motor step of turning left.

  lt 90
end

to turn-right
; The ant performs the motor step of turning right.

  rt 90
end

to-report trail-ahead
; The ant performs the sensing step of checking if the trail
; directly ahead.

  report ([pcolor] of patch-ahead 1) = colour-of-trail
end

to-report trail-on-my-left
; The ant performs the sensing step of checking if the trail
; on it's left side.

  report ([pcolor] of patch-left-and-ahead 90 1) = colour-of-trail
end

to-report trail-on-my-right
; The ant performs the sensing step of checking if the trail
; on it's right side.

  report ([pcolor] of patch-right-and-ahead 90 1) = colour-of-trail
end

to-report trail-ALR
; The ant performs the sensing step of checking if the trail is
; nearby. This reporter returns the action needed to head towards it.
; If there is no trail nearby, it defaults to an "ahead" action.
; (ALR stands for "Ahead, Left or Right").

  if ([pcolor] of patch-ahead 1 = colour-of-trail)
    [ report "ahead" ]
  if ([pcolor] of patch-left-and-ahead 90 1) = colour-of-trail
    [ report "turn-left" ]
  if ([pcolor] of patch-right-and-ahead 90 1) = colour-of-trail
    [ report "turn-right" ]
  report "ahead"
end

to-report trail-direction
; The ant performs the sensing step of checking if there is any food
; nearby. This reporter returns the direction of the food.
; If there is no trail nearby, it defaults to the current heading.

  if ([pcolor] of patch-ahead 1 = colour-of-trail)
    [ report 0 ]
  if ([pcolor] of patch-left-and-ahead 90 1) = colour-of-trail
    [ report (- 90) ]
  if ([pcolor] of patch-right-and-ahead 90 1) = colour-of-trail
    [ report 90 ]
  report 0
end

to wiggle
; The ant wiggles a bit i.e. This adds a bit of randomness to its behaviour.
; It can be used when it can't sense the trail ahead.

  rt random wiggle-angle
  lt random wiggle-angle
  move
end

to wiggle-maybe
; The ant performs a wiggle, but only with probability 1 / wiggle-maybe-ratio.

  if (random wiggle-maybe-ratio = 0)
    [ wiggle ]
end

;;;;;;;;;;;;;;;;;;;;;;;;
;;; OTHER OPERATIONS ;;;
;;;;;;;;;;;;;;;;;;;;;;;;

to draw-trail
;; Allows the user to draw the trail using the mouse.

  if mouse-down?
  [
    ask patch mouse-xcor mouse-ycor
    [ set pcolor colour-of-trail ]
    display
  ]
end

to erase-trail
;; Allows the user to erase the trail using the mouse.

  if mouse-down?
  [
    ask patch mouse-xcor mouse-ycor
    [ set pcolor colour-of-grass ]
    display
  ]
end

to erase-all-ant-paths
;; Allows the user to erase all the ants' paths.

  clear-drawing
end

to create-new-ants
;; Adds some new ants into the environment.

  create-ants no-of-ants-to-add [ setup-ant ]
end

;
; Copyright 2010 by William John Teahan and Loukas Georgiou.  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 William John Teahan.
; Contact William John Teahan for appropriate licenses for redistribution for
; profit.
;
; To refer to this model in publications, please use:
;
; Teahan, W. J. (2010).  Follow Trail NetLogo model.
; Exercises for Artificial Intelligence. Ventus Publishing Aps.
;