Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Convert howto.rst -> howto.md via rst2myst #2033

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions docs/howto.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# How-to Guide

Here you can find code that allows you to get to get started on common tasks in Mesa.

## Models with Discrete Time

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.

```python
if self.model.schedule.time in self.discrete_time:
self.model.space.move_agent(self, new_pos)
```

## Implementing Model Level Functions in Staged Activation

In staged activation, if you may want a function to be implemented only on the model level and not at the level of agents.
For such functions, include the prefix "model." before the model function name, when defining the function list.
For example, consider a central employment exchange which adjust the wage rate common to all laborers
in the direction of excess demand.

```python
stage_list=[Send_Labour_Supply, Send_Labour_Demand, model.Adjust_Wage_Rate] self.schedule = StagedActivation(self,stage_list,shuffle=True)
```

## Using `numpy.random`

Sometimes you need to use `numpy`'s `random` library, for example to get a Poisson distribution.

```python
class MyModel(Model):
def __init__(self, ...):
super().__init__()
self.random = np.random.default_rng(seed)
```

And just use `numpy`'s random as usual, e.g. `self.random.poisson()`.

## Using multi-process `batch_run` on Windows

You will have an issue with `batch_run` and `number_processes = None`. Your cell will
show no progress, and in your terminal you will receive *AttributeError: Can't get attribute 'MoneyModel' on
\<module '\_\_main\_\_' (built-in)>*. One way to overcome this is to take your code outside of Jupyter and adjust the above
code as follows.

```python
from multiprocessing import freeze_support

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

if __name__ == '__main__':
freeze_support()
results = batch_run(
MoneyModel,
parameters=params,
iterations=5,
max_steps=100,
number_processes=None,
data_collection_period=1,
display_progress=True,
)
```

If you would still like to run your code in Jupyter you will need to adjust the cell as noted above. Then you can
you can add the [nbmultitask library](https://nbviewer.org/github/micahscopes/nbmultitask/blob/39b6f31b047e8a51a0fcb5c93ae4572684f877ce/examples.ipynb)
or look at this [stackoverflow](https://stackoverflow.com/questions/50937362/multiprocessing-on-python-3-jupyter).
70 changes: 0 additions & 70 deletions docs/howto.rst

This file was deleted.