Why does GridSpace zero/ negative coordinates yields an Error but still adds the agents to the model

Hello guys,
I have a question regarding Agents.jl GridSpace does it allow coordinates that are negative or zero like (0,0) or (-5,-1).
In the test I’ve done (See code below) when I put a zero coordinates it returns an error but when checking the model I find the agent have been added to that position.

using Agents

@agent struct Customer(GridAgent{2}) 
    age
end

space = GridSpace((100,100); periodic = false, metric = :euclidean)
model = StandardABM(Customer, space)
add_agent!((0,0), model,25)

When executing this it gives a BoundsError:

BoundsError: attempt to access 100×100 Matrix{Vector{Int64}} at index [0, 0]

However when I check I find that the agent has been added:

allagents(model)

It gives the output:

ValueIterator for a Dict{Int64, Customer} with 1 entry. Values:
  Customer(1, (0, 0), 25)

The same behavior is observed for the negative position coordinates:

add_agent!((-1,-1), model,25)

Output:
BoundsError: attempt to access 100×100 Matrix{Vector{Int64}} at index [-1, -1]

And when I check the agent is added:

ValueIterator for a Dict{Int64, Customer} with 2 entries. Values:
  Customer(2, (-1, -1), 25)
  Customer(1, (0, 0), 25)

How does this happen is there any explanation of what going on ?
Thanks in advance for your help :slight_smile:

No It does not, because (on purpose and for simplicity) the positions of agents in a grid space are exactly the same as the indices of accessing the underlying storage array. and in julia the default array type has indices starting from 1 onwards.

You’ll need to approach your problem differently to avoid negative positions or use a continuous space.

1 Like

(post deleted by author)

Yes but the problem is that it is adding agents to the model so when using all_agents(model) it returns all the agents even the ones outside the grid space. However, when using dedicated functions such as nearby_ids(pos, model, radius) it only return the valid agents with valid positions (which is understandable). Isn’t this considered somewhat of a bug ??

yes, the fact that agents with negative positions can be added to the grid is a bug; you can open an issue at the GitHub repo or open a PR to fix it directly. Do not rely on it and follow the rules outlined by the documentation (positive indices).

1 Like