# Agents.jl problem with the step! function

**URL:** <https://discourse.julialang.org/t/agents-jl-problem-with-the-step-function/117598>\
**Category:** New to Julia\
**Tags:** agents\
**Created:** [July 29, 2024, 3:29pm UTC](https://discourse.julialang.org/t/agents-jl-problem-with-the-step-function/117598 "2024-07-29T15:29:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Farouk](https://avatars.discourse-cdn.com/v4/letter/f/f17d59/32.png) [@Farouk](https://discourse.julialang.org/u/Farouk)\
**Post date:** [July 29, 2024, 3:29pm UTC](https://discourse.julialang.org/t/agents-jl-problem-with-the-step-function/117598/1 "2024-07-29T15:29:43Z")

</div>

Hi I am new to Agents.jl and Julia, I am following the official online tutorial on the Shelling’s model, however I am facing a hard time with the step! function, I tried to use the same source code as in the tutorial but I always get an Error from the stepping function and a warning(see image below). **Note that the custom step function I created works fine I tested it in the REPL, and also when I use step!(model, n=5) I get MethodError.**  
I really appreciate your help and time.

```julia
using Agents

mutable struct Schelling <: AbstractAgent
    id::Int
    pos::Tuple{Int,Int}
    group::Int
    happy::Bool
end

function agent_step!(agent,model,)
    #println("checking agent: ",agent.id)
    #println("Happy: ",agent.happy)
    if agent.happy == true
        println("I am happy")
        return
    end
    nearby_same = 0
    for neighbor in nearby_agents(agent,model)
        println("nearby_same: ",nearby_same," neighbor: ",neighbor.id," neighbor group: ",neighbor.group)
        if neighbor.group == agent.group
            nearby_same += 1
        end
    end
    if nearby_same >= model.min_to_be_happy
        println("nearby_same:00 ",nearby_same)
        agent.happy = true
    else
        move_agent_single!(agent,model)
        println("jumped to: ",agent.pos)
    end
    return
end

function initilize(;N = 10, M = 4, min_to_be_happy = 2,steppingFunction = agent_step!)
    space = GridSpace((M,M))
    properties = Dict(:min_to_be_happy => min_to_be_happy)
    scheduler = Schedulers.Randomly
    model = AgentBasedModel(Schelling,space;properties = properties, scheduler = scheduler,agent_step! = steppingFunction)

    for n in 1:N
        agent = Schelling(n,(1,1),n < N/2 ? 1 : 2,false)
        add_agent_single!(agent,model) #This adds agents at a random and empty location in the space
    end
    return model
end

model = initilize()
step!(model,agent_step!,dummystep)

```

And here is the warning I face:

```julia
┌ Warning: Passing agent_step! and model_step! to step! is deprecated. Use 
     the new version
│ step!(model, n = 1, agents_first = true)

```

The Error:

```julia
ERROR: MethodError: Cannot `convert` an object of type StandardABM{GridSpace{2, true}, Schelling, Dict{Int64, Schelling}, Tuple{DataType}, typeof(agent_step!), typeof(dummystep), DataType, Dict{Symbol, Int64}, Random.TaskLocalRNG} to an object of type Vector{Int64}

```

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [July 29, 2024, 3:50pm UTC](https://discourse.julialang.org/t/agents-jl-problem-with-the-step-function/117598/2 "2024-07-29T15:50:05Z")

</div>

Hi! Which version of the package are you using?

The current version (6) has a tutorial for this ([Tutorial · Agents.jl](https://juliadynamics.github.io/Agents.jl/stable/tutorial/#tutorial_fast)) which differs in some key areas from your code.

---

<div class="post-metadata">

**Author:** ![Farouk](https://avatars.discourse-cdn.com/v4/letter/f/f17d59/32.png) [@Farouk](https://discourse.julialang.org/u/Farouk)\
**Post date:** [July 29, 2024, 3:59pm UTC](https://discourse.julialang.org/t/agents-jl-problem-with-the-step-function/117598/3 "2024-07-29T15:59:27Z")

</div>

I am using 6.0.17. Thank you for the tutorial page.

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [July 29, 2024, 4:00pm UTC](https://discourse.julialang.org/t/agents-jl-problem-with-the-step-function/117598/4 "2024-07-29T16:00:58Z")

</div>

Perhaps try the latest (current) version of the tutorial…?

---

<div class="post-metadata">

**Author:** ![Farouk](https://avatars.discourse-cdn.com/v4/letter/f/f17d59/32.png) [@Farouk](https://discourse.julialang.org/u/Farouk)\
**Post date:** [July 29, 2024, 4:10pm UTC](https://discourse.julialang.org/t/agents-jl-problem-with-the-step-function/117598/5 "2024-07-29T16:10:05Z")

</div>

The current tutorial is really different from [this tutorial](https://docs.sciml.ai/Agents/v5.3/tutorial/#Space-1).

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [July 29, 2024, 4:13pm UTC](https://discourse.julialang.org/t/agents-jl-problem-with-the-step-function/117598/6 "2024-07-29T16:13:52Z")

</div>

That says v5.3.0 at the bottom left. So it should work if you download Agents v5.3 and run it in that environment…

---

<div class="post-metadata">

**Author:** ![Farouk](https://avatars.discourse-cdn.com/v4/letter/f/f17d59/32.png) [@Farouk](https://discourse.julialang.org/u/Farouk)\
**Post date:** [July 30, 2024, 9:26am UTC](https://discourse.julialang.org/t/agents-jl-problem-with-the-step-function/117598/7 "2024-07-30T09:26:24Z")

</div>

You know I figured what was triggering the problem, but still did not delve into why it is happening though. The error in my previous (old version) and current code is triggered by the scheduler I used. These two schedulers causes the problem: **Schedulers.Randomly** and **Schedulers.ByID** , however, in the latter we can achieve the same behavior by using **Schedulers.ByProperty(:id)**. 😕
