forked from hnekoeiq/covid_p2p_simulation
/
base.py
588 lines (492 loc) · 21.3 KB
/
base.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
import simpy
import math
import copy
import datetime
import itertools
import numpy as np
from collections import defaultdict
from orderedset import OrderedSet
import copy
from config import *
from utils import compute_distance, _get_random_area
from track import Tracker
class Env(simpy.Environment):
def __init__(self, initial_timestamp):
super().__init__()
self.initial_timestamp = initial_timestamp
def time(self):
return self.now
@property
def timestamp(self):
return self.initial_timestamp + datetime.timedelta(
minutes=self.now * TICK_MINUTE)
def minutes(self):
return self.timestamp.minute
def hour_of_day(self):
return self.timestamp.hour
def day_of_week(self):
return self.timestamp.weekday()
def is_weekend(self):
return self.day_of_week() in [0, 6]
def time_of_day(self):
return self.timestamp.isoformat()
class City(object):
def __init__(self, env, n_people, rng, x_range, y_range, start_time, init_percent_sick, Human):
self.env = env
self.rng = rng
self.x_range = x_range
self.y_range = y_range
self.total_area = (x_range[1] - x_range[0]) * (y_range[1] - y_range[0])
self.n_people = n_people
self.start_time = start_time
self.init_percent_sick = init_percent_sick
self.last_date_to_check_tests = self.env.timestamp.date()
self.test_count_today = defaultdict(int)
self.test_type_preference = list(zip(*sorted(TEST_TYPES.items(), key=lambda x:x[1]['preference'])))[0]
print("Initializing locations ...")
self.initialize_locations()
self.humans = []
self.households = OrderedSet()
print("Initializing humans ...")
self.initialize_humans(Human)
self.log_static_info()
print("Computing their preferences")
self._compute_preferences()
self.tracker = Tracker(env, self)
self.tracker.track_initialized_covid_params(self.humans)
def create_location(self, specs, type, name, area=None):
_cls = Location
if type in ['household', 'senior_residency']:
_cls = Household
if type == 'hospital':
_cls = Hospital
return _cls(
env=self.env,
rng=self.rng,
name=f"{type}:{name}",
location_type=type,
lat=self.rng.randint(*self.x_range),
lon=self.rng.randint(*self.y_range),
area=area,
social_contact_factor=specs['social_contact_factor'],
capacity= None if not specs['rnd_capacity'] else self.rng.randint(*specs['rnd_capacity']),
surface_prob = specs['surface_prob']
)
@property
def tests_available(self):
if self.last_date_to_check_tests != self.env.timestamp.date():
self.last_date_to_check_tests = self.env.timestamp.date()
for k in self.test_count_today.keys():
self.test_count_today[k] = 0
return any(self.test_count_today[test_type] < TEST_TYPES[test_type]['capacity'] for test_type in self.test_type_preference)
def get_available_test(self):
for test_type in self.test_type_preference:
if self.test_count_today[test_type] < TEST_TYPES[test_type]['capacity']:
self.test_count_today[test_type] += 1
return test_type
def initialize_locations(self):
for location, specs in LOCATION_DISTRIBUTION.items():
if location in ['household']:
continue
n = math.ceil(self.n_people/specs["n"])
area = _get_random_area(n, specs['area'] * self.total_area, self.rng)
locs = [self.create_location(specs, location, i, area[i]) for i in range(n)]
setattr(self, f"{location}s", locs)
def initialize_humans(self, Human):
# allocate humans to houses such that (unsolved)
# 1. average number of residents in a house is (approx.) 2.6
# 2. not all residents are below 15 years of age
# 3. age occupancy distribution follows HUMAN_DSITRIBUTION.residence_preference.house_size
# current implementation is an approximate heuristic
# make humans
count_humans = 0
house_allocations = {2:[], 3:[], 4:[], 5:[]}
n_houses = 0
for age_bin, specs in HUMAN_DISTRIBUTION.items():
n = math.ceil(specs['p'] * self.n_people)
ages = self.rng.randint(*age_bin, size=n)
senior_residency_preference = specs['residence_preference']['senior_residency']
professions = ['healthcare', 'school', 'others', 'retired']
p = [specs['profession_profile'][x] for x in professions]
profession = self.rng.choice(professions, p=p, size=n)
for i in range(n):
count_humans += 1
age = ages[i]
# residence
res = None
if self.rng.random() < senior_residency_preference:
res = self.rng.choice(self.senior_residencys)
# workplace
if profession[i] == "healthcare":
workplace = self.rng.choice(self.hospitals + self.senior_residencys)
elif profession[i] == 'school':
workplace = self.rng.choice(self.schools)
elif profession[i] == 'others':
type_of_workplace = self.rng.choice([0,1,2], p=OTHERS_WORKPLACE_CHOICE, size=1).item()
type_of_workplace = [self.workplaces, self.stores, self.miscs][type_of_workplace]
workplace = self.rng.choice(type_of_workplace)
else:
workplace = res
self.humans.append(Human(
env=self.env,
rng=self.rng,
name=count_humans,
age=age,
household=res,
workplace=workplace,
profession=profession[i],
rho=0.3,
gamma=0.21,
infection_timestamp=self.start_time if self.rng.random() < self.init_percent_sick else None
)
)
# assign houses
# stores tuples - (location, current number of residents, maximum number of residents allowed)
remaining_houses = []
for human in self.humans:
if human.household is not None:
continue
if len(remaining_houses) == 0:
cap = self.rng.choice(range(1,6), p=HOUSE_SIZE_PREFERENCE, size=1)
x = self.create_location(LOCATION_DISTRIBUTION['household'], 'household', len(self.households))
remaining_houses.append((x, cap))
# get_best_match
res = None
for c, (house, n_vacancy) in enumerate(remaining_houses):
new_avg_age = (human.age + sum(x.age for x in house.residents))/(len(house.residents) + 1)
if new_avg_age > MIN_AVG_HOUSE_AGE:
res = house
n_vacancy -= 1
if n_vacancy == 0:
remaining_houses = remaining_houses[:c] + remaining_houses[c+1:]
break
if res is None:
for i, (l,u) in enumerate(HUMAN_DISTRIBUTION.keys()):
if l <= human.age < u:
bin = (l,u)
break
house_size_preference = HUMAN_DISTRIBUTION[(l,u)]['residence_preference']['house_size']
cap = self.rng.choice(range(1,6), p=house_size_preference, size=1)
res = self.create_location(LOCATION_DISTRIBUTION['household'], 'household', len(self.households))
if cap - 1 > 0:
remaining_houses.append((res, cap-1))
# FIXME: there is some circular reference here
res.residents.append(human)
human.assign_household(res)
self.households.add(res)
# assign area to house
area = _get_random_area(len(self.households), LOCATION_DISTRIBUTION['household']['area'] * self.total_area, self.rng)
for i,house in enumerate(self.households):
house.area = area[i]
def log_static_info(self):
for h in self.humans:
Event.log_static_info(self, h, self.env.timestamp)
@property
def events(self):
return list(itertools.chain(*[h.events for h in self.humans]))
def pull_events(self):
return list(itertools.chain(*[h.pull_events() for h in self.humans]))
def _compute_preferences(self):
""" compute preferred distribution of each human for park, stores, etc."""
for h in self.humans:
h.stores_preferences = [(compute_distance(h.household, s) + 1e-1) ** -1 for s in self.stores]
h.parks_preferences = [(compute_distance(h.household, s) + 1e-1) ** -1 for s in self.parks]
class Location(simpy.Resource):
def __init__(self, env, rng, area, name, location_type, lat, lon,
social_contact_factor, capacity, surface_prob):
if capacity is None:
capacity = simpy.core.Infinity
super().__init__(env, capacity)
self.humans = OrderedSet() #OrderedSet instead of set for determinism when iterating
self.name = name
self.rng = rng
self.lat = lat
self.lon = lon
self.area = area
self.location_type = location_type
self.social_contact_factor = social_contact_factor
self.env = env
self.contamination_timestamp = datetime.datetime.min
self.contaminated_surface_probability = surface_prob
self.max_day_contamination = 0
def infectious_human(self):
return any([h.is_infectious for h in self.humans])
def __repr__(self):
return f"{self.name} - occ:{len(self.humans)}/{self.capacity} - I:{self.infectious_human()}"
def add_human(self, human):
self.humans.add(human)
if human.is_infectious:
self.contamination_timestamp = self.env.timestamp
rnd_surface = float(self.rng.choice(a=MAX_DAYS_CONTAMINATION, size=1, p=self.contaminated_surface_probability))
self.max_day_contamination = max(self.max_day_contamination, rnd_surface)
def remove_human(self, human):
self.humans.remove(human)
@property
def is_contaminated(self):
return self.env.timestamp - self.contamination_timestamp <= datetime.timedelta(days=self.max_day_contamination)
@property
def contamination_probability(self):
if self.is_contaminated:
lag = (self.env.timestamp - self.contamination_timestamp)
lag /= datetime.timedelta(days=1)
p_infection = 1 - lag / self.max_day_contamination # linear decay; &envrionmental_contamination
return self.social_contact_factor * p_infection
return 0.0
def __hash__(self):
return hash(self.name)
def serialize(self):
""" This function serializes the location object"""
s = self.__dict__
if s.get('env'):
del s['env']
if s.get('rng'):
del s['rng']
if s.get('_env'):
del s['_env']
if s.get('contamination_timestamp'):
del s['contamination_timestamp']
if s.get('residents'):
del s['residents']
if s.get('humans'):
del s['humans']
return s
class Household(Location):
def __init__(self, **kwargs):
super(Household, self).__init__(**kwargs)
self.residents = []
class Hospital(Location):
ICU_AREA = 0.10
ICU_CAPACITY = 0.10
def __init__(self, **kwargs):
env = kwargs.get('env')
rng = kwargs.get('rng')
capacity = kwargs.get('capacity')
name = kwargs.get("name")
lat = kwargs.get('lat')
lon = kwargs.get('lon')
area = kwargs.get('area')
surface_prob = kwargs.get('surface_prob')
social_contact_factor = kwargs.get('social_contact_factor')
super(Hospital, self).__init__( env=env,
rng=rng,
area=area * (1-self.ICU_AREA),
name=name,
location_type="hospital",
lat=lat,
lon=lon,
social_contact_factor=social_contact_factor,
capacity=int(capacity* (1- self.ICU_CAPACITY)),
surface_prob=surface_prob,
)
self.location_contamination = 1
self.icu = ICU( env=env,
rng=rng,
area=area * (self.ICU_AREA),
name=f"{name}-icu",
location_type="hospital-icu",
lat=lat,
lon=lon,
social_contact_factor=social_contact_factor,
capacity=int(capacity* (self.ICU_CAPACITY)),
surface_prob=surface_prob,
)
def add_human(self, human):
human.obs_hospitalized = True
super().add_human(human)
def remove_human(self, human):
human.obs_hospitalized = False
super().remove_human(human)
class ICU(Location):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def add_human(self, human):
human.obs_hospitalized = True
human.obs_in_icu = True
super().add_human(human)
def remove_human(self, human):
human.obs_hospitalized = False
human.obs_in_icu = False
super().remove_human(human)
class Event:
test = 'test'
encounter = 'encounter'
symptom_start = 'symptom_start'
contamination = 'contamination'
recovered = 'recovered'
static_info = 'static_info'
visit = 'visit'
daily = 'daily'
@staticmethod
def members():
return [Event.test, Event.encounter, Event.symptom_start, Event.contamination, Event.static_info, Event.visit, Event.daily]
@staticmethod
def log_encounter(human1, human2, location, duration, distance, infectee, time):
h_obs_keys = ['has_app',
'obs_hospitalized', 'obs_in_icu',
'obs_lat', 'obs_lon']
h_unobs_keys = ['carefulness', 'viral_load', 'infectiousness',
'symptoms', 'is_exposed', 'is_infectious',
'infection_timestamp', 'is_really_sick',
'is_extremely_sick', 'sex', 'wearing_mask', 'mask_efficacy']
loc_obs_keys = ['location_type', 'lat', 'lon']
loc_unobs_keys = ['contamination_probability', 'social_contact_factor']
obs, unobs = [], []
same_household = (human1.household.name == human2.household.name) & (location.name == human1.household.name)
for human in [human1, human2]:
o = {key:getattr(human, key) for key in h_obs_keys}
obs.append(o)
u = {key:getattr(human, key) for key in h_unobs_keys}
u['human_id'] = human.name
u['location_is_residence'] = human.household == location
u['got_exposed'] = infectee == human.name if infectee else False
u['exposed_other'] = infectee != human.name if infectee else False
u['same_household'] = same_household
u['infectiousness_start_time'] = None if not u['got_exposed'] else human.infection_timestamp + datetime.timedelta(days=human.infectiousness_onset_days)
unobs.append(u)
loc_obs = {key:getattr(location, key) for key in loc_obs_keys}
loc_unobs = {key:getattr(location, key) for key in loc_unobs_keys}
loc_unobs['location_p_infection'] = location.contamination_probability / location.social_contact_factor
other_obs = {'duration':duration, 'distance':distance}
both_have_app = human1.has_app and human2.has_app
for i, human in [(0, human1), (1, human2)]:
if both_have_app:
obs_payload = {**loc_obs, **other_obs, 'human1':obs[i], 'human2':obs[1-i]}
unobs_payload = {**loc_unobs, 'human1':unobs[i], 'human2':unobs[1-i]}
else:
obs_payload = {}
unobs_payload = { **loc_obs, **loc_unobs, **other_obs, 'human1':{**obs[i], **unobs[i]},
'human2': {**obs[1-i], **unobs[1-i]} }
human.events.append({
'human_id':human.name,
'event_type':Event.encounter,
'time':time,
'payload':{'observed':obs_payload, 'unobserved':unobs_payload}
})
@staticmethod
def log_test(human, time):
human.events.append(
{
'human_id': human.name,
'event_type': Event.test,
'time': time,
'payload': {
'observed':{
'result': human.reported_test_result,
'test_type':human.reported_test_type,
'validated_test_result':human.test_result_validated
},
'unobserved':{
'test_type':human.test_type,
'result': human.test_result
}
}
}
)
@staticmethod
def log_daily(human, time):
human.events.append(
{
'human_id': human.name,
'event_type': Event.daily,
'time': time,
'payload': {
'observed':{
"reported_symptoms": human.all_reported_symptoms
},
'unobserved':{
'infectiousness': human.infectiousness,
"viral_load": human.viral_load,
"all_symptoms": human.all_symptoms,
"covid_symptoms":human.covid_symptoms,
"flu_symptoms":human.flu_symptoms,
"cold_symptoms":human.cold_symptoms
}
}
}
)
@staticmethod
def log_exposed(human, source, time):
human.events.append(
{
'human_id': human.name,
'event_type': Event.contamination,
'time': time,
'payload': {
'observed':{
},
'unobserved':{
'exposed': True,
'source':source.name,
'source_is_location': 'human' not in source.name,
'source_is_human': 'human' in source.name,
'infectiousness_start_time': human.infection_timestamp + datetime.timedelta(days=human.infectiousness_onset_days)
}
}
}
)
@staticmethod
def log_recovery(human, time, death):
human.events.append(
{
'human_id': human.name,
'event_type': Event.recovered,
'time': time,
'payload': {
'observed':{
},
'unobserved':{
'recovered': not death,
'death': death
}
}
}
)
@staticmethod
def log_static_info(city, human, time):
h_obs_keys = ['obs_preexisting_conditions', "obs_age", "obs_sex", "obs_is_healthcare_worker"]
h_unobs_keys = ['preexisting_conditions', "age", "sex", "is_healthcare_worker"]
obs_payload = {key:getattr(human, key) for key in h_obs_keys}
unobs_payload = {key:getattr(human, key) for key in h_unobs_keys}
if human.workplace.location_type in ['healthcare', 'store', 'misc', 'senior_residency']:
obs_payload['n_people_workplace'] = 'many people'
elif "workplace" == human.workplace.location_type:
obs_payload['n_people_workplace'] = 'few people'
else:
obs_payload['n_people_workplace'] = 'no people outside my household'
obs_payload['household_size'] = len(human.household.residents)
human.events.append(
{
'human_id': human.name,
'event_type':Event.static_info,
'time':time,
'payload':{
'observed': obs_payload,
'unobserved':unobs_payload
}
}
)
class DummyEvent:
@staticmethod
def log_encounter(*args, **kwargs):
pass
@staticmethod
def log_test(*args, **kwargs):
pass
@staticmethod
def log_symptom_start(*args, **kwargs):
pass
@staticmethod
def log_recovery(*args, **kwargs):
pass
@staticmethod
def log_exposed(*args, **kwargs):
pass
@staticmethod
def log_static_info(*args, **kwargs):
pass
@staticmethod
def log_visit(*args, **kwargs):
pass
@staticmethod
def log_daily(*args, **kwargs):
pass