Skip to content

Commit

Permalink
schelling: Switch to Jupyter viz
Browse files Browse the repository at this point in the history
  • Loading branch information
rht committed Jun 29, 2023
1 parent 6730e52 commit d560e8e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 53 deletions.
9 changes: 4 additions & 5 deletions examples/schelling/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ To install the dependencies use pip and the requirements.txt in this directory.

## How to Run

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

```
$ mesa runserver
$ solara run app.py
```

Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press Reset, then Run.
Then open your browser to [http://127.0.0.1:8765/](http://127.0.0.1:8765/) and click the Play button.

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/).

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

## Files

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

## Further Reading
Expand Down
54 changes: 54 additions & 0 deletions examples/schelling/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from mesa_models.experimental import JupyterViz

from model import Schelling


def get_happy_agents(model):
"""
Display a text count of how many happy agents there are.
"""
return f"Happy agents: {model.happy}"


def agent_portrayal(agent):
color = "tab:orange" if agent.type == 0 else "tab:blue"
return {"color": color}


model_params = {
"density": {
"type": "SliderFloat",
"value": 0.8,
"label": "Agent density",
"min": 0.1,
"max": 1.0,
"step": 0.1,
},
"minority_pc": {
"type": "SliderFloat",
"value": 0.2,
"label": "Fraction minority",
"min": 0.0,
"max": 1.0,
"step": 0.05,
},
"homophily": {
"type": "SliderInt",
"value": 3,
"label": "Homophily",
"min": 0,
"max": 8,
"step": 1,
},
"width": 20,
"height": 20,
}

page = JupyterViz(
Schelling,
model_params,
measures=["happy", get_happy_agents],
name="Schelling",
agent_portrayal=agent_portrayal,
)
page
3 changes: 0 additions & 3 deletions examples/schelling/run.py

This file was deleted.

45 changes: 0 additions & 45 deletions examples/schelling/server.py

This file was deleted.

0 comments on commit d560e8e

Please sign in to comment.