NetLogo banner

NetLogo Publications
Contact Us

Modeling Commons

Beginners Interactive NetLogo Dictionary (BIND)
NetLogo Dictionary

User Manuals:
Farsi / Persian


NetLogo User Community Models

(back to the NetLogo User Community Models)

[screen shot]

If clicking does not initiate a download, try right clicking or control clicking and choosing "Save" or "Download".(The run link is disabled for this model because it was made in a version prior to NetLogo 6.0, which NetLogo Web requires.)

21 OCT 2003

*** The standard flocking example (Flocking) supplied with Ver 1.3 of NetLogo is modified to include changes:

1. Turtles are now color-coded by the direction they are heading. This makes the flocking behavior much more obvious to the eye: all the agents in a flock have a similar color.

2. A subroutine GROUP-SET is added that finds from the agentset FLOCKMATES the lists of turtles in extended groups implied by the individual neighbor lists. For example, if your neighbor has a neighbor that is not in your list, then the three of you will be in the same extended group list.

To use the subroutine, just copy it into your program, define GLOBAL variables mate_size, group_lists and group_sizes, call GROUP-SET appropriately, provide an agentset with the neighbor lists (you can rename FLOCKMATES to whatever is your neighbor list). You may wish to comment out mate_size if you do not require it - it is included here for output.

The output of GROUP-SET is three lists:
- group-lists: a list of lists of the form:
[ (list of turtle numbers in a extended group) (list of turtle number in another extended group) ...]
The essential property is that all the sets of extended groups are disjoint in any combination (they have no common members). The order of the groups is not meaningful, and an extended group is only 2 or more turtles.
- group-sizes: an ordered list of the group sizes (you may wish to remove the sorting to retain correspondence between the two lists).
- mate_size: the number of neighbors for each turtle

Despite the calculation being at worst an N^2 problem (where N is the number of turtles), the code typically does not consider all N^2 combinations. But you will notice a speed penalty for large N.

3. Three diagnostics are included:
- Histogram of the directions of turtles
- Histogram of the numbers of flockmates
- Monitor of the group sizes

- look at the histogram of the directions to follow the coherence formation and the different extended groups that self-organize
- compare the histogram of the number of flockmates and the group size: this illustrates how the extended group is an emergent property of the system
- examine the dynamics of these diagnostics as you change the parameters (like the vision range) during a run. How quickly do the extended groups form, breakup, reform?

*** AN EXAMPLE: What determines what final direction of the flock after a some time?

If you repeat a simulation over and over, it is difficult to predict the final direction from visually observing the initial distribution of directions in the graphics. This illustrates the sensitivity of the final solution (the direction of an entire flock) on details of the initial distribution.

* Try this experiment:
- Toggle the plot to show the histogram of direction.
- Set vision to the maximum (10) and the minimum distance to minumum (0).
- Click SETUP, and then click GO. After the flocks align, click Go to stop.
- Repeat the last step a couple of times.
Note how the flock quickly aligns to one direction (observe in the histogram all individuals will be in one or two bins) because everyone sees almost everyone else (observe the extended group size). Can you predict from the initial histogram what will be the final direction?

* Now try this experiment:
- Repeat the first three steps from the previous example.
- While running, slide the mimimun distance to both extremes. Observe the histogram.
When the minimum distance is maximum, every one tries to avoid everyone else and the system will start to randomize. But note how the distribution in the histogram takes time to populate other angles. When you reduce the minimum distance to zero, the flock will quickly reorient to the original direction of the flocks. How long do you have to let the simulation run before it "forgets" the previous collective angle? Remember that the turtles have no individual memory except their direction. The answer might surprise you. Can you predict how long is long enough by looking at the histogram?

*** A suggestion: To better see the formation and interactions of flock mates, try reducing the speed of the agents by replacing "fd 1.0" with "fd 0.5"

Norman Johnson
Theoretical Division
Los Alamos National Laboratory

The following is the standard introduction:


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 apparent flocks emerge.

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

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

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

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

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 special procedures for subtracting two headings, and 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.

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

To refer to this model in academic publications, please use: Wilensky, U. (1998). NetLogo Flocking model. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.

(back to the NetLogo User Community Models)