Commit 4cc03a4
authored
Support discrete event scheduling (#2066)
# Summary
This PR adds an experiment feature that puts event scheduling at the heart of how MESA works. An earlier draft of this code was shown and discussed in #2032. This code generalizes #1890 by making discrete event scheduling central to how mesa models are run. This makes it trivial to maintain discrete time progression while allowing for event scheduling, thus making it possible to build hybrid ABM-DEVS models.
# Motive
Agent-based models can be quite slow because, typically, they involve the activation of all agents at each tick. However, this can become very expensive if it can be known upfront that the agent is not active. Combining ABM tick-based activation with event scheduling makes it easy to avoid activating dormant agents.
For example, in Epstein's civil violence model, agents who are in jail need not be activated until they are released from jail. This release from jail can be scheduled as an event, thus avoiding unnecessary agent activations. Likewise, in Wolf-Sheep with grass, the regrowth of grass patches can be scheduled instead of activating all patches for each tick only to decrement a counter.
# Implementation
The experimental feature adds three core new classes: Simulator, EventList, and SimulationEvent.
The simulator is analogous to a numerical solver for ODEs. Theoretically, the idea of a Simulator is rooted in the work of [Zeigler](https://www.sciencedirect.com/book/9780128133705/theory-of-modeling-and-simulation), The Simulator is responsible for controlling and advancing time. The EventList is a heapq sorted list of SimulationEvents. SimulationEvents are sorted based on their time of execution, their priority, and their unique_id. A SimulationEvent is, in essence, a callable that is to be executed at a particular simulation time instant.
This PR adds two specific simulators: ABMSimulator and DEVSSimulator. ABMSimulator uses integers as the base unit of time and automatically ensures that `model.step` is scheduled for each tick. DEVSSimulator uses float as the base unit of time. It allows for full discrete event scheduling.
Using these new classes requires a minor modification to a Model instance. It needs a simulator attribute to be able to schedule events.
# Usage
The basic usage is straightforward as shown below. We instantiate an ABMSimulator, instantiate the model, and call `simulator.setup`. Next, we can run the model for, e.g., 100 time steps).
```python
simulator = ABMSimulator()
model = WolfSheep(simulator,25, 25, 60, 40, 0.2, 0.1, 20, seed=15,)
simulator.setup(model)
simulator.run(100)
print(model.time) # prints 100
simulator.run(50)
print(model.time) # prints 150
```
The simulator comes with a whole range of methods for scheduling events: `schedule_event_now`, `schedule_event_relative`, `schedule_event_absolute`, and the ABMSimulator also has a `schedule_event_next_tick`. See `experimental/devs/examples/*.*` for more details on how to use these methods.1 parent b2856f4 commit 4cc03a4
File tree
13 files changed
+1363
-279
lines changed- benchmarks
- Flocking
- Schelling
- WolfSheep
- mesa
- experimental/devs
- examples
- tests
13 files changed
+1363
-279
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
102 | 102 | | |
103 | 103 | | |
104 | 104 | | |
| 105 | + | |
105 | 106 | | |
106 | 107 | | |
107 | 108 | | |
| |||
118 | 119 | | |
119 | 120 | | |
120 | 121 | | |
121 | | - | |
122 | | - | |
123 | | - | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
124 | 126 | | |
125 | | - | |
126 | | - | |
127 | | - | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
128 | 133 | | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | 134 | | |
134 | 135 | | |
135 | 136 | | |
| |||
138 | 139 | | |
139 | 140 | | |
140 | 141 | | |
141 | | - | |
| 142 | + | |
| 143 | + | |
142 | 144 | | |
143 | | - | |
144 | | - | |
| 145 | + | |
| 146 | + | |
145 | 147 | | |
146 | 148 | | |
147 | 149 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| 52 | + | |
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
| |||
62 | 63 | | |
63 | 64 | | |
64 | 65 | | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | 66 | | |
| 67 | + | |
69 | 68 | | |
70 | 69 | | |
71 | 70 | | |
| |||
75 | 74 | | |
76 | 75 | | |
77 | 76 | | |
78 | | - | |
79 | | - | |
80 | 77 | | |
81 | 78 | | |
82 | 79 | | |
83 | 80 | | |
84 | 81 | | |
85 | | - | |
| 82 | + | |
86 | 83 | | |
87 | 84 | | |
88 | 85 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
40 | 39 | | |
41 | 40 | | |
42 | 41 | | |
| |||
93 | 92 | | |
94 | 93 | | |
95 | 94 | | |
96 | | - | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
97 | 111 | | |
| 112 | + | |
| 113 | + | |
98 | 114 | | |
99 | 115 | | |
100 | 116 | | |
101 | 117 | | |
102 | 118 | | |
| 119 | + | |
| 120 | + | |
103 | 121 | | |
104 | 122 | | |
105 | | - | |
106 | | - | |
| 123 | + | |
| 124 | + | |
107 | 125 | | |
108 | | - | |
109 | 126 | | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
116 | 130 | | |
117 | 131 | | |
118 | 132 | | |
| |||
124 | 138 | | |
125 | 139 | | |
126 | 140 | | |
| 141 | + | |
127 | 142 | | |
128 | 143 | | |
129 | 144 | | |
| |||
139 | 154 | | |
140 | 155 | | |
141 | 156 | | |
| 157 | + | |
142 | 158 | | |
143 | 159 | | |
144 | 160 | | |
145 | 161 | | |
146 | 162 | | |
147 | | - | |
148 | 163 | | |
149 | 164 | | |
150 | 165 | | |
151 | | - | |
152 | | - | |
| 166 | + | |
153 | 167 | | |
154 | 168 | | |
155 | 169 | | |
156 | 170 | | |
157 | 171 | | |
| 172 | + | |
| 173 | + | |
158 | 174 | | |
159 | 175 | | |
160 | | - | |
161 | 176 | | |
162 | | - | |
163 | 177 | | |
164 | 178 | | |
165 | 179 | | |
| |||
175 | 189 | | |
176 | 190 | | |
177 | 191 | | |
178 | | - | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
179 | 197 | | |
180 | 198 | | |
181 | | - | |
182 | 199 | | |
183 | 200 | | |
184 | 201 | | |
| |||
188 | 205 | | |
189 | 206 | | |
190 | 207 | | |
191 | | - | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
192 | 213 | | |
193 | 214 | | |
194 | | - | |
195 | 215 | | |
196 | 216 | | |
197 | 217 | | |
198 | 218 | | |
199 | 219 | | |
200 | 220 | | |
201 | | - | |
| 221 | + | |
202 | 222 | | |
203 | | - | |
204 | | - | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
205 | 227 | | |
206 | | - | |
207 | 228 | | |
208 | 229 | | |
209 | | - | |
| 230 | + | |
| 231 | + | |
210 | 232 | | |
211 | 233 | | |
212 | 234 | | |
213 | 235 | | |
214 | 236 | | |
215 | | - | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
216 | 251 | | |
217 | 252 | | |
218 | | - | |
219 | | - | |
| 253 | + | |
220 | 254 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
| |||
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
18 | | - | |
19 | | - | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
20 | 23 | | |
21 | 24 | | |
22 | 25 | | |
23 | | - | |
24 | | - | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
25 | 30 | | |
26 | 31 | | |
27 | 32 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
0 commit comments