# Method Error object of type AgentBasedModel is not callable

**URL:** <https://discourse.julialang.org/t/method-error-object-of-type-agentbasedmodel-is-not-callable/79014>\
**Category:** Modelling & Simulations\
**Tags:** agents\
**Created:** [April 4, 2022, 4:58pm UTC](https://discourse.julialang.org/t/method-error-object-of-type-agentbasedmodel-is-not-callable/79014 "2022-04-04T16:58:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![imantha](https://avatars.discourse-cdn.com/v4/letter/i/6a8cbe/32.png) [@imantha](https://discourse.julialang.org/u/imantha)\
**Post date:** [April 4, 2022, 4:58pm UTC](https://discourse.julialang.org/t/method-error-object-of-type-agentbasedmodel-is-not-callable/79014/1 "2022-04-04T16:58:17Z")

</div>

Hi I am having the following error when I call the `step!()` or `abmvideo!()` functions and not sure what the cause of it is. Any ideas?

```julia
ERROR: MethodError: objects of type AgentBasedModel{Agents.OSM.OpenStreetMapSpace, Union{Casualty, Hospital, PMA, Rescuer}, typeof(Agents.Schedulers.fastest), Dict{Symbol, Real}, Random.TaskLocalRNG} are not callable
Stacktrace:
 [1] step!(model::AgentBasedModel{Agents.OSM.OpenStreetMapSpace, Union{Casualty, Hospital, PMA, Rescuer}, typeof(Agents.Schedulers.fastest), Dict{Symbol, Real}, Random.TaskLocalRNG}, agent_step!::AgentBasedModel{Agents.OSM.OpenStreetMapSpace, Union{Casualty, Hospital, PMA, Rescuer}, typeof(Agents.Schedulers.fastest), Dict{Symbol, Real}, Random.TaskLocalRNG}, model_step!::typeof(dummystep), n::Int64, agents_first::Bool)
   @ Agents ~/.julia/packages/Agents/nnELw/src/simulations/step.jl:55
 [2] step! (repeats 2 times)
   @ ~/.julia/packages/Agents/nnELw/src/simulations/step.jl:45 [inlined]
 [3] top-level scope
   @ ~/github/CRTT-lite/crtt_lite_osm.jl:234

```

— Part of the code —

```julia
#Initialisation
begin
    include("agent_types.jl")
    include("vis_agents.jl")
    using .Agent_Types: Casualty, Rescuer, PMA, Hospital
    using .Vis_Agents: agent_color, agent_markers, agent_size
end

function initialise(;map_path=OSM.test_map(),num_casualties, num_rescuers, num_pma, num_hospitals, properties)
    model = AgentBasedModel(
        Union{Casualty, Rescuer,PMA,Hospital},
        OpenStreetMapSpace(map_path),
        scheduler = Schedulers.fastest;
        properties = properties
    )
    # Add casualties to Model
    for i=1:num_casualties
        pos = random_position(model)
        casualty = Casualty(nextid(model),pos,trauma = rand([1,2,3]))
        add_agent_pos!(casualty,model)
    end
    
    #Add Rescuers to Model 
    for i=1:num_rescuers
        init_pos=(775,775,0.0)
        rescuer=Rescuer(nextid(model),init_pos) 
        add_agent_pos!(rescuer,model)
    end

    # Add other agents (PMA,Hospital) ...

    return model
end

```

```julia
#agent step functions

@doc "Return casualties/rescuers by their boolean attributes only" ->
function agent_by_property(model::ABM, agent_type::DataType, prop::Symbol, prop_value::Bool)::Union{Vector{Casualty}, Vector{Rescuer}}
    if prop_value
        return [model[id] for id in 1:length(model.agents) if model[id] isa agent_type && getproperty(model[id], prop)]
    else
        return [model[id] for id in 1:length(model.agents) if model[id] isa agent_type && !getproperty(model[id], prop)]
    end
end

# Updating agent attributes
@doc "Update agent attributes" ->
function update_agent_attributes!(agent::Union{Casualty,Rescuer},attr::Dict{Symbol,T}) where T <: Any
    for (k,v) in attr
        setfield!(agent,k,v)
    end 
end

# Assigns casualties to each rescuer "awaiting instructions"
@doc "Rescuers : awaiting_instructions, locate casualties" ->
function find_casualties!(agent,model)
    # Locate a random casualty who is awaiting rescue
    casualty_to_be_rescued = agent_by_property(model, Casualty, :awaiting_rescue, true) |> rand
    # Update rescuer attributes
    rescuer_attributes = Dict(
        :travel_to => casualty_to_be_rescued.id, 
        :destination => casualty_to_be_rescued.pos,
        :awaiting_instructions => false,
        :rescuing_in_progress => true
    )
    update_agent_attributes!(agent, rescuer_attributes)
    # update casualty attributes
    casualty_to_be_rescued_attributes = Dict(
        :awaiting_rescue => false,
        :rescued_by => agent.id
    )
    update_agent_attributes!(casualty_to_be_rescued, casualty_to_be_rescued_attributes)
end

function go2!()
    for (id,agent) in model.agents
        # Are there any casualties to be rescued
        any_to_be_rescued = filter(x -> x isa Casualty && x.awaiting_rescue == true, model.agents |> values |> collect)
        # Rescuers : awaiting_instructions --> find_casualties
        if (agent isa Rescuer) && (agent.awaiting_instructions) && length(any_to_be_rescued) > 0
            find_casualties!(agent,model)
        end

    end
    return model
end

```

```julia
step!(
    model,
    go2!(),
    1
)

ERROR: MethodError: objects of type AgentBasedModel{Agents.OSM.OpenStreetMapSpace, Union{Casualty, Hospital, PMA, Rescuer}, typeof(Agents.Schedulers.fastest), Dict{Symbol, Real}, Random.TaskLocalRNG} are not callable
Stacktrace:

```

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [April 4, 2022, 5:19pm UTC](https://discourse.julialang.org/t/method-error-object-of-type-agentbasedmodel-is-not-callable/79014/2 "2022-04-04T17:19:16Z")

</div>

Without having a closer look at your code

> [@imantha](#):
>
> `function go2!()`

should probably be `function go2!(model)`.

> [@imantha](#):
>
> ```julia
> step!(
> model,
> go2!(),
> 1
> )
> 
> ```

should probably be `step!(model, dummystep, go2!, 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:** [April 4, 2022, 7:32pm UTC](https://discourse.julialang.org/t/method-error-object-of-type-agentbasedmodel-is-not-callable/79014/3 "2022-04-04T19:32:07Z")

</div>

Yeap, these are standard Julia syntax mistakes, not really related with Agents.jl. `go2!()` returns a model. BTW thanks a lot @fbanning for being around and answering questions about Agents.jl here!
