Swap agent positions in Agents.jl


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. 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. I was wondering if someone can recommend a workaround until the new features are relased?

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.


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?

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

1 Like

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.