From f2fc90e28fd80ffaf83db613baab953a02348357 Mon Sep 17 00:00:00 2001 From: "niehues.mark@gmail.com" <niehues.mark@gmail.com> Date: Mon, 30 Mar 2020 17:23:11 +0200 Subject: [PATCH] tests --- evrouting/gasstation/routing.py | 46 +++++----- tests/config.py | 21 +++++ tests/gasstation/test_transformations.py | 109 ++++++++++++++++++++++- 3 files changed, 149 insertions(+), 27 deletions(-) diff --git a/evrouting/gasstation/routing.py b/evrouting/gasstation/routing.py index 0f107aa..c38a0bb 100644 --- a/evrouting/gasstation/routing.py +++ b/evrouting/gasstation/routing.py @@ -23,28 +23,26 @@ def fold_path(G: nx.Graph, path: Path, weight: str): return sum([G.edges[u, v][weight] for u, v in zip(path[:-1], path[1:])]) -def insert_temp_node(temp_node: Node, - graph_core: nx.Graph, - graph_contracted: nx.Graph, - capacity: SoC, - initial_soc: SoC, - dist: DistFunction = dijkstra - ): - # Add cheap charging station - graph_contracted.add_node(temp_node, **{CHARGING_COEFFICIENT_KEY: 0}) - - for cs in graph_contracted.nodes: - min_path = dist(graph_core, temp_node, cs) - consumption = fold_path(graph_core, min_path, CONSUMPTION_KEY) - distance = fold_path(graph_core, min_path, DISTANCE_KEY) - if consumption <= initial_soc: - graph_contracted.add_edge( - temp_node, cs, - **{ - CONSUMPTION_KEY: capacity - initial_soc + consumption, - DISTANCE_KEY: distance - } - ) +def insert_start_node(s: Node, + graph_core: nx.Graph, + gas_stations: Set[Node], + graph_extended: nx.DiGraph, + capacity: SoC, + initial_soc: SoC, + dist: DistFunction = dijkstra + ) -> nx.DiGraph: + return graph_extended + + +def insert_final_node(t: Node, + graph_core: nx.Graph, + gas_stations: Set[Node], + graph_extended: nx.DiGraph, + capacity: SoC, + initial_soc: SoC, + dist: DistFunction = dijkstra + ) -> nx.DiGraph: + return graph_extended def contract_graph(G: nx.Graph, charging_stations: Set[Node], capacity: SoC, @@ -136,8 +134,8 @@ def shortest_path(G: nx.Graph, charging_stations: Set[Node], s: Node, t: Node, S: nx.Graph = contract_graph(G, charging_stations, capacity) H = state_graph(S, capacity) - insert_temp_node(s, G, H, capacity, initial_soc) - insert_temp_node(t, G, H, capacity, final_soc) + insert_start_node(s, G, charging_stations, H, capacity, initial_soc) + insert_final_node(t, G, charging_stations, H, capacity, final_soc) path: Path = dijkstra(H, (s, 0), (t, 0)) diff --git a/tests/config.py b/tests/config.py index a28ee96..963d0e0 100644 --- a/tests/config.py +++ b/tests/config.py @@ -93,6 +93,27 @@ edge_case_start_node_no_cs = { ] } +gasstation_complete = { + 'beta_s': 0, + 'beta_t': 0, + 'U': 4, + 's': 0, + 't': 2, + 'nodes': [ + TemplateNode(), + TemplateNode(charging_coeff=1), + TemplateNode(charging_coeff=2), + TemplateNode(), + ], + 'edges': [ + TemplateEdge(0, 1, distance=6, consumption=3), + TemplateEdge(0, 2, distance=2, consumption=1), + TemplateEdge(1, 2, distance=6, consumption=3), + TemplateEdge(1, 3, distance=4, consumption=2), + TemplateEdge(2, 3, distance=8, consumption=4), + ] +} + def get_graph(config: dict) -> nx.Graph: G = nx.Graph() diff --git a/tests/gasstation/test_transformations.py b/tests/gasstation/test_transformations.py index 62364ab..1a79206 100644 --- a/tests/gasstation/test_transformations.py +++ b/tests/gasstation/test_transformations.py @@ -1,20 +1,26 @@ import networkx as nx import pytest +from evrouting.T import ChargingCoefficient from evrouting.gasstation.routing import ( contract_graph, dijkstra, fold_path, get_possible_arriving_soc, - state_graph + state_graph, + insert_final_node, + insert_start_node ) from evrouting.graph_tools import ( label, CONSUMPTION_KEY, DISTANCE_KEY, - CHARGING_COEFFICIENT_KEY + CHARGING_COEFFICIENT_KEY, + consumption, + distance, + charging_cofficient ) -from tests.config import edge_case, gasstation, init_config +from tests.config import edge_case, gasstation, init_config, gasstation_complete class TestDjikstra: @@ -236,3 +242,100 @@ class TestStateGraph(MinimalExamples): H: nx.Graph = state_graph(G, self.U) assert len(H.nodes) == 0 assert len(H.edges) == 0 + + +class TestIntegration: + + @pytest.fixture + def graph_config(self): + return init_config(gasstation_complete) + + @pytest.fixture + def graph(self, graph_config): + return graph_config['G'] + + @pytest.fixture + def contracted_graph(self, graph_config): + return contract_graph( + G=graph_config['G'], + charging_stations=graph_config['charging_stations'], + capacity=graph_config['capacity'] + ) + + @pytest.fixture + def extended_graph(self, contracted_graph, graph_config): + return state_graph( + G=contracted_graph, + capacity=graph_config['capacity'] + ) + + @pytest.fixture + def inserted_s(self, extended_graph, graph_config): + return insert_start_node( + s=0, + graph_core=graph_config['G'], + gas_stations=graph_config['charging_stations'], + graph_extended=extended_graph, + capacity=graph_config['capacity'], + initial_soc=4 + ) + + @pytest.fixture + def inserted_t(self, inserted_s, graph_config): + return insert_final_node( + t=3, + graph_core=graph_config['G'], + gas_stations=graph_config['charging_stations'], + graph_extended=inserted_s, + capacity=graph_config['capacity'], + initial_soc=4 + ) + + def test_contracted_graph(self, contracted_graph): + G = contracted_graph + assert len(G.nodes) == 2 + assert len(G.edges) == 1 + + assert G.edges[1, 2][CONSUMPTION_KEY] == 3 + assert G.edges[1, 2][DISTANCE_KEY] == 6 + + @pytest.mark.parametrize('u, v, weight', [ + ((1, 0), (2, 1), 4 * 1 + 6), + ((2, 0), (1, 0), 3 * 2 + 6), + ((2, 1), (1, 0), 2 * 2 + 6), + ]) + def test_extended_graph(self, extended_graph, graph, u, v, weight): + assert len(extended_graph.nodes) == 3 + assert len(extended_graph.edges) == 3 + assert extended_graph.edges[u, v]['weight'] == weight + + @pytest.mark.parametrize('u, v, weight', [ + ((1, 0), (2, 1), 4 * 1 + 6), + ((2, 0), (1, 0), 3 * 2 + 6), + ((2, 1), (1, 0), 2 * 2 + 6), + ((0, 4), (2, 3), 2), + ((0, 4), (1, 1), 6), + ((1, 1), (2, 1), (4 - 1) * 1 + 6) + ]) + def test_s_insert(self, inserted_s, u, v, weight): + assert set(inserted_s.nodes) == {(0, 4), (2, 3), (1, 1), (2, 1), (1, 0), (2, 0)} + assert len(inserted_s.edges) == 6 + assert inserted_s.edges[u, v]['weight'] == weight + + @pytest.mark.parametrize('u, v, weight', [ + ((1, 0), (2, 1), 4 * 1 + 6), + ((2, 0), (1, 0), 3 * 2 + 6), + ((2, 1), (1, 0), 2 * 2 + 6), + ((0, 4), (2, 3), 2), + ((0, 4), (1, 1), 6), + ((1, 1), (2, 1), (4 - 1) * 1 + 6), + ((2, 3), (3, 0), 1 * 2 + 8), + ((1, 1), (3, 0), 1 * 1 + 4), + ((2, 1), (3, 0), 3 * 2 + 8), + ((1, 0), (3, 0), 2 * 1 + 4), + ((2, 0), (3, 0), 4 * 2 + 8), + ]) + def test_t_insert(self, inserted_t, u, v, weight): + assert set(inserted_t.nodes) == {(0, 4), (2, 3), (1, 1), (2, 1), (1, 0), (2, 0), (3, 0)} + assert len(inserted_t.edges) == 11 + assert inserted_t.edges[u, v]['weight'] == weight -- GitLab