|
| 1 | +# How-to Guide |
| 2 | + |
| 3 | +Here you can find code that allows you to get to get started on common tasks in Mesa. |
| 4 | + |
| 5 | +## Models with Discrete Time |
| 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. |
| 8 | + |
| 9 | +```python |
| 10 | +if self.model.schedule.time in self.discrete_time: |
| 11 | + self.model.space.move_agent(self, new_pos) |
| 12 | +``` |
| 13 | + |
| 14 | +## Implementing Model Level Functions in Staged Activation |
| 15 | + |
| 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. |
| 20 | + |
| 21 | +```python stage_list=[Send_Labour_Supply, Send_Labour_Demand, model.Adjust_Wage_Rate] self.schedule = StagedActivation(self,stage_list,shuffle=True) |
| 22 | + |
| 23 | +``` |
| 24 | + |
| 25 | +## Using `` `numpy.random` `` |
| 26 | + |
| 27 | +Sometimes you need to use `numpy`'s `random` library, for example to get a Poisson distribution. |
| 28 | + |
| 29 | +```python |
| 30 | +class MyModel(Model): |
| 31 | + def __init__(self, ...): |
| 32 | + super().__init__() |
| 33 | + self.random = np.random.default_rng(seed) |
| 34 | +``` |
| 35 | + |
| 36 | +And just use `numpy`'s random as usual, e.g. `self.random.poisson()`. |
| 37 | + |
| 38 | +## Using multi-process `` `batch_run` `` on Windows |
| 39 | + |
| 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. |
| 44 | + |
| 45 | +```python |
| 46 | +from multiprocessing import freeze_support |
| 47 | + |
| 48 | +params = {"width": 10, "height": 10, "N": range(10, 500, 10)} |
| 49 | + |
| 50 | +if __name__ == '__main__': |
| 51 | + freeze_support() |
| 52 | + 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, |
| 60 | + ) |
| 61 | +``` |
| 62 | + |
| 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). |
0 commit comments