Skip to content

Commit 913dfb8

Browse files
authored
docs: Rewrite howto.md using ChatGPT for clarity and conciseness (#2037)
1 parent 7f10167 commit 913dfb8

File tree

1 file changed

+45
-27
lines changed

1 file changed

+45
-27
lines changed

docs/howto.md

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,83 @@
11
# How-to Guide
22

3-
Here you can find code that allows you to get to get started on common tasks in Mesa.
3+
This guide provides concise instructions and examples to help you start with common tasks in Mesa.
44

55
## Models with Discrete Time
66

7-
If you have `Multiple` type agents and one of them has time attribute you can still build a model that is run by discrete time. In this example, each step of the model, and the agents have a time attribute that is equal to the discrete time to run its own step.
7+
For models involving agents of multiple types, including those with a time attribute, you can construct a discrete-time model. This setup allows each agent to perform actions in steps that correspond to the model's discrete time.
88

9+
Example:
910
```python
1011
if self.model.schedule.time in self.discrete_time:
1112
self.model.space.move_agent(self, new_pos)
1213
```
1314

14-
## Implementing Model Level Functions in Staged Activation
15+
## Implementing Model-Level Functions with Staged Activation
1516

16-
In staged activation, if you may want a function to be implemented only on the model level and not at the level of agents.
17-
For such functions, include the prefix "model." before the model function name, when defining the function list.
18-
For example, consider a central employment exchange which adjust the wage rate common to all laborers
19-
in the direction of excess demand.
17+
In staged activation scenarios, to designate functions that should only operate
18+
at the model level (and not at the agent level), prepend the function name with
19+
"model." in the function list definition. This approach is useful for
20+
model-wide operations, like adjusting a wage rate based on demand.
2021

22+
Example:
2123
```python
22-
stage_list=[Send_Labour_Supply, Send_Labour_Demand, model.Adjust_Wage_Rate] self.schedule = StagedActivation(self,stage_list,shuffle=True)
24+
stage_list = [Send_Labour_Supply, Send_Labour_Demand, model.Adjust_Wage_Rate]
25+
self.schedule = StagedActivation(self, stage_list, shuffle=True)
2326
```
2427

2528
## Using `numpy.random`
2629

27-
Sometimes you need to use `numpy`'s `random` library, for example to get a Poisson distribution.
30+
To incorporate `numpy`'s random functions, such as for generating a Poisson
31+
distribution, initialize a random number generator in your model's constructor.
2832

33+
Example:
2934
```python
30-
class MyModel(Model):
31-
def __init__(self, ...):
35+
import mesa
36+
import numpy as np
37+
38+
class MyModel(mesa.Model):
39+
def __init__(self, seed=None):
3240
super().__init__()
3341
self.random = np.random.default_rng(seed)
3442
```
3543

36-
And just use `numpy`'s random as usual, e.g. `self.random.poisson()`.
44+
Usage example:
45+
```python
46+
lambda_value = 5
47+
sample = self.random.poisson(lambda_value)
48+
```
3749

38-
## Using multi-process `batch_run` on Windows
50+
## Multi-process `batch_run` on Windows
3951

40-
You will have an issue with `batch_run` and `number_processes = None`. Your cell will
41-
show no progress, and in your terminal you will receive *AttributeError: Can't get attribute 'MoneyModel' on
42-
\<module '\_\_main\_\_' (built-in)>*. One way to overcome this is to take your code outside of Jupyter and adjust the above
43-
code as follows.
52+
When using `batch_run` with `number_processes = None` on Windows, you might
53+
encounter progress display issues or `AttributeError: Can't get attribute
54+
'MoneyModel' on <module '__main__' (built-in)>`. To resolve this, run
55+
your code outside of Jupyter notebooks and use the following pattern,
56+
incorporating `freeze_support()` for multiprocessing support.
4457

58+
Example:
4559
```python
60+
from mesa.batchrunner import batch_run
4661
from multiprocessing import freeze_support
4762

4863
params = {"width": 10, "height": 10, "N": range(10, 500, 10)}
4964

5065
if __name__ == '__main__':
5166
freeze_support()
5267
results = batch_run(
53-
MoneyModel,
54-
parameters=params,
55-
iterations=5,
56-
max_steps=100,
57-
number_processes=None,
58-
data_collection_period=1,
59-
display_progress=True,
68+
MoneyModel,
69+
parameters=params,
70+
iterations=5,
71+
max_steps=100,
72+
number_processes=None,
73+
data_collection_period=1,
74+
display_progress=True,
6075
)
6176
```
6277

63-
If you would still like to run your code in Jupyter you will need to adjust the cell as noted above. Then you can
64-
you can add the [nbmultitask library](https://nbviewer.org/github/micahscopes/nbmultitask/blob/39b6f31b047e8a51a0fcb5c93ae4572684f877ce/examples.ipynb)
65-
or look at this [stackoverflow](https://stackoverflow.com/questions/50937362/multiprocessing-on-python-3-jupyter).
78+
If you would still like to run your code in Jupyter, adjust your code as
79+
described and consider integrating external libraries like
80+
[nbmultitask](https://nbviewer.org/github/micahscopes/nbmultitask/blob/39b6f31b047e8a51a0fcb5c93ae4572684f877ce/examples.ipynb)
81+
or refer to [Stack
82+
Overflow](https://stackoverflow.com/questions/50937362/multiprocessing-on-python-3-jupyter)
83+
for multiprocessing tips.

0 commit comments

Comments
 (0)