forked from yusef320/electricTSP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.py
More file actions
44 lines (34 loc) · 1.3 KB
/
algorithm.py
File metadata and controls
44 lines (34 loc) · 1.3 KB
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
import networkx as nx
import numpy as np
from nn_model_predictor import neural_network_energy_estimation
from bellmanFord import bellman_ford_path
from twoOptNearestNeigbors import RouteFinder
from datetime import datetime
import pickle
def electric_tsp(stops):
f = open('madrid_elevation_energy.pckl', 'rb')
G = pickle.load(f)
f.close()
grafo_tsp = nx.DiGraph()
date_today = datetime.now()
grafo_tsp.add_nodes_from(stops)
weekday = date_today.strftime("%A")
working = 1
if weekday in ["Saturday", "Sunday"]:
working = 0
for i in stops:
for j in stops:
if i != j:
d_temp = neural_network_energy_estimation(i, j, working, str(date_today.hour), weekday, G)
grafo_tsp.add_edge(i, j, weight=d_temp)
mat = nx.adjacency_matrix(grafo_tsp).todense()
mat = np.array(mat).reshape(-1, mat.shape[1])
for i in range(len(stops)):
mat[i][i] = np.inf
route_finder = RouteFinder(mat, stops, iterations=3000)
best_distance, best_route = route_finder.solve()
ruta_final = []
for i in range(len(best_route) - 1):
r_temp, dist = bellman_ford_path(G, best_route[i], best_route[i + 1])
ruta_final = ruta_final + r_temp[1:]
return ruta_final