forked from zhangrom000/Contagion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.py
More file actions
190 lines (158 loc) · 6.32 KB
/
Model.py
File metadata and controls
190 lines (158 loc) · 6.32 KB
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
"""
Class Model
The driver for the Contagion simulation. Contains data relevant to the
maintenance and analysis of the simulation such as: duration, the grid, metrics,
and graphical outputs.
"""
#Infection will take into acount base probability, number of infected
#rats and humans, population density, pollution level, resource availability,
# and whether the cell is quarantined or not.
import numpy as N
import matplotlib.pyplot as plt
from Grid import Grid
from Visualize import Visualize
## Runtime Variables ##
TIMELINE = 365 * 3 #Timestep duration of this simulation
DT = 1 #Timestep
TIME = (int) (TIMELINE / DT)
graph = True #Play the results in real time for single run
travelers = True
gridType = 0
plagueType = 0 #0=Black Plague, 1=Flu, 2=Modern Plague, 3=Justinian Plague
numSims = 25
multipleSims = 0 #0 for one run, 1 for multiple, 2 for full test
########################
## Methods ##
def singleRun(TIME, travelers, gridType, plagueType):
dataArr = N.zeros((TIME, 6)) #Array of infection data per timestep
G.init(travelers, gridType, plagueType) #Initialize the grid
f, axs = V.update_plot(G)
carrierScatter,travelerScatter = V.plot_agents(G)
for dt in range(TIME):
#print dt
dead, infected, alive, recovered, susceptible, carriers = G.updateGrid()
dataArr[dt][0] = dead
dataArr[dt][1] = infected
dataArr[dt][2] = alive
dataArr[dt][3] = recovered
dataArr[dt][4] = susceptible
dataArr[dt][5] = carriers
carrierScatter.remove()
travelerScatter.remove()
carrierScatter,travelerScatter = V.plot_agents(G)
V.update_plot(G, f, axs, graph)
if len(G.CARRIERS) == 0:
TIME = dt
break
plt.figure("Population")
# dead
plt.plot(range(TIME), dataArr[:TIME,0], 'k', label="Dead")
# infected
plt.plot(range(TIME), dataArr[:TIME,1], 'r', label="Infected")
# alive
plt.plot(range(TIME), dataArr[:TIME,2], 'g', label="Alive")
# recovered
plt.plot(range(TIME), dataArr[:TIME,3], 'b', label="Recovered")
# susceptible
plt.plot(range(TIME), dataArr[:TIME,4], 'y', label="Susceptible")
plt.xlabel("Time (days)")
plt.ylabel("Population Size")
plt.legend(fontsize=8, loc=1, borderaxespad=0.)
plt.show()
plt.figure("Carrier Population")
plt.plot(range(TIME), dataArr[:TIME,5], label="Carriers") # dead
plt.xlabel("Time (days)")
plt.ylabel("Carrier Population Size")
plt.show()
def multipleRuns(TIME, numSims, travelers, gridType, plagueType):
dataArr = N.zeros((TIME, 6)) #Array of infection data per timestep
for run in range(numSims):
G.init(travelers, gridType, plagueType) #Initialize the grid
print run
for dt in range(TIME):
dead, infected, alive, recovered, susceptible, carriers = \
G.updateGrid()
dataArr[dt][0] += dead
dataArr[dt][1] += infected
dataArr[dt][2] += alive
dataArr[dt][3] += recovered
dataArr[dt][4] += susceptible
dataArr[dt][5] += carriers
if len(G.CARRIERS) == 0:
dt = TIME
dataArr = dataArr / numSims
plt.figure("Average Population Versus Time")
# dead
plt.plot(range(TIME), dataArr[:TIME,0], 'k', label="Dead")
# infected
plt.plot(range(TIME), dataArr[:TIME,1], 'r', label="Infected")
# alive
plt.plot(range(TIME), dataArr[:TIME,2], 'g', label="Alive")
# recovered
plt.plot(range(TIME), dataArr[:TIME,3], 'b', label="Recovered")
# susceptible
plt.plot(range(TIME), dataArr[:TIME,4], 'y', label="Susceptible")
plt.xlabel("Time (days)")
plt.ylabel("Population Size")
plt.legend(fontsize=8, loc=1, borderaxespad=0.)
plt.show()
plt.figure("Average Carrier Population Versus Time")
plt.plot(range(TIME), dataArr[:TIME,5], label="Carriers") # dead
plt.xlabel("Time (days)")
plt.ylabel("Carrier Population Size")
plt.show()
def fullTest(TIME, numSims):
testCase("Base Case", TIME, numSims, True, 0, 0)
testCase("Modern Plague Case", TIME, numSims, True, 0, 2)
testCase("Justinian Plague Case", TIME, numSims, True, 0, 3)
testCase("Flu Case", TIME, numSims, True, 0, 1)
testCase("Low Density Grid Case", TIME, numSims, True, 1, 0)
testCase("High Density Grid Case", TIME, numSims, True, 2, 0)
testCase("No Traveler Case", TIME, numSims, False, 0, 0)
def testCase(name, TIME, numSims, travelers, gridType, plagueType):
dataArr = N.zeros((TIME, 6)) #Array of infection data per timestep
print name
for run in range(numSims):
G.init(travelers, gridType, plagueType) #Initialize the grid
print run
for dt in range(TIME):
print dt
dead, infected, alive, recovered, susceptible, carriers = G.updateGrid()
dataArr[dt][0] += dead
dataArr[dt][1] += infected
dataArr[dt][2] += alive
dataArr[dt][3] += recovered
dataArr[dt][4] += susceptible
dataArr[dt][5] += carriers
if len(G.CARRIERS) == 0:
dt = TIME
dataArr = dataArr / numSims
plt.figure(name + " Average Population Versus Time")
# dead
plt.plot(range(TIME), dataArr[:TIME,0], 'k', label="Dead")
# infected
plt.plot(range(TIME), dataArr[:TIME,1], 'r', label="Infected")
# alive
plt.plot(range(TIME), dataArr[:TIME,2], 'g', label="Alive")
# recovered
plt.plot(range(TIME), dataArr[:TIME,3], 'b', label="Recovered")
# susceptible
plt.plot(range(TIME), dataArr[:TIME,4], 'y', label="Susceptible")
plt.xlabel("Time (days)")
plt.ylabel("Population Size")
plt.legend(fontsize=8, loc=1, borderaxespad=0.)
plt.show()
plt.figure(name + "Average Carrier Population Versus Time")
plt.plot(range(TIME), dataArr[:TIME,5], label="Carriers") # dead
plt.xlabel("Time (days)")
plt.ylabel("Carrier Population Size")
plt.show()
######### MAIN #########
G = Grid()
V = Visualize()
if multipleSims == 0:
singleRun(TIME, travelers, gridType, plagueType)
elif multipleSims == 1:
multipleRuns(TIME, numSims, travelers, gridType, plagueType)
elif multipleSims == 2:
fullTest(TIME, numSims)