This page was automatically generated by NetLogo 5.0.

The applet requires Java 5 or higher. Java must be enabled in your browser settings. Mac users must have Mac OS X 10.4 or higher. Windows and Linux users may obtain the latest Java from Sun's Java site.


powered by NetLogo

view/download model file: flock.nlogo

WHAT IS IT?

This model is an attempt to mimic the flocking of birds (also called “boids”). 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.

[Note by William M. Spears - these comments are from the original NetLogo version, with a few minor edits.]

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 NUMBER_OF_BOIDS slider to that value.

Click SETUP AGENTS to create the birds, and click MOVE AGENTS 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 MAX_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.

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?

Using the default settings, 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. Why does this happen? You may need to slow down the model or run it step by step in order to observe this phenomenon.

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.

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.

[Again, in order to change any NetLogo simulation, you must have the source code (i.e., “flock.nlogo”) downloaded to your computer, as well as NetLogo itself. You can not change the code when you are running the simulation with your browser.]

NETLOGO FEATURES

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.

[Note by William M. Spears - this is explained in more detail in Chapter 3 of the book.]

RELATED MODELS

Moths
Flocking Vee Formation

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.

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.

CODE

; Flocking Tutorial Code
; Based on original model by Uri Wilenksy at
; http://ccl.northwestern.edu/netlogo/models/Flocking
; with modifications by William M. Spears September 2011
; For research and educational use only

globals [h0 h1 h2 h3 h4 h5 hh]                   ; For the histogram

turtles-own [hood them r min_r deltax deltay newh bearing nearest-neighbor]

; Called by "Setup Agents"
to setup
   clear-all                                     ; Clear everything
                                                 ; Create the boids and initialize them
   crt Number_of_Boids [set size 1.5 setxy random-xcor random-ycor]
   ask turtles [set them [who] of other turtles] ; Find out the IDs of all other boids
   
   setup-plot
   set h0 0 set h1 0 set h2 0 set h3 0 set h4 0 set h5 0
   ask turtles [monitor]
   do-plot
   reset-ticks
end

; Called forever by "Move Agents"
to go
   if (count turtles < 1) [user-message "Please click HALT and then SETUP AGENTS first" stop]
   set h0 0 set h1 0 set h2 0 set h3 0 set h4 0 set h5 0
   ask turtles [flock]
   do-plot
   tick
end

; Original NetLogo code, modified by William M. Spears
; For further explanation see Chapter 3 of the book
to flock
   find-neighbors
   if ((length hood) > 0) [
      ifelse (min_r < Minimum_Separation)
         [separation]
         [alignment cohesion]
   ]
   monitor
   fd 1
end

; Originally "find-flockmates", rewritten by William M. Spears
; Loop through all other boids, and find those
; within vision range and the boid closest to you.
to find-neighbors
   set min_r 100000 set hood []
   foreach them [
      set deltax (([xcor] of turtle ?) - xcor)
      if ((abs deltax) <= Vision) [              ; Yields speed improvement
         set deltay (([ycor] of turtle ?) - ycor)
         set r sqrt (deltax * deltax + deltay * deltay)
         if (r <= Vision) [
            set hood (fput ? hood)               ; Update list of boids within vision range
            if (r < min_r) [
               set min_r r
               set nearest-neighbor ?            ; This is the boid closest to you
            ]
         ]
      ]
   ]
end

; Separation - original NetLogo code
to separation
   turn-away ([heading] of turtle nearest-neighbor) Max_Separation_Turn
end

; Alignment, rewritten by William M. Spears
; For further explanation see Chapter 3 of the book
to alignment
   let x 0 let y 0        
   foreach hood [
       set y (y + (sin ([heading] of turtle ?)))
       set x (x + (cos ([heading] of turtle ?)))
   ]
   ifelse ((x = 0) and (y = 0)) 
      [set newh heading]
      [set newh (atan y x)]
   turn-towards newh Max_Alignment_Turn
end

; Cohesion, rewritten by William M. Spears
; For further explanation see Chapter 3 of the book
to cohesion
   let x 0 let y 0 let b 0
   foreach hood [
       set deltax (([xcor] of turtle ?) - xcor)
       set deltay (([ycor] of turtle ?) - ycor)
       set b (atan deltax deltay)
       set y y + (sin b)
       set x x + (cos b)
   ]  
   ifelse ((x = 0) and (y = 0)) 
      [set bearing heading]
      [set bearing (atan y x)]
   turn-towards bearing Max_Cohesion_Turn
end

; Helper Procedures from NetLogo
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

; Turn right by "turn" degrees (or left if "turn" is negative),
; but never turn more than "max-turn" degrees
to turn-at-most [turn max-turn]
   ifelse (abs turn > max-turn)
      [ifelse (turn > 0)
          [rt max-turn]
          [lt max-turn]
      ]
      [rt turn]
end

;;;;;;;;;;;;;;;;; For the histogram ;;;;;;;;;;;;;;;;;;;;;;
; Used to update the histogram bins and to make the boids the 
; same color as the corresponding bin, by William M. Spears
to monitor
   let h (floor (heading / 60))
   ifelse (h = 0) [set h0 h0 + 1 set color pink] [
      ifelse (h = 1) [set h1 h1 + 1 set color green] [
         ifelse (h = 2) [set h2 h2 + 1 set color blue] [  
            ifelse (h = 3) [set h3 h3 + 1 set color violet] [
               ifelse (h = 4) [set h4 h4 + 1 set color red] [
                  ifelse (h = 5) [set h5 h5 + 1 set color orange] []
   ]]]]]
end

; Setup the histogram, by William M. Spears
to setup-plot
   set-current-plot "Histogram of Heading"
end

; A histogram using pens of different colors, by William M. Spears
to do-plot
   clear-plot
   set-plot-x-range 0 360
   set-plot-y-range 0 count turtles

   set-current-plot-pen "pen0"
   plotxy 0 h0
   set-current-plot-pen "pen1"
   plotxy 60 h1
   set-current-plot-pen "pen2"
   plotxy 120 h2
   set-current-plot-pen "pen3"
   plotxy 180 h3
   set-current-plot-pen "pen4"
   plotxy 240 h4
   set-current-plot-pen "pen5"
   plotxy 300 h5
end

; Copyright 1998 Uri Wilensky. All rights reserved.
; The full copyright notice is in the Information tab.