An archetypical model of flocking behaviour of animals in continuous space. It is meant to test the performance of the software in continuous space: handling 64-bit floats, moving agents, searching for nearby agents.
Note that there are two versions of this model, a small one and a large one. The difference in the large is that the space extent, visual distance, and number of agents, are larger.
The flock model illustrates how flocking behaviour can emerge when each bird follows three simple rules:
ID, integer, a unique identifier for each agent.pos, a tuple/vector of 2 64-bit floats, the position of the agent.vel, a tuple/vector of 2 64-bit floats, the velocity of the agent. The rules of the ABM enforce vel to always be a unit vector.speed, 64-bit number, the speed of the agent (magnitude of velocity). Defines how much an agent will move in one simulation step.separation, 64-bit real, defines the minimum distance a bird must maintain from its neighbors.visual_distance refers to the distance a bird can see and defines a radius of neighboring birds.cohere_factor, 64-bit number, is the importance of maintaining the average position of neighbors.match_factor, 64bit real, is the importance of matching the average trajectory of neighboring birds.separate_factor, 64bit float, is the importance of maintaining the minimum distance from neighboring birds.visual_distance).speed agent property.The simulation is performed for exactly 100 steps.
Defaults that are the same for the small and large version of simulation:
Defaults for the small version:
Defaults for the large version:
Here is the code for the update rule of the flocking behaviour in Agents.jl:
function flocking_agent_step!(bird, model)
neighbor_ids = nearby_ids(bird, model, bird.visual_distance)
N = 0
match = separate = cohere = (0.0, 0.0)
for id in neighbor_ids
N += 1
neighbor = model[id].pos
heading = neighbor .- bird.pos
cohere = cohere .+ heading
if euclidean_distance(bird.pos, neighbor, model) < bird.separation
separate = separate .- heading
end
match = match .+ model[id].vel
end
N = max(N, 1)
cohere = cohere ./ N .* bird.cohere_factor
separate = separate ./ N .* bird.separate_factor
match = match ./ N .* bird.match_factor
bird.vel = (bird.vel .+ cohere .+ separate .+ match) ./ 2
bird.vel = bird.vel ./ norm(bird.vel)
move_agent!(bird, model, bird.speed)
end