I am trying to edit the code with yours, but it fails:
using Agents
using Random, LinearAlgebra
@agent Bird ContinuousAgent{2} begin
speed::Float64
cohere_factor::Float64
separation::Float64
separate_factor::Float64
match_factor::Float64
visual_distance::Float64
end
function initialize_model(;
n_birds = 100,
speed = 1.0,
cohere_factor = 0.25,
separation = 4.0,
separate_factor = 0.25,
match_factor = 0.01,
visual_distance = 5.0,
extent = (100, 100),
seed = 42,
)
space2d = ContinuousSpace(extent; spacing = visual_distance/1.5)
rng = Random.MersenneTwister(seed)
model = ABM(ContinuousAgent{2},ContinuousSpace((5,5)))
for _ in 1:n_birds
vel = Tuple(rand(model.rng, 2) * 2 .- 1)
add_agent!(
model,
vel,
speed,
cohere_factor,
separation,
separate_factor,
match_factor,
visual_distance,
)
end
return model
end
model = initialize_model()
# Defining the agent_step
function agent_step!(bird, model)
# Obtain the ids of neighbors within the bird's visual distance
neighbor_ids = nearby_ids(bird, model, bird.visual_distance)
N = 0
match = separate = cohere = (0.0, 0.0)
# Calculate behaviour properties based on neighbors
for id in neighbor_ids
N += 1
neighbor = model[id].pos
heading = get_direction( bird.pos, neighbor)
# `cohere` computes the average position of neighboring birds
cohere = cohere .+ heading
if euclidean_distance(bird.pos, neighbor, model) < bird.separation
# `separate` repels the bird away from neighboring birds
separate = separate .- heading
end
# `match` computes the average trajectory of neighboring birds
match = match .+ model[id].vel
end
N = max(N, 1)
# Normalise results based on model input and neighbor count
cohere = cohere ./ N .* bird.cohere_factor
separate = separate ./ N .* bird.separate_factor
match = match ./ N .* bird.match_factor
# Compute velocity based on rules defined above
bird.vel = (bird.vel .+ cohere .+ separate .+ match) ./ 2
bird.vel = bird.vel ./ norm(bird.vel)
# Move bird according to new velocity and speed
move_agent!(bird, model, bird.speed)
end
# Plotting the flock
using InteractiveDynamics
using CairoMakie
const bird_polygon = Polygon(Point2f[(-0.5, -0.5), (1, 0), (-0.5, 0.5)])
function bird_marker(b::Bird)
φ = atan(b.vel[2], b.vel[1]) #+ π/2 + π
scale(rotate2D(bird_polygon, φ), 2)
end
# add_agent!((0.1,3.0),model,(1.0,1.0))
# add_agent!((4.9,3.0),model,(1.0,1.0))
# nearest_neighbor(model[1],model,1.0)
# euclidean_distance(model[1],model[2],model)
# model[2].pos .- model[1].pos
model = initialize_model()
figure, = abmplot(model; am = bird_marker)
figure
abmvideo(
"flocking.mp4", model, agent_step!;
am = bird_marker,
framerate = 20, frames = 100,
title = "Flocking"
)
LoadError: MethodError: no method matching ContinuousAgent{2}(::Int64, ::Tuple{Float64, Float64}, ::Tuple{Float64, Float64}, ::Float64, ::Float64, ::Float64, ::Float64, ::Float64, ::Float64)
Closest candidates are:
ContinuousAgent{D}(::Any, ::Any, ::Any) where D at ~/.julia/packages/Agents/960Tr/src/core/agents.jl:196
Stacktrace: