Skip to content

Commit 8000505

Browse files
wang-boyuEwoutH
authored andcommitted
merge experimental schelling into schelling example
1 parent eae3f0b commit 8000505

File tree

5 files changed

+48
-77
lines changed

5 files changed

+48
-77
lines changed

examples/basic/schelling/README.md

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,13 @@ To install the dependencies use pip and the requirements.txt in this directory.
1616

1717
## How to Run
1818

19-
To run the model interactively, run ``mesa runserver`` in this directory. e.g.
19+
To run the model interactively, in this directory, run the following command
2020

2121
```
22-
$ mesa runserver
22+
$ solara run app.py
2323
```
2424

25-
or
26-
27-
Directly run the file ``run.py`` in the terminal. e.g.
28-
29-
```
30-
$ python run.py
31-
```
32-
33-
Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press Reset, then Run.
25+
Then open your browser to [http://127.0.0.1:8765/](http://127.0.0.1:8765/) and click the Play button.
3426

3527
To view and run some example model analyses, launch the IPython Notebook and open ``analysis.ipynb``. Visualizing the analysis also requires [matplotlib](http://matplotlib.org/).
3628

@@ -40,10 +32,9 @@ To run the model with the grid displayed as an ASCII text, run `python run_ascii
4032

4133
## Files
4234

43-
* ``run.py``: Launches a model visualization server.
35+
* ``app.py``: Code for the interactive visualization.
4436
* ``run_ascii.py``: Run the model in text mode.
4537
* ``schelling.py``: Contains the agent class, and the overall model class.
46-
* ``server.py``: Defines classes for visualizing the model in the browser via Mesa's modular server, and instantiates a visualization server.
4738
* ``analysis.ipynb``: Notebook demonstrating how to run experiments and parameter sweeps on the model.
4839

4940
## Further Reading

examples/basic/schelling/app.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import solara
2+
from mesa.visualization import (
3+
Slider,
4+
SolaraViz,
5+
make_plot_measure,
6+
make_space_matplotlib,
7+
)
8+
from model import Schelling
9+
10+
11+
def get_happy_agents(model):
12+
"""
13+
Display a text count of how many happy agents there are.
14+
"""
15+
return solara.Markdown(f"**Happy agents: {model.happy}**")
16+
17+
18+
def agent_portrayal(agent):
19+
return {"color": "tab:orange" if agent.type == 0 else "tab:blue"}
20+
21+
22+
model_params = {
23+
"density": Slider("Agent density", 0.8, 0.1, 1.0, 0.1),
24+
"minority_pc": Slider("Fraction minority", 0.2, 0.0, 1.0, 0.05),
25+
"homophily": Slider("Homophily", 3, 0, 8, 1),
26+
"width": 20,
27+
"height": 20,
28+
}
29+
30+
model1 = Schelling(20, 20, 0.8, 0.2, 3)
31+
32+
HappyPlot = make_plot_measure("happy")
33+
34+
page = SolaraViz(
35+
model1,
36+
components=[
37+
make_space_matplotlib(agent_portrayal),
38+
make_plot_measure("happy"),
39+
get_happy_agents,
40+
],
41+
model_params=model_params,
42+
)
43+
page # noqa
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
jupyter
22
matplotlib
3-
mesa~=2.0
3+
mesa[viz]>=3.0.0b0

examples/basic/schelling/run.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

examples/basic/schelling/server.py

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)