# Agents Based Modeling (using Agents.jl)

**URL:** <https://discourse.julialang.org/t/agents-based-modeling-using-agents-jl/30486>\
**Category:** New to Julia\
**Created:** [October 30, 2019, 11:27am UTC](https://discourse.julialang.org/t/agents-based-modeling-using-agents-jl/30486 "2019-10-30T11:27:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ennvvy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ennvvy/32/7816_2.png) [@ennvvy](https://discourse.julialang.org/u/ennvvy)\
**Post date:** [October 30, 2019, 11:27am UTC](https://discourse.julialang.org/t/agents-based-modeling-using-agents-jl/30486/1 "2019-10-30T11:27:04Z")

</div>

I am trying to use Agents.jl package to model my system. I use the following code

```julia
using Agents
using LightGraphs
using Distributions

mutable struct MyAgent{T<:Integer,Y<:AbstractFloat} <: AbstractAgent
    id::T ;
    pos::Y ; 
    W::Y ; 
end

mutable struct MyNet{T<:Integer,Y<:AbstractArray} <: AbstractSpace
    dimensions::T ; 
    space::SimpleDiGraph{Int64} ; 
    agent_positions::Y ;
end

mutable struct MyModel{T<:AbstractArray,Y<:AbstractFloat,Z<:AbstractSpace} <: AbstractModel
    space::Z ; 
    agents::T ;
    scheduler::Function ; 
    activation_prob::Y ; 
    interaction_prob::Y ; 
end
function instantiate_model(;num_agents,mean,sd,out_deg,prob_rew,prob_up,prob_int)
    agents = [MyAgent(i,rand(Normal(mean,sd))*pi/180,rand(Uniform(0.5,0.9))) for i in 1:num_agents] ;
    ntwk = MyNet(num_agents,watts_strogatz(num_agents,out_deg,prob_rew,is_directed=true),collect(1:num_agents)) ;
    model = MyModel(ntwk,agents,partial_activation,prob_up,prob_int) ;
    return model
end

function agent_step!(agent::AbstractAgent,model::AbstractModel)
agent.pos = agent.pos + agent[rand].pos ;
end

```

I want to make sure that the quantities used on the RHS `agent[rand].pos` are all from `[t-1]` time step. On using the `step!(agent_step!,model)` function, this is not possible as everything inside `agent_step!` gets executed, updated and no exceptions can be made. How can I have a copy of values of `agent.pos` from `[t-1]` ?

---

<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:** [December 9, 2019, 11:30am UTC](https://discourse.julialang.org/t/agents-based-modeling-using-agents-jl/30486/2 "2019-12-09T11:30:31Z")

</div>

Agents.jl is now in version 2.0, with a massively more simplified API. See [Agents.jl : Agent based modelling](https://discourse.julialang.org/t/agents-jl-agent-based-modelling/32043)

---

<div class="post-metadata">

**Author:** ![Ali\_Vahdati](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ali_vahdati/32/38715_2.png) [@Ali\_Vahdati](https://discourse.julialang.org/u/Ali_Vahdati)\
**Post date:** [December 9, 2019, 11:46am UTC](https://discourse.julialang.org/t/agents-based-modeling-using-agents-jl/30486/3 "2019-12-09T11:46:04Z")

</div>

It is a bit late reply. You need to specify a “model\_step!” function, instead of an agent\_step! to achieve your goal. In a model step function, you define what happens to the model in a single time step. So you can create an array of all agent positions, then update the positions from that array.

[The forest fire example in the documentations](https://juliadynamics.github.io/Agents.jl/dev/examples/forest_fire/) uses a model step function.

Make sure to check the version 2 of Agents.jl, building the model and collecting data is much simpler now.

---

<div class="post-metadata">

**Author:** ![patra9](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patra9/32/35929_2.png) [@patra9](https://discourse.julialang.org/u/patra9)\
**Post date:** [May 5, 2022, 2:34pm UTC](https://discourse.julialang.org/t/agents-based-modeling-using-agents-jl/30486/4 "2022-05-05T14:34:22Z")

</div>

hi, this link no longer works. Could you please resend it…

---

<div class="post-metadata">

**Author:** ![Ali\_Vahdati](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ali_vahdati/32/38715_2.png) [@Ali\_Vahdati](https://discourse.julialang.org/u/Ali_Vahdati)\
**Post date:** [May 5, 2022, 2:45pm UTC](https://discourse.julialang.org/t/agents-based-modeling-using-agents-jl/30486/5 "2022-05-05T14:45:38Z")

</div>

Check [this page](https://juliadynamics.github.io/AgentsExampleZoo.jl/dev/) for all the example models, including [forest fire](https://juliadynamics.github.io/AgentsExampleZoo.jl/dev/examples/forest_fire/#Forest-fire).

---

<div class="post-metadata">

**Author:** ![patra9](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patra9/32/35929_2.png) [@patra9](https://discourse.julialang.org/u/patra9)\
**Post date:** [May 5, 2022, 2:55pm UTC](https://discourse.julialang.org/t/agents-based-modeling-using-agents-jl/30486/6 "2022-05-05T14:55:20Z")

</div>

thank you so much. Have you ever tried modelling the vacuum problem in Julia? I am very new to Julia and AI and have no idea how to solve my problem.

---

<div class="post-metadata">

**Author:** ![Atiyah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atiyah/32/46462_2.png) [@Atiyah](https://discourse.julialang.org/u/Atiyah)\
**Post date:** [May 5, 2022, 3:03pm UTC](https://discourse.julialang.org/t/agents-based-modeling-using-agents-jl/30486/7 "2022-05-05T15:03:56Z")

</div>

Hi,

As a newbie for Julia too, and a new user of Agents.jl, I believe it would be beneficial if you tag the question with “agents” under modeling and simulation category, otherwise potential interested members of the community may not be notified.

---

<div class="post-metadata">

**Author:** ![Ali\_Vahdati](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ali_vahdati/32/38715_2.png) [@Ali\_Vahdati](https://discourse.julialang.org/u/Ali_Vahdati)\
**Post date:** [May 5, 2022, 3:09pm UTC](https://discourse.julialang.org/t/agents-based-modeling-using-agents-jl/30486/8 "2022-05-05T15:09:19Z")

</div>

No, not this specific model and I don’t know the problem either. I would read through the [tutorial](https://juliadynamics.github.io/Agents.jl/stable/tutorial/) section first. Then if you have specific questions, such as how to write a specific behavior or how to design a specific model, people will be able to help.
