Agents.jl: subset object returned from nearby_positions

I am making a predator-prey model, following the wolf-sheep example using Agents.jl.

Following on from this question, instead of sheep moving to a random cell I am trying to make the sheep move to a cell that contains grass.

I can get all cells around a sheep with the following:

near_cells = nearby_positions(sheep.pos, model, 1)

I then want to subset those cells to only get the ones that contain grass e.g. this is pseudo code of what I want to do (doesn’t work):

cell_w_grass = near_cells[near_cells.fully_grown == true]

I am struggling with the above step, I cannot figure out how to subset the object near_cells I am having difficulty understanding the data structure which looks like this: Base.Iterators.Filter{Agents.var"#74#76"{Matrix{Vector{Int64}}} ...

Again, probably an obvious question that I am missing due to being a Julia/Agents novice, any advice would be greatly appreciated!

I think what you want to do is to iterate over near_cells and check whether those positions are set to true inside model.fully_grown. Correct?

A simple way to do it without modifying the near_cells variable (which is possibly used somewhere else) could be:

grassy_cells = []
for cell in near_cells
    if model.fully_grown[cell...]
        push!(grassy_cells, cell)
    end
end

(Didn’t test this snippet but it might explain to you what you need to know to solve your problem.)

2 Likes