diff --git a/evrouting/gasstation/routing.py b/evrouting/gasstation/routing.py index 669a8d82aba46d67659553972fceac360ddb82b1..b142a7dfb5260b6a9ca63a279def792857022ef0 100644 --- a/evrouting/gasstation/routing.py +++ b/evrouting/gasstation/routing.py @@ -24,7 +24,7 @@ def insert_start_node(s: Node, v: Node for v in gas_stations: try: - shortest_p: List[Node] = nx.shortest_path(graph_core, s, v, weight=DISTANCE_KEY) + shortest_p: List[Node] = f.shortest_path(graph_core, s, v) except nx.NetworkXNoPath: continue @@ -70,7 +70,7 @@ def insert_final_node(t: Node, u: Node for u in gas_stations: try: - shortest_p: List[Node] = nx.shortest_path(graph_core, t, u, weight=DISTANCE_KEY) + shortest_p: List[Node] = f.shortest_path(graph_core, t, u) except nx.NetworkXNoPath: continue @@ -115,7 +115,7 @@ def contract_graph(G: nx.Graph, charging_stations: Set[Node], capacity: SoC, # Iterate unvisited charging stations for n_cs in all_cs[i + 1:]: try: - path = nx.algorithms.shortest_path(G, cs, n_cs, weight=DISTANCE_KEY) + path = f.shortest_path(G, cs, n_cs) except nx.NetworkXNoPath: continue w_cs_n: SoC = f.path_consumption(G, path) @@ -183,11 +183,10 @@ def compose_result(graph_core: nx.Graph, extended_graph: nx.DiGraph, v, g_v = path[i + 1] t: Time = extended_graph.edges[(u, g_u), (v, g_v)]['weight'] trip_time += t - path_in_between = nx.shortest_path( + path_in_between = f.shortest_path( graph_core, u, - v, - weight=DISTANCE_KEY + v ) charge_time_u: Time = t - f.path_distance(graph_core, path_in_between) charge_path.append((u, charge_time_u)) @@ -223,7 +222,7 @@ def shortest_path(G: nx.Graph, """ # Check if t is reachable from s try: - _path = nx.shortest_path(G, s, t, weight=DISTANCE_KEY) + _path = f.shortest_path(G, s, t) except nx.NetworkXNoPath: return EmptyResult() diff --git a/evrouting/graph_tools.py b/evrouting/graph_tools.py index a66eb82d8dc60c6e2adaabfbba39ad255175146f..584129e710c32a29e7abb6d8046a2f51ab4812d1 100644 --- a/evrouting/graph_tools.py +++ b/evrouting/graph_tools.py @@ -116,3 +116,21 @@ class AccessFunctions: def path_consumption(self, G, path): """:returns: consumption of given path.""" return sum_weights(G, path, weight=CONSUMPTION_KEY) + + def shortest_path(self, G, u, v): + return nx.algorithms.shortest_path(G, u, v, weight=DISTANCE_KEY) + + +class GasstationAccessFunctions(AccessFunctions): + def __init__(self, consumption_coefficient): + super().__init__() + self.c = consumption_coefficient + + def consumption(self, G, u, v): + return self.c * self._distance(G, u, v) + + def charging_coefficient(self, G, v): + return 1 / self._charging_coefficient(G, v) + + def path_consumption(self, G, path): + return self.c * self.path_distance(G, path) diff --git a/evrouting/osm/imports.py b/evrouting/osm/imports.py index e0420247d04eb21e9e1f8d9eb215eff2a9e1722e..992834d2c0ac47aa86dbd1ace61b39c027ec3948 100644 --- a/evrouting/osm/imports.py +++ b/evrouting/osm/imports.py @@ -174,7 +174,7 @@ class Way(object): def slice_array(waypoints, node_pass_count): slices = [] start_last_slice = 0 - for i, w in range(1, len(waypoints) - 1): + for i in range(1, len(waypoints) - 1): if node_pass_count[waypoints[i]] > 1: slices.append(waypoints[start_last_slice: i + 1]) start_last_slice = i diff --git a/evrouting/osm/routing.py b/evrouting/osm/routing.py index af61effddb22be7c4e33110ec3745d6192965a6e..1745554c68c17e36428f2d9f5508c0a1e105296c 100644 --- a/evrouting/osm/routing.py +++ b/evrouting/osm/routing.py @@ -8,6 +8,7 @@ from evrouting.graph_tools import ( DISTANCE_KEY, sum_weights, AccessFunctions ) from evrouting.osm.const import ms_to_kmh +from evrouting.osm.profiles import car lat = float lon = float @@ -96,3 +97,6 @@ class GasstationAccessFunctions(AccessFunctions): def path_consumption(self, G, path): return self.c * self.path_distance(G, path) + + def shortest_path(self, G, u, v): + return nx.astar_path(G, u, v, heuristic=a_start_heuristic(G, profile=car))