# Agents.jl: Equivalent of nearby\_agents for grid cells?

**URL:** <https://discourse.julialang.org/t/agents-jl-equivalent-of-nearby-agents-for-grid-cells/91290>\
**Category:** Modelling & Simulations\
**Tags:** agents\
**Created:** [December 6, 2022, 4:08am UTC](https://discourse.julialang.org/t/agents-jl-equivalent-of-nearby-agents-for-grid-cells/91290 "2022-12-06T04:08:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sea\_slug](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sea_slug/32/52907_2.png) [@sea\_slug](https://discourse.julialang.org/u/sea_slug)\
**Post date:** [December 6, 2022, 4:08am UTC](https://discourse.julialang.org/t/agents-jl-equivalent-of-nearby-agents-for-grid-cells/91290/1 "2022-12-06T04:08:42Z")

</div>

Mandatory I’m brand new to Julia and Agents.jl so apologies if this is obvious.

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

My `GridSpace` has a property “grass”. In the example predator-prey model, sheep move at random to a nearby cell. I would like my sheep to “look” around and move to the nearest cell where there is grass. To do this I think I need something like `nearby_agents` (but for nearby grid cells) to retrieve a vector of grid cell IDs within range _r_ (sheep vision range). I would then be able to iterate over the nearby cells to see a) which have grass and b) which is closest.

My question: Is there an equivalent of `nearby_agents` for nearby cells? And does my approach seem sensible?

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [December 6, 2022, 9:43am UTC](https://discourse.julialang.org/t/agents-jl-equivalent-of-nearby-agents-for-grid-cells/91290/2 "2022-12-06T09:43:44Z")

</div>

welcome 🙂

Hah, I was about to say “I think you need `nearby_positions`”. But I realized, this function doesn’t exist. Which is obviously a neglect from our end, since the code exists. In fact, when you call `nearby_agents`, an “function” `nearby_positions` is called first. Please open an issue in the Agents.jl GitHub repository asking for a nearyb positions function 🙂

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 6, 2022, 10:12am UTC](https://discourse.julialang.org/t/agents-jl-equivalent-of-nearby-agents-for-grid-cells/91290/3 "2022-12-06T10:12:57Z")

</div>

In the Agents API there is a function `Agents.nearby_positions`. And in the code as well:  
[https://github.com/JuliaDynamics/Agents.jl/blob/ac265f9cd094684c1768e4170da70ce1cc220771/src/spaces/grid\_general.jl](https://github.com/JuliaDynamics/Agents.jl/blob/ac265f9cd094684c1768e4170da70ce1cc220771/src/spaces/grid_general.jl)  
@Datseris Are these the ones you referring to?

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [December 6, 2022, 10:20am UTC](https://discourse.julialang.org/t/agents-jl-equivalent-of-nearby-agents-for-grid-cells/91290/4 "2022-12-06T10:20:32Z")

</div>

lol oh well I was dumb 😛 Yeap, [`nearby_positions`](https://juliadynamics.github.io/Agents.jl/stable/api/#Agents.nearby_positions) is the function you need 🙂
