Practical Problem 12: Collective Behavior
In this problem, we will implemement a simple model for collective motion of bird flocks or animal swarms.
This is going to be a turtles-only simulation.
The model works like this: At each iteration turtles move forward at a predefined speed (slider) and they should randomly change their heading within a predefined wiggle angle much like in some of the other models.
At each step a turtle interacts with other turtles in an interaction radius $R$: The turtle tries to align its heading to the average heading of the other turtles in the radius.
We need $100-200$ turtles with a shape of your choice. The setup function should place them randomly in the world. The parameters b, e and the interaction radius should be sliders.
First code the system without interactions, so the turtles move around randomly. Now code the interaction. The difficulty here is computing the average of the headings of the neighbors. It is tempting to write:
ask turtles [ set heading mean [heading] of turtles in-radius R ]
Why does this not work?
Instead write a reporter (function) that takes the headings of all turtles in the neighbors and computes the mean like this:
to-report mean-heading [ headings ] let mean-x mean map sin headings let mean-y mean map cos headings report atan mean-x mean-y end
Try to understand what's going on here.
Now use the function
hsb to color turtles according to their heading.
Now we add a few predators. These are also turtles and we need a way to distinguish the swarming prey fish from the predators. We do this using netlogo breeds. Go to the netlogo manual and read about breeds. In a nutshell breeds are types of turtles that you can define in the beginning and the refer to them later.
The predators should first just move around in the world and the prey should avoid them. If a predator is within range of a prey the head should change its direction away from it. In order to make this more realistic the prey should only be able to see a predator in a visual cone ahead of it. You can implement this using the