diff --git a/evrouting/gasstation/routing.py b/evrouting/gasstation/routing.py index 1e3edbd6357cbf628ce3ff53f5f5125d569e1973..db9697b5f442f10c20b094566f3fbc68fb4bc4ab 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 1a79206c99f64f96136d7caa665d49fb857670d6..988c923d242418ddb59e82e2829aea7db0daee86 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'],