-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Optimise _remove_agent in Continuous Space #3490
Copy link
Copy link
Closed
Description
Currently, _remove_agent is incredibly slow for large models. When an agent is removed, the code uses a for loop to shift every single subsequent agent down by one index in the dictionaries, and then does an
mesa/mesa/experimental/continuous_space/continuous_space.py
Lines 140 to 166 in d8f5f1c
| def _remove_agent(self, agent: Agent) -> None: | |
| """Remove an agent from the space. | |
| This method is automatically called by ContinuousSpaceAgent.remove. | |
| """ | |
| index = self._agent_to_index[agent] | |
| self._agent_to_index.pop(agent, None) | |
| self._index_to_agent.pop(index, None) | |
| del self.active_agents[index] | |
| # Shift all subsequent agents up by 1 | |
| for agent in self.active_agents[index::]: | |
| old_index = self._agent_to_index[agent] | |
| self._agent_to_index[agent] = old_index - 1 | |
| self._index_to_agent[old_index - 1] = agent | |
| # Clean up the stale entry from the last shifted agent | |
| if len(self.active_agents) > index: | |
| self._index_to_agent.pop(len(self.active_agents), None) | |
| # we move all data below the removed agent one row up | |
| self._agent_positions[index : self._n_agents - 1] = self._agent_positions[ | |
| index + 1 : self._n_agents | |
| ] | |
| self._n_agents -= 1 | |
| self.agent_positions = self._agent_positions[0 : self._n_agents] |
Or Am I missing something? Please let me know.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels