-
Notifications
You must be signed in to change notification settings - Fork 11
/
covidmodel.py
1637 lines (1364 loc) · 73.8 KB
/
covidmodel.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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Santiago Nunez-Corrales and Eric Jakobsson
# Illinois Informatics and Molecular and Cell Biology
# University of Illinois at Urbana-Champaign
# {nunezco,jake}@illinois.edu
# A simple tunable model for COVID-19 response
import math
from operator import mod
from sqlite3 import DatabaseError
import timeit
import mesa.batchrunner
from mesa import Agent, Model
from mesa.time import RandomActivation
from mesa.space import MultiGrid
from datacollection import DataCollector
from scipy.stats import poisson, bernoulli
from enum import Enum
import numpy as np
import random
import sys
import psutil as psu
import timeit as time
import os
import pandas as pd
from agent_data_class import AgentDataClass
from model_data_class import ModelDataClass
import uuid
from database import Database
from policyhandler import PolicyHandler
def bernoulli_rvs(p):
# Return a sample from a Bernoulli-distributed random source
# We convert from a Uniform(0, 1)
r = random.random()
if r >= p:
return 1
return 0
def poisson_rvs(mu):
p0 = math.exp(-mu)
F = p0
i = 0
sample = random.random()
while sample >= F:
i += 1
F += p0 * (mu ** i) / math.factorial(i)
return i
class Stage(Enum):
SUSCEPTIBLE = 1
EXPOSED = 2
ASYMPTOMATIC = 3
SYMPDETECTED = 4
ASYMPDETECTED = 5
SEVERE = 6
RECOVERED = 7
DECEASED = 8
class AgeGroup(Enum):
C00to09 = 0
C10to19 = 1
C20to29 = 2
C30to39 = 3
C40to49 = 4
C50to59 = 5
C60to69 = 6
C70to79 = 7
C80toXX = 8
class SexGroup(Enum):
MALE = 1
FEMALE = 2
class ValueGroup(Enum):
PRIVATE = 1
PUBLIC = 2
class VaccinationStage(Enum):
C00to09 = 0
C10to19 = 1
C20to29 = 2
C30to39 = 3
C40to49 = 4
C50to59 = 5
C60to69 = 6
C70to79 = 7
C80toXX = 8
class CovidAgent(Agent):
""" An agent representing a potential covid case"""
def __init__(self, unique_id, ageg, sexg, mort, model):
super().__init__(unique_id, model)
self.stage = Stage.SUSCEPTIBLE
self.astep = 0
is_checkpoint = False
params = [0, ageg, sexg, mort]
self.agent_data = AgentDataClass(model, is_checkpoint, params)
def alive(self):
print(f'{self.unique_id} {self.agent_data.age_group} {self.agent_data.sex_group} is alive')
def is_contagious(self):
return (self.stage == Stage.EXPOSED) or (self.stage == Stage.ASYMPTOMATIC) or (self.stage == Stage.SYMPDETECTED)
def dmult(self):
# In this function, we simulate aerosol effects exhibited by droplets due to
# both the contributions of a) a minimum distance with certainty of infection
# and a the decreasing bioavailability of droplets, modeled as a sigmoid function.
# Units are in meters. We assume that after 1.5 meter bioavailability decreases as a
# sigmoid. This case supposses infrequent sneezing, but usual saliva droplets when
# masks are not in use. A multiplier of k = 10 is used as a sharpening parameter
# of the distribution and must be further callibrated.
mult = 1.0
if self.model.model_data.distancing >= 1.5:
k = 10
mult = 1.0 - (1.0 / (1.0 + np.exp(k*(-(self.model.model_data.distancing - 1.5) + 0.5))))
return mult
# In this function, we count effective interactants
def interactants(self):
count = 0
if (self.stage != Stage.DECEASED) and (self.stage != Stage.RECOVERED):
for agent in self.model.grid.get_cell_list_contents([self.pos]):
if agent.unique_id != self.unique_id:
if not(agent.agent_data.isolated) or self.agent_data.isolated_but_inefficient:
count = count + 1
return count
# A function that applies a contact tracing test
def test_contact_trace(self):
# We may have an already tested but it had a posterior contact and became infected
if self.stage == Stage.SUSCEPTIBLE:
self.agent_data.tested_traced = True
elif self.stage == Stage.EXPOSED:
self.agent_data.tested_traced = True
if bernoulli_rvs(self.model.model_data.prob_asymptomatic):
self.stage = Stage.ASYMPDETECTED
else:
self.stage = Stage.SYMPDETECTED
elif self.stage == Stage.ASYMPTOMATIC:
self.stage = Stage.ASYMPDETECTED
self.agent_data.tested_traced = True
else:
return
def add_contact_trace(self, other):
if self.model.model_data.tracing_now:
self.agent_data.contacts.add(other)
#helper function that reveals if an agent is vaccinated
def is_vaccinated(self):
return self.agent_data.vaccinated
#Vaccination decision process, prone to change to find the ideal method.
#Implementing the standard set that those who are older will be prioritized.
#For now implementing random vaccination.
def general_vaccination_chance(self):
eligible_count = compute_age_group_count(self.model, self.agent_data.age_group)
vaccination_chance = 1/eligible_count
if self.stage == Stage.ASYMPTOMATIC or self.stage == Stage.SUSCEPTIBLE or self.stage == Stage.EXPOSED:
if bernoulli_rvs(vaccination_chance):
return True
return False
return False
def should_be_vaccinated(self):
if self.general_vaccination_chance():
if self.agent_data.age_group == AgeGroup.C80toXX and self.model.model_data.vaccination_stage == VaccinationStage.C80toXX:
update_vaccination_stage(self.model)
return True
elif self.agent_data.age_group == AgeGroup.C70to79 and self.model.model_data.vaccination_stage == VaccinationStage.C70to79:
update_vaccination_stage(self.model)
return True
elif self.agent_data.age_group == AgeGroup.C60to69 and self.model.model_data.vaccination_stage == VaccinationStage.C60to69:
update_vaccination_stage(self.model)
return True
elif self.agent_data.age_group == AgeGroup.C50to59 and self.model.model_data.vaccination_stage == VaccinationStage.C50to59:
update_vaccination_stage(self.model)
return True
elif self.agent_data.age_group == AgeGroup.C40to49 and self.model.model_data.vaccination_stage == VaccinationStage.C40to49:
update_vaccination_stage(self.model)
return True
elif self.agent_data.age_group == AgeGroup.C30to39 and self.model.model_data.vaccination_stage == VaccinationStage.C30to39:
update_vaccination_stage(self.model)
return True
elif self.agent_data.age_group == AgeGroup.C20to29 and self.model.model_data.vaccination_stage == VaccinationStage.C20to29:
update_vaccination_stage(self.model)
return True
elif self.agent_data.age_group == AgeGroup.C10to19 and self.model.model_data.vaccination_stage == VaccinationStage.C10to19:
update_vaccination_stage(self.model)
return True
elif self.agent_data.age_group == AgeGroup.C00to09 and self.model.model_data.vaccination_stage == VaccinationStage.C00to09:
update_vaccination_stage(self.model)
return True
else :
update_vaccination_stage(self.model)
return False
return False
def step(self):
# We compute unemployment in general as a probability of 0.00018 per day.
# In 60 days, this is equivalent to a probability of 1% unemployment filings.
if self.agent_data.employed:
if self.agent_data.isolated:
if bernoulli_rvs(32*0.00018/self.model.model_data.dwell_15_day):
self.agent_data.employed = False
else:
if bernoulli_rvs(8*0.00018/self.model.model_data.dwell_15_day):
self.agent_data.employed = False
# We also compute the probability of re-employment, which is at least ten times
# as smaller in a crisis.
if not(self.agent_data.employed):
if bernoulli_rvs(0.000018/self.model.model_data.dwell_15_day):
self.agent_data.employed = True
# Social distancing
if not(self.agent_data.in_distancing) and (self.astep >= self.model.model_data.distancing_start):
self.agent_data.prob_contagion = self.dmult() * self.model.model_data.prob_contagion_base
self.agent_data.in_distancing = True
if self.agent_data.in_distancing and (self.astep >= self.model.model_data.distancing_end):
self.agent_data.prob_contagion = self.model.model_data.prob_contagion_base
self.agent_data.in_distancing = False
# Testing
if not(self.agent_data.in_testing) and (self.astep >= self.model.model_data.testing_start):
self.agent_data.test_chance = self.model.model_data.testing_rate
self.agent_data.in_testing = True
if self.agent_data.in_testing and (self.astep >= self.model.model_data.testing_end):
self.agent_data.test_chance = 0
self.agent_data.in_testing = False
#Implementing the vaccine
#Will process based on whether all older agents in an older group are vaccinated
if (not(self.agent_data.vaccinated) or self.agent_data.dosage_eligible) and self.model.model_data.vaccination_now and (not(self.agent_data.fully_vaccinated) and (self.agent_data.vaccine_count < self.model.model_data.vaccine_dosage)):
if self.should_be_vaccinated() and self.model.model_data.vaccine_count > 0 and self.agent_data.vaccine_willingness:
if not (bernoulli_rvs(0.1)): # Chance that someone doesnt show up for the vaccine/ vaccine expires.
self.agent_data.vaccinated = True
self.agent_data.vaccination_day = self.model.stepno
self.agent_data.vaccine_count = self.agent_data.vaccine_count + 1
self.agent_data.dosage_eligible = False
self.model.model_data.vaccine_count = self.model.model_data.vaccine_count - 1
self.model.model_data.vaccinated_count = self.model.model.data.vaccinated_count + 1
else:
other_agent = self.random.choice(self.model.schedule.agents)
while not(other_agent.dosage_eligible and other_agent.vaccine_willingness):
other_agent = self.random.choice(self.model.schedule.agents)
other_agent.vaccinated = True
other_agent.vaccination_day = self.model.stepno
other_agent.vaccine_count = other_agent.vaccine_count +1
other_agent.dosage_eligible = False
self.model.model_data.vaccinated_count = self.model.model_data.vaccinated_count + 1
self.model.model_data.vaccine_count = self.model.model_data.vaccine_count - 1
# Self isolation is tricker. We only isolate susceptibles, incubating and asymptomatics
if not(self.agent_data.in_isolation):
if (self.astep >= self.model.model_data.isolation_start):
if (self.stage == Stage.SUSCEPTIBLE) or (self.stage == Stage.EXPOSED) or \
(self.stage == Stage.ASYMPTOMATIC):
if bool(bernoulli_rvs(self.model.model_data.isolation_rate)):
self.agent_data.isolated = True
else:
self.agent_data.isolated = False
self.agent_data.in_isolation = True
elif (self.astep >= self.model.model_data.isolation_end):
if (self.stage == Stage.SUSCEPTIBLE) or (self.stage == Stage.EXPOSED) or \
(self.stage == Stage.ASYMPTOMATIC):
if bool(bernoulli_rvs(self.model.model_data.after_isolation)):
self.agent_data.isolated = True
else:
self.agent_data.isolated = False
self.agent_data.in_isolation = True
# Using a similar logic, we remove isolation for all relevant agents still locked
if self.agent_data.in_isolation and (self.astep >= self.model.model_data.isolation_end):
if (self.stage == Stage.SUSCEPTIBLE) or (self.stage == Stage.EXPOSED) or \
(self.stage == Stage.ASYMPTOMATIC):
self.agent_data.isolated = False
self.agent_data.in_isolation = False
#Implementing the current safety factor for maximum effectiveness
vaccination_time = self.model.stepno - self.agent_data.vaccination_day
#In this model I will assume that the vaccine is only half as effective once 2 weeks have passed given one dose.
effective_date = self.model.model_data.dwell_15_day * 14
if (vaccination_time < effective_date) and self.agent_data.vaccinated == True:
self.agent_data.safetymultiplier = 1 - (self.model.agent_data.effectiveness_per_dosage * (vaccination_time/effective_date)) - self.agent_data.current_effectiveness #Error the vaccination will go to 0 once it is done.
else:
self.agent_data.current_effectiveness = self.model.model_data.effectiveness_per_dosage * self.agent_data.vaccine_count
self.agent_data.safetymultiplier = 1 - self.agent_data.current_effectiveness * self.model.model_data.variant_data_list[self.agent_data.variant]["Vaccine_Multiplier"]
if (self.agent_data.vaccine_count < self.model.model_data.vaccine_dosage):
self.agent_data.dosage_eligible = True # Once this number is false, the person is eligible and is not fully vaccinated.
elif self.agent_data.fully_vaccinated == False:
self.agent_data.dosage_eligible = False
self.agent_data.fully_vaccinated = True
self.model.model_data.fully_vaccinated_count = self.model.model_data.fully_vaccinated_count + 1
# Using the model, determine if a susceptible individual becomes infected due to
# being elsewhere and returning to the community
if self.stage == Stage.SUSCEPTIBLE:
# if bernoulli_rvs(self.model.rate_inbound):
# self.stage = Stage.EXPOSED
# self.model.generally_infected = self.model.generally_infected + 1
#
# if self.stage == Stage.SUSCEPTIBLE:
# # Important: infected people drive the spread, not
# # the number of healthy ones
#
# # If testing is available and the date is reached, test
# # Testing of a healthy person should maintain them as
# still susceptible.
# We take care of testing probability at the top level step
# routine to avoid this repeated computation
if not(self.agent_data.tested or self.agent_data.tested_traced) and bernoulli_rvs(self.agent_data.test_chance):
self.agent_data.tested = True
self.model.model_data.cumul_test_cost = self.model.model_data.cumul_test_cost + self.model.model_data.test_cost
# First opportunity to get infected: contact with others
# in near proximity
cellmates = self.model.grid[self.pos[0]][self.pos[1]]
infected_contact = 0 #Changed to account for asymptomatic threat of infection
# Isolated people should only be contagious if they do not follow proper
# shelter-at-home measures
#Future implementaions would allow for multiple strains of the virus to stack on top of the same agent if exposed more than once but there is not much research showing what would really happen or what
#values we would have to account for
variant = "Standard"
for c in cellmates:
if c.is_contagious() and (c.stage == Stage.SYMPDETECTED or c.stage == Stage.SEVERE) and self.agent_data.variant_immune[c.agent_data.variant] == False:
c.add_contact_trace(self)
if self.agent_data.isolated and bernoulli_rvs(1 - self.model.model_data.prob_isolation_effective):
self.agent_data.isolated_but_inefficient = True
infected_contact = 1
variant = c.agent_data.variant
break
else:
infected_contact = 1
variant = c.agent_data.variant
break
elif c.is_contagious() and (c.stage == Stage.ASYMPTOMATIC or c.stage == Stage.ASYMPDETECTED) and self.agent_data.variant_immune[c.agent_data.variant] == False:
c.add_contact_trace(self)
if self.agent_data.isolated and bernoulli_rvs(1 - self.model.model_data.prob_isolation_effective):
self.agent_data.isolated_but_inefficient = True
infected_contact = 2
variant = c.agent_data.variant
else:
infected_contact = 2
variant = c.agent_data.variant
# Value is computed before infected stage happens
isolation_private_divider = 1
isolation_public_divider = 1
if self.agent_data.employed:
if self.agent_data.isolated:
isolation_private_divider = 0.3
isolation_public_divider = 0.01
self.agent_data.cumul_private_value = self.agent_data.cumul_private_value + \
((len(cellmates) - 1) * self.model.model_data.stage_value_dist[ValueGroup.PRIVATE][Stage.SUSCEPTIBLE])*isolation_private_divider
self.agent_data.cumul_public_value = self.agent_data.cumul_public_value + \
((len(cellmates) - 1) * self.model.model_data.stage_value_dist[ValueGroup.PUBLIC][Stage.SUSCEPTIBLE])*isolation_public_divider
else:
self.agent_data.cumul_private_value = self.agent_data.cumul_private_value + 0
self.agent_data.cumul_public_value = self.agent_data.cumul_public_value - 2*self.model.model_data.stage_value_dist[ValueGroup.PUBLIC][Stage.SUSCEPTIBLE]
current_prob = self.agent_data.prob_contagion * self.model.model_data.variant_data_list[variant]["Contagtion_Multiplier"]
if self.agent_data.vaccinated:
current_prob = current_prob * self.agent_data.safetymultiplier
if infected_contact == 2:
current_prob = current_prob * 0.42
if infected_contact > 0:
if self.agent_data.isolated:
if bernoulli_rvs(current_prob) and not(bernoulli_rvs(self.model.model_data.prob_isolation_effective)):
self.stage = Stage.EXPOSED
self.agent_data.variant = variant
self.model.model_data.generally_infected = self.model.model_data.generally_infected + 1
else:
if bernoulli_rvs(current_prob):
#Added vaccination account after being exposed to determine exposure.
self.stage = Stage.EXPOSED
self.agent_data.variant = variant
self.model.model_data.generally_infected = self.model.model_data.generally_infected + 1
# Second opportunity to get infected: residual droplets in places
# TODO
if not(self.agent_data.isolated):
self.move()
elif self.stage == Stage.EXPOSED:
# Susceptible patients only move and spread the disease.
# If the incubation time is reached, it is immediately
# considered as detected since it is severe enough.
# We compute the private value as usual
cellmates = self.model.grid.get_cell_list_contents([self.pos])
isolation_private_divider = 1
isolation_public_divider = 1
if self.agent_data.employed:
if self.agent_data.isolated:
isolation_private_divider = 0.3
isolation_public_divider = 0.01
self.agent_data.cumul_private_value = self.agent_data.cumul_private_value + \
((len(cellmates) - 1) * self.model.model_data.stage_value_dist[ValueGroup.PRIVATE][Stage.EXPOSED])*isolation_private_divider
self.agent_data.cumul_public_value = self.agent_data.cumul_public_value + \
((len(cellmates) - 1) * self.model.model_data.stage_value_dist[ValueGroup.PUBLIC][Stage.EXPOSED])*isolation_public_divider
else:
self.agent_data.cumul_private_value = self.agent_data.cumul_private_value + 0
self.agent_data.cumul_public_value = self.agent_data.cumul_public_value - 2*self.model.model_data.stage_value_dist[ValueGroup.PUBLIC][Stage.EXPOSED]
# Assignment is less expensive than comparison
do_move = True
current_prob_asymptomatic = self.model.model_data.prob_asymptomatic * self.model.model_data.variant_data_list[self.agent_data.variant]["Asymtpomatic_Multiplier"]
if self.agent_data.vaccinated:
current_prob_asymptomatic = 1-(1-self.model.model_data.prob_asymptomatic) * self.agent_data.safetymultiplier #Probability of asymptomatic becomes 1-(probability of symptomatic)*safety_multiplier
# If testing is available and the date is reached, test
if not(self.agent_data.tested or self.agent_data.tested_traced) and bernoulli_rvs(self.agent_data.test_chance):
if bernoulli_rvs(current_prob_asymptomatic):
self.stage = Stage.ASYMPDETECTED
else:
self.stage = Stage.SYMPDETECTED
do_move = False
self.agent_data.tested = True
self.model.model_data.cumul_test_cost = self.model.model_data.cumul_test_cost + self.model.model_data.test_cost
else:
if self.agent_data.curr_incubation < self.agent_data.incubation_time:
self.agent_data.curr_incubation = self.agent_data.curr_incubation + 1
else:
if bernoulli_rvs(current_prob_asymptomatic):
self.stage = Stage.ASYMPTOMATIC
else:
self.stage = Stage.SYMPDETECTED
do_move = False
# Now, attempt to move
if do_move and not(self.agent_data.isolated):
self.move()
# Perform the move once the condition has been determined
elif self.stage == Stage.ASYMPTOMATIC:
# Asymptomayic patients only roam around, spreading the
# disease, ASYMPDETECTEDimmune system
cellmates = self.model.grid.get_cell_list_contents([self.pos])
isolation_private_divider = 1
isolation_public_divider = 1
if self.agent_data.employed:
if self.agent_data.isolated:
isolation_private_divider = 0.3
isolation_public_divider = 0.01
self.agent_data.cumul_private_value = self.agent_data.cumul_private_value + \
((len(cellmates) - 1) * self.model.model_data.stage_value_dist[ValueGroup.PRIVATE][Stage.ASYMPTOMATIC])*isolation_private_divider
self.agent_data.cumul_public_value = self.agent_data.cumul_public_value + \
((len(cellmates) - 1) * self.model.model_data.stage_value_dist[ValueGroup.PUBLIC][Stage.ASYMPTOMATIC])*isolation_public_divider
else:
self.agent_data.cumul_private_value = self.agent_data.cumul_private_value + 0
self.agent_data.cumul_public_value = self.agent_data.cumul_public_value - 2*self.model.model_data.stage_value_dist[ValueGroup.PUBLIC][Stage.ASYMPTOMATIC]
if not(self.agent_data.tested or self.agent_data.tested_traced) and bernoulli_rvs(self.agent_data.test_chance):
self.stage = Stage.ASYMPDETECTED
self.agent_data.tested = True
self.model.model_data.cumul_test_cost = self.model.model_data.cumul_test_cost + self.model.model_data.test_cost
if self.agent_data.curr_recovery >= self.agent_data.recovery_time:
self.stage = Stage.RECOVERED
self.agent_data.variant_immune[self.agent_data.variant] = True
else:
self.agent_data.curr_recovery += 1
if not (self.agent_data.isolated):
self.move()
elif self.stage == Stage.SYMPDETECTED:
# Once a symptomatic patient has been detected, it does not move and starts
# the road to severity, recovery or death. We assume that, by reaching a health
# unit, they are tested as positive.
self.agent_data.isolated = True
self.agent_data.tested = True
current_severe_chance = self.agent_data.mortality_value * self.model.model_data.variant_data_list[self.agent_data.variant]["Mortality_Multiplier"] * (1/(self.model.model_data.dwell_15_day))
if (self.agent_data.vaccinated):
current_severe_chance = current_severe_chance * self.agent_data.safetymultiplier
# Contact tracing logic: use a negative number to indicate trace exhaustion
if self.model.model_data.tracing_now and self.agent_data.tracing_counter >= 0:
# Test only when the count down has been reached
if self.agent_data.tracing_counter == self.agent_data.tracing_delay:
for t in self.agent_data.contacts:
t.test_contact_trace()
self.agent_data.tracing_counter = -1
else:
self.agent_data.tracing_counter = self.agent_data.tracing_counter + 1
self.agent_data.cumul_private_value = self.agent_data.cumul_private_value + \
self.model.model_data.stage_value_dist[ValueGroup.PRIVATE][Stage.SYMPDETECTED]
self.agent_data.cumul_public_value = self.agent_data.cumul_public_value + \
self.model.model_data.stage_value_dist[ValueGroup.PUBLIC][Stage.SYMPDETECTED]
if self.agent_data.curr_incubation + self.agent_data.curr_recovery < self.agent_data.incubation_time + self.agent_data.recovery_time:
self.agent_data.curr_recovery = self.agent_data.curr_recovery + 1
if bernoulli_rvs(current_severe_chance):
self.stage = Stage.SEVERE
else:
self.stage = Stage.RECOVERED
self.agent_data.variant_immune[self.agent_data.variant] = True
elif self.stage == Stage.ASYMPDETECTED:
self.agent_data.isolated = True
# Contact tracing logic: use a negative number to indicate trace exhaustion
if self.model.model_data.tracing_now and self.agent_data.tracing_counter >= 0:
# Test only when the count down has been reached
if self.agent_data.tracing_counter == self.agent_data.tracing_delay:
for t in self.agent_data.contacts:
t.test_contact_trace()
self.agent_data.tracing_counter = -1
else:
self.agent_data.tracing_counter = self.agent_data.tracing_counter + 1
self.agent_data.cumul_private_value = self.agent_data.cumul_private_value + \
self.model.model_data.stage_value_dist[ValueGroup.PRIVATE][Stage.ASYMPDETECTED]
self.agent_data.cumul_public_value = self.agent_data.cumul_public_value + \
self.model.model_data.stage_value_dist[ValueGroup.PUBLIC][Stage.ASYMPDETECTED]
# The road of an asymptomatic patients is similar without the prospect of death
if self.agent_data.curr_incubation + self.agent_data.curr_recovery < self.agent_data.incubation_time + self.agent_data.recovery_time:
self.agent_data.curr_recovery = self.agent_data.curr_recovery + 1
else:
self.stage = Stage.RECOVERED
self.agent_data.variant_immune[self.agent_data.variant] = True
elif self.stage == Stage.SEVERE:
self.agent_data.cumul_private_value = self.agent_data.cumul_private_value + \
self.model.model_data.stage_value_dist[ValueGroup.PRIVATE][Stage.SEVERE]
self.agent_data.cumul_public_value = self.agent_data.cumul_public_value + \
self.model.model_data.stage_value_dist[ValueGroup.PUBLIC][Stage.SEVERE]
# Severe patients are in ICU facilities
if self.agent_data.curr_recovery < self.agent_data.recovery_time:
# Not recovered yet, may pass away depending on prob.
if self.model.model_data.bed_count > 0 and self.agent_data.occupying_bed == False:
self.agent_data.occupying_bed = True
self.model.model_data.bed_count -= 1
if self.agent_data.occupying_bed == False:
if bernoulli(1/(self.agent_data.recovery_time)): #Chance that someone dies at this stage is current_time/time that they should recover. This ensures that they may die at a point during recovery.
self.stage = Stage.DECEASED
# else:
# if bernoulli(0 * 1/self.recovery_time): #Chance that someone dies on the bed is 42% less likely so I will also add that they have a 1/recovery_time chance of dying
# self.stage = Stage.DECEASED
# self.occupying_bed == False
# self.model.bed_count += 1
self.agent_data.curr_recovery = self.agent_data.curr_recovery + 1
else:
self.stage = Stage.RECOVERED
self.agent_data.variant_immune[self.variant] = True
if (self.agent_data.occupying_bed == True):
self.agent_data.occupying_bed == False
self.model.model_data.bed_count += 1
elif self.stage == Stage.RECOVERED:
cellmates = self.model.grid.get_cell_list_contents([self.pos])
if self.agent_data.employed:
isolation_private_divider = 1
isolation_public_divider = 1
if self.agent_data.isolated:
isolation_private_divider = 0.3
isolation_public_divider = 0.01
self.agent_data.cumul_private_value = self.agent_data.cumul_private_value + \
((len(cellmates) - 1) * self.model.model_data.stage_value_dist[ValueGroup.PRIVATE][Stage.RECOVERED])*isolation_private_divider
self.agent_data.cumul_public_value = self.agent_data.cumul_public_value + \
((len(cellmates) - 1) * self.model.model_data.stage_value_dist[ValueGroup.PUBLIC][Stage.RECOVERED])*isolation_public_divider
else:
self.agent_data.cumul_private_value = self.agent_data.cumul_private_value + 0
self.agent_data.cumul_public_value = self.agent_data.cumul_public_value - 2*self.model.model_data.stage_value_dist[ValueGroup.PUBLIC][Stage.RECOVERED]
# A recovered agent can now move freely within the grid again
self.agent_data.curr_recovery = 0
self.agent_data.isolated = False
self.agent_data.isolated_but_inefficient = False
infected_contact = 0
variant = "Standard"
for c in cellmates:
if c.is_contagious() and self.model.model_data.variant_data_list[c.variant]["Reinfection"] == True and (c.stage == Stage.SYMPDETECTED or c.stage == Stage.SEVERE) and self.agent_data.variant_immune[c.variant] != True:
if self.agent_data.isolated and bernoulli_rvs(1 - self.model.model_data.prob_isolation_effective):
self.agent_data.isolated_but_inefficient = True
infected_contact = 1
variant = c.variant
break
else:
infected_contact = 1
variant = c.variant
break
elif c.is_contagious() and (c.stage == Stage.ASYMPTOMATIC or c.stage == Stage.ASYMPDETECTED) and self.agent_data.variant_immune[c.variant] == False:
c.add_contact_trace(self)
if self.agent_data.isolated and bernoulli_rvs(1 - self.model.model_data.prob_isolation_effective):
self.agent_data.isolated_but_inefficient = True
infected_contact = 2
variant = c.variant
else:
infected_contact = 2
variant = c.variant
current_prob = self.agent_data.prob_contagion * self.model.model_data.variant_data_list[variant]["Contagtion_Multiplier"]
if self.agent_data.vaccinated:
current_prob = current_prob * self.agent_data.safetymultiplier
if infected_contact == 2:
current_prob = current_prob * 0.42
if infected_contact > 0:
if self.agent_data.isolated:
if bernoulli_rvs(current_prob) and not (bernoulli_rvs(self.model.model_data.prob_isolation_effective)):
self.stage = Stage.EXPOSED
self.agent_data.variant = variant
else:
if bernoulli_rvs(current_prob):
# Added vaccination account after being exposed to determine exposure.
self.stage = Stage.EXPOSED
self.agent_data.variant = variant
self.move()
elif self.stage == Stage.DECEASED:
self.agent_data.cumul_private_value = self.agent_data.cumul_private_value + \
self.model.model_data.stage_value_dist[ValueGroup.PRIVATE][Stage.DECEASED]
self.agent_data.cumul_public_value = self.agent_data.cumul_public_value + \
self.model.model_data.stage_value_dist[ValueGroup.PUBLIC][Stage.DECEASED]
else:
# If we are here, there is a problem
sys.exit("Unknown stage: aborting.")
#Insert a new trace into the database (AgentDataClass)
id = str(uuid.uuid4())
agent_params = [(
id,
self.agent_data.age_group.value,
self.agent_data.sex_group.value,
self.agent_data.vaccine_willingness,
self.agent_data.incubation_time,
self.agent_data.dwelling_time,
self.agent_data.recovery_time,
self.agent_data.prob_contagion,
self.agent_data.mortality_value,
self.agent_data.severity_value,
self.agent_data.curr_dwelling,
self.agent_data.curr_incubation,
self.agent_data.curr_recovery,
self.agent_data.curr_asymptomatic,
self.agent_data.isolated,
self.agent_data.isolated_but_inefficient,
self.agent_data.test_chance,
self.agent_data.in_isolation,
self.agent_data.in_distancing,
self.agent_data.in_testing,
self.agent_data.astep,
self.agent_data.tested,
self.agent_data.occupying_bed,
self.agent_data.cumul_private_value,
self.agent_data.cumul_public_value,
self.agent_data.employed,
self.agent_data.tested_traced,
self.agent_data.tracing_delay,
self.agent_data.tracing_counter,
self.agent_data.vaccinated,
self.agent_data.safetymultiplier,
self.agent_data.current_effectiveness,
self.agent_data.vaccination_day,
self.agent_data.vaccine_count,
self.agent_data.dosage_eligible,
self.agent_data.fully_vaccinated,
self.agent_data.variant
)]
self.model.db.insert_agent(agent_params)
self.model.db.commit()
self.astep = self.astep + 1
def move(self):
# If dwelling has not been exhausted, do not move
if self.agent_data.curr_dwelling > 0:
self.agent_data.curr_dwelling = self.agent_data.curr_dwelling - 1
# If dwelling has been exhausted, move and replenish the dwell
else:
possible_steps = self.model.grid.get_neighborhood(
self.pos,
moore=True,
include_center=False
)
new_position = self.random.choice(possible_steps)
self.model.grid.move_agent(self, new_position)
self.agent_data.curr_dwelling = poisson_rvs(self.model.model_data.avg_dwell)
########################################
def compute_variant_stage(model, variant, stage):
count = 0
for agent in model.schedule.agents:
if stage == Stage.SUSCEPTIBLE:
if agent.agent_data.variant == variant:
count += 1
else:
if agent.stage == stage and agent.agent_data.variant == variant:
count += 1
return count
def compute_vaccinated_stage(model, stage):
count = 0
for agent in model.schedule.agents:
if agent.stage == stage and agent.agent_data.vaccinated == True:
count += count
vaccinated_count = compute_vaccinated_count(model)
if vaccinated_count == 0:
return 0
else:
return count
def compute_stage(model,stage):
return count_type(model,stage)
def count_type(model, stage):
count = 0
for agent in model.schedule.agents:
if agent.stage == stage:
count = count + 1
return count
def compute_isolated(model):
count = 0
for agent in model.schedule.agents:
if agent.agent_data.isolated:
count = count + 1
return count
def compute_employed(model):
count = 0
for agent in model.schedule.agents:
if agent.agent_data.employed:
count = count + 1
return count
def compute_unemployed(model):
count = 0
for agent in model.schedule.agents:
if not(agent.agent_data.employed):
count = count + 1
return count
def compute_contacts(model):
count = 0
for agent in model.schedule.agents:
count = count + agent.interactants()
return count
def compute_stepno(model):
return model.stepno
def compute_cumul_private_value(model):
value = 0
for agent in model.schedule.agents:
value = value + agent.agent_data.cumul_private_value
return np.sign(value)*np.power(np.abs(value), model.model_data.alpha_private)/model.num_agents
def compute_cumul_public_value(model):
value = 0
for agent in model.schedule.agents:
value = value + agent.agent_data.cumul_public_value
return np.sign(value)*np.power(np.abs(value), model.model_data.alpha_public)/model.num_agents
# Changed the method for calculating the test cost. This will occur in more linear time,
# can also differentiate being tested from being percieved as infected. This will be a rising value,
# will change testing to be based on necessity along with the vaccine.
def compute_cumul_testing_cost(model):
return model.model_data.cumul_test_cost
def compute_cumul_vaccination_cost(model):
return model.model_data.cumul_vaccine_cost
def compute_total_cost(model):
return model.model_data.cumul_test_cost + model.model_data.cumul_vaccine_cost
def compute_tested(model):
tested = 0
for agent in model.schedule.agents:
if agent.agent_data.tested:
tested = tested + 1
return tested
# Added to track the number of vaccinated agents.
def compute_vaccinated(model):
vaccinated_count = 0
for agent in model.schedule.agents:
if agent.agent_data.vaccinated:
vaccinated_count = vaccinated_count + 1
return vaccinated_count
def compute_vaccinated_count(model):
vaccinated_count = 0
for agent in model.schedule.agents:
if agent.agent_data.vaccinated:
vaccinated_count = vaccinated_count + 1
return vaccinated_count
def compute_vaccinated_1(model):
vaccinated_count = 0
for agent in model.schedule.agents:
if agent.agent_data.vaccine_count == 1:
vaccinated_count = vaccinated_count + 1
return vaccinated_count
def compute_vaccinated_2(model):
vaccinated_count = 0
for agent in model.schedule.agents:
if agent.agent_data.vaccine_count == 2:
vaccinated_count = vaccinated_count + 1
return vaccinated_count
def compute_willing_agents(model):
count = 0
for agent in model.schedule.agents:
if agent.agent_data.vaccine_willingness:
count = count + 1
return count
# Another helper function to determine the vaccination of agents based on agegroup.
def compute_vaccinated_in_group_count(model,agegroup):
vaccinated_count = 0
for agent in model.schedule.agents:
if (agent.agent_data.vaccinated) and (agent.agent_data.age_group == agegroup):
vaccinated_count = vaccinated_count + 1
return vaccinated_count
def compute_vaccinated_in_group(model,agegroup):
vaccinated_count = 0
for agent in model.schedule.agents:
if (agent.agent_data.vaccinated) and (agent.agent_data.age_group == agegroup):
vaccinated_count = vaccinated_count + 1
return vaccinated_count
def compute_fully_vaccinated_in_group(model,agegroup):
vaccinated_count = 0
for agent in model.schedule.agents:
if (agent.agent_data.fully_vaccinated) and (agent.agent_data.age_group == agegroup):
vaccinated_count = vaccinated_count + 1
return vaccinated_count
def compute_vaccinated_in_group_percent_vaccine_count(model, agegroup, count):
vaccinated_count = 0
for agent in model.schedule.agents:
if (agent.agent_data.vaccine_count == count) and (agent.agent_data.age_group == agegroup):
vaccinated_count = vaccinated_count + 1
return vaccinated_count
def cumul_effectiveness_per_group_vaccinated(model,agegroup):
vaccinated_count = 0
effectiveness = 0
for agent in model.schedule.agents:
if (agent.agent_data.age_group == agegroup and agent.agent_data.vaccinated == True):
vaccinated_count = vaccinated_count + 1
effectiveness += agent.agent_data.safetymultiplier
if (vaccinated_count > 0):
return 1-(effectiveness / vaccinated_count)
else:
return 0
def cumul_effectiveness_per_group(model,agegroup):
agent_count = 0
effectiveness = 0
for agent in model.schedule.agents:
if (agent.agent_data.age_group == agegroup):
agent_count = agent_count + 1
effectiveness += agent.agent_data.safetymultiplier
if (agent_count > 0):
return 1-(effectiveness / agent_count)
else:
return 0
def compute_age_group_count(model,agegroup):
count = 0
for agent in model.schedule.agents:
if agent.agent_data.age_group == agegroup:
count = count + 1
return count
def compute_eligible_age_group_count(model,agegroup):
count = 0
for agent in model.schedule.agents:
if (agent.agent_data.age_group == agegroup) and (agent.agent_data.stage == Stage.SUSCEPTIBLE or agent.agent_data.stage == Stage.EXPOSED or agent.agent_data.stage == Stage.ASYMPTOMATIC) and agent.agent_data.dosage_eligible and agent.agent_data.vaccine_willingness:
count = count + 1
return count
def update_vaccination_stage(model):
initial_stage = model.model_data.vaccination_stage
# if compute_eligible_age_group_count(model, AgeGroup.C80toXX) < 1:
# model.model_data.vaccination_stage = VaccinationStage.C70to79
# if compute_eligible_age_group_count(model, AgeGroup.C70to79) < 1:
# model.model_data.vaccination_stage = VaccinationStage.C60to69
# if compute_eligible_age_group_count(model, AgeGroup.C60to69) < 1:
# model.model_data.vaccination_stage = VaccinationStage.C50to59
# if compute_eligible_age_group_count(model, AgeGroup.C50to59) < 1:
# model.model_data.vaccination_stage = VaccinationStage.C40to49
# if compute_eligible_age_group_count(model, AgeGroup.C40to49) < 1:
# model.model_data.vaccination_stage = VaccinationStage.C30to39
# if compute_eligible_age_group_count(model, AgeGroup.C30to39) < 1:
# model.model_data.vaccination_stage = VaccinationStage.C20to29
# if compute_eligible_age_group_count(model, AgeGroup.C20to29) < 1:
# model.model_data.vaccination_stage = VaccinationStage.C10to19
# if compute_eligible_age_group_count(model, AgeGroup.C10to19) < 1:
# model.model_data.vaccination_stage = VaccinationStage.C00to09
# else:
# model.model_data.vaccination_stage = VaccinationStage.C80toXX
eligible_age_group_dict = {}
eligible_age_group_dict[compute_eligible_age_group_count(model, AgeGroup.C80toXX)] = VaccinationStage.C70to79
eligible_age_group_dict[compute_eligible_age_group_count(model, AgeGroup.C70to79)] = VaccinationStage.C60to69
eligible_age_group_dict[compute_eligible_age_group_count(model, AgeGroup.C60to69)] = VaccinationStage.C50to59
eligible_age_group_dict[compute_eligible_age_group_count(model, AgeGroup.C50to59)] = VaccinationStage.C40to49
eligible_age_group_dict[compute_eligible_age_group_count(model, AgeGroup.C40to49)] = VaccinationStage.C30to39
eligible_age_group_dict[compute_eligible_age_group_count(model, AgeGroup.C30to39)] = VaccinationStage.C20to29
eligible_age_group_dict[compute_eligible_age_group_count(model, AgeGroup.C10to19)] = VaccinationStage.C00to09
model.model_data.vaccination_stage = VaccinationStage.C80toXX
for key,value in sorted(eligible_age_group_dict.items(), reverse=True):
if (key < 1):