-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.py
49 lines (29 loc) · 1.16 KB
/
simulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import logging
log = logging.getLogger(__name__)
class Params(object):
def __init__(self, **kwargs):
self.__dict__ = kwargs
class Simulation(object):
""" A forward simulation of village states over a number of generations """
def __init__(self, params):
self.generation = 0
self.reports = params.pop('reports', [])
initializer_fn, initializer_kwargs = params.pop('initializer_fn')
self.villages = initializer_fn(self, **initializer_kwargs)
self.notify('villages_init', villages=self.villages)
self.params = Params(**params)
def run(self):
for _ in range(self.params.sim_duration):
self.update()
def update(self):
self.notify('simulation_update_begin', simulation=self)
for v in self.villages:
v.update()
for v in self.villages:
v.transmit()
self.notify('village_summary', simulation=self, village=v)
self.notify('simulation_update_end')
self.generation += 1
def notify(self, event, *args, **kwargs):
for listener in self.reports:
listener.report(event, *args, **kwargs)