JuliaDynamics / Agents.jl

Agent-based modeling framework in Julia
https://juliadynamics.github.io/Agents.jl/stable/
MIT License
710 stars 114 forks source link

Error when trying to search different distances across multiple dimensions of GridSpace #1053

Open flee598 opened 2 days ago

flee598 commented 2 days ago

I'm trying to return nearby_positions() of an agent, searching different distances for each of the dimensions in my GridSpace. When I change r = 3 to r = (1, 3) I get an error. I also get an error with nearby_ids().

Minimal Working Example

using Agents

space = GridSpace((20, 20))

@agent struct test_ag(GridAgent{2}) 
end

function test_step!(agent, model)
        move_agent_single!(agent, model)
end

model = StandardABM(
    test_ag, # type of agents
    space; # space they live in
    agent_step! = test_step!
)

for n in 1:5
    add_agent_single!(model)
end

# works fine
nearby_positions(model[1], model, 3)

# does not work
nearby_positions(model[1], model, (1,3))

# does not work, but returns a different error
nearby_ids(model[1], model, (1,3))

nearby_positions() error: ERROR: MethodError: no method matching offsets_within_radius_no_0(::GridSpace{2, true}, ::Tuple{Int64, Int64})

nearby_ids() error: ERROR: MethodError: no method matching bound_range(::UnitRange{Int64}, ::Int64, ::GridSpace{2, true})

Agents.jl version 6.0.16 Julia version 1.10.4

Datseris commented 2 days ago

Right, yeah this is a bug. It must have crept in somewhere during the transition to v6, as it required a lot of refactoring. The reason this bug exists is because we haven't tested this particular function in the test suite.

If you would like to have a crack at fixing the bug via a Pull Request, it would be appreciated. From the error messages we just have to make sure offsets_within_radius_no_0(::GridSpace{2, true}, ::Tuple{Int64, Int64}) works.

flee598 commented 1 day ago

I had a look at the code, but it is a little over my head. I'm new to Julia and Agents, and I am not confident I could fix the bug without creating more issues.