Skip to content

Commit

Permalink
Remove unnecessary transformations in schedulers
Browse files Browse the repository at this point in the history
  • Loading branch information
Tortar authored and rht committed Oct 20, 2022
1 parent b80df23 commit af23b8b
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions mesa/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def step(self) -> None:

def get_agent_count(self) -> int:
"""Returns the current number of agents in the queue."""
return len(self._agents.keys())
return len(self._agents)

@property
def agents(self) -> list[Agent]:
Expand Down Expand Up @@ -142,14 +142,12 @@ class SimultaneousActivation(BaseScheduler):

def step(self) -> None:
"""Step all agents, then advance them."""
agent_keys = list(self._agents.keys())
for agent_key in agent_keys:
self._agents[agent_key].step()
# We recompute the keys because some agents might have been removed in
# the previous loop.
agent_keys = list(self._agents.keys())
for agent_key in agent_keys:
self._agents[agent_key].advance()
for agent in self._agents.values():
agent.step()
# the previous steps might remove some agents, but
# this loop will go over the remaining existing agents
for agent in self._agents.values():
agent.advance()
self.steps += 1
self.time += 1

Expand Down Expand Up @@ -292,4 +290,4 @@ def get_type_count(self, type_class: type[Agent]) -> int:
"""
Returns the current number of agents of certain type in the queue.
"""
return len(self.agents_by_type[type_class].values())
return len(self.agents_by_type[type_class])

0 comments on commit af23b8b

Please sign in to comment.