Skip to content

Commit

Permalink
Added permanent step interval slider to SolaraViz for precise adjustm…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
HMNS19 committed Jan 9, 2025
1 parent faea9f4 commit 51022e3
Show file tree
Hide file tree
Showing 17 changed files with 23 additions and 54 deletions.
1 change: 0 additions & 1 deletion mesa/examples/advanced/epstein_civil_violence/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def post_process(ax):
"cop_vision": Slider("Cop Vision", 7, 1, 10, 1),
"legitimacy": Slider("Government Legitimacy", 0.82, 0.0, 1, 0.01),
"max_jail_term": Slider("Max Jail Term", 30, 0, 50, 1),
"stepsize": 1,
}

space_component = make_space_component(
Expand Down
1 change: 0 additions & 1 deletion mesa/examples/advanced/epstein_civil_violence/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ def __init__(
movement=True,
max_iters=1000,
seed=None,
stepsize=1,
):
super().__init__(seed=seed)
self.movement = movement
Expand Down
1 change: 0 additions & 1 deletion mesa/examples/advanced/pd_grid/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def pd_agent_portrayal(agent):
"value": 42,
"label": "Random Seed",
},
"stepsize": Slider("Step Interval", 1, 1, 20, 1),
"width": Slider("Grid Width", value=50, min=10, max=100, step=1),
"height": Slider("Grid Height", value=50, min=10, max=100, step=1),
"activation_order": {
Expand Down
1 change: 0 additions & 1 deletion mesa/examples/advanced/pd_grid/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def __init__(
activation_order="Random",
payoffs=None,
seed=None,
stepsize=1,
):
"""
Create a new Spatial Prisoners' Dilemma Model.
Expand Down
7 changes: 0 additions & 7 deletions mesa/examples/advanced/sugarscape_g1mt/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ def portray(g):
"value": 42,
"label": "Random Seed",
},
"stepsize": Slider(
label="Steps Interval",
value=1,
min=1,
max=20,
step=1,
),
"width": 50,
"height": 50,
# Population parameters
Expand Down
1 change: 0 additions & 1 deletion mesa/examples/advanced/sugarscape_g1mt/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def __init__(
vision_max=5,
enable_trade=True,
seed=None,
stepsize=1,
):
super().__init__(seed=seed)
# Initiate width and height of sugarscape
Expand Down
7 changes: 0 additions & 7 deletions mesa/examples/basic/boid_flockers/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ def boid_draw(agent):
"value": 42,
"label": "Random Seed",
},
"stepsize": Slider(
label="Steps Interval",
value=1,
min=1,
max=20,
step=1,
),
"population": Slider(
label="Number of boids",
value=100,
Expand Down
1 change: 0 additions & 1 deletion mesa/examples/basic/boid_flockers/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def __init__(
separate=0.015,
match=0.05,
seed=None,
stepsize=1,
):
"""Create a new Boids Flocking model.
Expand Down
8 changes: 0 additions & 8 deletions mesa/examples/basic/boltzmann_wealth_model/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ def agent_portrayal(agent):
"max": 100,
"step": 1,
},
"stepsize": {
"type": "SliderInt",
"value": 1,
"label": "Steps Interval:",
"min": 1,
"max": 100,
"step": 1,
},
"width": 10,
"height": 10,
}
Expand Down
2 changes: 1 addition & 1 deletion mesa/examples/basic/boltzmann_wealth_model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class BoltzmannWealth(Model):
datacollector (DataCollector): Collects and stores model data
"""

def __init__(self, n=100, width=10, height=10, seed=None, stepsize=5):
def __init__(self, n=100, width=10, height=10, seed=None):
"""Initialize the model.
Args:
Expand Down
8 changes: 0 additions & 8 deletions mesa/examples/basic/conways_game_of_life/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ def post_process(ax):
"value": 42,
"label": "Random Seed",
},
"stepsize": {
"type": "SliderInt",
"value": 1,
"label": "Steps Interval:",
"min": 1,
"max": 100,
"step": 1,
},
"width": {
"type": "SliderInt",
"value": 50,
Expand Down
4 changes: 1 addition & 3 deletions mesa/examples/basic/conways_game_of_life/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
class ConwaysGameOfLife(Model):
"""Represents the 2-dimensional array of cells in Conway's Game of Life."""

def __init__(
self, width=50, height=50, initial_fraction_alive=0.2, seed=None, stepsize=1
):
def __init__(self, width=50, height=50, initial_fraction_alive=0.2, seed=None):
"""Create a new playing area of (width, height) cells."""
super().__init__(seed=seed)
# Use a simple grid, where edges wrap around.
Expand Down
1 change: 0 additions & 1 deletion mesa/examples/basic/schelling/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def agent_portrayal(agent):
"value": 42,
"label": "Random Seed",
},
"stepsize": Slider("Step Interval", 1, 1, 100, 1),
"density": Slider("Agent density", 0.8, 0.1, 1.0, 0.1),
"minority_pc": Slider("Fraction minority", 0.2, 0.0, 1.0, 0.05),
"homophily": Slider("Homophily", 0.4, 0.0, 1.0, 0.125),
Expand Down
1 change: 0 additions & 1 deletion mesa/examples/basic/schelling/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def __init__(
homophily: float = 0.4,
radius: int = 1,
seed=None,
stepsize=1,
):
"""Create a new Schelling model.
Expand Down
8 changes: 0 additions & 8 deletions mesa/examples/basic/virus_on_network/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ def get_resistant_susceptible_ratio(model):
"value": 42,
"label": "Random Seed",
},
"stepsize": {
"type": "SliderInt",
"value": 1,
"label": "Steps Interval:",
"min": 1,
"max": 100,
"step": 1,
},
"num_nodes": Slider(
label="Number of agents",
value=10,
Expand Down
1 change: 0 additions & 1 deletion mesa/examples/basic/virus_on_network/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def __init__(
recovery_chance=0.3,
gain_resistance_chance=0.5,
seed=None,
stepsize=1,
):
super().__init__(seed=seed)
self.num_nodes = num_nodes
Expand Down
24 changes: 21 additions & 3 deletions mesa/visualization/solara_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def SolaraViz(
| Literal["default"] = "default",
*,
play_interval: int = 100,
step_interval: int = 1,
simulator: Simulator | None = None,
model_params=None,
name: str | None = None,
Expand All @@ -72,6 +73,9 @@ def SolaraViz(
Defaults to "default", which uses the default Altair space visualization.
play_interval (int, optional): Interval for playing the model steps in milliseconds.
This controls the speed of the model's automatic stepping. Defaults to 100 ms.
step_interval (int, optional): Interval for updating the visualization every n steps.
This controls how often the visualization is updated during the model's execution.
simulator: A simulator that controls the model (optional)
model_params (dict, optional): Parameters for (re-)instantiating a model.
Can include user-adjustable parameters and fixed parameters. Defaults to None.
Expand Down Expand Up @@ -103,7 +107,7 @@ def SolaraViz(
# set up reactive model_parameters shared by ModelCreator and ModelController
reactive_model_parameters = solara.use_reactive({})
reactive_play_interval = solara.use_reactive(play_interval)

reactive_step_interval = solara.use_reactive(step_interval)
with solara.AppBar():
solara.AppBarTitle(name if name else model.value.__class__.__name__)

Expand All @@ -117,18 +121,28 @@ def SolaraViz(
max=500,
step=10,
)
solara.SliderInt(
label="Step Interval",
value=reactive_step_interval,
on_value=lambda v: reactive_step_interval.set(v),
min=1,
max=100,
step=2,
)
if not isinstance(simulator, Simulator):
ModelController(
model,
model_parameters=reactive_model_parameters,
play_interval=reactive_play_interval,
step_interval=reactive_step_interval,
)
else:
SimulatorController(
model,
simulator,
model_parameters=reactive_model_parameters,
play_interval=reactive_play_interval,
step_interval=reactive_step_interval,
)
with solara.Card("Model Parameters"):
ModelCreator(
Expand Down Expand Up @@ -189,13 +203,15 @@ def ModelController(
*,
model_parameters: dict | solara.Reactive[dict] = None,
play_interval: int | solara.Reactive[int] = 100,
step_interval: int | solara.Reactive[int] = 1,
):
"""Create controls for model execution (step, play, pause, reset).
Args:
model: Reactive model instance
model_parameters: Reactive parameters for (re-)instantiating a model.
play_interval: Interval for playing the model steps in milliseconds.
step_interval: Interval for updating the visualization every n steps.
"""
playing = solara.use_reactive(False)
Expand All @@ -216,7 +232,7 @@ async def step():
@function_logger(__name__)
def do_step():
"""Advance the model by one step."""
for _ in range(model_parameters.value["stepsize"]):
for _ in range(step_interval.value):
model.value.step()

running.value = model.value.running
Expand Down Expand Up @@ -262,6 +278,7 @@ def SimulatorController(
*,
model_parameters: dict | solara.Reactive[dict] = None,
play_interval: int | solara.Reactive[int] = 100,
step_interval: int | solara.Reactive[int] = 1,
):
"""Create controls for model execution (step, play, pause, reset).
Expand All @@ -270,6 +287,7 @@ def SimulatorController(
simulator: Simulator instance
model_parameters: Reactive parameters for (re-)instantiating a model.
play_interval: Interval for playing the model steps in milliseconds.
step_interval: Interval for updating the visualization every n steps.
"""
playing = solara.use_reactive(False)
Expand All @@ -289,7 +307,7 @@ async def step():

def do_step():
"""Advance the model by one step."""
simulator.run_for(1)
simulator.run_for(step_interval.value)
running.value = model.value.running
force_update()

Expand Down

0 comments on commit 51022e3

Please sign in to comment.