I want to implement various custom agent moves. I need to obtain the x and y positions of an agent, and then update them. For example, this does not work:
‘’’
function agent_step!(fish, model)
xAdd = rand( -1:1 )
yAdd = rand( -1:1 )
fish.pos( (fish.pos.x + xAdd), (fish.pos.y + yAdd) )
move_agent!(fish, model, fish.pos)
end
‘’’
How should I correct this?
You should show the error message for questions like these, and you should give runnable code.
Just use move_agent!
and pass the new position.
julia> using Agents
julia> @agent struct Fish(ContinuousAgent{2,Float64}) end
julia> model = StandardABM(Fish);
julia> fish = Fish(1, SVector(0.0, 0.0), SVector(-1.0, -1.0));
julia> move_agent!(fish, SVector(1.0, 1.0), model) # to a set pos
Fish(1, [1.0, 1.0], [-1.0, -1.0])
julia> dx, dy = rand(-1:1), rand(-1:1);
julia> move_agent!(fish, fish.pos + SVector(dx, dy), model) # shift
Fish(1, [0.0, 2.0], [-1.0, -1.0])
1 Like
Ah! Adding SVectors! That makes sense. The error had to do with trying to access elements in an SVector. Your solution gets around this nicely. Many thanks!