diff --git a/evrouting/graph_tools.py b/evrouting/graph_tools.py index 664c3716edddde2decdf52d2d26bca6e075ca42f..38c8ab6744f31ab79e0d5c07cf9256858e16d899 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 e752c39c16b6edacaa6a8d7e675bbffb39975349..2dda124d00633c306dc821f51e1a12632a032ab5 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 a5f410f6d856d5b271f91a35415e23aab1c9fe40..82516727ef4206d47bb32c52fbc64e87a2fb39e7 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 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000