Skip to content
Snippets Groups Projects
Commit f2fc90e2 authored by markn92's avatar markn92
Browse files

tests

parent 16c827e2
No related branches found
No related tags found
No related merge requests found
...@@ -23,28 +23,26 @@ def fold_path(G: nx.Graph, path: Path, weight: str): ...@@ -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:])]) return sum([G.edges[u, v][weight] for u, v in zip(path[:-1], path[1:])])
def insert_temp_node(temp_node: Node, def insert_start_node(s: Node,
graph_core: nx.Graph, graph_core: nx.Graph,
graph_contracted: nx.Graph, gas_stations: Set[Node],
capacity: SoC, graph_extended: nx.DiGraph,
initial_soc: SoC, capacity: SoC,
dist: DistFunction = dijkstra initial_soc: SoC,
): dist: DistFunction = dijkstra
# Add cheap charging station ) -> nx.DiGraph:
graph_contracted.add_node(temp_node, **{CHARGING_COEFFICIENT_KEY: 0}) return graph_extended
for cs in graph_contracted.nodes:
min_path = dist(graph_core, temp_node, cs) def insert_final_node(t: Node,
consumption = fold_path(graph_core, min_path, CONSUMPTION_KEY) graph_core: nx.Graph,
distance = fold_path(graph_core, min_path, DISTANCE_KEY) gas_stations: Set[Node],
if consumption <= initial_soc: graph_extended: nx.DiGraph,
graph_contracted.add_edge( capacity: SoC,
temp_node, cs, initial_soc: SoC,
**{ dist: DistFunction = dijkstra
CONSUMPTION_KEY: capacity - initial_soc + consumption, ) -> nx.DiGraph:
DISTANCE_KEY: distance return graph_extended
}
)
def contract_graph(G: nx.Graph, charging_stations: Set[Node], capacity: SoC, 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, ...@@ -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) S: nx.Graph = contract_graph(G, charging_stations, capacity)
H = state_graph(S, capacity) H = state_graph(S, capacity)
insert_temp_node(s, G, H, capacity, initial_soc) insert_start_node(s, G, charging_stations, H, capacity, initial_soc)
insert_temp_node(t, G, H, capacity, final_soc) insert_final_node(t, G, charging_stations, H, capacity, final_soc)
path: Path = dijkstra(H, (s, 0), (t, 0)) path: Path = dijkstra(H, (s, 0), (t, 0))
......
...@@ -93,6 +93,27 @@ edge_case_start_node_no_cs = { ...@@ -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: def get_graph(config: dict) -> nx.Graph:
G = nx.Graph() G = nx.Graph()
......
import networkx as nx import networkx as nx
import pytest import pytest
from evrouting.T import ChargingCoefficient
from evrouting.gasstation.routing import ( from evrouting.gasstation.routing import (
contract_graph, contract_graph,
dijkstra, dijkstra,
fold_path, fold_path,
get_possible_arriving_soc, get_possible_arriving_soc,
state_graph state_graph,
insert_final_node,
insert_start_node
) )
from evrouting.graph_tools import ( from evrouting.graph_tools import (
label, label,
CONSUMPTION_KEY, CONSUMPTION_KEY,
DISTANCE_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: class TestDjikstra:
...@@ -236,3 +242,100 @@ class TestStateGraph(MinimalExamples): ...@@ -236,3 +242,100 @@ class TestStateGraph(MinimalExamples):
H: nx.Graph = state_graph(G, self.U) H: nx.Graph = state_graph(G, self.U)
assert len(H.nodes) == 0 assert len(H.nodes) == 0
assert len(H.edges) == 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment