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

Revert changes of #1478 and #1456 #1516

Merged
merged 4 commits into from
Nov 8, 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
39 changes: 22 additions & 17 deletions mesa/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from collections import defaultdict

# mypy
from typing import Iterator, Union, Iterable
from typing import Iterator, Union
from mesa.agent import Agent
from mesa.model import Model

Expand Down Expand Up @@ -96,9 +96,10 @@ def agent_buffer(self, shuffled: bool = False) -> Iterator[Agent]:
"""Simple generator that yields the agents while letting the user
remove and/or add agents during stepping.
"""
agent_keys = self._agents.keys()
# To be able to remove and/or add agents during stepping
# it's necessary to cast the keys view to a list.
agent_keys = list(self._agents.keys())
if shuffled:
agent_keys = list(agent_keys)
self.model.random.shuffle(agent_keys)

for key in agent_keys:
Expand Down Expand Up @@ -137,12 +138,16 @@ class SimultaneousActivation(BaseScheduler):

def step(self) -> None:
"""Step all agents, then advance them."""
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()
# To be able to remove and/or add agents during stepping
# it's necessary to cast the keys view to a list.
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()
self.steps += 1
self.time += 1

Expand Down Expand Up @@ -185,18 +190,18 @@ def __init__(

def step(self) -> None:
"""Executes all the stages for all agents."""
agent_keys = self._agents.keys()
# To be able to remove and/or add agents during stepping
# it's necessary to cast the keys view to a list.
agent_keys = list(self._agents.keys())
if self.shuffle:
agent_keys = list(agent_keys)
self.model.random.shuffle(agent_keys)
for stage in self.stage_list:
for agent_key in agent_keys:
getattr(self._agents[agent_key], stage)() # Run stage
# We recompute the keys because some agents might have been removed
# in the previous loop.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to document why the casting to list is necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

agent_keys = self._agents.keys()
agent_keys = list(self._agents.keys())
if self.shuffle_between_stages:
agent_keys = list(agent_keys)
self.model.random.shuffle(agent_keys)
self.time += self.stage_time

Expand Down Expand Up @@ -259,9 +264,10 @@ def step(self, shuffle_types: bool = True, shuffle_agents: bool = True) -> None:
shuffle_agents: If True, the order of execution of each agents in a
type group is shuffled.
"""
type_keys: Iterable[type[Agent]] = self.agents_by_type.keys()
# To be able to remove and/or add agents during stepping
# it's necessary to cast the keys view to a list.
type_keys: list[type[Agent]] = list(self.agents_by_type.keys())
if shuffle_types:
type_keys = list(type_keys)
self.model.random.shuffle(type_keys)
for agent_class in type_keys:
self.step_type(agent_class, shuffle_agents=shuffle_agents)
Expand All @@ -276,9 +282,8 @@ def step_type(self, type_class: type[Agent], shuffle_agents: bool = True) -> Non
Args:
type_class: Class object of the type to run.
"""
agent_keys: Iterable[int] = self.agents_by_type[type_class].keys()
agent_keys: list[int] = list(self.agents_by_type[type_class].keys())
if shuffle_agents:
agent_keys = list(agent_keys)
self.model.random.shuffle(agent_keys)
for agent_key in agent_keys:
self.agents_by_type[type_class][agent_key].step()
Expand Down