Problem in integrating Agents.jl and DifferentialEquations.jl

Hello, Everyone Im running this example from Agents.jl page – example. I’m getting Error as shown below. It will be really helpful if someone can help me out here as its extracted directly from website. I will expect it to run without error. Did I miss something ?

Thank you :slight_smile:

using Agents
using Distributions
using CairoMakie
using DifferentialEquations
using Random


mutable struct Fisher <: AbstractAgent
    id::Int
    competence::Int
    yearly_catch::Float64
end


function agent_diffeq_step!(agent, model)

    agent.yearly_catch = rand(model.rng, Poisson(agent.competence))
    
end

function model_diffeq_step!(model)
    # We step 364 days with this call.
    OrdinaryDiffEq.step!(model.i, 364.0, true)
    # Only allow fishing if stocks are high enough
    model.i.p[2] =
        model.i.u[1] > model.min_threshold ? sum(a.yearly_catch for a in allagents(model)) :
        0.0
    # Notify the integrator that conditions may be altered
    OrdinaryDiffEq.u_modified!(model.i, true)
    # Then apply our catch modifier
    OrdinaryDiffEq.step!(model.i, 1.0, true)
    # Store yearly stock in the model for plotting
    model.stock = model.i.u[1]
    # And reset for the next year
    model.i.p[2] = 0.0
    OrdinaryDiffEq.u_modified!(model.i, true)
end

function initialise_diffeq(;
    stock = 400.0, # Initial population of fish (lets move to an equilibrium position)
    max_population = 500.0, # Maximum value of fish stock
    min_threshold = 60.0, # Regulate fishing if population drops below this value
    nagents = 50,
)

    function fish_stock!(ds, s, p, t)
        max_population, h = p
        ds[1] = s[1] * (1 - (s[1] / max_population)) - h
    end

    prob = OrdinaryDiffEq.ODEProblem(fish_stock!, [stock], (0.0, Inf), [max_population, 0.0])
    integrator = OrdinaryDiffEq.init(prob, OrdinaryDiffEq.Tsit5(); advance_to_tstop = true)

    model = ABM(
        Fisher;
        properties = Dict(
            :stock => stock,
            :max_population => max_population,
            :min_threshold => min_threshold,
            :rng => MersenneTwister(1234),
            :i => integrator, # The OrdinaryDiffEq integrator
        ),
    )
    for _ in 1:nagents
        add_agent!(model, floor(rand(model.rng, truncated(LogNormal(), 1, 6))), 0.0)
    end
    model
end

modeldeq = initialise_diffeq()

_, resultsdeq = run!(modeldeq, agent_diffeq_step!, model_diffeq_step!, 20; mdata = [:stock])

Error:

ERROR: MethodError: no method matching should_we_collect(::Int64, ::StandardABM{…}, ::Bool)

Closest candidates are:
  should_we_collect(::Any, ::Any, ::Any, ::Function)
   @ Agents ~/.julia/packages/Agents/8JW8b/src/simulations/collect.jl:197
  should_we_collect(::Any, ::Any, ::Any, ::AbstractVector)
   @ Agents ~/.julia/packages/Agents/8JW8b/src/simulations/collect.jl:196
  should_we_collect(::Any, ::Any, ::Any, ::Real)
   @ Agents ~/.julia/packages/Agents/8JW8b/src/simulations/collect.jl:195

Stacktrace:
 [1] run!(model::StandardABM{…}, agent_step!::typeof(agent_diffeq_step!), model_step!::typeof(model_diffeq_step!), n::Int64; when::Bool, when_model::Bool, mdata::Vector{…}, adata::Nothing, obtainer::Function, agents_first::Bool, showprogress::Bool, warn_deprecation::Bool)
   @ Agents ~/.julia/packages/Agents/8JW8b/src/deprecations.jl:0
 [2] top-level scope
   @ Untitled-2:72
Some type information was truncated. Use `show(err)` to see complete types.

heh, sure, but which website?

Each version of Agents.jl has its own “website”. Have you checked that the version of Agents.jl you have installed matches the version in the “website” you copied the code from? That is one likely source of error.

It is best if you follow the advice here Please read: make it easier to help you which includes reporting the package versions you are using and using the latest stable versions.

1 Like

From this code you are either using Agents.jl less than v6 or the example in the docs was not updated. THis is incorrect syntax in Agents.jl v6.

EDIT: the docs of v6 are properly updated: DifferentialEquations.jl · Agents.jl

3 Likes

Thank you for help. I thought website get updated after update in version. My bad :smiling_face_with_tear: