|
| 1 | +# Best Practices |
| 2 | + |
| 3 | +Here are some general principles that have proven helpful for developing models. |
| 4 | + |
| 5 | +## Model Layout |
| 6 | + |
| 7 | +A model should be contained in a folder named with lower-case letters and |
| 8 | +underscores, such as `thunder_cats`. Within that directory: |
| 9 | + |
| 10 | +- `README.md` describes the model, how to use it, and any other details. |
| 11 | + Github will automatically show this file to anyone visiting the directory. |
| 12 | +- `model.py` should contain the model class. If the file gets large, it may |
| 13 | + make sense to move the complex bits into other files, but this is the first |
| 14 | + place readers will look to figure out how the model works. |
| 15 | +- `server.py` should contain the visualization support, including the server |
| 16 | + class. |
| 17 | +- `run.py` is a Python script that will run the model when invoked via |
| 18 | + `mesa runserver`. |
| 19 | + |
| 20 | +After the number of files grows beyond a half-dozen, try to use sub-folders to |
| 21 | +organize them. For example, if the visualization uses image files, put those in |
| 22 | +an `images` directory. |
| 23 | + |
| 24 | +The [Schelling](https://github.com/projectmesa/mesa-examples/tree/main/examples/schelling) model is |
| 25 | +a good example of a small well-packaged model. |
| 26 | + |
| 27 | +It's easy to create a cookiecutter mesa model by running `mesa startproject` |
| 28 | + |
| 29 | +## Randomization |
| 30 | + |
| 31 | +If your model involves some random choice, you can use the built-in `random` |
| 32 | +property that Mesa `Model` and `Agent` objects have. This works exactly |
| 33 | +like the built-in `random` library. |
| 34 | + |
| 35 | +```python |
| 36 | +class AwesomeModel(Model): |
| 37 | + # ... |
| 38 | + |
| 39 | + def cool_method(self): |
| 40 | + interesting_number = self.random.random() |
| 41 | + print(interesting_number) |
| 42 | + |
| 43 | +class AwesomeAgent(Agent): |
| 44 | + # ... |
| 45 | + def __init__(self, unique_id, model, ...): |
| 46 | + super().__init__(unique_id, model) |
| 47 | + # ... |
| 48 | + |
| 49 | + def my_method(self): |
| 50 | + random_number = self.random.randint(0, 100) |
| 51 | +``` |
| 52 | + |
| 53 | +(The agent's random property is just a reference to its parent model's |
| 54 | +`random` property). |
| 55 | + |
| 56 | +When a model object is created, its random property is automatically seeded |
| 57 | +with the current time. The seed determines the sequence of random numbers; if |
| 58 | +you instantiate a model with the same seed, you will get the same results. |
| 59 | +To allow you to set the seed, make sure your model has a `seed` argument in its |
| 60 | +constructor. |
| 61 | + |
| 62 | +```python |
| 63 | +class AwesomeModel(Model): |
| 64 | + |
| 65 | + def __init__(self, seed=None): |
| 66 | + pass |
| 67 | + |
| 68 | + def cool_method(self): |
| 69 | + interesting_number = self.random.random() |
| 70 | + print(interesting_number) |
| 71 | + |
| 72 | +>>> model0 = AwesomeModel(seed=0) |
| 73 | +>>> model0._seed |
| 74 | +0 |
| 75 | +>>> model0.cool_method() |
| 76 | +0.8444218515250481 |
| 77 | +>>> model1 = AwesomeModel(seed=0) |
| 78 | +>>> model1.cool_method() |
| 79 | +0.8444218515250481 |
| 80 | +``` |
0 commit comments