-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
75 lines (56 loc) · 2.41 KB
/
main.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
import time
import numpy as np
from osmnx import graph_from_place, config
from route_optimization import SARouteOptimizer, SAModel
from utils import calculate_distance_matrix, create_map, calculate_full_path, create_init_route
class Model(SAModel):
def __init__(self, cost_matrix):
self.cost_matrix = cost_matrix
def cost(self, route):
cost = 0
for i in range(len(route) - 1):
from_index = route[i]
to_index = route[i + 1]
cost += self.cost_matrix[from_index][to_index]
return cost
#всегда false 1 арг
#
def main(USE_SAVED_DISTANCES,coordinate,START_HILL,END_HILL):
config(log_console=True, use_cache=True, cache_folder='./cache')
print("Calculating graph")
graph = graph_from_place('Moscow', network_type='walk')
hill_names, coordinates = [], []
for a, b in coordinate.items():
hill_names.append(a)
coordinates.append(b)
print(hill_names, coordinates)
print([START_HILL, END_HILL])
distances_matrix = None
if USE_SAVED_DISTANCES:
print("Loading distances from file")
try:
distances_matrix = np.loadtxt("distances.txt")
except OSError:
USE_SAVED_DISTANCES = False
print("Could not find distances.txt file.")
if not USE_SAVED_DISTANCES:
print("Calculating distances between points")
distances_matrix = calculate_distance_matrix(graph, coordinates)
print("Saving distances to file")
np.savetxt("distances.txt", distances_matrix)
optimizer = SARouteOptimizer(model=Model(cost_matrix=distances_matrix),
max_iter=1000,
max_iter_without_improvement=300)
print("Calculating optimal order of hills")
init_route = create_init_route(hill_names.index(START_HILL), hill_names.index(END_HILL), distances_matrix.shape[0])
start_time = time.time()
optimal_route, total_distance = optimizer.run(init_route)
end_time = time.time()
duration_ms = 1000 * (end_time - start_time)
print(f"Solution took {duration_ms:0.0f} ms")
print("Creating optimal path")
full_path, distances = calculate_full_path(graph, optimal_route, coordinates)
print("Creating map")
map = create_map(graph, full_path, optimal_route, hill_names, coordinates, distances)
print("Saving map to file")
map.save('results/optimal_route.html')