Skip to content

Commit

Permalink
Move examples from mesa repo to mesa-examples repo.
Browse files Browse the repository at this point in the history
  • Loading branch information
jackiekazil authored and rht committed Dec 5, 2022
1 parent b3760b5 commit 3aa8185
Show file tree
Hide file tree
Showing 151 changed files with 4,907 additions and 5,969 deletions.
Binary file removed examples/.DS_Store
Binary file not shown.
6 changes: 0 additions & 6 deletions examples/.ipynb_checkpoints/Untitled-checkpoint.ipynb

This file was deleted.

8 changes: 4 additions & 4 deletions examples/Boltzmann_Wealth_Model/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ To follow the tutorial examples, launch the Jupyter Notebook and run the code in
To launch the interactive server, as described in the [last section of the tutorial](http://mesa.readthedocs.io/en/latest/intro-tutorial.html#adding-visualization), run:

```
$ python Viz_MoneyModel.py
$ python viz_money_model.py
```

Then open your browser to [http://127.0.0.1:8889/](http://127.0.0.1:8889/) and press Reset, then Run.
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.


## Files

* ``Introduction to Mesa Tutorial Code.ipynb``: Jupyter Notebook with all the steps as described in the tutorial.
* ``MoneyModel.py``: Final version of the model.
* ``Viz_MoneyModel.py``: Creates and launches interactive visualization.
* ``money_model.py``: Final version of the model.
* ``viz_money_model.py``: Creates and launches interactive visualization.

## Further Reading

Expand Down
30 changes: 0 additions & 30 deletions examples/Boltzmann_Wealth_Model/Viz_MoneyModel.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,51 +1,54 @@
import random

from mesa import Agent, Model
from mesa.time import RandomActivation
from mesa.space import MultiGrid
from mesa.datacollection import DataCollector
import mesa


def compute_gini(model):
agent_wealths = [agent.wealth for agent in model.schedule.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)
return 1 + (1 / N) - 2 * B


class BoltzmannWealthModel(mesa.Model):
"""A simple model of an economy where agents exchange currency at random.
class MoneyModel(Model):
"""A model with some number of agents."""
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, width, height):
def __init__(self, N=100, width=10, height=10):
self.num_agents = N
self.running = True
self.grid = MultiGrid(height, width, True)
self.schedule = RandomActivation(self)
self.datacollector = DataCollector(
model_reporters={"Gini": compute_gini},
agent_reporters={"Wealth": lambda a: a.wealth}
self.grid = mesa.space.MultiGrid(width, height, True)
self.schedule = mesa.time.RandomActivation(self)
self.datacollector = mesa.DataCollector(
model_reporters={"Gini": compute_gini}, agent_reporters={"Wealth": "wealth"}
)
# Create agents
for i in range(self.num_agents):
a = MoneyAgent(i, self)
self.schedule.add(a)
# Add the agent to a random grid cell
x = random.randrange(self.grid.width)
y = random.randrange(self.grid.height)
x = self.random.randrange(self.grid.width)
y = self.random.randrange(self.grid.height)
self.grid.place_agent(a, (x, y))

def step(self):
self.running = True
self.datacollector.collect(self)

def step(self):
self.schedule.step()
# collect data
self.datacollector.collect(self)

def run_model(self, n):
for i in range(n):
self.step()


class MoneyAgent(Agent):
""" An agent with fixed initial wealth."""
class MoneyAgent(mesa.Agent):
"""An agent with fixed initial wealth."""

def __init__(self, unique_id, model):
super().__init__(unique_id, model)
self.wealth = 1
Expand All @@ -54,13 +57,13 @@ def move(self):
possible_steps = self.model.grid.get_neighborhood(
self.pos, moore=True, include_center=False
)
new_position = random.choice(possible_steps)
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])
if len(cellmates) > 1:
other = random.choice(cellmates)
other = self.random.choice(cellmates)
other.wealth += 1
self.wealth -= 1

Expand Down
40 changes: 40 additions & 0 deletions examples/Boltzmann_Wealth_Model/boltzmann_wealth_model/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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
3 changes: 3 additions & 0 deletions examples/Boltzmann_Wealth_Model/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from boltzmann_wealth_model.server import server

server.launch()
50 changes: 0 additions & 50 deletions examples/ColorPatches/color_patches/server.py

This file was deleted.

12 changes: 0 additions & 12 deletions examples/ConwaysGameOfLife/game_of_life/server.py

This file was deleted.

3 changes: 0 additions & 3 deletions examples/ConwaysGameOfLife/run.py

This file was deleted.

This file was deleted.

133 changes: 0 additions & 133 deletions examples/EpsteinCivilViolence/Epstein Civil Violence.ipynb

This file was deleted.

46 changes: 0 additions & 46 deletions examples/EpsteinCivilViolence/civil_violence/server.py

This file was deleted.

3 changes: 0 additions & 3 deletions examples/EpsteinCivilViolence/run.py

This file was deleted.

Loading

0 comments on commit 3aa8185

Please sign in to comment.