-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain-flu.py
206 lines (176 loc) · 6.56 KB
/
main-flu.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
import sys
from CellularAutomata import CellularAutomata
from Grid2D import Grid2D_Fixed, Grid2D_Periodic, Grid2D_Reflective
from GUI import GUILoop
from Color import Color
from numpy import random
import numpy as np
import matplotlib.pyplot as plt
class Flu(CellularAutomata):
pass
#number of states is 5
EMPTY = 0
SUSCEPTIBLE = 1
INFECTED = 2
RECOVERED = 3
DIED = 4
#input parameters
DAYS = 28
P_INFECTED = 0.125
P_RECOVERED = 0.00001
P_DIED = 0.03
def __init__(self):
self.m_TimeStep = 0
self.m_I = None
self.m_Sus = []
self.m_Inf = []
self.m_Rec = []
self.m_Died = []
self.m_Days = []
self.m_ElapsedDays = 0
def initCond(self):
self.m_I = np.zeros((self.m_Grid2D.getHeight(), self.m_Grid2D.getWidth()), dtype=np.int32)
for j in range(0, self.m_Grid2D.getHeight()):
for i in range(0, self.m_Grid2D.getWidth()):
self.m_I[j][i] = 0
self.m_Grid2D.initCond(j, i, self.SUSCEPTIBLE)
if i == int(self.m_Grid2D.getWidth() / 2) and j == int(self.m_Grid2D.getHeight() / 2) :
self.m_Grid2D.initCond(j, i, self.INFECTED)
self.m_I[j][i] = self.DAYS
def statistic(self):
acc_s = 0
acc_i = 0
acc_r = 0
acc_d = 0
for j in range(0, self.m_Grid2D.getHeight()):
for i in range(0, self.m_Grid2D.getWidth()):
s = self.m_Grid2D.getState(j,i)
if s == self.SUSCEPTIBLE:
acc_s = acc_s + 1
elif s == self.INFECTED:
acc_i = acc_i + 1
elif s == self.DIED:
acc_d = acc_d + 1
elif s == self.RECOVERED:
acc_r = acc_r + 1
self.m_Sus.append(acc_s)
self.m_Inf.append(acc_i)
self.m_Rec.append(acc_r)
self.m_Died.append(acc_d)
self.m_Days.append(self.m_ElapsedDays)
def update(self):
'''
Orientação da vizinhança
nw | n | ne
----|---|----
w | c | e
----|---|----
sw | s | se
'''
for j in range(0, self.m_Grid2D.getHeight()):
for i in range(0, self.m_Grid2D.getWidth()):
sum = 0
if self.m_Grid2D.getState(j+1, i-1) == self.INFECTED:
sum = sum + 1
if self.m_Grid2D.getState(j+1, i) == self.INFECTED:
sum = sum + 1
if self.m_Grid2D.getState(j+1, i+1) == self.INFECTED:
sum = sum + 1
if self.m_Grid2D.getState(j, i-1) == self.INFECTED:
sum = sum + 1
if self.m_Grid2D.getState(j, i+1) == self.INFECTED:
sum = sum + 1
if self.m_Grid2D.getState(j-1, i-1) == self.INFECTED:
sum = sum + 1
if self.m_Grid2D.getState(j-1, i) == self.INFECTED:
sum = sum + 1
if self.m_Grid2D.getState(j-1, i+1) == self.INFECTED:
sum = sum + 1
my_state = self.m_Grid2D.getState(j, i)
if my_state == self.SUSCEPTIBLE:
if random.random() < (sum * self.P_INFECTED):
self.m_I[j][i] = self.DAYS
self.m_Grid2D.setState(j, i, self.INFECTED)
else:
self.m_Grid2D.setState(j, i, my_state)
elif my_state == self.INFECTED:
self.m_I[j][i] = self.m_I[j][i] - 1
if self.m_I[j][i] == 0:
if random.random() < self.P_DIED:
self.m_Grid2D.setState(j, i, self.DIED)
else:
self.m_Grid2D.setState(j, i, self.RECOVERED)
elif random.random() < self.P_RECOVERED:
self.m_I[j][i] == 0
self.m_Grid2D.setState(j, i, self.RECOVERED)
else:
self.m_Grid2D.setState(j, i, my_state)
else:
self.m_Grid2D.setState(j, i, my_state)
self.m_ElapsedDays = self.m_ElapsedDays + 1
CellularAutomata.update(self)
self.statistic()
def finalCond(self):
print('Final')
x = self.m_Grid2D.getWidth() * self.m_Grid2D.getHeight()
for i in range(0, len(self.m_Sus)):
self.m_Sus[i] = self.m_Sus[i] / x
for i in range(0, len(self.m_Inf)):
self.m_Inf[i] = self.m_Inf[i] / x
for i in range(0, len(self.m_Rec)):
self.m_Rec[i] = self.m_Rec[i] / x
for i in range(0, len(self.m_Died)):
self.m_Died[i] = self.m_Died[i] / x
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(self.m_Days, self.m_Sus, color='black', linewidth=3)
ax.plot(self.m_Days, self.m_Inf, color='blue', linewidth=3)
ax.plot(self.m_Days, self.m_Rec, color='green', linewidth=3)
ax.plot(self.m_Days, self.m_Died, color='red', linewidth=3)
ax.legend(['Suscetível', 'Infectado', 'Recuperado', 'Morto'])
plt.show()
def name(self):
return 'Flu demo based on Cellular Automaton'
if __name__ == '__main__':
#grid = Grid2D_Fixed(128, 64)
#grid.setConstant(0) #fixed value on the boundary condition
#boundary = 'fixed'
#boundary = 'periodic'
boundary = 'fixed'
w = 100
h = 100
palettes = []
palettes.append(Color(0, 0, 0)) #black
palettes.append(Color(255, 255, 255)) #white
palettes.append(Color(138, 43, 226)) #BlueViolet
palettes.append(Color(0, 191, 255)) #DeepSkyBlue
palettes.append(Color(32, 32, 32))
if boundary == 'fixed':
grid = Grid2D_Fixed(w, h)
elif boundary == 'periodic':
grid = Grid2D_Periodic(w, h)
elif boundary == 'reflective':
grid = Grid2D_Reflective(w,h)
else:
print('NO BOUNDARIES CONDITION DEFINED')
sys.exit(-1)
CA = Flu()
CA.setGrid(grid)
CA.initCond()
gui = GUILoop(CA)
gui.setPalette(palettes)
gui.setCellularAutomata(CA)
gui.init()
gui.loop()
#for i in range(0, 150):
# print('Dias: ', i)
# CA.update()
CA.finalCond()
'''
for i in range(0, 10):
print('Step: ', i)
CA.update()
CA.finalCond()
'''
print('Hello')
# See PyCharm help at https://www.jetbrains.com/help/pycharm/