-
Notifications
You must be signed in to change notification settings - Fork 0
/
Covid Simulation.py
271 lines (228 loc) · 9.17 KB
/
Covid 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
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
# Author : Ketan Bhailikar
# Requrements : 1) Python3.x
# 2) pygame [https://pypi.org/project/pygame/]
import pygame
import matplotlib.pyplot as plt
import random
import math
import time
pygame.init()
startT = time.time()
# Colour Code :-
# Alive and Uninfected : White
# Alive and Infected : Red
# Alive and Immune : Blue ( People who have been infected before )
# Dead : Grey
# Variable that you can alter:
screenSize = 800 # Screen Size
cellSize = 5 # Size of individual Person
populationSize = 400 # Number of People
speed = 0.09 # Increase/Decrease in Speed of individual person (Recomended speed = 0.001 - 0.009)
infectedTime = 5 # Amount of time for which a person can remain infected
probImmunity = 50 # Probability if a person getting immune to the virus after being infected ( If the person does not die )
probabilityOfDying = 50 # Probability that an infected person will die
radiusOfSpread = 20 # The maximum distance that a person has to remain from a infected person to not get infecte
probabilityOfInfecting = 30 # Probability of a person gettinTrueg infected if he comes in contact with a infected person
lockDown = False # Is there lockdown?
selfIsolation = False # Are the infected people isolated?s
infectionRecognitionTime = 3 # Time required to isolate infected people ( Works only if self-isolation is True )
showGraph = True # Should the graph be shown at the end?
people = [] # this array holds the list of people
class person():
def __init__(self):
self.infected = False
self.removed = False
if selfIsolation:
while True:
self.x = random.randint(0, screenSize)
self.y = random.randint(0, screenSize)
if (self.x < (screenSize//4) and self.y > (screenSize - (screenSize//4))) == False:
break
else:
self.x = random.randint(0, screenSize)
self.y = random.randint(0, screenSize)
self.xvel = 0
self.yvel = 0
self.carryingTime = 0
self.dead = False
self.infectionStartTime = 0
self.isolated = False
# If there is Lockdown then only 4% of the people will move around
if lockDown:
if random.randint(0, 100) < 4:
self.moveable = True
else:
self.moveable = False
else:
self.moveable = True
def move(self):
# removing the person
if time.time() - self.carryingTime > infectedTime and self.carryingTime != 0 and self.removed == False:
self.remove()
# infect people around the infected person
if self.infected:
if selfIsolation and self.isolated == False:
if self.infectionStartTime != 0:
if time.time() - self.infectionStartTime > infectionRecognitionTime:
self.isolate()
else:
self.infectionStartTime = time.time()
for i in range(len(people)):
d = math.sqrt(
(math.pow(self.x - people[i].x, 2)) + (math.pow(self.y - people[i].y, 2)))
if (d < radiusOfSpread) and random.randint(0, 100) < probabilityOfInfecting and people[i].removed == False and people[i].infected == False:
people[i].infect()
if self.moveable:
# Randomly changing the speed
self.yvel += random.randint(-1, 1)*speed
self.xvel += random.randint(-1, 1)*speed
else:
self.xvel = 0
self.yvel = 0
# Changing the positon according to the change in time ( Euler Integration (I guess) )
self.x += self.xvel
self.y += self.yvel
# this method removes a person
def remove(self):
self.removed = True
self.infected = False
# Determining the person will die or not ( probability )
k = random.randint(0, 100)
if k < probabilityOfDying:
self.dead = True
else:
self.dead = False
if random.randint(0, 100) > probImmunity:
self.removed = False
# this method infects a person
def infect(self):
self.infected = True
self.carryingTime = time.time()
# this method checks if the person hits a wall,
# if the person hits a wall then reverse their direction
def checkBounds(self):
if self.x <= 0 or self.x >= screenSize-cellSize:
if self.x < screenSize//2:
self.x = 1
else:
self.x = screenSize - cellSize - 1
self.xvel *= -0.5
if self.y <= 0 or self.y >= screenSize-cellSize:
if self.y < screenSize//2:
self.y = 1
else:
self.y = screenSize - cellSize - 1
self.yvel *= -0.5
# Check collisions with the isolation chamber ( If it is enabled )
if selfIsolation:
if self.isolated == False and self.x < (screenSize//4) and self.y > (screenSize - (screenSize//4)):
self.xvel *= -1
self.yvel *= -1
# this method draws the person onto the screen
def drawCell(self, win):
colour = (255, 255, 255)
if self.removed:
if self.dead:
colour = (50, 50, 50)
else:
colour = (0, 0, 200)
elif self.infected:
colour = (200, 0, 0)
pygame.draw.circle(win, colour, (self.x, self.y), cellSize)
if selfIsolation:
pygame.draw.rect(win, (255, 255, 255), pygame.Rect(
0, screenSize-(screenSize//4), (screenSize//4), (screenSize//4)), 3)
def isolate(self):
self.isolated = True
self.moveable = False
self.x = random.randint(0, (screenSize//4)-radiusOfSpread)
self.y = random.randint(
screenSize-(screenSize//4)+radiusOfSpread, screenSize)
# driver code
def main():
global people
finfected = open("infected.txt", "w")
fimmune = open("immune.txt", "w")
fdead = open("dead.txt", "w")
# add multiple "person" objects in the "people" array
for i in range(populationSize):
people.append(person())
# infect a single person randomly
people[random.randint(0, 49)].infect()
# create a window to display the simulation
win = pygame.display.set_mode((screenSize, screenSize))
# set the name of the window to "Covid Simulation"
pygame.display.set_caption("Covid Simulation")
# the main loop:
loop = True
while loop:
infectedc = 0
immunec = 0
deadc = 0
# draw the black background
pygame.draw.rect(win, (0, 0, 0), pygame.Rect(
0, 0, screenSize, screenSize))
# change the position, draw the people
for pers in people:
if pers.infected:
infectedc += 1
if pers.removed and pers.dead == False:
immunec += 1
if pers.dead:
deadc += 1
pers.move()
pers.drawCell(win)
pers.checkBounds()
if showGraph:
finfected.write(str(time.time()-startT)+","+str(infectedc)+"\n")
fimmune.write(str(time.time()-startT)+","+str(immunec)+"\n")
fdead.write(str(time.time()-startT)+","+str(deadc)+"\n")
if infectedc == 0:
loop = False
# check if "X" is pressed if yes then Quit
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop = False
quit()
# update the display
pygame.display.flip()
# Display the Graph after the virus is extinct
if showGraph:
xs1 = []
ys1 = []
xs2 = []
ys2 = []
xs3 = []
ys3 = []
graph_data1 = open('infected.txt', 'r').read()
lines1 = graph_data1.split('\n')
graph_data2 = open('immune.txt', 'r').read()
lines2 = graph_data2.split('\n')
graph_data3 = open('dead.txt', 'r').read()
lines3 = graph_data3.split('\n')
for line1 in lines1:
if len(line1) > 1:
x, y = line1.split(',')
xs1.append(float(x))
ys1.append(float(y))
for line2 in lines2:
if len(line2) > 1:
x, y = line2.split(',')
xs2.append(float(x))
ys2.append(float(y))
for line3 in lines3:
if len(line3) > 1:
x, y = line3.split(',')
xs3.append(float(x))
ys3.append(float(y))
plt.plot(xs1, ys1)
plt.plot(xs2, ys2)
plt.plot(xs3, ys3)
plt.xlabel("Time")
plt.ylabel("Number of People")
plt.legend(["Infected", "Immune", "Dead"])
plt.ylim(0, 400)
plt.xlim(0, 35)
plt.show()
# run the simulation
main()