MIHS-18 Period 4 Brandon H, Kit S

MIHS-18 Period 4 Brandon H, Kit S preview image

1 collaborator

Photo_on_12-19-18_at_9.05_pm kit stoll (Author)


(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 37 times • Downloaded 0 times • Run 0 times
Download the 'MIHS-18 Period 4 Brandon H, Kit S' modelDownload this modelEmbed this model

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


We changed the behavior of the patches so that they might reflect coral within a reef. We changed the shape and color of the predator so that it might reflect a reef shark within a reef ecosystem. We also changed the shape and color of the birds so that they reflect parrotfish within a reef ecosystem. When the orange parrotfish pass over pink coral, they effectively "eat" the coral and gain energy. This results in the coral changing to a blue color which represents water or dead coral. We also added an energy cost to movement. As the fish swim, they use up energy until they are able to regain that energy by eating coral or die. If fish lose all of their energy, they die. If fish have enough energy, they reproduce. Both of these properties were changes which we made. Finally, we made the coral regenerate so that the fish have a renewing supply of energy. These additions result in some interesting emergent properties. Fish which swim towards the back of the school usually are not able to eat the coral because fish towards the front of the school have already gotten to it first. These fish are usually the ones to die. Fish towards the front of the school, however, eat most of the coral. This causes them to gain enough energy to reproduce. When the simulation is started, the fish population changes by increasing or declining until the fish reach a sustainable population for their environment. We added a graph of fish population over time on the interface to monitor this behavior.


This model builds upon Laurel Schrementi's extension of Uri's classic Flocking model. It shows the turtles flocking (or schooling) with enemies that float throught the world and make the turtles flee with added variables of energy which cause the turtles to interact with their environment. Just like with Uri's original, the schools that appear in this model are not created or led in any way by special leader birds. Rather, each fish is following exactly the same set of rules, from which schools emerge.


The sliders determine the population of turtles and number of enemies. The enemies tell the other turtles when and how to flee.

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.


First, determine the number of birds you want in the simulation and set the POPULATION slider to that value. Then determine the number of enemies you want and set the ENEMY slider. 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-ANGLE 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.


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.


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.


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.


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.


  • Moths
  • Flocking Vee Formation


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/.


If you mention this model in a publication, we ask that you include these citations for the model itself and for the NetLogo software:


Copyright 1998 Uri Wilensky.


This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.

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.

Comments and Questions

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

Click to Run Model

turtles-own [
  flockmates         ;; agentset of nearby turtles
  nearest-neighbor   ;; closest one of our flockmates
  enemy?      ;; true or false: is this turtle an enemy?

to setup
  crt population
    [ set size 3  ;; easier to see
      setxy random-xcor random-ycor
      set enemy? false ] ;; because these are the good guys!
  ask turtles [set shape "fish"]
    crt enemy
     [ set size 5 ;; we want a big bad guy!
       setxy random-xcor random-ycor
       set enemy? true ]   ;; all the other turtles will now act like this turtle is a bad guy.
    ask turtles [ recolor ]
  ask turtles [set shape "fish"]

to flee  ;; turtle procedure
  ask turtles with [color = black][
  let close-turtles other turtles with [distance myself < vision]  ;; Nathan showed us how to use let to form a temporary agent set we're calling close-turtles
  ask close-turtles [set color yellow  face myself rt 180  ] ] ;; this is asking all the turtles close to the enemy to flash yellow and head the opposite direction from the enemy

; color enemy shark, and keep the fish orange

to recolor  ;; turtle procedure
  ifelse enemy?
    [ set color black ]
    [ set color orange ]

to go
  ask turtles with [ color = orange ] [ flock ]  ;; this way only the regular fish school, the enemy will not school and attempt to chase the other fish, he will just cruise through the world following the fd 0.2 procedure
  ask turtles [ flee ]
  ;; the following line is used to make the turtles
  ;; animate more smoothly.
  repeat 5 [ ask turtles [ fd 0.2 ] display ]
  ask turtles [ recolor ] ;; this makes the fish that are near the enemy flash yellow but return to orange when they have fled

to setup-patches
  ask patches [set pcolor 19]

to eat-coral
  ask turtles with [color = orange] [if pcolor = 19 [set pcolor 105 set energy energy + 5]]

to check-fish-death
  ask turtles with [color = orange] [if energy <= 0 [die]]

to regrow-coral
  ask patches [if random 100 < 3 [set pcolor 19]]

to reproduce
  ask turtles with [color = orange] [
    if energy > 50 [
    set energy energy - 30
      hatch 1 [ set energy 10 rt random-float 360 fd 1 ]

to flock  ;; turtle procedure
  if any? flockmates
    [ find-nearest-neighbor
      ifelse distance nearest-neighbor < minimum-separation
        [ separate ]
        [ align
          cohere ] ]

to find-flockmates  ;; turtle procedure
  set flockmates other turtles in-radius vision

to find-nearest-neighbor ;; turtle procedure
  set nearest-neighbor min-one-of flockmates [distance myself]


to separate  ;; turtle procedure
  turn-away ([heading] of nearest-neighbor) max-separate-turn


to align  ;; turtle procedure
  turn-towards average-flockmate-heading max-align-turn
  set energy energy - 2

to-report average-flockmate-heading  ;; turtle procedure
  ;; We can't just average the heading variables here.
  ;; For example, the average of 1 and 359 should be 0,
  ;; not 180.  So we have to use trigonometry.
  let x-component sum [dx] of flockmates
  let y-component sum [dy] of flockmates
  ifelse x-component = 0 and y-component = 0
    [ report heading ]
    [ report atan x-component y-component ]


to cohere  ;; turtle procedure
  turn-towards average-heading-towards-flockmates max-cohere-turn

to-report average-heading-towards-flockmates  ;; turtle procedure
  ;; "towards myself" gives us the heading from the other turtle
  ;; to me, but we want the heading from me to the other turtle,
  ;; so we add 180
  let x-component mean [sin (towards myself + 180)] of flockmates
  let y-component mean [cos (towards myself + 180)] of flockmates
  ifelse x-component = 0 and y-component = 0
    [ report heading ]
    [ report atan x-component y-component ]


to turn-towards [new-heading max-turn]  ;; turtle procedure
  turn-at-most (subtract-headings new-heading heading) max-turn

to turn-away [new-heading max-turn]  ;; turtle procedure
  turn-at-most (subtract-headings heading new-heading) max-turn

;; 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]  ;; turtle procedure
  ifelse abs turn > max-turn
    [ ifelse turn > 0
        [ rt max-turn ]
        [ lt max-turn ] ]
    [ rt turn ]

; Copyright 1998 Uri Wilensky.
; See Info tab for full copyright and license.

There is only one version of this model, created 3 months ago by kit stoll.

Attached files

File Type Description Last updated
MIHS-18 Period 4 Brandon H, Kit S.png preview Preview for 'MIHS-18 Period 4 Brandon H, Kit S' 3 months ago, by kit stoll Download

This model does not have any ancestors.

This model does not have any descendants.