-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster.py
298 lines (240 loc) · 8.71 KB
/
cluster.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
from pymysql import connect
from math import sin, cos, sqrt, atan2, radians
import math
import random
from geopy import distance as calc_distance
import pickle
def get_distance(point1, point2):
# approximate radius of earth in km
earth_radius = 6373.0
lat1 = math.radians(point1[0])
lon1 = math.radians(point1[1])
lat2 = math.radians(point2[0])
lon2 = math.radians(point2[1])
dlon = lon2 - lon1
dlat = lat2 - lat1
angle = math.sin(dlat / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2
circ = 2 * math.atan2(math.sqrt(angle), math.sqrt(1 - angle))
distance = earth_radius * circ
return distance * 1000
def get_middle_of_coord_list(list_of_coords):
if len(list_of_coords) == 1:
return list_of_coords[0]
coord_x = 0
coord_y = 0
coord_z = 0
for coord in list_of_coords:
# transform to radians...
lat_rad = math.radians(coord.lat)
lng_rad = math.radians(coord.lon)
coord_x += math.cos(lat_rad) * math.cos(lng_rad)
coord_y += math.cos(lat_rad) * math.sin(lng_rad)
coord_z += math.sin(lat_rad)
amount_of_coords = len(list_of_coords)
coord_x = coord_x / amount_of_coords
coord_y = coord_y / amount_of_coords
coord_z = coord_z / amount_of_coords
central_lng = math.atan2(coord_y, coord_x)
central_square_root = math.sqrt(coord_x * coord_x + coord_y * coord_y)
central_lat = math.atan2(coord_z, central_square_root)
return (math.degrees(central_lat), math.degrees(central_lng))
import json
with open("config.json", "r") as f:
config = json.load(f)
max_distance = config["view_distance"]
class Spawn:
def __init__(self, sid, lat, lon):
self.lat = lat
self.lon = lon
self.id = sid
self.done = False
self.clusters = []
class Range:
def __init__(self, lat, lon):
self.lat = lat
self.lon = lon
self.spawns = []
self.double_spawns = []
def get_spawns(self):
return [spawn for spawn in self.spawns if not spawn.done]
def get_done_spawns(self):
return [spawn for spawn in self.spawns if spawn.done]
def get_spawn_id(self):
return [s.id for s in self.get_spawns()]
@property
def unique_spawns(self):
return [s for s in self.spawns if s.id not in [d.id for d in self.double_spawns]]
def get_spawns():
connection = connect(
host=config["host"],
user=config["user"],
password=config["password"],
database=config["db_name"],
port=config["port"],
autocommit=True
)
with open("fence.txt", "r") as f:
area = f.read()
area = area.replace(",", " ").strip("\n")
area = area.split("\n")
area.append(area[0])
area = ",".join(area)
cursor = connection.cursor()
# Add gym and spawnpoint option from config.json
# gym or spawnpoint valid options
print(f"poitype= " + config["poitype"])
print(f"spawnpoints known: " + config["time_known"])
if config["time_known"] == "true":
time_known = " and despawn_sec IS NOT NULL "
elif config["time_known"] == "false":
time_known = " and despawn_sec IS NULL "
else:
time_known = " "
query = f"select id, lat, lon from " + config["poitype"] + f" where ST_CONTAINS(ST_GEOMFROMTEXT('POLYGON(({area}))'), point(lat, lon))" + time_known + "order by lon, lat"
cursor.execute(query)
r = cursor.fetchall()
cursor.close()
connection.close()
spawns = []
for sid, (ssid, lat, lon) in enumerate(r):
spawns.append(Spawn(ssid, lat, lon))
print(f"Number of spawn points: " + str(len(r)))
return spawns
spawns = get_spawns()
def middle_points():
points = []
i = 1
total = f"{len(spawns)}"
for spawn1 in spawns:
for spawn2 in spawns:
distance = get_distance((spawn1.lat, spawn1.lon), (spawn2.lat, spawn2.lon))
if distance < config["max_distance_between_spawns"]:
middle = get_middle_of_coord_list([spawn1, spawn2])
routepoint = Range(middle[0], middle[1])
for spawn3 in spawns:
if get_distance(middle, (spawn3.lat, spawn3.lon)) <= max_distance:
routepoint.spawns.append(spawn3)
spawn_amount = len(routepoint.spawns)
if spawn_amount >= config["min_total_spawns_in_cluster"]:
points.append(routepoint)
i += 1
print(f"Generating possible routepoints: {i}/{total}")
return points
def point_points():
points = []
i = 1
total = len(spawns)
for spawn in spawns:
routepoint = Range(spawn.lat, spawn.lon)
for spawn3 in spawns:
if get_distance((spawn.lat, spawn.lon), (spawn3.lat, spawn3.lon)) <= max_distance:
routepoint.spawns.append(spawn3)
spawn_amount = len(routepoint.spawns)
if spawn_amount > 0:
print(f"{i}/{total}: {spawn_amount}")
points.append(routepoint)
i += 1
return points
"""try:
with open("points.pickle", "rb") as handle:
spawns, points = pickle.load(handle)
except:
points = middle_points()
to_save = (spawns, points)
with open("points.pickle", "wb") as handle:
pickle.dump(to_save, handle)"""
points = middle_points()
def cluster_v1(spawns, final=[]):
total = len(spawns)
final_len = 0
for i, spawn in enumerate(spawns, start=1):
if spawn.done:
continue
circles = []
for point in points:
if spawn.id in point.get_spawn_id():
circles.append(point)
if len(circles) == 0:
continue
circle = max(circles, key=lambda point: (len(point.get_spawns())))
for circlespawn in circle.get_spawns():
circlespawn.done = True
final_len += 1
final.append(circle)
print(f"Generating route: {i}/{total} (length: {final_len})")
return final
"""try:
with open("route.pickle", "rb") as handle:
final = pickle.load(handle)
except:
final = cluster_v1(spawns)
with open("route.pickle", "wb") as handle:
pickle.dump(final, handle)"""
final = cluster_v1(spawns)
def check_doubles(cluster_list):
for cluster in cluster_list:
cluster_ids = [s.id for s in cluster.spawns]
for cluster2 in cluster_list:
if cluster2 == cluster:
continue
for spawn in cluster2.spawns:
if spawn.id in cluster_ids:
if spawn.id not in [s.id for s in cluster2.double_spawns]:
cluster2.double_spawns.append(spawn)
return cluster_list
def check_final_route(final):
total_removed = 0
for cluster in final:
if len(cluster.spawns) - len(cluster.double_spawns) < config["min_added_spawns_in_cluster"]:
recheck_clusters = []
for spawn in cluster.spawns:
recheck_clusters += spawn.clusters
final.remove(cluster)
total_removed += 1
check_doubles(recheck_clusters)
print("Removed one point from route that didn't add enough spawns")
if total_removed > 0:
final = check_final_route(final)
return final
def edit_route(smallest, route):
for spawn in smallest.spawns:
for point in points:
if not spawn.id in [s.id for s in point.spawns]:
continue
if point == smallest:
continue
other_clusters = []
for spawn2 in point.spawns:
for cluster in spawn2.clusters:
if cluster != smallest and cluster not in other_clusters and cluster in route:
other_clusters.append(cluster)
if len(other_clusters) == 0:
continue
for cluster in other_clusters:
all_spawns = smallest.unique_spawns + cluster.unique_spawns
if [1 for s in all_spawns if s.id not in [s.id for s in point.spawns]]:
continue
print("Optimize: Merged 2 routepoints")
route.remove(cluster)
route.remove(smallest)
route.append(point)
return
for circle in final:
for spawn in circle.spawns:
spawn.clusters.append(circle)
check_doubles(final)
final = check_final_route(final)
final_len = len(final)
for i in range(final_len):
try:
smallest = final[i]
except:
break
edit_route(smallest, final)
final = check_final_route(final)
route = ""
for circle in final:
route += f"{circle.lat},{circle.lon}\n"
print(f"Wrote route to route.txt. Total length: {len(final)}")
with open("route.txt", "w+") as f:
f.write(route)