Crowd Path Following NetLogo Model

Produced for the book series "Artificial Intelligence";

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

powered by NetLogo

view/download model file: Crowd-Path-Following.nlogo

WHAT IS IT?

This model is an attempt to recreate boids (see Craig Reynold's work) that use the crowd path following steering behaviour.


HOW IT WORKS

Each turtle (boid) has two levels of vision - a forward facing cone and a boundary radius. The Cone looks out for coloured patches (i.e. NOT black) in front and if a patch is detected it runs a simple routine to navigate the path. The radius detection simply looks out for other turtles at a given distance threshold. When a turtle bumps into another turtle it does not move forward but instead turns right; this causes a slight pause in its forward motion which prevents it from going through the colliding turtle. As an addition, to highlight the collision effect, each turtle is given a variable speed based on its who number. This makes the first turtle the slowest and the last the fastest, making the turtles more likely to bump into one another. The faster turtles try to get past the slower ones.


HOW TO USE IT

Decide on the size of the population then press setup, then press the go button. The slider bars allow various parameters to be altered. To draw the boid trails, press the trail button; to clear them, press the clear button. To follow one particular boid, press the follow button.


THE INTERFACE

The model's Interface buttons are defined as follows:

- Setup: This sets up the environment with a black path across the middle, and the rest shown as a green texture to represent the surrounding grass. A number of boids (determined by the population variable) are created and placed at a random location within the black path.

- Go: The boids start moving along the path and after a while, most of them end up heading in the same direction.

- Follow: The simulation follows the path of one particular boid.

- Trail: This draws the paths made by all boids.

- Clear: This clears the trails.

The model's Interface sliders, switches and chooser are defined as follows:

1. For initialising the simulation:
- population: This specifies the number of boids.
- turtle-size: This specifies the size of the boids.
- labelID: If this is set to On, then the boids are labelled with their who numbers.

2. For controlling the movement of the boids:
- rate-of-random-turn: This controls how much the wandering boid turns each time tick. The boid has a tendency to head in a right turning direction as the rate of random turn to the right (as specified by the slider) is twice that of the rate of random turn to the left.
- boid-speed: This controls the speed of the boid i.e. how much it moves forward each tick.

3. For specifying parameters that control boid collision avoidance behaviour:
- collision: This turns on collision detection and avoidance if set to On.
- turtle-radius-angle: This defines the radius angle of the boid's vision cone in relation to other boids.
- turtle-radius-length: This defines the radius length of the boid's vision cone in relation to other boids.

4. For specifying parameters that control patch collision avoidance behaviour (where the patches are the boundaries of the path):
- radius-angle: This defines the radius angle of the boid's vision cone in relation to the edges of the path.
- radius-length: This defines the radius length of the boid's vision cone in relation to the edges of the path.
- behaviour: This defines the boid's behaviour. It can be set to the following values:
"Basic crowd following": This is very basic crowd path following behaviour without collision avoidance.
"With collision avoidance": This combines crowd path following behaviour with collision avoidance if the collision slider is set to On.


THINGS TO NOTICE

Notice how the turtles come close to the sides but manage to avoid them. How is this achieved, and what is the mechanism for keeping them in track?

Notice how the speed variances are produced. Each turtle has a unique speed setting.

Notice that the colours are randomised based on the chromatic point at which it was created. This allows a random looking range of colours to be produced while avoiding very dark and black colours, which would clash with the path.

Notice the Follow turtle button, and how a single turtle can be analysed more closely; the 3D button also is of interest here.

Notice that the turtles only appear on the black patches. How is this done?

Notice that when the boid-speed variable is set to a large number (5.0, say), some of the boids start moving off the path and end up going across the surrounding green grass. Why is this? Is there some way we can code the turtle agent's movement to prevent this from happening?


THINGS TO TRY

Try altering the random rate of turn. How does this affect the turtles' ability to follow the path? Why does it change?

Change the radius-angle and radius-length. What does this do to the model? Why does this change?

Change the turtle collision parameters. How does this change the model? What are the optimum settings for accurate collision detection?

Try altering the turtle speed. Why does this change the ability to stay in the path?

Click on the trail button (here is the real reason for variable colours).


EXTENDING THE MODEL

Adding a turtles-own variable for speed, we can make the turtles accelerate and decelerate. This will enable a colliding turtle to simply slow down and alter direction to avoid a collision.


NETLOGO FEATURES

The who, myself, in-cone, in-radius, heading, sprout and turtle editor features are used in this model.


RELATED MODELS

See the following models: Crowd Path Following, Flocking With Obstacles, Follow and Avoid, Obstacle Avoidance 1, Obstacle Avoidance 2, Vision Cone Example 2, Wall Following Example 2. These are basic implementations of various Craig Reynold's steering behaviours for boids.

Another boid related model is the Biology/Flocking model in the Models Library.


CREDITS AND REFERENCES

This model was created by Thomas Christy at Bangor University 2009 and modified by William John Teahan.

To refer to this model in publications, please use:

Crowd Path Following NetLogo model.
Teahan, W. J. (2010). Artificial Intelligence. Ventus Publishing Aps


PROCEDURES

; Crowd Path Following Demo
;
; Demo to simulate crowd path following in boids.
;
; Written by Thomas Christy (2009).
; Modified by William Teahan (2009).
;
; Copyright 2009 Thomas Christy and William John Teahan. All Rights Reserved.

breed [wanderers wanderer]   
wanderers-own [rotation-direction-trigger]                    ; turtle specific variable           

to setup  
    clear-all    
    import-pcolors "Image-Path.jpg"                            ; loads image as patches                             
    set-default-shape wanderers "directional-circle"           ; Custom shape      
    ask n-of population (patches with [pcolor = black])
    [
        sprout-wanderers 1
        [ 
           set color ((who * 10) + 5)                          ; sets a random colour (avoiding using black)
           set size turtle-size  
           if (labelID) [set label who]                        ; sets the ID on or off during initialisation
        ]
    ]
    import-drawing "Image-Path.jpg"                            ; load image averlay 
end   

to go
  if (behaviour = "Basic crowd following")
    [ go-basic-crowd-following-behaviour ]
  if (behaviour = "With collision avoidance" )
    [ go-collision-avoidance-behaviour ]
end

to go-basic-crowd-following-behaviour
                                         
  ask wanderers                              
  [
    rt random-float rate-of-random-turn 
    lt (rate-of-random-turn  / 2)                      
    fd boid-speed 
    ask patches in-cone radius-length radius-angle ; checks patches in its vision
    [
      if pcolor != black [                ; if they are coloured
        ask myself                        ; return control to turtle
        [
          bk .5 / turtle-size             ; bounces back if hit wall
          if heading < 30 [ set rotation-direction-trigger 1]  ; sets a boolean direction control variable
          if heading > 150 [ set rotation-direction-trigger 0] ; to determine path navigation
          ifelse rotation-direction-trigger = 1
            [ set heading (heading + 10) ]
            [ set heading (heading - 10) ]
        ]
       ]
     ]   
  ]
end    
                                                 
to go-collision-avoidance-behaviour                                           
  ask wanderers                              
  [
    rt random-float rate-of-random-turn 
    lt (rate-of-random-turn  / 2)                      
    
    ; these nested IFELSE statements add collision avoidance and variable movement speed (the latter to highlight the reaction to collisions)
    ; if a turtle detects another within its cone vision it turns right otherwise it moves forward by its chronological ID (times a multiplyer and capped to the boid-speed variable
    
    ifelse (collision) ;if collisions are ON do the following
    [
      ifelse (any? other wanderers in-cone turtle-radius-length turtle-radius-angle) ; if a turtle detects another do the following
      [ rt 5 ] ; simply turn right (no forward movement)            
      [
        ifelse who > 0  ; otherwise, if no turtles detected and your ID is not zero, do the following
        [    ; for uniform speeds change this line to fd (value)
          fd (boid-speed / count wanderers) * who ; move forward according to your chronological ID number 
        ]
        [
          fd 0.2 ; However if you're ID zero then move forward slowly
        ] 
      ] 
    ]
    [ ; for uniform speeds change this line to fd (value)
      fd (boid-speed / count wanderers) * who + 0.2 ; move forward according to your chronological ID number
    ]                                                               
    
    ask patches in-cone radius-length radius-angle ; checks patches in its vision
    [
      if pcolor != black [ ; if they are coloured (ie NOT black)
        ask myself ; return control to turtle
        [
          bk .5 / turtle-size ; bounces back if hit wall
          if heading < 30  [ set rotation-direction-trigger 1 ] ; sets a boolean direction control variable
          if heading > 150 [ set rotation-direction-trigger 0 ] ; to determine path navigation
          ifelse rotation-direction-trigger = 1
            [ set heading (heading + 10) ]
            [ set heading (heading - 10) ]
        ]
       ]
     ]   
  ]
end    
;
; Copyright 2009 by Thomas Christy and William John Teahan.  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:
;
; Crowd Path Following NetLogo model.
; Teahan, W. J. (2010). Artificial Intelligence. Ventus Publishing Aps
;