From 23bdf8b00adb56f33f5e85dbeabcfef210413149 Mon Sep 17 00:00:00 2001 From: "niehues.mark@gmail.com" <niehues.mark@gmail.com> Date: Tue, 28 Apr 2020 18:54:43 +0200 Subject: [PATCH] cleanup --- evrouting/graph_tools.py | 37 +++++++++++++++++++++++++++++++++++- evrouting/osm/routing.py | 18 ++---------------- tests/osm/test_osm_charge.py | 21 +++++++------------- tests/osm/test_routing.py | 0 4 files changed, 45 insertions(+), 31 deletions(-) delete mode 100644 tests/osm/test_routing.py diff --git a/evrouting/graph_tools.py b/evrouting/graph_tools.py index 664c371..38c8ab6 100644 --- a/evrouting/graph_tools.py +++ b/evrouting/graph_tools.py @@ -1,7 +1,15 @@ from collections import namedtuple import networkx as nx -from evrouting.T import Wh, ChargingCoefficient, Time, Node, NodeData, EdgeData +from evrouting.T import ( + Wh, + ChargingCoefficient, + Time, + Node, + NodeData, + EdgeData, + ConsumptionFunction +) TemplateEdge = namedtuple('Edge', ['u', 'v', 'distance', 'consumption']) TemplateNode = namedtuple( @@ -41,3 +49,30 @@ def label(G: nx.Graph, u: Node) -> str: def sum_weights(G, path, weight) -> float: return sum(G[u][v][weight] for u, v in zip(path[:-1], path[1:])) + + +def consumption_function_distance_factory(consumption: float) -> ConsumptionFunction: + """ + :param consumption: in kWh/km + """ + + def c(G, u, v): + """Returns consumption in Wh from u to v.""" + try: + return G[u][v][HAVERSINE_KEY] * consumption + except KeyError: + return G[u][v][CONSUMPTION_KEY] + + return c + + +def consumption_function_time_factory(consumption: float) -> ConsumptionFunction: + """ + :param consumption: in kWh/s + """ + + def c(G, u, v): + """Returns consumption in Wh from u to v.""" + return G[u][v][DISTANCE_KEY] * consumption * 1000 + + return c diff --git a/evrouting/osm/routing.py b/evrouting/osm/routing.py index e752c39..2dda124 100644 --- a/evrouting/osm/routing.py +++ b/evrouting/osm/routing.py @@ -3,9 +3,9 @@ from math import radians, cos, sin, asin, sqrt import networkx as nx -from evrouting.T import ConsumptionFunction, Result, EmptyResult +from evrouting.T import Result, EmptyResult from evrouting.graph_tools import ( - DISTANCE_KEY, HAVERSINE_KEY, CONSUMPTION_KEY, sum_weights + DISTANCE_KEY, sum_weights ) from evrouting.osm.const import ms_to_kmh @@ -73,17 +73,3 @@ def to_coordinates(G, path): return list(map(get_coordinates, path)) -def consumption_function_factory(consumption: float) -> ConsumptionFunction: - """ - :param consumption: in kWh/km - :return: consi - """ - - def c(G, u, v): - """Returns consumption in Wh from u to v.""" - try: - return G[u][v][HAVERSINE_KEY] * consumption - except KeyError: - return G[u][v][CONSUMPTION_KEY] - - return c diff --git a/tests/osm/test_osm_charge.py b/tests/osm/test_osm_charge.py index a5f410f..8251672 100644 --- a/tests/osm/test_osm_charge.py +++ b/tests/osm/test_osm_charge.py @@ -9,7 +9,11 @@ from evrouting.osm.imports import read_osm, OSMGraph from evrouting.osm.profiles import car from evrouting.osm.routing import shortest_path from evrouting.graph_tools import ( - CHARGING_COEFFICIENT_KEY, DISTANCE_KEY, CONSUMPTION_KEY, HAVERSINE_KEY + CHARGING_COEFFICIENT_KEY, + DISTANCE_KEY, + CONSUMPTION_KEY, + HAVERSINE_KEY, + consumption_function_distance_factory ) @@ -110,13 +114,7 @@ def test_charge_shortest_route_dimensions(map_graph): consumption = 1 # kWh/km cost_path = 6 * consumption * 1000 # distance * consumption in Wh - def c(G, u, v): - """Returns consumption in Wh from u to v.""" - try: - return G[u][v][HAVERSINE_KEY] * consumption - except KeyError: - return G[u][v][CONSUMPTION_KEY] - + c = consumption_function_distance_factory(consumption) result = charge.routing.shortest_path( G=map_graph, charging_stations=map_graph.charging_stations, @@ -144,12 +142,7 @@ def test_charge_shortest_route_stop(map_graph): consumption = 1 # kWh/km cost_path = 6 * consumption * 1000 # distance * consumption in Wh - def c(G, u, v): - """Returns consumption in Wh from u to v.""" - try: - return G[u][v][HAVERSINE_KEY] * consumption - except KeyError: - return G[u][v][CONSUMPTION_KEY] + c = consumption_function_distance_factory(consumption) result = charge.routing.shortest_path( G=map_graph, diff --git a/tests/osm/test_routing.py b/tests/osm/test_routing.py deleted file mode 100644 index e69de29..0000000 -- GitLab