-
Notifications
You must be signed in to change notification settings - Fork 0
/
propagation.py
213 lines (162 loc) · 4.94 KB
/
propagation.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
import numpy as np
import matplotlib.pyplot as plt
import psycopg2
import math
from matplotlib.animation import FuncAnimation
import datetime
import csv
from postgis import Polygon,MultiPolygon
from postgis.psycopg import register
import random
import pandas as pd
import queue
import time
from heapq import heappush, heappop
start_time = time.time()
def polygon_to_points(polygon_string):
xs, ys = [],[]
points = polygon_string[9:-2].split(',')
for point in points:
(x,y) = point.split()
xs.append(float(x))
ys.append(float(y))
return xs,ys
def points_list_to_points(points_list):
xs = []
ys = []
for point in points_list:
point_string = point[0]
point_string = point_string[6:-1]
(x,y) = point_string.split()
xs.append(float(x))
ys.append(float(y))
return xs, ys
conn = psycopg2.connect("dbname=postgres user=postgres")
register(conn)
cursor_psql = conn.cursor()
# Selecionar os primeiros 10 taxis em serviço com origem no Porto
sql = """
select taxi, ts
from tracks as tr, cont_aad_caop2018 as caop
where caop.concelho='PORTO' and
st_within(st_pointn(tr.proj_track,1), caop.proj_boundary) and
tr.state='BUSY' order by ts limit 10;
"""
cursor_psql.execute(sql)
results = cursor_psql.fetchall()
i = random.randint(0,len(results))
taxi_porto = results[i-1][0]
print(taxi_porto)
# Selecionar os primeiros 10 taxis em serviço com origem em Lisboa
sql = """
select taxi, ts
from tracks as tr, cont_aad_caop2018 as caop
where caop.concelho='LISBOA' and
st_within(st_pointn(tr.proj_track,1), caop.proj_boundary) and
tr.state='BUSY' order by ts limit 10;
"""
cursor_psql.execute(sql)
results = cursor_psql.fetchall()
i = random.randint(0,len(results))
taxi_lisboa = results[i-1][0]
print(taxi_lisboa)
#Para saber a que coluna corresponde cada taxi no ficheiro offsets
sql = " select distinct taxi from tracks order by 1"
cursor_psql.execute(sql)
results = cursor_psql.fetchall()
infetados = queue.Queue(maxsize=1660)
taxi_id = {}
visitados = [0] * 1660
a = 0
for i in results:
i = int(i[0])
taxi_id[i] = a
a = a+1
print(taxi_id[int(taxi_porto)])
#Obter a coluna do taxi escolhido aleatóriamente do Porto e de Lisboa
id_taxi_porto = taxi_id[int(taxi_porto)]
id_taxi_lisboa = taxi_id[int(taxi_lisboa)]
#Criar queue para saber quais taxis já foram infetados
infetados = queue.Queue(maxsize=1660)
infetados.put(id_taxi_porto)
infetados.put(id_taxi_lisboa)
visitados[id_taxi_porto] = 1
visitados[id_taxi_lisboa] = 1
offsets = []
with open('offsets3.csv', 'r') as csvFile:
reader = csv.reader(csvFile)
i = 0
for row in reader:
l = []
for j in row:
x,y = j.split()
x = float(x)
y = float(y)
l.append([x,y])
offsets.append(l)
infetados = 0
t_infetados = [0] * len(offsets)
anim = [[0 for count in range(1660)] for i in range(len(offsets))]
heap = []
flag_1 = 0
conta = 2
heappush(heap,(0,id_taxi_porto))
heappush(heap,(0,id_taxi_lisboa))
anim[0][id_taxi_porto] = 1
anim[0][id_taxi_lisboa] = 1
t_infetados[0] = 2
while len(heap)>0:
print("SIZE")
print(len(heap))
a = heappop(heap)
print("Taxi: " + str(a))
taxi_1 = a[1]
ts_i = a[0] # Para saber o ts em que foi infetado, pois só apartir daí é que importa
for k in range(ts_i,len(offsets)):
anim[k][taxi_1] = 1
for i in range(ts_i,len(offsets),6):
p1 = []
p1 = offsets[i][taxi_1]
x_p1 = p1[0]
y_p1 = p1[1]
for j in range(1660):
p2 = []
p2 = offsets[i][j]
x_p2 = p2[0]
y_p2 = p2[1]
if ((x_p1 == 0) and (y_p1 == 0)) or ((x_p2 == 0) and (y_p2 == 0)) or (math.dist(p1,p2)>50) or (visitados[j]==1) or (taxi_1==j):
continue;
else:
prob = random.randint(1,10)
if prob == 1:
visitados[j] = 1
heappush(heap, (i,j))
infetados = infetados + 1
conta = conta + 1
flag_1=1
anim[i][j] = 1
if flag_1==1:
t_infetados[i] = t_infetados[i] + infetados
flag_1=0
infetados = 0
with open('anim.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(anim)
print("Conta " + str(conta))
total = [0] * 24
flag = 0
j = 0
for i in range (0, len(t_infetados)):
if flag < 360:
total[j] = total[j] + t_infetados[i]
flag = flag + 1
elif flag == 360:
j = j + 1
total[j] = total[j] + t_infetados[i]
flag = 0
print(total)
df=pd.DataFrame({'x': range(0,24), 'y': total })
plt.plot( 'x', 'y', data=df, color='skyblue', linestyle='-', marker='o')
plt.savefig('infetados_por_hora.png')
plt.show()
print("Duraçao da execuçao: %s" % (time.time() - start_time))