Flocking with Obstacles 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: Flocking-With-Obstacles.nlogo

WHAT IS IT?

(This model was created by Uri Wilensky and modified by Thomas Christy and William John Teahan. Most of the Information included here was provided by Uri Wilensky, with some modifications).

This model is an attempt to mimic the flocking of birds. (The resulting motion also resembles schools of fish.) The flocks that appear in this model are not created or led in any way by special leader birds. Rather, each bird is following exactly the same set of rules, from which flocks emerge.


HOW IT WORKS

The birds follow three rules: "alignment", "separation", and "cohesion".

"Alignment" means that a bird tends to turn so that it is moving in the same direction that nearby birds are moving.

"Separation" means that a bird will turn to avoid another bird which gets too close.

"Cohesion" means that a bird will move towards other nearby birds (unless another bird is too close).

When two birds are too close, the "separation" rule overrides the other two, which are deactivated until the minimum separation is achieved.

The three rules affect only the bird's heading. Each bird always moves forward at the same constant speed.


HOW TO USE IT

First, determine the number of birds you want in the simulation and set the population slider to that value. Press Setup to create the birds, and press GO to have them start flying around.

The default settings for the sliders will produce reasonably good flocking behavior. However, you can play with them to get variations:

Three ...-turn sliders control the maximum angle a bird can turn as a result of each rule.

vision is the distance that each bird can see 360 degrees around it.


THE INTERFACE

The buttons in the Interface are defined as follows:

- Setup: Clears all variables and creates the population of birds at random locations in the environment.

- Step: Runs a single tick of the simulation.

- Go: Runs the simulation continuously.

- Draw: This allows the user to draw collision patches to provide some obstacles for the birds to avoid hitting.

- Erase: This allows the user to erase the drawn obstacles.

- Load Image: This loads an image from the file named by the Filename slider into the background. Although this makes the simulation look more impressive, the bird agents cannot "see" this image. If you want to have the birds react to what appears in the image, you also need to load the patches for the same image using the Load Patches button. Note: Since the Load Image button will overwrite patches, make sure you synchronise both the Filename and patch-name (see below) so that they match, otherwise the birds will react to patches that can't be seen by us as the observers but are there in fact in the environment.

- Load Patches: This will load the patches for a given image specified by the patch-name slider so that the bird agents are able to "see" and react to them.

The sliders in the Interface are defined as follows:

- population: This is the number of bird agents created at the beginning.

- obstacle-colour: This is the colour used for drawing the obstacles using the Draw button.

- speed: This controls the speed of the bird agents.

- vision: This defines the radius of the bird agent's cone of vision. (The angle is 360 degrees, so the bird can "see" in all directions).

- minimum-separation: This defines the minimum separation for the flockmates before the flockmates will start to separate.

- max-align-turn, max-cohere-turn, max-separate-turn: These sliders control the maximum angle a bird can turn as a result of each rule - alignment, cohesion and separation, respectively.

The choosers in the Interface are defined as follows:

- Filename: This is the name of the image file which is loaded into the background when the Load Image button is pressed.

- patch-name: This is the name of the image file which is converted into patches in the environment when the Load Patches button is pressed.


THINGS TO NOTICE

Central to the model is the observation that flocks form without a leader.

There are no random numbers used in this model, except to position the birds initially. The fluid, lifelike behavior of the birds is produced entirely by deterministic rules.

Also, notice that each flock is dynamic. A flock, once together, is not guaranteed to keep all of its members. Why do you think this is?

After running the model for a while, all of the birds have approximately the same heading. Why?

Sometimes a bird breaks away from its flock. How does this happen? You may need to slow down the model or run it step by step in order to observe this phenomenon.

Around obstacles, sometimes the flock splits in two to head around different sides of an obstacle, but then returns to a single flock. The same behaviour can be seen in Craig Reynold's simulations. What are the conditions that flock splitting seems to occur in? Does it sometimes occur that the flock remains split in two?


THINGS TO TRY

Play with the sliders to see if you can get tighter flocks, looser flocks, fewer flocks, more flocks, more or less splitting and joining of flocks, more or less rearranging of birds within flocks, etc.

You can turn off a rule entirely by setting that rule's angle slider to zero. Is one rule by itself enough to produce at least some flocking? What about two rules? What's missing from the resulting behavior when you leave out each rule?

Will running the model for a long time produce a static flock? Or will the birds never settle down to an unchanging formation? Remember, there are no random numbers used in this model.

What happens if the in-cone command is used instead of the in-radius command? Does substantially reducing the vision cone angle affect the behaviour of the birds?


EXTENDING THE MODEL

Currently the birds can "see" all around them. What happens if birds can only see in front of them? The IN-CONE primitive can be used for this.

Is there some way to get V-shaped flocks, like migrating geese?

What happens if you put walls around the edges of the world that the birds can't fly into?

Can you get the birds to fly around obstacles in the middle of the world?

What would happen if you gave the birds different velocities? For example, you could make birds that are not near other birds fly faster to catch up to the flock. Or, you could simulate the diminished air resistance that birds experience when flying together by making them fly faster when in a group.

Are there other interesting ways you can make the birds different from each other? There could be random variation in the population, or you could have distinct "species" of bird.

Try adding your own images to the model. How is the behaviour of the turtle agents affected by the different environments?

Many of the birds get stuck inside or near to an obstacle. This detracts a little from the believability of the simulation. Is there some way this "bug" can be removed from the model?


NETLOGO FEATURES

The model uses the command IN-RADIUS to implement the boids' cone of vision.

Notice the need for the SUBTRACT-HEADINGS primitive and special procedure for averaging groups of headings. Just subtracting the numbers, or averaging the numbers, doesn't give you the results you'd expect, because of the discontinuity where headings wrap back to 0 once they reach 360.


CREDITS AND REFERENCES

This model is inspired by the Boids simulation invented by Craig Reynolds. The algorithm we use here is roughly similar to the original Boids algorithm, but it is not the same. The exact details of the algorithm tend not to matter very much -- as long as you have alignment, separation, and cohesion, you will usually get flocking behavior resembling that produced by Reynolds' original model. Information on Boids is available at http://www.red3d.com/cwr/boids/.


HOW TO CITE

If you mention this model in an academic publication, we ask that you include these citations for the model itself and for the NetLogo software:
- Wilensky, U. (1998). NetLogo Flocking model. http://ccl.northwestern.edu/netlogo/models/Flocking. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.
- Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.

In other publications, please use:
- Copyright 1998 Uri Wilensky. All rights reserved. See http://ccl.northwestern.edu/netlogo/models/Flocking for terms of use.

To refer to this modified model in publications, please use:

Flocking With Ostacles NetLogo model.
Teahan, W. J. (2010). Artificial Intelligence. Ventus Publishing Aps.
Based on: Wilensky, U. (1998). NetLogo Flocking model. http://ccl.northwestern.edu/netlogo/models/Flocking. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.


COPYRIGHT NOTICE

Copyright 1998 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 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, 2002.


PROCEDURES

;------------------------------------------------------------------------------------
; Flocking With Obstacles model based on the Flocking model written by Uri Wilensky.
; Adapted by Thomas Christy & William John Teahan
; -----------------------------------------------------------------------------------

turtles-own [
  flockmates         ;; agentset of nearby turtles
  nearest-neighbor   ;; closest one of our flockmates
]

to setup
  clear-all
  crt population
    [ set color blue - 2 + random 7   
      set size 1.5  
      setxy random-xcor random-ycor ]
end

to go
  ask turtles [ flock ]
  repeat 5 [ ask turtles [ fd speed / 200 ] display ]
  tick
end

to clear-obstacles
  ask patches                          
  [ set pcolor black ] 
end

to make-obstacle
  if mouse-down?
  [ ask patches
    [ if ((abs (pxcor - mouse-xcor)) < 1) and ((abs (pycor - mouse-ycor)) < 1)
      [ set pcolor obstacle-colour ]]]
  display
end

to eraser
  if mouse-down?
  [ ask patches
    [ if ((abs (pxcor - mouse-xcor)) < 1) and ((abs (pycor - mouse-ycor)) < 1)
      [ set pcolor black ]]]
  display
end

to flock   
  find-flockmates
  if any? flockmates
    [ find-nearest-neighbor
      ifelse distance nearest-neighbor < minimum-separation
        [ separate ]
        [ align
          cohere ] ]
  avoid-obstacles
end

to avoid-obstacles
; avoid anything nearby that is not black
    if (any? patches in-cone 2 300 with [pcolor != black])
    [ rt 180 ] ; head in opposite direction
end

to find-flockmates   
  set flockmates other turtles in-radius vision
end

to find-nearest-neighbor  
  set nearest-neighbor min-one-of flockmates [distance myself]
end
 
to separate   
  turn-away ([heading] of nearest-neighbor) max-separate-turn
end

to align 
  turn-towards average-flockmate-heading max-align-turn
end

to-report average-flockmate-heading 
  report atan sum [sin heading] of flockmates
              sum [cos heading] of flockmates
end

to cohere  
  turn-towards average-heading-towards-flockmates max-cohere-turn
end

to-report average-heading-towards-flockmates 
  report atan mean [sin (towards myself + 180)] of flockmates
              mean [cos (towards myself + 180)] of flockmates
end

to turn-towards [new-heading max-turn]
  turn-at-most (subtract-headings new-heading heading) max-turn
end

to turn-away [new-heading max-turn] 
  turn-at-most (subtract-headings heading new-heading) max-turn
end

to turn-at-most [turn max-turn]  
  ifelse abs turn > max-turn
    [ ifelse turn > 0
        [ rt max-turn ]
        [ lt max-turn ] ]
    [ rt turn ]
end
;
; To refer to this model in publications, please use:
;
; Teahan, W. J. (2009).  Flocking With Obstacles NetLogo model.
;   Artificial Intelligence. Ventus Publishing Aps.
;
;
;
; THIS NOTICE IS LEFT IN AS A CITATION AS SOME OF THE 
; ORIGINAL FLOCKING CODE HAS BEEN RE-USED FOR THIS ADAPTATION
;------------------------------------------------------------
;
; *** NetLogo 4.0.4 Model Copyright Notice ***
;
; This model was created as part of the project: COnearest-neighborECTED 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.
;
; Copyright 1998 by 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 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, 2002.
;
; To refer to this model in academic publications, please use:
; Wilensky, U. (1998).  NetLogo Flocking model.
; http://ccl.northwestern.edu/netlogo/models/Flocking.
; Center for Conearest-neighborected Learning and Computer-Based Modeling,
; Northwestern University, Evanston, IL.
;
; In other publications, please use:
; Copyright 1998 Uri Wilensky.  All rights reserved.
; See http://ccl.northwestern.edu/netlogo/models/Flocking
; for terms of use.
;
; *** End of NetLogo 4.0.4 Model Copyright Notice ***