Skip to content

Commit 0d2d6e2

Browse files
rhttpike3
authored andcommitted
docs: Convert overview.rst -> overview.md via rst2myst
1 parent 1e443b9 commit 0d2d6e2

File tree

1 file changed

+95
-105
lines changed

1 file changed

+95
-105
lines changed
Lines changed: 95 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,156 +1,146 @@
1-
Mesa Overview
2-
=============
1+
# Mesa Overview
32

43
Mesa is a modular framework for building, analyzing and visualizing agent-based models.
54

65
**Agent-based models** are computer simulations involving multiple entities (the agents) acting and interacting with one another based on their programmed behavior. Agents can be used to represent living cells, animals, individual humans, even entire organizations or abstract entities. Sometimes, we may have an understanding of how the individual components of a system behave, and want to see what system-level behaviors and effects emerge from their interaction. Other times, we may have a good idea of how the system overall behaves, and want to figure out what individual behaviors explain it. Or we may want to see how to get agents to cooperate or compete most effectively. Or we may just want to build a cool toy with colorful little dots moving around.
76

8-
9-
Mesa Modules
10-
------------
7+
## Mesa Modules
118

129
Mesa is modular, meaning that its modeling, analysis and visualization components are kept separate but intended to work together. The modules are grouped into three categories:
1310

1411
1. **Modeling:** Modules used to build the models themselves: a model and agent classes, a scheduler to determine the sequence in which the agents act, and space for them to move around on.
1512
2. **Analysis:** Tools to collect data generated from your model, or to run it multiple times with different parameter values.
1613
3. **Visualization:** Classes to create and launch an interactive model visualization, using a server with a JavaScript interface.
1714

18-
19-
Modeling modules
20-
~~~~~~~~~~~~~~~~
15+
### Modeling modules
2116

2217
Most models consist of one class to represent the model itself; one class (or more) for agents; a scheduler to handle time (what order the agents act in), and possibly a space for the agents to inhabit and move through. These are implemented in Mesa's modeling modules:
2318

24-
* ``mesa.Model``, ``mesa.Agent``
25-
* `mesa.time <apis/time.html>`_
26-
* `mesa.space <apis/space.html>`_
19+
- `mesa.Model`, `mesa.Agent`
20+
- [mesa.time](apis/time.html)
21+
- [mesa.space](apis/space.html)
2722

2823
The skeleton of a model might look like this:
2924

30-
.. code:: python
31-
32-
import mesa
33-
34-
class MyAgent(mesa.Agent):
35-
def __init__(self, name, model):
36-
super().__init__(name, model)
37-
self.name = name
38-
39-
def step(self):
40-
print("{} activated".format(self.name))
41-
# Whatever else the agent does when activated
42-
43-
class MyModel(mesa.Model):
44-
def __init__(self, n_agents):
45-
super().__init__()
46-
self.schedule = mesa.time.RandomActivation(self)
47-
self.grid = mesa.space.MultiGrid(10, 10, torus=True)
48-
for i in range(n_agents):
49-
a = MyAgent(i, self)
50-
self.schedule.add(a)
51-
coords = (self.random.randrange(0, 10), self.random.randrange(0, 10))
52-
self.grid.place_agent(a, coords)
53-
54-
def step(self):
55-
self.schedule.step()
25+
```python
26+
import mesa
27+
28+
class MyAgent(mesa.Agent):
29+
def __init__(self, name, model):
30+
super().__init__(name, model)
31+
self.name = name
32+
33+
def step(self):
34+
print("{} activated".format(self.name))
35+
# Whatever else the agent does when activated
36+
37+
class MyModel(mesa.Model):
38+
def __init__(self, n_agents):
39+
super().__init__()
40+
self.schedule = mesa.time.RandomActivation(self)
41+
self.grid = mesa.space.MultiGrid(10, 10, torus=True)
42+
for i in range(n_agents):
43+
a = MyAgent(i, self)
44+
self.schedule.add(a)
45+
coords = (self.random.randrange(0, 10), self.random.randrange(0, 10))
46+
self.grid.place_agent(a, coords)
47+
48+
def step(self):
49+
self.schedule.step()
50+
```
5651

5752
If you instantiate a model and run it for one step, like so:
5853

59-
.. code:: python
54+
```python
55+
model = MyModel(5)
56+
model.step()
57+
```
6058

61-
model = MyModel(5)
62-
model.step()
59+
You should see agents 0-4, activated in random order. See the [tutorial](tutorials/intro_tutorial.html) or API documentation for more detail on how to add model functionality.
6360

64-
You should see agents 0-4, activated in random order. See the `tutorial <tutorials/intro_tutorial.html>`_ or API documentation for more detail on how to add model functionality.
61+
To bootstrap a new model install mesa and run `mesa startproject`
6562

66-
To bootstrap a new model install mesa and run ``mesa startproject``
67-
68-
Analysis modules
69-
~~~~~~~~~~~~~~~~
63+
### Analysis modules
7064

7165
If you're using modeling for research, you'll want a way to collect the data each model run generates. You'll probably also want to run the model multiple times, to see how some output changes with different parameters. Data collection and batch running are implemented in the appropriately-named analysis modules:
7266

73-
* `mesa.datacollection <apis/datacollection.html>`_
74-
* `mesa.batchrunner <apis/batchrunner.html>`_
67+
- [mesa.datacollection](apis/datacollection.html)
68+
- [mesa.batchrunner](apis/batchrunner.html)
7569

7670
You'd add a data collector to the model like this:
7771

78-
.. code:: python
79-
80-
import mesa
72+
```python
73+
import mesa
8174

82-
# ...
75+
# ...
8376

84-
class MyModel(mesa.Model):
85-
def __init__(self, n_agents):
86-
# ...
87-
self.dc = mesa.DataCollector(model_reporters={"agent_count":
88-
lambda m: m.schedule.get_agent_count()},
89-
agent_reporters={"name": lambda a: a.name})
77+
class MyModel(mesa.Model):
78+
def __init__(self, n_agents):
79+
# ...
80+
self.dc = mesa.DataCollector(model_reporters={"agent_count":
81+
lambda m: m.schedule.get_agent_count()},
82+
agent_reporters={"name": lambda a: a.name})
9083

91-
def step(self):
92-
self.schedule.step()
93-
self.dc.collect(self)
84+
def step(self):
85+
self.schedule.step()
86+
self.dc.collect(self)
87+
```
9488

95-
The data collector will collect the specified model- and agent-level data at each step of the model. After you're done running it, you can extract the data as a `pandas <http://pandas.pydata.org/>`_ DataFrame:
96-
97-
.. code:: python
98-
99-
model = MyModel(5)
100-
for t in range(10):
101-
model.step()
102-
model_df = model.dc.get_model_vars_dataframe()
103-
agent_df = model.dc.get_agent_vars_dataframe()
89+
The data collector will collect the specified model- and agent-level data at each step of the model. After you're done running it, you can extract the data as a [pandas](http://pandas.pydata.org/) DataFrame:
10490

91+
```python
92+
model = MyModel(5)
93+
for t in range(10):
94+
model.step()
95+
model_df = model.dc.get_model_vars_dataframe()
96+
agent_df = model.dc.get_agent_vars_dataframe()
97+
```
10598

10699
To batch-run the model while varying, for example, the n_agents parameter, you'd use the `batch_run` function:
107100

108-
.. code:: python
109-
110-
import mesa
111-
112-
parameters = {"n_agents": range(1, 20)}
113-
mesa.batch_run(
114-
MyModel,
115-
parameters,
116-
max_steps=10,
117-
)
101+
```python
102+
import mesa
118103

104+
parameters = {"n_agents": range(1, 20)}
105+
mesa.batch_run(
106+
MyModel,
107+
parameters,
108+
max_steps=10,
109+
)
110+
```
119111

120112
As with the data collector, once the runs are all over, you can extract the data as a data frame.
121113

122-
.. code:: python
114+
```python
115+
batch_df = batch_run.get_model_vars_dataframe()
116+
```
123117

124-
batch_df = batch_run.get_model_vars_dataframe()
125-
126-
127-
Visualization modules
128-
~~~~~~~~~~~~~~~~~~~~~
118+
### Visualization modules
129119

130120
Finally, you may want to directly observe your model as it runs. Mesa's main visualization tool uses a small local web server to render the model in a browser, using JavaScript. There are different components for drawing different types of data: for example, grids for drawing agents moving around on a grid, or charts for showing how some data changes as the model runs. A few core modules are:
131121

132-
* mesa.visualization.ModularVisualization
133-
* mesa.visualization.modules
122+
- mesa.visualization.ModularVisualization
123+
- mesa.visualization.modules
134124

135125
To quickly spin up a model visualization, you might do something like:
136126

137-
.. code:: python
138-
139-
import mesa
140-
141-
def agent_portrayal(agent):
142-
portrayal = {"Shape": "circle",
143-
"Filled": "true",
144-
"Layer": 0,
145-
"Color": "red",
146-
"r": 0.5}
147-
return portrayal
148-
149-
grid = mesa.visualization.CanvasGrid(agent_portrayal, 10, 10, 500, 500)
150-
server = mesa.visualization.ModularServer(MyModel,
151-
[grid],
152-
"My Model",
153-
{'n_agents': 10})
154-
server.launch()
127+
```python
128+
import mesa
129+
130+
def agent_portrayal(agent):
131+
portrayal = {"Shape": "circle",
132+
"Filled": "true",
133+
"Layer": 0,
134+
"Color": "red",
135+
"r": 0.5}
136+
return portrayal
137+
138+
grid = mesa.visualization.CanvasGrid(agent_portrayal, 10, 10, 500, 500)
139+
server = mesa.visualization.ModularServer(MyModel,
140+
[grid],
141+
"My Model",
142+
{'n_agents': 10})
143+
server.launch()
144+
```
155145

156146
This will launch the browser-based visualization, on the default port 8521.

0 commit comments

Comments
 (0)