diff --git a/examples/schelling/README.md b/examples/schelling/README.md index 64cc9c83..b0116b55 100644 --- a/examples/schelling/README.md +++ b/examples/schelling/README.md @@ -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/). @@ -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 diff --git a/examples/schelling/app.py b/examples/schelling/app.py new file mode 100644 index 00000000..8f2e242a --- /dev/null +++ b/examples/schelling/app.py @@ -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 diff --git a/examples/schelling/run.py b/examples/schelling/run.py deleted file mode 100644 index f20cebcb..00000000 --- a/examples/schelling/run.py +++ /dev/null @@ -1,3 +0,0 @@ -from server import server - -server.launch(open_browser=True) diff --git a/examples/schelling/server.py b/examples/schelling/server.py deleted file mode 100644 index 1396e9c7..00000000 --- a/examples/schelling/server.py +++ /dev/null @@ -1,45 +0,0 @@ -import mesa -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 schelling_draw(agent): - """ - Portrayal Method for canvas - """ - if agent is None: - return - portrayal = {"Shape": "circle", "r": 0.5, "Filled": "true", "Layer": 0} - - if agent.type == 0: - portrayal["Color"] = ["#FF0000", "#FF9999"] - portrayal["stroke_color"] = "#00FF00" - else: - portrayal["Color"] = ["#0000FF", "#9999FF"] - portrayal["stroke_color"] = "#000000" - return portrayal - - -canvas_element = mesa.visualization.CanvasGrid(schelling_draw, 20, 20, 500, 500) -happy_chart = mesa.visualization.ChartModule([{"Label": "happy", "Color": "Black"}]) - -model_params = { - "height": 20, - "width": 20, - "density": mesa.visualization.Slider("Agent density", 0.8, 0.1, 1.0, 0.1), - "minority_pc": mesa.visualization.Slider("Fraction minority", 0.2, 0.00, 1.0, 0.05), - "homophily": mesa.visualization.Slider("Homophily", 3, 0, 8, 1), -} - -server = mesa.visualization.ModularServer( - Schelling, - [canvas_element, get_happy_agents, happy_chart], - "Schelling", - model_params, -)