Skip to content

Grid: Calling place_agent twice in different positions causes an agent to exist in two positions in the grid #1522

@rht

Description

@rht

Describe the bug

Context: #1508 (comment)
Currently, calling place_agent twice or more in a row without calling remove_agent beforehand causes the agent to exist in several locations.

Expected behavior

An error should be raised if an agent's pos attribute is not None, when calling place_agent.
A stronger guarantee would be to check the entire grid for the existence of the agent, but this is very costly to do, unless there is a dict attribute for the grid to track the agents existence.

To Reproduce

import mesa

a = mesa.Agent(0, None)
grid = mesa.space.SingleGrid(3, 3, False)
grid.place_agent(a, (1, 1))
print("a.pos", a.pos)
grid.place_agent(a, (0, 0))
print("a.pos", a.pos)
for e in grid.coord_iter():
    print(e)

Output

a.pos (1, 1)
a.pos (0, 0)
(<mesa.agent.Agent object at 0x7ffa9311a9e0>, 0, 0)
(None, 0, 1)
(None, 0, 2)
(None, 1, 0)
(<mesa.agent.Agent object at 0x7ffa9311a9e0>, 1, 1)
(None, 1, 2)
(None, 2, 0)
(None, 2, 1)
(None, 2, 2)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions