diff --git a/evrouting/gasstation/routing.py b/evrouting/gasstation/routing.py index 7f26a2ac30b5e02a6f6b52766fb665ac72351b46..1c5378af288cfc2b39bd38580d3eae664b5c50b7 100644 --- a/evrouting/gasstation/routing.py +++ b/evrouting/gasstation/routing.py @@ -166,10 +166,47 @@ def state_graph(G: nx.Graph, capacity: SoC) -> nx.DiGraph: return H +<<<<<<< refs/remotes/origin/dev def compose_result(graph_core: nx.Graph, extended_graph: nx.DiGraph, path: List[Tuple[Node, Node]], dist=dijkstra) -> Result: trip_time = sum([extended_graph.edges[u, v]['weight'] for u, v in zip(path[:-1], path[1:])]) +======= +def init(G: nx.Graph, charging_stations: Set[Node], capacity: SoC, + dist: DistFunction = dijkstra) -> nx.Graph: + H: nx.DiGraph = nx.DiGraph() + + for v in charging_stations: + c_v = charging_cofficient(G, v) + H.add_node((v, 0)) + # Iterate unvisited charging stations + for u in charging_stations: + if u == v: + continue + + min_path: List[Node] = dist(G, v, u) + w: SoC = fold_path(G, min_path, weight=CONSUMPTION_KEY) + if w <= capacity: # neighbour + g = capacity - w + c_u = charging_cofficient(G, u) + + if c_u < c_v: # c_u cheaper + H.add_node((v, g)) + + if c_v <= c_u and g < w: # c_v at least as cheap as c_u + weight = (w - g) * c_u + distance(G, u, v) + H.add_edge((u, g), (v, 0), weight=weight) + elif c_v > c_u: # c_u cheaper + weight = (capacity - g) * c_u + distance(G, u, v) + H.add_edge((u, g), (v, capacity - w), weight=weight) + + return H + + +def compose_result(graph_core: nx.Graph, extended_graph: nx.DiGraph, + path: List[State], dist=dijkstra) -> Result: + trip_time: Time = 0 +>>>>>>> local charge_path = [] for i in range(len(path) - 1): u, g_u = path[i] diff --git a/tests/config.py b/tests/config.py index 102e4c335e95634625930c544ef3a4b5de021642..78bc08218ea42968f63b0ed771b810e77d0f9594 100644 --- a/tests/config.py +++ b/tests/config.py @@ -11,7 +11,8 @@ config_list = [ 'edge_case', 'edge_case_start_node_no_cs', 'edge_case_a_slow', - 'gasstation' + 'gasstation', + 'gasstation_complete' ] edge_case = { @@ -100,10 +101,10 @@ gasstation_complete = { 's': 0, 't': 3, 'nodes': [ - TemplateNode(), - TemplateNode(charging_coeff=1), - TemplateNode(charging_coeff=2), - TemplateNode(), + TemplateNode('0'), + TemplateNode('1', charging_coeff=1), + TemplateNode('2', charging_coeff=2), + TemplateNode('3'), ], 'edges': [ TemplateEdge(0, 1, distance=6, consumption=3), diff --git a/tests/gasstation/test_transformations.py b/tests/gasstation/test_transformations.py index e6c5c9fdb23f416fe2d4f6eaad91459b9471b49c..347da2001fe15b3281b06013f424d5f89e9000b5 100644 --- a/tests/gasstation/test_transformations.py +++ b/tests/gasstation/test_transformations.py @@ -9,7 +9,8 @@ from evrouting.gasstation.routing import ( state_graph, insert_final_node, insert_start_node, - compose_result + compose_result, + init ) from evrouting.graph_tools import ( label, @@ -289,6 +290,21 @@ class Integration: ) +class TestInit(Integration): + + def test_init(self, extended_graph, graph_config): + h = init( + graph_config['G'], + charging_stations=graph_config['charging_stations'], + capacity=graph_config['capacity'] + ) + assert len(extended_graph.nodes) == len(h.nodes) + assert set(extended_graph.nodes) == set(h.nodes) + + assert len(extended_graph.edges) == len(h.edges) + assert set(extended_graph.edges) == set(h.edges) + + class TestIntegration(Integration): def test_contracted_graph(self, contracted_graph):