diff --git a/README.md b/README.md index 34f2647c..6364996d 100644 --- a/README.md +++ b/README.md @@ -114,18 +114,14 @@ This is based on Dorigo's Ant System "Swarm Intelligence" algorithm for generati ## Visualization Examples -### [Boltzmann Wealth Model (Experimental)](https://github.com/projectmesa/mesa-examples/tree/main/examples/boltzmann_wealth_model_experimental) +### [Boltzmann Wealth Model)](https://github.com/projectmesa/mesa-examples/tree/main/examples/boltzmann_wealth_model) -Boltzmann Wealth model with the experimental Juptyer notebook visualization feature. +Boltzmann Wealth model with an optional visualization using Streamlit. ### [Charts Example](https://github.com/projectmesa/mesa-examples/tree/main/examples/charts) A modified version of the [Bank Reserves](https://github.com/projectmesa/mesa-examples/tree/main/examples/bank_reserves) example made to provide examples of Mesa's charting tools. -### [Schelling Segregation Model (Experimental)](https://github.com/projectmesa/mesa-examples/tree/main/examples/schelling_experimental) - -Schelling segregation model with the experimental Juptyer notebook visualization feature. - ### [Shape Example](https://github.com/projectmesa/mesa-examples/tree/main/examples/shape_example) Example of grid display and direction showing agents in the form of arrow-head shape. diff --git a/examples/boltzmann_wealth_model/Readme.md b/examples/boltzmann_wealth_model/Readme.md index 4a6e21f1..8f7f7c81 100644 --- a/examples/boltzmann_wealth_model/Readme.md +++ b/examples/boltzmann_wealth_model/Readme.md @@ -12,32 +12,41 @@ As the model runs, the distribution of wealth among agents goes from being perfe To follow the tutorial example, launch the Jupyter Notebook and run the code in ``Introduction to Mesa Tutorial Code.ipynb`` which you can find in the main mesa repo [here](https://github.com/projectmesa/mesa/blob/main/docs/tutorials/intro_tutorial.ipynb) -To launch the interactive server, as described in the [last section of the tutorial](https://mesa.readthedocs.io/en/latest/tutorials/intro_tutorial.html#adding-visualization), run: +Make sure to install the requirements first: ``` - $ python server.py + $ pip install -r requirements.txt ``` -Make sure to install the requirements first: +To launch the interactive server, as described in the [last section of the tutorial](https://mesa.readthedocs.io/en/latest/tutorials/intro_tutorial.html#adding-visualization), run: ``` - pip install -r requirements.txt + $ solara run app.py ``` -If your browser doesn't open automatically, point it to [http://127.0.0.1:8521/](http://127.0.0.1:8521/). When the visualization loads, press Reset, then Run. +If your browser doesn't open automatically, point it to [http://127.0.0.1:8765/](http://127.0.0.1:8765/). When the visualization loads, click on the Play button. ## Files -* ``boltzmann_wealth_model/model.py``: Final version of the model. -* ``boltzmann_wealth_model/server.py``: Code for the interactive visualization. -* ``run.py``: Launches the server. +* ``model.py``: Final version of the model. +* ``app.py``: Code for the interactive visualization. ## Optional -* ``boltzmann_wealth_model/app.py``: can be used to run the simulation via the streamlit interface. -* For this some additional packages like ``streamlit`` and ``altair`` needs to be installed. -* Once installed, the app can be opened in the browser using : ``streamlit run app.py`` +An optional visualization is also provided using Streamlit, which is another popular Python library for creating interactive web applications. + +To run the Streamlit app, you will need to install the `streamlit` and `altair` libraries: + +``` + $ pip install streamlit altair +``` + +Then, you can run the Streamlit app using the following command: + +``` + $ streamlit run st_app.py +``` ## Further Reading diff --git a/examples/boltzmann_wealth_model/app.py b/examples/boltzmann_wealth_model/app.py index 97f0f20c..199b3a1a 100644 --- a/examples/boltzmann_wealth_model/app.py +++ b/examples/boltzmann_wealth_model/app.py @@ -1,113 +1,65 @@ -import time - -import altair as alt -import pandas as pd -import streamlit as st -from boltzmann_wealth_model.model import BoltzmannWealthModel - -model = st.title("Boltzman Wealth Model") -num_agents = st.slider( - "Choose how many agents to include in the model", - min_value=1, - max_value=100, - value=50, +from mesa.visualization import ( + SolaraViz, + make_plot_measure, + make_space_matplotlib, ) -num_ticks = st.slider( - "Select number of Simulation Runs", min_value=1, max_value=100, value=50 -) -height = st.slider("Select Grid Height", min_value=10, max_value=100, step=10, value=15) -width = st.slider("Select Grid Width", min_value=10, max_value=100, step=10, value=20) -model = BoltzmannWealthModel(num_agents, height, width) - +from model import BoltzmannWealthModel -status_text = st.empty() -run = st.button("Run Simulation") +def agent_portrayal(agent): + size = 10 + color = "tab:red" + if agent.wealth > 0: + size = 50 + color = "tab:blue" + return {"size": size, "color": color} -if run: - tick = time.time() - step = 0 - # init grid - df_grid = pd.DataFrame() - df_gini = pd.DataFrame({"step": [0], "gini": [-1]}) - for x in range(width): - for y in range(height): - df_grid = pd.concat( - [df_grid, pd.DataFrame({"x": [x], "y": [y], "agent_count": 0})], - ignore_index=True, - ) - heatmap = ( - alt.Chart(df_grid) - .mark_point(size=100) - .encode(x="x", y="y", color=alt.Color("agent_count")) - .interactive() - .properties(width=800, height=600) - ) +model_params = { + "N": { + "type": "SliderInt", + "value": 50, + "label": "Number of agents:", + "min": 10, + "max": 100, + "step": 1, + }, + "width": 10, + "height": 10, +} - line = ( - alt.Chart(df_gini) - .mark_line(point=True) - .encode(x="step", y="gini") - .properties(width=800, height=600) - ) +# Create initial model instance +model1 = BoltzmannWealthModel(50, 10, 10) - # init progress bar - my_bar = st.progress(0, text="Simulation Progress") # progress - placeholder = st.empty() - st.subheader("Agent Grid") - chart = st.altair_chart(heatmap) - st.subheader("Gini Values") - line_chart = st.altair_chart(line) +# Create visualization elements. The visualization elements are solara components +# that receive the model instance as a "prop" and display it in a certain way. +# Under the hood these are just classes that receive the model instance. +# You can also author your own visualization elements, which can also be functions +# that receive the model instance and return a valid solara component. +SpaceGraph = make_space_matplotlib(agent_portrayal) +GiniPlot = make_plot_measure("Gini") - color_scale = alt.Scale( - domain=[0, 1, 2, 3, 4], range=["red", "cyan", "white", "white", "blue"] - ) - for i in range(num_ticks): - model.step() - my_bar.progress((i / num_ticks), text="Simulation progress") - placeholder.text("Step = %d" % i) - for cell in model.grid.coord_iter(): - cell_content, (x, y) = cell - agent_count = len(cell_content) - selected_row = df_grid[(df_grid["x"] == x) & (df_grid["y"] == y)] - df_grid.loc[selected_row.index, "agent_count"] = ( - agent_count # random.choice([1,2]) - ) - - df_gini = pd.concat( - [ - df_gini, - pd.DataFrame( - {"step": [i], "gini": [model.datacollector.model_vars["Gini"][i]]} - ), - ] - ) - # st.table(df_grid) - heatmap = ( - alt.Chart(df_grid) - .mark_circle(size=100) - .encode(x="x", y="y", color=alt.Color("agent_count", scale=color_scale)) - .interactive() - .properties(width=800, height=600) - ) - chart.altair_chart(heatmap) - - line = ( - alt.Chart(df_gini) - .mark_line(point=True) - .encode(x="step", y="gini") - .properties(width=800, height=600) - ) - line_chart.altair_chart(line) +# Create the SolaraViz page. This will automatically create a server and display the +# visualization elements in a web browser. +# Display it using the following command in the example directory: +# solara run app.py +# It will automatically update and display any changes made to this file +page = SolaraViz( + model1, + components=[SpaceGraph, GiniPlot], + model_params=model_params, + name="Boltzmann Wealth Model", +) +page # noqa - time.sleep(0.01) - tock = time.time() - st.success(f"Simulation completed in {tock - tick:.2f} secs") +# In a notebook environment, we can also display the visualization elements directly +# SpaceGraph(model1) +# GiniPlot(model1) - # st.subheader('Agent Grid') - # fig = px.imshow(agent_counts,labels={'color':'Agent Count'}) - # st.plotly_chart(fig) - # st.subheader('Gini value over sim ticks (Plotly)') - # chart = st.line_chart(model.datacollector.model_vars['Gini']) +# The plots will be static. If you want to pick up model steps, +# you have to make the model reactive first +# reactive_model = solara.reactive(model1) +# SpaceGraph(reactive_model) +# In a different notebook block: +# reactive_model.value.step() diff --git a/examples/boltzmann_wealth_model/boltzmann_wealth_model/__init__.py b/examples/boltzmann_wealth_model/boltzmann_wealth_model/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/examples/boltzmann_wealth_model/boltzmann_wealth_model/server.py b/examples/boltzmann_wealth_model/boltzmann_wealth_model/server.py deleted file mode 100644 index a49546ce..00000000 --- a/examples/boltzmann_wealth_model/boltzmann_wealth_model/server.py +++ /dev/null @@ -1,40 +0,0 @@ -import mesa - -from .model import BoltzmannWealthModel - - -def agent_portrayal(agent): - portrayal = {"Shape": "circle", "Filled": "true", "r": 0.5} - - if agent.wealth > 0: - portrayal["Color"] = "red" - portrayal["Layer"] = 0 - else: - portrayal["Color"] = "grey" - portrayal["Layer"] = 1 - portrayal["r"] = 0.2 - return portrayal - - -grid = mesa.visualization.CanvasGrid(agent_portrayal, 10, 10, 500, 500) -chart = mesa.visualization.ChartModule( - [{"Label": "Gini", "Color": "#0000FF"}], data_collector_name="datacollector" -) - -model_params = { - "N": mesa.visualization.Slider( - "Number of agents", - 100, - 2, - 200, - 1, - description="Choose how many agents to include in the model", - ), - "width": 10, - "height": 10, -} - -server = mesa.visualization.ModularServer( - BoltzmannWealthModel, [grid, chart], "Money Model", model_params -) -server.port = 8521 diff --git a/examples/boltzmann_wealth_model/boltzmann_wealth_model/model.py b/examples/boltzmann_wealth_model/model.py similarity index 100% rename from examples/boltzmann_wealth_model/boltzmann_wealth_model/model.py rename to examples/boltzmann_wealth_model/model.py diff --git a/examples/boltzmann_wealth_model/requirements.txt b/examples/boltzmann_wealth_model/requirements.txt index 25d263f4..95044bed 100644 --- a/examples/boltzmann_wealth_model/requirements.txt +++ b/examples/boltzmann_wealth_model/requirements.txt @@ -1 +1 @@ -mesa~=2.0 +mesa[viz]>=3.0.0b0 diff --git a/examples/boltzmann_wealth_model/run.py b/examples/boltzmann_wealth_model/run.py deleted file mode 100644 index f1767593..00000000 --- a/examples/boltzmann_wealth_model/run.py +++ /dev/null @@ -1,3 +0,0 @@ -from boltzmann_wealth_model.server import server - -server.launch(open_browser=True) diff --git a/examples/boltzmann_wealth_model/st_app.py b/examples/boltzmann_wealth_model/st_app.py new file mode 100644 index 00000000..665f8067 --- /dev/null +++ b/examples/boltzmann_wealth_model/st_app.py @@ -0,0 +1,113 @@ +import time + +import altair as alt +import pandas as pd +import streamlit as st +from model import BoltzmannWealthModel + +model = st.title("Boltzman Wealth Model") +num_agents = st.slider( + "Choose how many agents to include in the model", + min_value=1, + max_value=100, + value=50, +) +num_ticks = st.slider( + "Select number of Simulation Runs", min_value=1, max_value=100, value=50 +) +height = st.slider("Select Grid Height", min_value=10, max_value=100, step=10, value=15) +width = st.slider("Select Grid Width", min_value=10, max_value=100, step=10, value=20) +model = BoltzmannWealthModel(num_agents, height, width) + + +status_text = st.empty() +run = st.button("Run Simulation") + + +if run: + tick = time.time() + step = 0 + # init grid + df_grid = pd.DataFrame() + df_gini = pd.DataFrame({"step": [0], "gini": [-1]}) + for x in range(width): + for y in range(height): + df_grid = pd.concat( + [df_grid, pd.DataFrame({"x": [x], "y": [y], "agent_count": 0})], + ignore_index=True, + ) + + heatmap = ( + alt.Chart(df_grid) + .mark_point(size=100) + .encode(x="x", y="y", color=alt.Color("agent_count")) + .interactive() + .properties(width=800, height=600) + ) + + line = ( + alt.Chart(df_gini) + .mark_line(point=True) + .encode(x="step", y="gini") + .properties(width=800, height=600) + ) + + # init progress bar + my_bar = st.progress(0, text="Simulation Progress") # progress + placeholder = st.empty() + st.subheader("Agent Grid") + chart = st.altair_chart(heatmap) + st.subheader("Gini Values") + line_chart = st.altair_chart(line) + + color_scale = alt.Scale( + domain=[0, 1, 2, 3, 4], range=["red", "cyan", "white", "white", "blue"] + ) + for i in range(num_ticks): + model.step() + my_bar.progress((i / num_ticks), text="Simulation progress") + placeholder.text("Step = %d" % i) + for cell in model.grid.coord_iter(): + cell_content, (x, y) = cell + agent_count = len(cell_content) + selected_row = df_grid[(df_grid["x"] == x) & (df_grid["y"] == y)] + df_grid.loc[selected_row.index, "agent_count"] = ( + agent_count # random.choice([1,2]) + ) + + df_gini = pd.concat( + [ + df_gini, + pd.DataFrame( + {"step": [i], "gini": [model.datacollector.model_vars["Gini"][i]]} + ), + ] + ) + # st.table(df_grid) + heatmap = ( + alt.Chart(df_grid) + .mark_circle(size=100) + .encode(x="x", y="y", color=alt.Color("agent_count", scale=color_scale)) + .interactive() + .properties(width=800, height=600) + ) + chart.altair_chart(heatmap) + + line = ( + alt.Chart(df_gini) + .mark_line(point=True) + .encode(x="step", y="gini") + .properties(width=800, height=600) + ) + line_chart.altair_chart(line) + + time.sleep(0.01) + + tock = time.time() + st.success(f"Simulation completed in {tock - tick:.2f} secs") + + # st.subheader('Agent Grid') + # fig = px.imshow(agent_counts,labels={'color':'Agent Count'}) + # st.plotly_chart(fig) + # st.subheader('Gini value over sim ticks (Plotly)') + # chart = st.line_chart(model.datacollector.model_vars['Gini']) diff --git a/examples/boltzmann_wealth_model_experimental/Readme.md b/examples/boltzmann_wealth_model_experimental/Readme.md deleted file mode 100644 index fc27fdb2..00000000 --- a/examples/boltzmann_wealth_model_experimental/Readme.md +++ /dev/null @@ -1,44 +0,0 @@ -# Boltzmann Wealth Model (Tutorial) - -## Summary - -A simple model of agents exchanging wealth. All agents start with the same amount of money. Every step, each agent with one unit of money or more gives one unit of wealth to another random agent. This is the model described in the [Intro Tutorial](https://mesa.readthedocs.io/en/latest/tutorials/intro_tutorial.html), with the completed code. - -If you want to go over the step-by-step tutorial, please go and run the [Jupyter Notebook](https://github.com/projectmesa/mesa/blob/main/docs/tutorials/intro_tutorial.ipynb). The code here runs the finalized code in the last cells directly. - -As the model runs, the distribution of wealth among agents goes from being perfectly uniform (all agents have the same starting wealth), to highly skewed -- a small number have high wealth, more have none at all. - -## How to Run - -To follow the tutorial example, launch the Jupyter Notebook and run the code in ``Introduction to Mesa Tutorial Code.ipynb`` which you can find in the main mesa repo [here](https://github.com/projectmesa/mesa/blob/main/docs/tutorials/intro_tutorial.ipynb) - -Make sure to install the requirements first: - -``` - pip install -r requirements.txt -``` - -To launch the interactive server, as described in the [last section of the tutorial](https://mesa.readthedocs.io/en/latest/tutorials/intro_tutorial.html#adding-visualization), run: - -``` - $ solara run app.py -``` - -If your browser doesn't open automatically, point it to [http://127.0.0.1:8765/](http://127.0.0.1:8765/). When the visualization loads, click on the Play button. - - -## Files - -* ``model.py``: Final version of the model. -* ``app.py``: Code for the interactive visualization. - -## Further Reading - -The full tutorial describing how the model is built can be found at: -https://mesa.readthedocs.io/en/latest/tutorials/intro_tutorial.html - -This model is drawn from econophysics and presents a statistical mechanics approach to wealth distribution. Some examples of further reading on the topic can be found at: - -[Milakovic, M. A Statistical Equilibrium Model of Wealth Distribution. February, 2001.](https://editorialexpress.com/cgi-bin/conference/download.cgi?db_name=SCE2001&paper_id=214) - -[Dragulescu, A and Yakovenko, V. Statistical Mechanics of Money, Income, and Wealth: A Short Survey. November, 2002](http://arxiv.org/pdf/cond-mat/0211175v1.pdf) diff --git a/examples/boltzmann_wealth_model_experimental/__init__.py b/examples/boltzmann_wealth_model_experimental/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/examples/boltzmann_wealth_model_experimental/app.py b/examples/boltzmann_wealth_model_experimental/app.py deleted file mode 100644 index 199b3a1a..00000000 --- a/examples/boltzmann_wealth_model_experimental/app.py +++ /dev/null @@ -1,65 +0,0 @@ -from mesa.visualization import ( - SolaraViz, - make_plot_measure, - make_space_matplotlib, -) -from model import BoltzmannWealthModel - - -def agent_portrayal(agent): - size = 10 - color = "tab:red" - if agent.wealth > 0: - size = 50 - color = "tab:blue" - return {"size": size, "color": color} - - -model_params = { - "N": { - "type": "SliderInt", - "value": 50, - "label": "Number of agents:", - "min": 10, - "max": 100, - "step": 1, - }, - "width": 10, - "height": 10, -} - -# Create initial model instance -model1 = BoltzmannWealthModel(50, 10, 10) - -# Create visualization elements. The visualization elements are solara components -# that receive the model instance as a "prop" and display it in a certain way. -# Under the hood these are just classes that receive the model instance. -# You can also author your own visualization elements, which can also be functions -# that receive the model instance and return a valid solara component. -SpaceGraph = make_space_matplotlib(agent_portrayal) -GiniPlot = make_plot_measure("Gini") - -# Create the SolaraViz page. This will automatically create a server and display the -# visualization elements in a web browser. -# Display it using the following command in the example directory: -# solara run app.py -# It will automatically update and display any changes made to this file -page = SolaraViz( - model1, - components=[SpaceGraph, GiniPlot], - model_params=model_params, - name="Boltzmann Wealth Model", -) -page # noqa - - -# In a notebook environment, we can also display the visualization elements directly -# SpaceGraph(model1) -# GiniPlot(model1) - -# The plots will be static. If you want to pick up model steps, -# you have to make the model reactive first -# reactive_model = solara.reactive(model1) -# SpaceGraph(reactive_model) -# In a different notebook block: -# reactive_model.value.step() diff --git a/examples/boltzmann_wealth_model_experimental/model.py b/examples/boltzmann_wealth_model_experimental/model.py deleted file mode 100644 index ac091a6c..00000000 --- a/examples/boltzmann_wealth_model_experimental/model.py +++ /dev/null @@ -1,77 +0,0 @@ -import mesa - - -def compute_gini(model): - agent_wealths = [agent.wealth for agent in model.agents] - x = sorted(agent_wealths) - N = model.num_agents - B = sum(xi * (N - i) for i, xi in enumerate(x)) / (N * sum(x)) - return 1 + (1 / N) - 2 * B - - -class BoltzmannWealthModel(mesa.Model): - """A simple model of an economy where agents exchange currency at random. - - All the agents begin with one unit of currency, and each time step can give - a unit of currency to another agent. Note how, over time, this produces a - highly skewed distribution of wealth. - """ - - def __init__(self, N=100, width=10, height=10): - super().__init__() - self.num_agents = N - self.grid = mesa.space.MultiGrid(width, height, True) - - self.datacollector = mesa.DataCollector( - model_reporters={"Gini": compute_gini}, agent_reporters={"Wealth": "wealth"} - ) - # Create agents - for _ in range(self.num_agents): - a = MoneyAgent(self) - - # Add the agent to a random grid cell - x = self.random.randrange(self.grid.width) - y = self.random.randrange(self.grid.height) - self.grid.place_agent(a, (x, y)) - - self.running = True - self.datacollector.collect(self) - - def step(self): - self.agents.shuffle_do("step") - # collect data - self.datacollector.collect(self) - - def run_model(self, n): - for i in range(n): - self.step() - - -class MoneyAgent(mesa.Agent): - """An agent with fixed initial wealth.""" - - def __init__(self, model): - super().__init__(model) - self.wealth = 1 - - def move(self): - possible_steps = self.model.grid.get_neighborhood( - self.pos, moore=True, include_center=False - ) - new_position = self.random.choice(possible_steps) - self.model.grid.move_agent(self, new_position) - - def give_money(self): - cellmates = self.model.grid.get_cell_list_contents([self.pos]) - cellmates.pop( - cellmates.index(self) - ) # Ensure agent is not giving money to itself - if len(cellmates) > 0: - other = self.random.choice(cellmates) - other.wealth += 1 - self.wealth -= 1 - - def step(self): - self.move() - if self.wealth > 0: - self.give_money() diff --git a/examples/boltzmann_wealth_model_experimental/requirements.txt b/examples/boltzmann_wealth_model_experimental/requirements.txt deleted file mode 100644 index 479011dd..00000000 --- a/examples/boltzmann_wealth_model_experimental/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -mesa~=2.0 -solara -git+https://github.com/projectmesa/mesa-examples \ No newline at end of file diff --git a/examples/schelling/README.md b/examples/schelling/README.md index fe8971f6..b0116b55 100644 --- a/examples/schelling/README.md +++ b/examples/schelling/README.md @@ -16,21 +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 ``` -or - -Directly run the file ``run.py`` in the terminal. e.g. - -``` - $ python run.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/). @@ -40,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_experimental/app.py b/examples/schelling/app.py similarity index 66% rename from examples/schelling_experimental/app.py rename to examples/schelling/app.py index e2b45583..fb837351 100644 --- a/examples/schelling_experimental/app.py +++ b/examples/schelling/app.py @@ -1,4 +1,10 @@ -from mesa.visualization import Slider, SolaraViz, make_plot_measure +import solara +from mesa.visualization import ( + Slider, + SolaraViz, + make_plot_measure, + make_space_matplotlib, +) from model import Schelling @@ -6,7 +12,7 @@ def get_happy_agents(model): """ Display a text count of how many happy agents there are. """ - return f"Happy agents: {model.happy}" + return solara.Markdown(f"**Happy agents: {model.happy}**") def agent_portrayal(agent): @@ -27,7 +33,11 @@ def agent_portrayal(agent): page = SolaraViz( model1, - components=[HappyPlot, get_happy_agents], + components=[ + make_space_matplotlib(agent_portrayal), + make_plot_measure("happy"), + get_happy_agents, + ], model_params=model_params, ) page # noqa diff --git a/examples/schelling/requirements.txt b/examples/schelling/requirements.txt index da2b9972..79bc3555 100644 --- a/examples/schelling/requirements.txt +++ b/examples/schelling/requirements.txt @@ -1,3 +1,3 @@ jupyter matplotlib -mesa~=2.0 +mesa[viz]>=3.0.0b0 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 1f0d5f92..00000000 --- a/examples/schelling/server.py +++ /dev/null @@ -1,60 +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( - portrayal_method=schelling_draw, - grid_width=20, - grid_height=20, - canvas_width=500, - canvas_height=500, -) -happy_chart = mesa.visualization.ChartModule([{"Label": "happy", "Color": "Black"}]) - -model_params = { - "height": 20, - "width": 20, - "density": mesa.visualization.Slider( - name="Agent density", value=0.8, min_value=0.1, max_value=1.0, step=0.1 - ), - "minority_pc": mesa.visualization.Slider( - name="Fraction minority", value=0.2, min_value=0.00, max_value=1.0, step=0.05 - ), - "homophily": mesa.visualization.Slider( - name="Homophily", value=3, min_value=0, max_value=8, step=1 - ), - "radius": mesa.visualization.Slider( - name="Search Radius", value=1, min_value=1, max_value=5, step=1 - ), -} - -server = mesa.visualization.ModularServer( - model_cls=Schelling, - visualization_elements=[canvas_element, get_happy_agents, happy_chart], - name="Schelling Segregation Model", - model_params=model_params, -) diff --git a/examples/schelling_experimental/README.md b/examples/schelling_experimental/README.md deleted file mode 100644 index b0116b55..00000000 --- a/examples/schelling_experimental/README.md +++ /dev/null @@ -1,48 +0,0 @@ -# Schelling Segregation Model - -## Summary - -The Schelling segregation model is a classic agent-based model, demonstrating how even a mild preference for similar neighbors can lead to a much higher degree of segregation than we would intuitively expect. The model consists of agents on a square grid, where each grid cell can contain at most one agent. Agents come in two colors: red and blue. They are happy if a certain number of their eight possible neighbors are of the same color, and unhappy otherwise. Unhappy agents will pick a random empty cell to move to each step, until they are happy. The model keeps running until there are no unhappy agents. - -By default, the number of similar neighbors the agents need to be happy is set to 3. That means the agents would be perfectly happy with a majority of their neighbors being of a different color (e.g. a Blue agent would be happy with five Red neighbors and three Blue ones). Despite this, the model consistently leads to a high degree of segregation, with most agents ending up with no neighbors of a different color. - -## Installation - -To install the dependencies use pip and the requirements.txt in this directory. e.g. - -``` - $ pip install -r requirements.txt -``` - -## How to Run - -To run the model interactively, in this directory, run the following command - -``` - $ solara run app.py -``` - -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/). - -## How to Run without the GUI - -To run the model with the grid displayed as an ASCII text, run `python run_ascii.py` in this directory. - -## Files - -* ``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. -* ``analysis.ipynb``: Notebook demonstrating how to run experiments and parameter sweeps on the model. - -## Further Reading - -Schelling's original paper describing the model: - -[Schelling, Thomas C. Dynamic Models of Segregation. Journal of Mathematical Sociology. 1971, Vol. 1, pp 143-186.](https://www.stat.berkeley.edu/~aldous/157/Papers/Schelling_Seg_Models.pdf) - -An interactive, browser-based explanation and implementation: - -[Parable of the Polygons](http://ncase.me/polygons/), by Vi Hart and Nicky Case. diff --git a/examples/schelling_experimental/__init__.py b/examples/schelling_experimental/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/examples/schelling_experimental/analysis.ipynb b/examples/schelling_experimental/analysis.ipynb deleted file mode 100644 index 71d925c1..00000000 --- a/examples/schelling_experimental/analysis.ipynb +++ /dev/null @@ -1,457 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Schelling Segregation Model\n", - "\n", - "## Background\n", - "\n", - "The Schelling (1971) segregation model is a classic of agent-based modeling, demonstrating how agents following simple rules lead to the emergence of qualitatively different macro-level outcomes. Agents are randomly placed on a grid. There are two types of agents, one constituting the majority and the other the minority. All agents want a certain number (generally, 3) of their 8 surrounding neighbors to be of the same type in order for them to be happy. Unhappy agents will move to a random available grid space. While individual agents do not have a preference for a segregated outcome (e.g. they would be happy with 3 similar neighbors and 5 different ones), the aggregate outcome is nevertheless heavily segregated.\n", - "\n", - "## Implementation\n", - "\n", - "This is a demonstration of running a Mesa model in an IPython Notebook. The actual model and agent code are implemented in Schelling.py, in the same directory as this notebook. Below, we will import the model class, instantiate it, run it, and plot the time series of the number of happy agents." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "import matplotlib.pyplot as plt\n", - "\n", - "%matplotlib inline\n", - "\n", - "from model import Schelling" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now we instantiate a model instance: a 10x10 grid, with an 80% change of an agent being placed in each cell, approximately 20% of agents set as minorities, and agents wanting at least 3 similar neighbors." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "model = Schelling(10, 10, 0.8, 0.2, 3)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "We want to run the model until all the agents are happy with where they are. However, there's no guarantee that a given model instantiation will *ever* settle down. So let's run it for either 100 steps or until it stops on its own, whichever comes first:" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "100\n" - ] - } - ], - "source": [ - "while model.running and model.steps < 100:\n", - " model.step()\n", - "print(model.steps) # Show how many steps have actually run" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "The model has a DataCollector object, which checks and stores how many agents are happy at the end of each step. It can also generate a pandas DataFrame of the data it has collected:" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], - "source": [ - "model_out = model.datacollector.get_model_vars_dataframe()" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
happy
00
173
267
372
472
\n", - "
" - ], - "text/plain": [ - " happy\n", - "0 0\n", - "1 73\n", - "2 72\n", - "3 73\n", - "4 72" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "model_out.head()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Finally, we can plot the 'happy' series:" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - }, - { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAGw5JREFUeJzt3XuYFPWd7/H3dy5cR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Xow6dmdWSC/N73f3BZPbG1o9iye2mnqqvDE4HLjKz1eTaaGeTG7EPSj6aQ+Vt63XAOndfnEzPIxfwlbydzwHecPcmd28BHgROo7K3c6u07Vq2TAsh0Cv+YtRJ73g2sMLdf9zmoQVAfXK/Hpjf3bWVi7t/x91Huftoctv0cXf/IvAEcHGyWKWt89vAWjM7Npk1BXiZCt7O5Fotk82sX/I+b13nit3ObaRt1wXA9ORol8nAe62tmU5z94P+H/Bp4C/A68D3erqeMqzfJ8l95HoBWJb8+zS5nvIiYGVyO6Snay3T+v934OHk/ljgGeA14AGgd0/X18XrOgFoTLb1vwODK307A/8AvAIsB+4BelfadgbmkNtH0EJuBD4jbbuSa7n8NMmzF8kdAdQldehMURGRChFCy0VEREqgQBcRqRAKdBGRCqFAFxGpEAp0EZEKoUAXEakQCnQRkQqhQBcRqRD/H51mCpU1j1b7AAAAAElFTkSuQmCC\n", - "text/plain": [ - "
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "model_out.happy.plot()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "For testing purposes, here is a table giving each agent's x and y values at each step." - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [], - "source": [ - "x_positions = model.datacollector.get_agent_vars_dataframe()" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
\n", - "\n", - "\n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - " \n", - "
xy
StepAgentID
0(0, 0)01
(0, 1)89
(0, 2)52
(0, 3)00
(0, 4)17
\n", - "
" - ], - "text/plain": [ - " x y\n", - "Step AgentID \n", - "0 (0, 0) 0 1\n", - " (0, 1) 8 9\n", - " (0, 2) 5 2\n", - " (0, 3) 0 0\n", - " (0, 4) 1 7" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x_positions.head()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Effect of Homophily on segregation\n", - "\n", - "Now, we can do a parameter sweep to see how segregation changes with homophily.\n", - "\n", - "First, we create a function which takes a model instance and returns what fraction of agents are segregated -- that is, have no neighbors of the opposite type." - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [], - "source": [ - "from mesa.batchrunner import BatchRunner" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [], - "source": [ - "def get_segregation(model):\n", - " \"\"\"\n", - " Find the % of agents that only have neighbors of their same type.\n", - " \"\"\"\n", - " segregated_agents = 0\n", - " for agent in model.agents:\n", - " segregated = True\n", - " for neighbor in model.grid.iter_neighbors(agent.pos, True):\n", - " if neighbor.type != agent.type:\n", - " segregated = False\n", - " break\n", - " if segregated:\n", - " segregated_agents += 1\n", - " return segregated_agents / len(model.agents)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Now, we set up the batch run, with a dictionary of fixed and changing parameters. Let's hold everything fixed except for Homophily." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [], - "source": [ - "fixed_params = {\"height\": 10, \"width\": 10, \"density\": 0.8, \"minority_pc\": 0.2}\n", - "variable_parms = {\"homophily\": range(1, 9)}" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [], - "source": [ - "model_reporters = {\"Segregated_Agents\": get_segregation}" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [], - "source": [ - "param_sweep = BatchRunner(\n", - " Schelling,\n", - " variable_parameters=variable_parms,\n", - " fixed_parameters=fixed_params,\n", - " iterations=10,\n", - " max_steps=200,\n", - " model_reporters=model_reporters,\n", - ")" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "80it [00:15, 3.13it/s]\n" - ] - } - ], - "source": [ - "param_sweep.run_all()" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [], - "source": [ - "df = param_sweep.get_model_vars_dataframe()" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAHQhJREFUeJzt3X+QVfd53/H3wwpGK1YBxVLX1gUbRUPIIG8lwhYp1Yy7KzsFOQnasUUGGmnqjFUmM0K2Y5UWxhpZUtWKmio/ptVkQpXWzTjWVsHqFts7wR3DTmM1UhBCeI0QKcKqYImRYgPW2uuwLE//2Hvh7nJ/LXvPnvOc/bxmGO0599y9j+6e+9zveb4/jrk7IiKSL3PSDkBERJpPyV1EJIeU3EVEckjJXUQkh5TcRURySMldRCSHlNxFRHJIyV1EJIeU3EVEcuiqtF74+uuv9yVLllzx83/yk58wf/785gWUIMWanEjxRooVYsU7m2Ldv3//37n7DXUPdPdU/q1cudKnY+/evdN6/kxSrMmJFG+kWN1jxTubYgVe8QZyrMoyIiI5pOQuIpJDSu4iIjmk5C4ikkNK7iIiOaTkLiKSQ0ruIiI5pOQuIpJDSu4iIjmk5C4h9R0Y4s5texgcOsud2/bQd2Ao7ZBEMiW1tWVErlTfgSG2vjDIyOgYLIahMyNsfWEQgJ4VhZSjE8kGtdwlnO27j4wn9jIjo2Ns330kpYhEskfJXcI5eWZ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rYEWAG8nG4k1RXLHK8B7wD/y90zGyvwB8C/Ai6kHUiDHPiWme03s41pB1PDLwDvAv+1WPJ61szmpx1Ug9YDz6UdRDXuPgT8B+Bt4G+Bs+7+raReT8l9BphZG/A14HPu/uO046nG3cfc/TZgEbDKzDJZ9jKzXwfecff9accyBXe6+y8DdwMPFsuLWXQV8MvAH7n7CuAnwJZ0Q6qvWD5aC/x52rFUY2bXAfcANwE3AvPN7L6kXk/JPWHF+vXXgD9z9xfSjqcRxcvwAWBNyqFUcyewtljH7gXuMrOvpBtSbe5+svjfd4D/AaxKN6KqTgAnyq7adjKe7LPubuBVdz+VdiA1fAz4vru/6+6jwAvAP07qxZTcE1TspPwT4LC7/17a8dRiZjeY2cLiz62Mn4hvpBtVZe6+1d0XufsSxi/F97h7Yi2g6TKz+cUOdYoljn8KZHK0l7v/ADhuZsuKuz4KZG4AQAUbyHBJpuht4A4zu6aYGz7KeD9cIsIldzN7DvgrYJmZnTCzT6cdUw13Avcz3rIsDdX6eNpBVfEBYK+ZfRfYx3jNPfNDDINoB75jZgeBvwa+6e5/kXJMtTwE/FnxXLgN+Hcpx1OTmV0D/CrjLeHMKl4N7QReBQYZz7+JzVYNNxRSRETqC9dyFxGR+pTcRURySMldRCSHlNxFRHJIyV1EJIeU3EVEckjJXUQkh5TcRURy6P8DovBSQVnzoQgAAAAASUVORK5CYII=\n", - "text/plain": [ - "
" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "plt.scatter(df.homophily, df.Segregated_Agents)\n", - "plt.grid(True)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.9.9" - }, - "widgets": { - "state": {}, - "version": "1.1.2" - } - }, - "nbformat": 4, - "nbformat_minor": 1 -} diff --git a/examples/schelling_experimental/model.py b/examples/schelling_experimental/model.py deleted file mode 100644 index d7239a97..00000000 --- a/examples/schelling_experimental/model.py +++ /dev/null @@ -1,72 +0,0 @@ -import mesa - - -class SchellingAgent(mesa.Agent): - """ - Schelling segregation agent - """ - - def __init__(self, model, agent_type): - """ - Create a new Schelling agent. - - Args: - agent_type: Indicator for the agent's type (minority=1, majority=0) - """ - super().__init__(model) - self.type = agent_type - - def step(self): - similar = 0 - for neighbor in self.model.grid.iter_neighbors(self.pos, True): - if neighbor.type == self.type: - similar += 1 - - # If unhappy, move: - if similar < self.model.homophily: - self.model.grid.move_to_empty(self) - else: - self.model.happy += 1 - - -class Schelling(mesa.Model): - """ - Model class for the Schelling segregation model. - """ - - def __init__(self, width=20, height=20, density=0.8, minority_pc=0.2, homophily=3): - super().__init__() - self.width = width - self.height = height - self.homophily = homophily - - self.grid = mesa.space.SingleGrid(width, height, torus=True) - - self.happy = 0 - self.datacollector = mesa.DataCollector( - {"happy": "happy"}, # Model-level count of happy agents - ) - - # Set up agents - # We use a grid iterator that returns - # the coordinates of a cell as well as - # its contents. (coord_iter) - for _, pos in self.grid.coord_iter(): - if self.random.random() < density: - agent_type = 1 if self.random.random() < minority_pc else 0 - agent = SchellingAgent(self, agent_type) - self.grid.place_agent(agent, pos) - - self.datacollector.collect(self) - - def step(self): - """ - Run one step of the model. If All agents are happy, halt the model. - """ - self.happy = 0 # Reset counter of happy agents - self.agents.shuffle_do("step") - # collect data - self.datacollector.collect(self) - - if self.happy == len(self.agents): - self.running = False diff --git a/examples/schelling_experimental/requirements.txt b/examples/schelling_experimental/requirements.txt deleted file mode 100644 index 02cb0c47..00000000 --- a/examples/schelling_experimental/requirements.txt +++ /dev/null @@ -1,5 +0,0 @@ -jupyter -matplotlib -mesa~=2.0 -solara -git+https://github.com/projectmesa/mesa-examples \ No newline at end of file diff --git a/examples/schelling_experimental/run_ascii.py b/examples/schelling_experimental/run_ascii.py deleted file mode 100644 index 460fabbb..00000000 --- a/examples/schelling_experimental/run_ascii.py +++ /dev/null @@ -1,48 +0,0 @@ -import mesa -from model import Schelling - - -class SchellingTextVisualization(mesa.visualization.TextVisualization): - """ - ASCII visualization for schelling model - """ - - def __init__(self, model): - """ - Create new Schelling ASCII visualization. - """ - self.model = model - - grid_viz = mesa.visualization.TextGrid(self.model.grid, self.print_ascii_agent) - happy_viz = mesa.visualization.TextData(self.model, "happy") - self.elements = [grid_viz, happy_viz] - - @staticmethod - def print_ascii_agent(a): - """ - Minority agents are X, Majority are O. - """ - if a.type == 0: - return "O" - if a.type == 1: - return "X" - - -if __name__ == "__main__": - model_params = { - "height": 20, - "width": 20, - # Agent density, from 0.8 to 1.0 - "density": 0.8, - # Fraction minority, from 0.2 to 1.0 - "minority_pc": 0.2, - # Homophily, from 3 to 8 - "homophily": 3, - } - - model = Schelling(**model_params) - viz = SchellingTextVisualization(model) - for i in range(10): - print("Step:", i) - viz.step() - print("---") diff --git a/setup.cfg b/setup.cfg index 380caba4..fcc4451c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [options] package_dir = - mesa_models.boltzmann_wealth_model = examples/boltzmann_wealth_model/boltzmann_wealth_model + mesa_models.boltzmann_wealth_model = examples/boltzmann_wealth_model mesa_models.schelling = examples/schelling mesa_models.epstein_civil_violence = examples/epstein_civil_violence/epstein_civil_violence mesa_models.wolf_sheep = examples/wolf_sheep/wolf_sheep