-
Notifications
You must be signed in to change notification settings - Fork 1
/
nodeGrid.py
131 lines (96 loc) · 3.99 KB
/
nodeGrid.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
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib as mpl
import networkx as nx
from random import *
import numpy as np
from scipy.integrate import odeint
#from all import *
#local global ?
#utiliser les modeles ?
#changer x et y en p
class NodeGrid(object):
"""docstring for NodeGrid."""
def __init__(self, scenario='global',N=101,I0=1,R0=0,S0=100,t=1000,param={"beta" : 0.2, "gamma" : 1./10},sim_type="SIR"):
self.sizeX = int((N**0.5)+1)
self.sizeY = int(N/int(N**0.5))
self.N=N
self.scenario=scenario
#self.b_Color = 'grey'
#self.s_Color = 'yellow'
#self.e_Color = 'blue'
#self.i_Color = 'red'
#self.r_Color = 'green'
self.prepareGraph()
Ep = Epidemy(N,I0,R0,S0,t,param)
self.S, self.I, self.R = Ep.solve(sim_type)
#pour l'instant, ne pas choisir autre chose que sir
#print(len(self.S),len(self.I),len(self.R))
#print(self.S[i],self.I[t],self.R[t])
pas=5
for i in range (0,t-1,pas):
#print(self.S[i],self.I[i],self.R[i])
self.spread(i,pas)
self.plot()
def prepareGraph(self):
self.G = nx.grid_2d_graph(self.sizeX, self.sizeY)
# Sans le pos, le graphe s'imprime de manière aléatoire et non ordonée
self.pos = dict( (n, n) for n in self.G.nodes() )
# Sans le labels on voit rien, y'a un label automatique : (i,j)
# sur chaque noeud
self.labels = dict( ((i, j), " ") for i, j in self.G.nodes() )
self.colors = ['yellow' for i in range(len(self.G.nodes()))]
def changeNodeAtColor(self, i, j, color):
self.colors[i*self.sizeY + j] = color
def spread(self,t,dt):
#print(t,t-dt)
#to_change=[]
#delta_e=
#delta_s=int(((self.I[t]-self.R[t-1])/100)*self.N)
delta_i=int(((self.I[t]-self.I[t-dt])/100)*self.N)
delta_r=int(((self.R[t]-self.R[t-dt])/100)*self.N)
#print((int(self.S[t-dt])/100)*self.N, (int(self.I[t])/100)*self.N, (int(self.R[t])/100)*self.N)
#print((int(self.S[t])/100)*self.N, (int(self.I[t])/100)*self.N, (int(self.R[t])/100)*self.N)
#Ajout des nouveaux infectés
if delta_i>0:
for new_victim in range (delta_i):
if 'yellow' in self.colors:
_new_x=randint(0,self.sizeX)
_new_y=randint(0,self.sizeY)
col='red'
while col!='yellow':
_new_x=randint(0,self.sizeX)
_new_y=randint(0,self.sizeY)
try:
col=self.colors[_new_x*self.sizeY + _new_y]
except:
col='red'
#to_change.append((i*j,i_victim*j_victim))
self.changeNodeAtColor(_new_x,_new_y,'red')
else:
print('error 404, pas de susceptible')
# suceptible jaune
# infecté rouge
# gueri vert
#Ajout des nouveaux recovered
if delta_r>0 :
for new_victim in range (delta_r):
if 'red' in self.colors:
_new_x=randint(0,self.sizeX)
_new_y=randint(0,self.sizeY)
col='green'
while col!='red':
_new_x=randint(0,self.sizeX)
_new_y=randint(0,self.sizeY)
try:
col=self.colors[_new_x*self.sizeY + _new_y]
except:
col='green'
#to_change.append((i*j,i_victim*j_victim))
self.changeNodeAtColor(_new_x,_new_y,'green')
else:
print("erreur 404, pas d'infectés")
#self.G.add_edges_from(to_change)
def plot(self):
nx.draw_networkx(self.G, pos=self.pos, labels=self.labels, node_color=self.colors )
plt.show()