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

Remove unnecessary list operations #1456

Merged
merged 1 commit into from
Oct 20, 2022
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
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])