forked from jorgevc/epi-network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Control_protocol.py
142 lines (121 loc) · 4.18 KB
/
Control_protocol.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
# control_protocol.py
#
# Copyright 2018 Jorge Velazquez Castro
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
import numpy as np
import random as rnd
class controlProtocol:
def __init__(self,params,P_network):
self.number_of_patches = len(params)
self.observations = [[0]*5]*self.number_of_patches
self.params = params
self.P_network = P_network
self.last_observation_time = None
self.observation_interval = 1.
self.control = [[0.,0.]]*self.number_of_patches
self.TRh=[] #human Transmission index
self.VR=[] #Vulnerability index (human)
self.TRv=[] #vector Transmission idex
self.last_control_time = 0.
self.control_interval = 1.
def observe(self,y,t):
self.observations=y
self.last_observation_time=t
self.calculate_indices(t)
def observation_time(self,t):
if(self.last_observation_time == None or (t - self.last_observation_time) > self.observation_interval):
return True
else:
return False
def recalculate_control(self):
TRh = self.TRh[-1][1]
TRh_max = max(TRh)
max_transmition_patch = TRh.index(TRh_max)
#if( TRh_max > 2.):
self.control = [[0.,0.]] * self.number_of_patches
self.control[max_transmition_patch] = [0.5,0.5]
def calculate_control(self,t):
if(t-self.last_control_time > self.control_interval):
self.recalculate_control()
def get_control(self,i):
return self.control[i]
def calculate_indices(self,t):
n=self.number_of_patches
Sh=[]
Ih=[]
Rh=[]
Sv=[]
Iv=[]
beta=[]
gamma=[]
mu=[]
W=[]
Nh=[]
for i in range(n):
Sh.append(self.observations[i][0])
Ih.append(self.observations[i][1])
Rh.append(self.observations[i][2])
Sv.append(self.observations[i][3])
Iv.append(self.observations[i][4])
beta.append(self.params[i][0])
gamma.append(self.params[i][1])
mu.append(self.params[i][4])
Nh.append(Sh[i] + Ih[i] + Rh[i])
Rv=np.zeros((n,n))
Rh=np.zeros((n,n))
p=self.P_network.matrix
for k in range(n):
W.append(np.dot(Nh,p[:,k]))
for j in range(n):
Rv[k,j]=(beta[j]*Sh[j]*p[j,k]*Iv[k])/(mu[k]*W[k]) #Secondary human infections of residentes from patch j produced by infected vectors in k (See ec. 20 of ref [1])
Rh[j,k]=beta[k]*Ih[j]*p[j,k]*Sv[k]/(gamma[j]*W[k]) #Secondary vector cases in patch k caused by infected residents of patch i
self.TRh.append((t,[(sum(np.dot(Rh,Rv)[i,:])) for i in range(n)])) #secondary infections generated by a single individual of patch i
self.VR.append((t,[(sum(np.dot(Rh,Rv)[:,j])) for j in range(n)])) #vulnerability of patch j
self.TRv.append((t, [sum(Rv[i,:]) for i in range(n)])) #vector transmission index (verificar)
return self.TRh[-1][1]
def set_observation_interval(self, dt):
self.observation_interval = dt
if(self.control_interval==None):
self.control_interval = dt
def set_control_interval(self, dt):
self.control_interval = dt
if(self.observation_interval == None):
self.observation_interval=dt
def plot_TRindex(self, i):
plt.figure()
plt.xlabel('Tiempo')
plt.ylabel('TR_i')
plt.plot(self.time,self.evolution[i][1][:])
plt.show()
class noControl:
def __init__(self,default = 0):
self.default_control= [default,default]
def observe(self,y,t):
pass
def observation_time(self,t):
return False
def calculate_control(self,t):
pass
def get_control(self,i):
return self.default_control
class randomControl(controlProtocol):
def recalculate_control(self):
random_patch = rnd.randint(0,self.number_of_patches-1)
self.control = [[0.,0.]] * self.number_of_patches
self.control[random_patch] = [0.5,0.5]