# Add and kill one new agent every time step

**URL:** <https://discourse.julialang.org/t/add-and-kill-one-new-agent-every-time-step/52974>\
**Category:** New to Julia\
**Created:** [January 7, 2021, 4:55am UTC](https://discourse.julialang.org/t/add-and-kill-one-new-agent-every-time-step/52974 "2021-01-07T04:55:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SDA](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@SDA](https://discourse.julialang.org/u/SDA)\
**Post date:** [January 7, 2021, 4:55am UTC](https://discourse.julialang.org/t/add-and-kill-one-new-agent-every-time-step/52974/1 "2021-01-07T04:55:12Z")

</div>

I am playing with the Agents.jl Flocking example here ([Flocking model · Agents.jl](https://juliadynamics.github.io/Agents.jl/stable/examples/flock/)). I would like to add a new agent and kill the oldest agent every time step. I can kill an agent by including

```julia
age::Int 

```

as a property of Bird and the following inside the function agent\_step!

```julia
 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:

```julia
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!

---

<div class="post-metadata">

**Author:** ![mikkoku](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikkoku/32/16274_2.png) [@mikkoku](https://discourse.julialang.org/u/mikkoku)\
**Post date:** [January 7, 2021, 7:44am UTC](https://discourse.julialang.org/t/add-and-kill-one-new-agent-every-time-step/52974/2 "2021-01-07T07:44:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![SDA](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@SDA](https://discourse.julialang.org/u/SDA)\
**Post date:** [January 7, 2021, 8:46am UTC](https://discourse.julialang.org/t/add-and-kill-one-new-agent-every-time-step/52974/3 "2021-01-07T08:46:11Z")

</div>

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 🙂
