I apologize in advance if the issue is trivial. I’m still learning how to use Agents.jl and I cannot figure out what this issue is.
I will post my code and comment around it. The issue is stated below.
The packages in use:
using LightGraphs
using SparseArrays: findnz
using Plots
using Statistics
Defining my agent:
mutable struct AgentZ <: AbstractAgent
id::Int
cond::Array{Float64,1}
delta::Float64
lambda::Float64
learning_rate::Float64
extinction_rate::Float64
signal::Array{Float64,1}
end
Function to create model:
function create_model(; num_agents,num_signals,p)
space = nothing
properties = Dict(
:network => erdos_renyi(num_agents, p),
)
model = AgentBasedModel(
AgentZ,
space,
scheduler = Schedulers.randomly,
properties = properties,
)
for i=1:num_agents
add_agent!(
model,
cond,
delta,
lambda,
learning_rate,
extinction_rate,
signal,
)
end
return model
end
This is a function that assigns to each agent’s signal the value 0 or 1. This part works.
function update_sig!(model)
for i in allagents(model)
i.signal=rand(0:1,num_signals)
end
end
This function, conditioned on the random signal, changes an agent’s cond by plus/minus 1. The issue lies here. When I step the model forward agent.cond, for all agents, takes the same value, where some should go down and some up.
function update_cond!(agent)
if agent.signal[1] == 1
agent.cond[1] += 1
else
agent.cond[1] -= 1
end
end
Defining the agent_step!
function agent_step!(agent,model)
update_sig!(model)
update_cond!(agent)
end
Setting the parameters and creating the model:
num_signals = 1
num_agents = 10
p=0.3
cond = zeros(num_signals).+.001
delta = 0
lambda = 1
learning_rate = .05
extinction_rate = 1
signal = zeros(num_signals)
model= create_model(;num_agents,num_signals,p)
You can check the issue here by stepping the model and printing the value of the two properties for all agents, signal and cond. Notice signal works well, but cond takes the same value across all the agents. Where is the mistake, how do I get cond to take the proper values?
step!(model, agent_step!)
for i =1:num_agents
print(model[i].cond)
end
for i =1:num_agents
print(model[i].signal)
end
Thank you,
Bfried1