Skip to content

Commit

Permalink
Make MultiGrid.place_agent faster (projectmesa#1508)
Browse files Browse the repository at this point in the history
The condition tested now is a constant time operation when an agent has not been placed in a grid yet, while before it was a linear time operation in relation to the size of the list of agents.
  • Loading branch information
Tortar authored Nov 11, 2022
1 parent 77599fa commit a409675
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,10 +582,10 @@ def default_val() -> MultiGridContent:
def place_agent(self, agent: Agent, pos: Coordinate) -> None:
"""Place the agent at the specified location, and set its pos variable."""
x, y = pos
if agent not in self.grid[x][y]:
if agent.pos is None or agent not in self.grid[x][y]:
self.grid[x][y].append(agent)
self.empties.discard(pos)
agent.pos = pos
agent.pos = pos
self.empties.discard(pos)

def remove_agent(self, agent: Agent) -> None:
"""Remove the agent from the given location and set its pos attribute to None."""
Expand Down

0 comments on commit a409675

Please sign in to comment.