# Data Collection Issue With Julia Agents

**URL:** <https://discourse.julialang.org/t/data-collection-issue-with-julia-agents/66458>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 16, 2021, 2:19am UTC](https://discourse.julialang.org/t/data-collection-issue-with-julia-agents/66458 "2021-08-16T02:19:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bfried1](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@bfried1](https://discourse.julialang.org/u/bfried1)\
**Post date:** [August 16, 2021, 2:19am UTC](https://discourse.julialang.org/t/data-collection-issue-with-julia-agents/66458/1 "2021-08-16T02:19:14Z")

</div>

![Screen Shot 2021-08-16 at 4.12.03 AM](https://global.discourse-cdn.com/julialang/original/3X/f/0/f0e0babd2744c55bf84ea6cb166cd435e757a6bc.png)  
 ![Screen Shot 2021-08-16 at 4.12.31 AM](https://global.discourse-cdn.com/julialang/original/3X/3/6/365513baf8bc6c92a69dcdbbba5dcfd7b61e5cc6.png)

 ![Screen Shot 2021-08-16 at 4.17.02 AM](https://global.discourse-cdn.com/julialang/original/3X/3/4/34e51be88fa200a2b5d19a32aaab6fcd651afb78.png)

mdata = [:Social] returns the same graph over and over without updating, I can’t seem to find the reason, any ideas?

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [August 16, 2021, 8:37am UTC](https://discourse.julialang.org/t/data-collection-issue-with-julia-agents/66458/2 "2021-08-16T08:37:25Z")

</div>

From what you’ve posted it seems as if you only create your graph once and never change it. Do you ever update `model.Social` during your `model_step!` function?

Of course we don’t know what your use case/model purpose is, so this is a bit of a wild guess but are you sure you want to use a `GridSpace` and then create a graph from the present agents? Perhaps look at our [SIR example](https://juliadynamics.github.io/Agents.jl/stable/examples/sir/) to see how you can directly use a `GraphSpace` instead.

* * *

Please note that reading code on screenshots is a bit unpleasant. You might want to consider using backticks ```` to fence your code and allow for nice formatting like this:

```julia
using Agents
model = create_model(; dims=(L,L), density)
# and so on

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 16, 2021, 8:44am UTC](https://discourse.julialang.org/t/data-collection-issue-with-julia-agents/66458/3 "2021-08-16T08:44:05Z")

</div>

Welcome! To make it easer to help you, please consult this short post with some tips & tricks:

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![bfried1](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@bfried1](https://discourse.julialang.org/u/bfried1)\
**Post date:** [August 16, 2021, 11:07am UTC](https://discourse.julialang.org/t/data-collection-issue-with-julia-agents/66458/4 "2021-08-16T11:07:07Z")

</div>

I will fence from now on, thanks for the tip!

---

<div class="post-metadata">

**Author:** ![bfried1](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@bfried1](https://discourse.julialang.org/u/bfried1)\
**Post date:** [August 16, 2021, 11:24am UTC](https://discourse.julialang.org/t/data-collection-issue-with-julia-agents/66458/6 "2021-08-16T11:24:47Z")

</div>

I do update model.Social, but I do this during the agent\_step!  
This is another thing I am confused about, as far as I understand you can only have `run!(model,agent_step!,model_step!,etc...)`, but I need something like `(model,agent_step1!,model_step1!,agent_step2!,model_step2!,etc...)`, this is just the way my model needs to be updated. Regarding the use of` `GridSpace` over `GraphSpace`, I don’t need the agents walking over a network per-say, but rather a grid torus topology, then I construct social networks based on some other criterion. Anyways the code that updates model.Social is here:

```nohighlight
    for i = 1:numagents
        for j = 1:numagents
            if i==j
                continue
                else
                add_edge!(model.Social, i, j, (model[i].probability + model[j].probability) * (1-abs(model[j].probability-model[i].probability))/2)
            end
        end
    end        
end

```

Note that i’ve fixed the issue by including `obtainer = copy` at the end of my `run!()`, I saw someone having similar trouble and this seemed to be the trick, but is this the best solution? Sorry I am new to using agents / julia and I’m still figuring things out, thanks for the advice!

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [August 16, 2021, 11:46am UTC](https://discourse.julialang.org/t/data-collection-issue-with-julia-agents/66458/7 "2021-08-16T11:46:33Z")

</div>

> [@bfried1](#):
>
> but I need something like (model,agent\_step1!,model\_step1!,agent\_step2!,model\_step2!,etc…).

This can be easily done if you implement your `agent_step!` logic inside the `model_step!` function. Just iterate over the agents (or a subset) in whichever way you like: either by using a predefined scheduler from the `Schedulers` submodule or by simply defining your own iteration specifications. Something like this:

```julia
function model_step!(model)
    for agent in allagents(model)
        agent.variable += rand()
    end
    
    model.variable = mean(agent.variable for agent in allagents(model))

    for agent in allagents(model)
        agent.variable2 = agent.variable / model.variable
    end
end

```

For reference, see [Tutorial · Agents.jl](https://juliadynamics.github.io/Agents.jl/stable/tutorial/#Advanced-stepping).

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [August 16, 2021, 11:54am UTC](https://discourse.julialang.org/t/data-collection-issue-with-julia-agents/66458/8 "2021-08-16T11:54:29Z")

</div>

> [@bfried1](#):
>
> Note that i’ve fixed the issue by including `obtainer = copy` at the end of my `run!()` , I saw someone having similar trouble and this seemed to be the trick, but is this the best solution?

From the docs of [`run!`](https://juliadynamics.github.io/Agents.jl/stable/tutorial/#Agents.run!):

> `obtainer = identity` : method to transfer collected data to the `DataFrame` . Typically only change this to [`copy`](https://docs.julialang.org/en/v1/base/base/#Base.copy) if some data are mutable containers (e.g. `Vector` ) which change during evolution, or [`deepcopy`](https://docs.julialang.org/en/v1/base/base/#Base.deepcopy) if some data are nested mutable containers. Both of these options have performance penalties.

So yes, I think this is the correct approach. Note that I haven’t faced a case like yours yet, so I’m not totally sure if there are other or even better ways to do it.

---

<div class="post-metadata">

**Author:** ![Libbum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/libbum/32/3935_2.png) [@Libbum](https://discourse.julialang.org/u/Libbum)\
**Post date:** [August 16, 2021, 11:56am UTC](https://discourse.julialang.org/t/data-collection-issue-with-julia-agents/66458/9 "2021-08-16T11:56:44Z")

</div>

This [lightgraphs](https://juliadynamics.github.io/Agents.jl/stable/examples/schoolyard/) integration example in the docs may also help you out.

---

<div class="post-metadata">

**Author:** ![bfried1](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@bfried1](https://discourse.julialang.org/u/bfried1)\
**Post date:** [August 16, 2021, 12:04pm UTC](https://discourse.julialang.org/t/data-collection-issue-with-julia-agents/66458/10 "2021-08-16T12:04:54Z")

</div>

I suppose I will be doing a lot of testing and might end up preprocessing the data to avoid the performance penalties attributed to using `obtainer = copy`. Thanks for your speedy replies @fbanning, I’m writing my masters thesis (coding it all in Julia), so I’m sure you will see me on the forums again haha…  
Big thanks!
