Add and kill one new agent every time step

I am playing with the Agents.jl Flocking example here (Flocking · Agents.jl). I would like to add a new agent and kill the oldest agent every time step. I can kill an agent by including

age::Int 

as a property of Bird and the following inside the function agent_step!

 max_age=100
 bird.age += 1
 bird.age >= max_age && kill_agent!(bird, model)

I had thought the following would add a single new agent to a random position at each timestep:

n_steps = 10
model = initialize_model()

for t=1:n_steps
    print(allids(model))
   vel = Tuple(rand(2) * 2 .- 1)
        add_agent!(
            Tuple(rand(2) * 2 .- 1),
            model,
            vel,
            speed,
            cohere_factor,
            separation,
            separate_factor,
            match_factor,
            visual_distance,
            age,
        )
    step!(model, agent_step!, 1)
end

But unfortunately it is adding one new agent and then doubling this number as can be seen with the output printed in the loop. i.e. If I start with 10 agents I will have 2*(10+1) in the next tilmestep etc.

I’m fairly new to Julia so this is possibly the wrong approach. Does anyone have any ideas?

Thank you for your time!

I tried to modify the flocking example following your example, but it worked as expected, only one bird was added on each timestep. Maybe you accidentally modified something else too?

2 Likes

Thank you, I really appreciate you looking into it! I’ve just recopied it all over and as you say it is working - no idea what I had changed for it to not work initially! Thanks again :slight_smile: