Adding agents in mixed agent model with continuous space (Agents.jl)

Hi all, I am trying to create an ABM with mixed agents on a continuous space. I tried to do the same on a grid space and everything worked well but when moving to a continuous space I face some issues I didn’t find the way to solve.
This is my code:

@agent Bird ContinuousAgent{2} begin
    in_thermal::Bool
end

@agent Thermal ContinuousAgent{2} begin
    t_status::Int
end

function initialize_model(;
    n_birds = 5,
    n_thermals = 5,
    dims = (1000, 1000),
    in_thermal = false,
    t_status = 0,
)
    rng = MersenneTwister(42)
    space = ContinuousSpace(dims; periodic = true)
    model = ABM(Union{Bird, Thermal}, space;
                rng, scheduler = Schedulers.Randomly()
            )

    for _ in 1:n_thermals
        add_agent!(Thermal, model, t_status)
    end
end

model =  initialize_model()

The error I get is on the line

add_agent!(Thermal, model, t_status)

and it is:
ERROR: MethodError: no method matching Thermal(::Int64, ::Tuple{Float64, Float64}, ::Int64)

Trying to add the agent in this way

add_agent!(Thermal(t_status), model)

doesn’t work either.
Does anyone know what is going on and a possible solution?

Hi, welcome to the Julia discourse.

Hm, that is odd. The code seems valid to me. Can you please post the versions of Agents.jl you are using, and also make your code a minimal working example, so that copy pasting the full code runs and reproduces the error? (currently for example your code is missing headers like using Agents).

For this:

This makes sense. Thermal(t_status) is not valid syntax because the type Thermal needs 3 inputs to be instantiated (incorrect it needs 4). Julia will try to evaluate this before calling teh add_agent! function.

1 Like

Hi, thank you for the kind reply. I’m using v5.17.1 of Agents.jl
Here you find the full code to copy and paste

using Agents
using Random

@agent Bird ContinuousAgent{2} begin
    in_thermal::Bool
end

@agent Thermal ContinuousAgent{2} begin
    t_status::Int
end

function initialize_model(;
    n_birds = 5,
    n_thermals = 5,
    dims = (1000, 1000),
    in_thermal = false,
    t_status = 0,
)
    rng = MersenneTwister(42)
    space = ContinuousSpace(dims; periodic = true)
    model = ABM(Union{Bird, Thermal}, space;
                rng, scheduler = Schedulers.Randomly()
            )

    for _ in 1:n_thermals
        add_agent!(Thermal, model, t_status)
    end
end

model =  initialize_model()

Thanks a lot already :slight_smile:

Can confirm the issue with your MWE. Seems as if add_agent! calls Thermal without passing a Tuple to the vel field?

2 Likes

oooooooh of course!!! :smiley: haha

Yes, add_agent! adds automatically a random position, but does not add a velocity. Yet, the continous agents have velocity. You need to adjust your code to:

vel = (0.0, 0.0) # or anything else
add_agent!(Thermal, model, vel, t_status)
2 Likes

And you might want to add return model to the end of your init function because otherwise your function doesn’t return anything.

2 Likes

All clear!! It also explains why on the grid everything worked properly! Thanks @fbanning and @Datseris :blush: