# Swap agent positions in Agents.jl

**URL:** <https://discourse.julialang.org/t/swap-agent-positions-in-agents-jl/107774>\
**Category:** General Usage\
**Tags:** question, agents\
**Created:** [December 18, 2023, 2:46pm UTC](https://discourse.julialang.org/t/swap-agent-positions-in-agents-jl/107774 "2023-12-18T14:46:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [December 18, 2023, 2:46pm UTC](https://discourse.julialang.org/t/swap-agent-positions-in-agents-jl/107774/1 "2023-12-18T14:46:26Z")

</div>

Hello,

My model uses a 2D grid called GridSpaceSingle, which restricts each cell to a maximum of one agent. I would like to switch the positions of two agents. Unfortunately, this functionality is not currently available, but appears to be a new feature in an upcoming [release](https://github.com/JuliaDynamics/Agents.jl/blob/c71a26a86743dca8038041ce2031b588bfc17935/src/spaces/discrete.jl#L281). I thought I might copy and paste the function into my project for the time being, but that quickly became messy, as it relies on internal functions, and some do not appear to be [implemented](https://github.com/JuliaDynamics/Agents.jl/blob/c71a26a86743dca8038041ce2031b588bfc17935/src/core/space_interaction_API.jl#L53). I was wondering if someone can recommend a workaround until the new features are relased?

---

<div class="post-metadata">

**Author:** ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)\
**Post date:** [December 18, 2023, 3:07pm UTC](https://discourse.julialang.org/t/swap-agent-positions-in-agents-jl/107774/2 "2023-12-18T15:07:37Z")

</div>

The swap function you linked to is pretty descriptive of what needs to be done:

1. Remove the two agents from the space.
2. Swap their positions.
3. Add the two agents back to the space.

This should be pretty straightforward to re-implemented yourself in your local environment if you really need it asap.

But to be quite honest, you might just want to wait until Agents.jl v6.0 is released.

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [December 18, 2023, 3:14pm UTC](https://discourse.julialang.org/t/swap-agent-positions-in-agents-jl/107774/3 "2023-12-18T15:14:44Z")

</div>

Thanks. Unfortunately, I am at an impasse. So I would like to implement a stopgap solution. What you described makes sense. I should have been more descriptive about the workaround. Given that GridSpaceSingle is is restricted to one agent per cell, are there any precautions I need to take? Can I just use remove\_agent! and add\_agent!, and directly modify the pos fields, or is there anything special I need to do?

---

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [December 18, 2023, 7:57pm UTC](https://discourse.julialang.org/t/swap-agent-positions-in-agents-jl/107774/4 "2023-12-18T19:57:51Z")

</div>

the point indeed is to first remove **both** agents and then to re-add them, this way you respect the one-agent per position restriction in a GridSpaceSingle

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [December 18, 2023, 9:05pm UTC](https://discourse.julialang.org/t/swap-agent-positions-in-agents-jl/107774/5 "2023-12-18T21:05:05Z")

</div>

Got it. Thank you. I just wanted to make sure that it was OK to perform the swap via remove\_agent! and add\_agent!. It sounds like it is.
