|
1 | 1 | # How-to Guide
|
2 | 2 |
|
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. |
4 | 4 |
|
5 | 5 | ## Models with Discrete Time
|
6 | 6 |
|
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. |
8 | 8 |
|
| 9 | +Example: |
9 | 10 | ```python
|
10 | 11 | if self.model.schedule.time in self.discrete_time:
|
11 | 12 | self.model.space.move_agent(self, new_pos)
|
12 | 13 | ```
|
13 | 14 |
|
14 |
| -## Implementing Model Level Functions in Staged Activation |
| 15 | +## Implementing Model-Level Functions with Staged Activation |
15 | 16 |
|
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. |
20 | 21 |
|
| 22 | +Example: |
21 | 23 | ```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) |
23 | 26 | ```
|
24 | 27 |
|
25 | 28 | ## Using `numpy.random`
|
26 | 29 |
|
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. |
28 | 32 |
|
| 33 | +Example: |
29 | 34 | ```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): |
32 | 40 | super().__init__()
|
33 | 41 | self.random = np.random.default_rng(seed)
|
34 | 42 | ```
|
35 | 43 |
|
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 | +``` |
37 | 49 |
|
38 |
| -## Using multi-process `batch_run` on Windows |
| 50 | +## Multi-process `batch_run` on Windows |
39 | 51 |
|
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. |
44 | 57 |
|
| 58 | +Example: |
45 | 59 | ```python
|
| 60 | +from mesa.batchrunner import batch_run |
46 | 61 | from multiprocessing import freeze_support
|
47 | 62 |
|
48 | 63 | params = {"width": 10, "height": 10, "N": range(10, 500, 10)}
|
49 | 64 |
|
50 | 65 | if __name__ == '__main__':
|
51 | 66 | freeze_support()
|
52 | 67 | 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, |
60 | 75 | )
|
61 | 76 | ```
|
62 | 77 |
|
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