Original post below.
It seems the mistake must be somewhere else. I simplified the code to create a running example like this:
using Agents
mutable struct Agent <: AbstractAgent
id::Int64
end
function my_model_step!(model)
## dummy function
end
function my_agent_step!(agent, model)
## dummy function
end
function run_my_model!(model, steps::Integer; kwargs...)
run!(model, my_agent_step!, my_model_step!, steps, agents_first = false; kwargs...)
end
function data_function(agent::Agent)
## dummy function
end
function test_my_model()
model = ABM(Agent)
add_agent!(Agent(1), model)
adata = [data_function]
# run!(model, my_agent_step!, my_model_step!, 1, agents_first = false; adata)
run_my_model!(model, 1; adata)
end
test_my_model()
and that works.
So, hereâs the code that doesnât work, for a reason that I donât understand.
using EconoSim # Package is on Github: https://github.com/HapponomyOrg/EconoSim.jl
using Random
using Agents
using DataFrames
NUM_ACTORS = 1000
SIM_LENGTH = 1000
BASIC_SUMSY = SuMSy(2000, 0, 0.1, 30)
function random_purchase(model, actor::Actor)
target = random_agent(model)
transfer_asset!(actor.balance, target.balance, SUMSY_DEP, asset_value(actor.balance, SUMSY_DEP) / 10)
end
function balance(actor)
return asset_value(actor.balance, SUMSY_DEP)
end
function add_actors(model, behaviors::Vector)
for i in 1:NUM_ACTORS
actor = Actor()
for behavior in behaviors
add_behavior!(actor, behavior)
end
add_agent!(actor, model)
end
end
function run_sumsy_simulation!(sumsy::SuMSy, behavior)
model = create_sumsy_model(sumsy)
add_actors(model, [behavior])
adata = [balance]
data, _ = run_econo_model!(model, SIM_LENGTH; adata)
# this DOES work: data, _ = run!(model, actor_step!, econo_model_step!, SIM_LENGTH, agents_first = false; adata)
return data
end
run_sumsy_simulation!(BASIC_SUMSY, random_purchase)
The code for run_econoModel!() is:
function run_econo_model!(model, steps::Integer; kwargs...)
run!(model, actor_step!, econo_model_step!, steps, agents_first = false; kwargs...)
end
Original post
Hi,
Iâm implementing a framework build on Agents.jl and want to create a wrapper function for run!() with fewer parameters. In short, I want to do the following:
function run_my_model!(model, steps::Integer; kwargs...)
run!(model, my_agent_step!, my_model_step!, steps, agents_first = false; kwargs...)
end
All goes well as long as I donât pass anything after steps. But when I want to substitute:
adata = [data_function]
run!(model, my_agent_step!, my_model_step!, 10, agents_first = false; adata)
which works, with
adata = [data_function]
run_my_model!(model, 10; adata)
which I expected to be the same, I get the following error:
ERROR: MethodError: no method matching run_my_model!(::AgentBasedModel{Nothing, Actor, typeof(Agents.Schedulers.fastest), Dict{Symbol, Any}, TaskLocalRNG}, ::Int64; adata=[data_function])
Closest candidates are:
run_my_model!(::Any, ::Any, ::AnyâŚ) at ~/.julia/packages/EconoSim/mpKhJ/src/models/econo_model.jl:44 got unsupported keyword argument âadataâ
Stacktrace:
[1] run_sumsy_simulation!(sumsy::SuMSy, behavior::Function)
@ Main ~/Programming/Visual Studio/MoneySim.jl/src/SuMSySimulation.jl:72
[2] top-level scope
@ REPL[7]:1
Actor is a subtype of AbstractActor.
My problem seems to be with passing the kwargs in the correct way but I canât figure out what Iâm doing wrong.
Thanks in advance,
Stef