Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warn if placing already placed agent #2083

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ def is_integer(x: Real) -> bool:
return isinstance(x, _types_integer)


def warn_if_agent_has_position_already(placement_func):
def wrapper(self, agent, *args, **kwargs):
if agent.pos is not None:
warnings.warn(
f"""Agent {agent.unique_id} is being placed with
place_agent() despite already having the position {agent.pos}. In most
cases, you'd want to clear the current position with remove_agent()
before placing the agent again.""",
stacklevel=2,
)
placement_func(self, agent, *args, **kwargs)

return wrapper


class _Grid:
"""Base class for a rectangular grid.

Expand Down Expand Up @@ -976,6 +991,7 @@ class SingleGrid(_PropertyGrid):
the grid for empty spaces.
"""

@warn_if_agent_has_position_already
def place_agent(self, agent: Agent, pos: Coordinate) -> None:
"""Place the agent at the specified location, and set its pos variable."""
if self.is_cell_empty(pos):
Expand Down Expand Up @@ -1024,6 +1040,7 @@ def default_val() -> MultiGridContent:
"""Default value for new cell elements."""
return []

@warn_if_agent_has_position_already
def place_agent(self, agent: Agent, pos: Coordinate) -> None:
"""Place the agent at the specified location, and set its pos variable."""
x, y = pos
Expand Down Expand Up @@ -1355,6 +1372,7 @@ def _invalidate_agent_cache(self):
self._agent_points = None
self._index_to_agent = {}

@warn_if_agent_has_position_already
def place_agent(self, agent: Agent, pos: FloatCoordinate) -> None:
"""Place a new agent in the space.

Expand Down Expand Up @@ -1515,6 +1533,7 @@ def default_val() -> list:
"""Default value for a new node."""
return []

@warn_if_agent_has_position_already
def place_agent(self, agent: Agent, node_id: int) -> None:
"""Place an agent in a node."""
self.G.nodes[node_id]["agent"].append(agent)
Expand Down