From 6b964b6b8b42e7bdb0aef47559fa0e7ecf46946b Mon Sep 17 00:00:00 2001 From: "niehues.mark@gmail.com" <niehues.mark@gmail.com> Date: Mon, 30 Mar 2020 18:06:45 +0200 Subject: [PATCH] s insertion --- evrouting/gasstation/routing.py | 32 +++++++++++++++++++++++- tests/gasstation/test_transformations.py | 3 ++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/evrouting/gasstation/routing.py b/evrouting/gasstation/routing.py index 1e3edbd..db9697b 100644 --- a/evrouting/gasstation/routing.py +++ b/evrouting/gasstation/routing.py @@ -12,7 +12,7 @@ from evrouting.graph_tools import ( ) Path = List[Node] -DistFunction = Callable[[nx.Graph, Node, Node], Path] +DistFunction = Callable[[nx.Graph, Node, Node, str], Path] def dijkstra(G: nx.Graph, u: Any, v: Any, weight: str = 'weight') -> Path: @@ -25,12 +25,42 @@ def fold_path(G: nx.Graph, path: Path, weight: str): def insert_start_node(s: Node, graph_core: nx.Graph, + graph_contracted: nx.Graph, gas_stations: Set[Node], graph_extended: nx.DiGraph, capacity: SoC, initial_soc: SoC, dist: DistFunction = dijkstra ) -> nx.DiGraph: + """Insert s into extended graph an create states and edges as necessary.""" + for v in gas_stations: + shortest_p = dist(graph_core, s, v, weight=CONSUMPTION_KEY) + w = fold_path(graph_core, shortest_p, weight=CONSUMPTION_KEY) + if w > initial_soc: + continue + + d = fold_path(graph_core, shortest_p, weight=DISTANCE_KEY) + c_v = charging_cofficient(graph_core, v) + g = initial_soc - w + + graph_extended.add_edge((s, initial_soc), (v, g), weight=d) + for u in graph_contracted.neighbors(v): + c_u = charging_cofficient(graph_contracted, u) + w_v_u = consumption(graph_contracted, u, v) + d_v_u = distance(graph_contracted, u, v) + if c_v < c_u: + graph_extended.add_edge( + (v, g), + (u, capacity - w_v_u), + weight=(capacity - g) * c_v + d_v_u + ) + elif g < w_v_u: + graph_extended.add_edge( + (v, g), + (u, 0), + weight=(w_v_u - g) * c_v + d_v_u + ) + return graph_extended diff --git a/tests/gasstation/test_transformations.py b/tests/gasstation/test_transformations.py index 1a79206..988c923 100644 --- a/tests/gasstation/test_transformations.py +++ b/tests/gasstation/test_transformations.py @@ -270,10 +270,11 @@ class TestIntegration: ) @pytest.fixture - def inserted_s(self, extended_graph, graph_config): + def inserted_s(self, extended_graph, contracted_graph, graph_config): return insert_start_node( s=0, graph_core=graph_config['G'], + graph_contracted=contracted_graph, gas_stations=graph_config['charging_stations'], graph_extended=extended_graph, capacity=graph_config['capacity'], -- GitLab