Skip to content

Commit

Permalink
Replace RandomActivation scheduler with AgentSet
Browse files Browse the repository at this point in the history
Replace the old RandomActivation scheduler with AgentSet functionality. self.agents.shuffle.do("step") is equivalent and self.schedule.step() with an RandomActivation scheduler, and model behavior should not change.
  • Loading branch information
EwoutH committed Aug 17, 2024
1 parent 96da4e3 commit b7f67c7
Show file tree
Hide file tree
Showing 14 changed files with 25 additions and 28 deletions.
4 changes: 2 additions & 2 deletions examples/bank_reserves/bank_reserves/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def __init__(
self.height = height
self.width = width
self.init_people = init_people
self.schedule = mesa.time.RandomActivation(self)

self.grid = mesa.space.MultiGrid(self.width, self.height, torus=True)
# rich_threshold is the amount of savings a person needs to be considered "rich"
self.rich_threshold = rich_threshold
Expand Down Expand Up @@ -154,7 +154,7 @@ def __init__(

def step(self):
# tell all the agents in the model to run their step function
self.schedule.step()
self.agents.shuffle().do("step")
# collect data
self.datacollector.collect(self)

Expand Down
4 changes: 2 additions & 2 deletions examples/bank_reserves/batch_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def __init__(
self.height = height
self.width = width
self.init_people = init_people
self.schedule = mesa.time.RandomActivation(self)

self.grid = mesa.space.MultiGrid(self.width, self.height, torus=True)
# rich_threshold is the amount of savings a person needs to be considered "rich"
self.rich_threshold = rich_threshold
Expand Down Expand Up @@ -169,7 +169,7 @@ def step(self):
# collect data
self.datacollector.collect(self)
# tell all the agents in the model to run their step function
self.schedule.step()
self.agents.shuffle().do("step")

def run_model(self):
for i in range(self.run_time):
Expand Down
4 changes: 2 additions & 2 deletions examples/boid_flockers/boid_flockers/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def __init__(
self.vision = vision
self.speed = speed
self.separation = separation
self.schedule = mesa.time.RandomActivation(self)

self.space = mesa.space.ContinuousSpace(width, height, True)
self.factors = {"cohere": cohere, "separate": separate, "match": match}
self.make_agents()
Expand All @@ -147,4 +147,4 @@ def make_agents(self):
self.schedule.add(boid)

def step(self):
self.schedule.step()
self.agents.shuffle().do("step")
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, N=100, width=10, height=10):
super().__init__()
self.num_agents = N
self.grid = mesa.space.MultiGrid(width, height, True)
self.schedule = mesa.time.RandomActivation(self)

self.datacollector = mesa.DataCollector(
model_reporters={"Gini": compute_gini}, agent_reporters={"Wealth": "wealth"}
)
Expand All @@ -38,7 +38,7 @@ def __init__(self, N=100, width=10, height=10):
self.datacollector.collect(self)

def step(self):
self.schedule.step()
self.agents.shuffle().do("step")
# collect data
self.datacollector.collect(self)

Expand Down
4 changes: 2 additions & 2 deletions examples/boltzmann_wealth_model_experimental/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, N=100, width=10, height=10):
super().__init__()
self.num_agents = N
self.grid = mesa.space.MultiGrid(width, height, True)
self.schedule = mesa.time.RandomActivation(self)

self.datacollector = mesa.DataCollector(
model_reporters={"Gini": compute_gini}, agent_reporters={"Wealth": "wealth"}
)
Expand All @@ -38,7 +38,7 @@ def __init__(self, N=100, width=10, height=10):
self.datacollector.collect(self)

def step(self):
self.schedule.step()
self.agents.shuffle().do("step")
# collect data
self.datacollector.collect(self)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self, num_agents=7, num_nodes=10):
self.num_nodes = num_nodes if num_nodes >= self.num_agents else self.num_agents
self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=0.5)
self.grid = mesa.space.NetworkGrid(self.G)
self.schedule = mesa.time.RandomActivation(self)

self.datacollector = mesa.DataCollector(
model_reporters={"Gini": compute_gini},
agent_reporters={"Wealth": lambda _: _.wealth},
Expand All @@ -38,7 +38,7 @@ def __init__(self, num_agents=7, num_nodes=10):
self.datacollector.collect(self)

def step(self):
self.schedule.step()
self.agents.shuffle().do("step")
# collect data
self.datacollector.collect(self)

Expand Down
3 changes: 1 addition & 2 deletions examples/caching_and_replay/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ def __init__(
self.homophily = homophily
self.radius = radius

self.schedule = mesa.time.RandomActivation(self)
self.grid = mesa.space.SingleGrid(width, height, torus=True)

self.happy = 0
Expand All @@ -96,7 +95,7 @@ def step(self):
Run one step of the model.
"""
self.happy = 0 # Reset counter of happy agents
self.schedule.step()
self.agents.shuffle().do("step")

self.datacollector.collect(self)

Expand Down
4 changes: 2 additions & 2 deletions examples/charts/charts/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def __init__(
self.height = height
self.width = width
self.init_people = init_people
self.schedule = mesa.time.RandomActivation(self)

self.grid = mesa.space.MultiGrid(self.width, self.height, torus=True)
# rich_threshold is the amount of savings a person needs to be considered "rich"
self.rich_threshold = rich_threshold
Expand Down Expand Up @@ -137,7 +137,7 @@ def __init__(

def step(self):
# tell all the agents in the model to run their step function
self.schedule.step()
self.agents.shuffle().do("step")
# collect data
self.datacollector.collect(self)

Expand Down
3 changes: 1 addition & 2 deletions examples/el_farol/el_farol/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def __init__(
super().__init__()
self.running = True
self.num_agents = N
self.schedule = mesa.time.RandomActivation(self)

# Initialize the previous attendance randomly so the agents have a history
# to work with from the start.
Expand All @@ -35,7 +34,7 @@ def __init__(
def step(self):
self.datacollector.collect(self)
self.attendance = 0
self.schedule.step()
self.agents.shuffle.do("step")
# We ensure that the length of history is constant
# after each step.
self.history.pop(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(
self.movement = movement
self.max_iters = max_iters
self.iteration = 0
self.schedule = mesa.time.RandomActivation(self)

self.grid = mesa.space.SingleGrid(width, height, torus=True)

model_reporters = {
Expand Down Expand Up @@ -109,7 +109,7 @@ def step(self):
"""
Advance the model by one step and collect data.
"""
self.schedule.step()
self.agents.shuffle().do("step")
# collect data
self.datacollector.collect(self)
self.iteration += 1
Expand Down
4 changes: 2 additions & 2 deletions examples/forest_fire/forest_fire/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self, width=100, height=100, density=0.65):
"""
super().__init__()
# Set up model objects
self.schedule = mesa.time.RandomActivation(self)

self.grid = mesa.space.SingleGrid(width, height, torus=False)

self.datacollector = mesa.DataCollector(
Expand Down Expand Up @@ -47,7 +47,7 @@ def step(self):
"""
Advance the model by one step.
"""
self.schedule.step()
self.agents.shuffle().do("step")
# collect data
self.datacollector.collect(self)

Expand Down
3 changes: 1 addition & 2 deletions examples/schelling/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def __init__(
self.homophily = homophily
self.radius = radius

self.schedule = mesa.time.RandomActivation(self)
self.grid = mesa.space.SingleGrid(width, height, torus=True)

self.happy = 0
Expand All @@ -94,7 +93,7 @@ def step(self):
Run one step of the model.
"""
self.happy = 0 # Reset counter of happy agents
self.schedule.step()
self.agents.shuffle().do("step")

self.datacollector.collect(self)

Expand Down
4 changes: 2 additions & 2 deletions examples/shape_example/shape_example/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(self, N=2, width=20, height=10):
self.N = N # num of agents
self.headings = ((1, 0), (0, 1), (-1, 0), (0, -1)) # tuples are fast
self.grid = mesa.space.SingleGrid(width, height, torus=False)
self.schedule = mesa.time.RandomActivation(self)

self.make_walker_agents()
self.running = True

Expand All @@ -36,4 +36,4 @@ def make_walker_agents(self):
unique_id += 1

def step(self):
self.schedule.step()
self.agents.shuffle().do("step")
4 changes: 2 additions & 2 deletions examples/virus_on_network/virus_on_network/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(
prob = avg_node_degree / self.num_nodes
self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=prob)
self.grid = mesa.space.NetworkGrid(self.G)
self.schedule = mesa.time.RandomActivation(self)

self.initial_outbreak_size = (
initial_outbreak_size if initial_outbreak_size <= num_nodes else num_nodes
)
Expand Down Expand Up @@ -96,7 +96,7 @@ def resistant_susceptible_ratio(self):
return math.inf

def step(self):
self.schedule.step()
self.agents.shuffle().do("step")
# collect data
self.datacollector.collect(self)

Expand Down

0 comments on commit b7f67c7

Please sign in to comment.