# Tracking population vs agent level attributes in Agents.jl

**URL:** <https://discourse.julialang.org/t/tracking-population-vs-agent-level-attributes-in-agents-jl/117050>\
**Category:** Modelling & Simulations\
**Tags:** agents\
**Created:** [July 15, 2024, 9:24am UTC](https://discourse.julialang.org/t/tracking-population-vs-agent-level-attributes-in-agents-jl/117050 "2024-07-15T09:24:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amynang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amynang/32/210682_2.png) [@amynang](https://discourse.julialang.org/u/amynang)\
**Post date:** [July 15, 2024, 9:24am UTC](https://discourse.julialang.org/t/tracking-population-vs-agent-level-attributes-in-agents-jl/117050/1 "2024-07-15T09:24:32Z")

</div>

I am translating an ABM model from NetLogo to Julia (i.e. Agents.jl).

The model, “particle world” is described in Paul Smaldino’s Modeling Social Behavior, where it is used to introduce basic NetLogo functionality. Agents perform correlated random walks and when they encounter another agent, they change direction. These “collision” events are tracked over time. In NetLogo collisions can be introduced as global variables, which allows us to track the cumulative number of collision events over time, regardless of which agents are involved (we do not need to track collisions at an agent level).

In my version of this model with Agents.jl, I have managed to get the desired collisions over time by adding a .collisions property to agents, which I then update at every step `particle.collisions += new_collisions`. I can then get the sum of these agent-level level collision records at each time-step.  
Actually writing this I now realize I am double-counting… 😑

My question is if it is possible in Agents.jl to track global attributes, similar to NetLogo, in cases where agent-level information is unnecessary and we are only interested in general model level aggregates.

The NetLogo [version](https://github.com/amynang/MSB/blob/main/Chapter2/ch2_0_particleworld.nlogo)  
The Agents.jl [version](https://github.com/amynang/MSB/blob/main/Chapter2/ch2_0_particleworld.jl)

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [July 15, 2024, 1:00pm UTC](https://discourse.julialang.org/t/tracking-population-vs-agent-level-attributes-in-agents-jl/117050/2 "2024-07-15T13:00:38Z")

</div>

Welcome @amynang !

Global variables are generally speaking bad practice for performant code, but in Agents.jl a “global” variable is most naturally represented as a _model property_. In this way, during any collision you simply increment this model property. For example, [as in Schelling model in our docs](https://juliadynamics.github.io/Agents.jl/stable/tutorial/#Step-4:-the-AgentBasedModel), if you have the model properties

```julia
properties = Dict(:total_collisions => 0)

```

then inside your agent stepping function and/or the model stepping function you can do:

```julia
function agent_step!(agent, model)
    if collision
        model.total_colisions += 1
    end
end

```

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [July 15, 2024, 1:02pm UTC](https://discourse.julialang.org/t/tracking-population-vs-agent-level-attributes-in-agents-jl/117050/3 "2024-07-15T13:02:06Z")

</div>

p.s.: reading from [the docs instructions on getting help](https://juliadynamics.github.io/Agents.jl/stable/#Getting-help), in the future please open questions in the category “Modelling and simulations” and use `agents` not `agentsjl` as a tag. I saw this post out of pure luck!

---

<div class="post-metadata">

**Author:** ![amynang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amynang/32/210682_2.png) [@amynang](https://discourse.julialang.org/u/amynang)\
**Post date:** [July 15, 2024, 7:23pm UTC](https://discourse.julialang.org/t/tracking-population-vs-agent-level-attributes-in-agents-jl/117050/4 "2024-07-15T19:23:55Z")

</div>

Thank you @Datseris!

This seems to get me what I want.

```julia
mdata = [:total_collisions]
mdf = run!(model, 100; mdata)

```
