Skip to content

Commit e76259a

Browse files
authored
refactor: Simplify experimental Schelling example (#88)
* Schelling: Use JupyterViz slider API * Reduce line use of app.py * Schelling: Use default viz name given by JupyterViz * schelling experimental: Add _advance_time() * Apply ruff --fix
1 parent d19d1ff commit e76259a

File tree

2 files changed

+7
-28
lines changed

2 files changed

+7
-28
lines changed
Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from mesa.experimental import JupyterViz, make_text
1+
from mesa.experimental import JupyterViz, Slider, make_text
22
from model import Schelling
33

44

@@ -10,35 +10,13 @@ def get_happy_agents(model):
1010

1111

1212
def agent_portrayal(agent):
13-
color = "tab:orange" if agent.type == 0 else "tab:blue"
14-
return {"color": color}
13+
return {"color": "tab:orange" if agent.type == 0 else "tab:blue"}
1514

1615

1716
model_params = {
18-
"density": {
19-
"type": "SliderFloat",
20-
"value": 0.8,
21-
"label": "Agent density",
22-
"min": 0.1,
23-
"max": 1.0,
24-
"step": 0.1,
25-
},
26-
"minority_pc": {
27-
"type": "SliderFloat",
28-
"value": 0.2,
29-
"label": "Fraction minority",
30-
"min": 0.0,
31-
"max": 1.0,
32-
"step": 0.05,
33-
},
34-
"homophily": {
35-
"type": "SliderInt",
36-
"value": 3,
37-
"label": "Homophily",
38-
"min": 0,
39-
"max": 8,
40-
"step": 1,
41-
},
17+
"density": Slider("Agent density", 0.8, 0.1, 1.0, 0.1),
18+
"minority_pc": Slider("Fraction minority", 0.2, 0.0, 1.0, 0.05),
19+
"homophily": Slider("Homophily", 3, 0, 8, 1),
4220
"width": 20,
4321
"height": 20,
4422
}
@@ -47,7 +25,6 @@ def agent_portrayal(agent):
4725
Schelling,
4826
model_params,
4927
measures=["happy", make_text(get_happy_agents)],
50-
name="Schelling",
5128
agent_portrayal=agent_portrayal,
5229
)
5330
page # noqa

examples/schelling_experimental/model.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ def step(self):
6868
"""
6969
self.happy = 0 # Reset counter of happy agents
7070
self.agents.shuffle().do("step")
71+
# Must be before data collection.
72+
self._advance_time() # Temporary API; will be finalized by Mesa 3.0 release
7173
# collect data
7274
self.datacollector.collect(self)
7375

0 commit comments

Comments
 (0)