-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTABD.py
215 lines (137 loc) · 4.65 KB
/
TABD.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
import numpy as np
import matplotlib.pyplot as plt
import psycopg2
import math
from matplotlib.animation import FuncAnimation
import datetime
from postgis import Polygon,MultiPolygon
from postgis.psycopg import register
#define the step in seconds of the animation
step = 10
conn = psycopg2.connect("dbname=postgres user=bmfpinto")
register(conn)
cursor_psql = conn.cursor()
sql = """select distinct taxi from tracks order by 1"""
cursor_psql.execute(sql)
results = cursor_psql.fetchall()
taxis_x ={}
taxis_y ={}
ts_i = 1570665600
#porque este tempo final? ha registos mais tarde
ts_f = 1570667000
array_size = int(24*60*60/step)
for row in results:
taxis_x[int(row[0])] = np.zeros(array_size)
taxis_y[int(row[0])] = np.zeros(array_size)
for i in range(ts_i,ts_f,10):
sql = "select taxi,st_pointn(proj_track," + str(i) + "-ts) from tracks where ts<" + str(i) + " and ts+st_numpoints(proj_track)>" + str(i)
cursor_psql.execute(sql)
results = cursor_psql.fetchall()
for row in results:
x,y = row[1].coords
taxis_x[int(row[0])][int((i-ts_i)/10)] = x
taxis_y[int(row[0])][int((i-ts_i)/10)] = y
offsets = []
for i in range(array_size):
l = []
for j in taxis_x:
l.append([taxis_x[j][i],taxis_y[j][i]])
offsets.append(l)
conn.close()
offsets = np.array(offsets)
# first 2 infections at random
conn = psycopg2.connect("dbname=postgres user=bmfpinto")
register(conn)
cursor_psql = conn.cursor()
#select first 10 taxis in PORTO
sql3 = """select distinct taxi , ts from tracks where st_contains((select st_collect(proj_boundary) from cont_aad_caop2018 where concelho='PORTO'), st_startpoint(proj_track)) ORDER BY ts limit 10"""
cursor_psql.execute(sql3)
results_i = cursor_psql.fetchall()
t_P=[]
for taxi in results_i:
t_P.append(taxi[0])
print(t_P)
#select first 10 taxis in LISBON
sql4 = """select distinct taxi , ts from tracks where st_contains((select st_collect(proj_boundary) from cont_aad_caop2018 where concelho='LISBOA'), st_startpoint(proj_track)) ORDER BY ts limit 10"""
cursor_psql.execute(sql4)
results_j = cursor_psql.fetchall()
t_L=[]
for taxi in results_j:
t_L.append(taxi[0])
print(t_L)
# select 2 at random, one from each district
import random as rd
start_infection = []
start_infection.append(int(rd.choice(t_P)))
start_infection.append(int(rd.choice(t_L)))
print(start_infection)
#find index in array
i_infection =[0,0]
taxis_l=list(taxis_x.keys())
i_infection[0] , i_infection[1] = taxis_l.index(start_infection[0]) , taxis_l.index(start_infection[1])
print(i_infection)
# initial infection states
s = np.zeros(len(offsets[0]))
i_infection =[0,0]
taxis_l=list(taxis_x.keys())
i_infection[0] , i_infection[1] = taxis_l.index(start_infection[0]) , taxis_l.index(start_infection[1])
print(i_infection)
# initial infection states
s = np.zeros(len(offsets[0]))
s[i_infection[0]] , s[i_infection[1]] = 1 , 1
s[i_infection[0]] , s[i_infection[1]] = 1 , 1
states=[]
for time in range(len(offsets)):
states.append(s)
print(len(states),len(offsets),len(offsets[0]),len(states[0]))
min_dist=50
count=0
#calculate distances
interactions=[]
for time in range(200):
print('time:',time)
time_i=[]
for t_i in range(len(offsets[0])):
interacts=[]
for t_j in range(t_i +1,len(offsets[0])):
coord_i , coord_j = offsets[time][t_i] , offsets[time][t_j]
if coord_i.all() != 0 and coord_j.all() != 0:
sql_d = """select st_distance(ST_GeomFromText('POINT(""" + str(coord_i[0])+ " " + str(coord_i[1]) + """)',4326),
ST_GeomFromText('POINT(""" + str(coord_j[0])+ " " + str(coord_j[1]) + """)',4326));"""
cursor_psql.execute(sql_d)
results_d = cursor_psql.fetchall()
distance = int(results_d[0][0])
if distance <= min_dist:
interacts.append(t_j)
count+=1
time_i.append(interacts)
interactions.append(time_i)
interactions=np.array(interactions)
print(np.size(interactions))
print(count,'interações')
print(len(interactions),len(interactions[0]))
prob = 0.01826
contador = 0
for time in range(len(interactions)-1):
ntaxis = len(interactions[0])
states_ti = states[time]
for taxi in range(ntaxis):
interactions_i = interactions[time][taxi]
if len(interactions_i) != 0:
for taxi2 in interactions_i:
if states_ti[taxi] == 1 and states_ti[taxi2] == 0:
p = rd.random()
print(p)
if p <= prob :
for time_j in range(time +1,len(interactions)):
states[time_j][taxi2] = 1
contador += 1
elif states_ti[taxi] == 1 and states_ti[taxi2] == 0:
p = rd.random()
print(p)
if p <= prob :
for time_j in range(time +1,len(interactions)):
states[time_j][taxi] = 1
contador +=1
print('contaminações: ',contador)
conn.close()