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

basic case running

parent 01445456
No related branches found
No related tags found
No related merge requests found
from typing import Set, Callable, List, Any, Dict from typing import Set, Callable, List, Any, Dict, Tuple
import networkx as nx import networkx as nx
from evrouting.T import Node, SoC from evrouting.T import Node, SoC
...@@ -14,7 +14,7 @@ Path = List[Node] ...@@ -14,7 +14,7 @@ Path = List[Node]
DistFunction = Callable[[nx.Graph, Node, Node, str], Path] DistFunction = Callable[[nx.Graph, Node, Node, str], Path]
def dijkstra(G: nx.Graph, u: Any, v: Any, weight: str = 'weight') -> Path: def dijkstra(G: nx.Graph, u: Any, v: Any, weight: str = 'weight') -> list:
return nx.algorithms.shortest_path(G, u, v, weight=weight) return nx.algorithms.shortest_path(G, u, v, weight=weight)
...@@ -161,8 +161,25 @@ def state_graph(G: nx.Graph, capacity: SoC) -> nx.DiGraph: ...@@ -161,8 +161,25 @@ def state_graph(G: nx.Graph, capacity: SoC) -> nx.DiGraph:
return H return H
def compose_result(G: nx.Graph, path: Path) -> dict: def compose_result(graph_core: nx.Graph, extended_graph: nx.DiGraph, path: List[Tuple[Node, Node]],
return {'trip_time': 0, 'path': path} dist=dijkstra) -> dict:
charge_path = [(path[0][0], 0)] # Start Node
trip_time = sum([extended_graph.edges[u, v]['weight'] for u, v in zip(path[:-1], path[1:])])
for i in range(1, len(path) - 1):
u, g_u = path[i]
v, g_v = path[i + 1]
t = extended_graph.edges[(u, g_u), (v, g_v)]['weight']
charge_time_u = t - fold_path(
graph_core,
dist(graph_core, u, v, weight=DISTANCE_KEY),
weight=DISTANCE_KEY
)
charge_path.append((u, charge_time_u))
charge_path.append((path[-1][0], 0)) # Final Node
return {'trip_time': trip_time, 'path': charge_path}
def shortest_path(G: nx.Graph, charging_stations: Set[Node], s: Node, t: Node, def shortest_path(G: nx.Graph, charging_stations: Set[Node], s: Node, t: Node,
...@@ -178,12 +195,32 @@ def shortest_path(G: nx.Graph, charging_stations: Set[Node], s: Node, t: Node, ...@@ -178,12 +195,32 @@ def shortest_path(G: nx.Graph, charging_stations: Set[Node], s: Node, t: Node,
:param U: Capacity :param U: Capacity
:return: :return:
""" """
S: nx.Graph = contract_graph(G, charging_stations, capacity) contracted_graph: nx.Graph = contract_graph(G, charging_stations, capacity)
H = state_graph(S, capacity) extended_graph = state_graph(contracted_graph, capacity)
insert_start_node(s, G, charging_stations, H, capacity, initial_soc) extended_graph = insert_start_node(
insert_final_node(t, G, charging_stations, H, capacity, final_soc) s=s,
graph_core=G,
path: Path = dijkstra(H, (s, 0), (t, 0)) graph_contracted=contracted_graph,
gas_stations=charging_stations,
return compose_result(H, path) graph_extended=extended_graph,
capacity=capacity,
initial_soc=initial_soc
)
extended_graph = insert_final_node(
t=t,
graph_core=G,
gas_stations=charging_stations,
graph_extended=extended_graph,
capacity=capacity,
final_soc=final_soc
)
path: List[Tuple[Node, Node]] = dijkstra(extended_graph, (s, initial_soc), (t, final_soc))
return compose_result(
graph_core=G,
extended_graph=extended_graph,
path=path
)
...@@ -94,11 +94,11 @@ edge_case_start_node_no_cs = { ...@@ -94,11 +94,11 @@ edge_case_start_node_no_cs = {
} }
gasstation_complete = { gasstation_complete = {
'beta_s': 0, 'beta_s': 4,
'beta_t': 0, 'beta_t': 0,
'U': 4, 'U': 4,
's': 0, 's': 0,
't': 2, 't': 3,
'nodes': [ 'nodes': [
TemplateNode(), TemplateNode(),
TemplateNode(charging_coeff=1), TemplateNode(charging_coeff=1),
......
from evrouting.gasstation.routing import ( from evrouting.gasstation.routing import (
shortest_path, shortest_path,
) )
from tests.config import edge_case, get_graph from tests.config import gasstation_complete, init_config
class TestRouting: def test_shortest_path():
def test_shortest_path(self): conf = init_config(gasstation_complete)
G = get_graph(edge_case)
path = shortest_path( path = shortest_path(
G, G=conf['G'],
s=edge_case['s'], charging_stations=conf['charging_stations'],
t=edge_case['t'], s=conf['s'],
b_0=edge_case['b_0'], t=conf['t'],
b_t=edge_case['b_t'], initial_soc=conf['initial_soc'],
U=edge_case['U'] final_soc=conf['final_soc'],
) capacity=conf['capacity']
)
assert path == [(0, 4), (2, 0)] assert path == {'path': [(0, 0), (1, 1), (3, 0)], 'trip_time': 11}
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,
...@@ -9,16 +8,14 @@ from evrouting.gasstation.routing import ( ...@@ -9,16 +8,14 @@ from evrouting.gasstation.routing import (
get_possible_arriving_soc, get_possible_arriving_soc,
state_graph, state_graph,
insert_final_node, insert_final_node,
insert_start_node insert_start_node,
compose_result
) )
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, gasstation_complete from tests.config import edge_case, gasstation, init_config, gasstation_complete
...@@ -244,8 +241,7 @@ class TestStateGraph(MinimalExamples): ...@@ -244,8 +241,7 @@ class TestStateGraph(MinimalExamples):
assert len(H.edges) == 0 assert len(H.edges) == 0
class TestIntegration: class Integration:
@pytest.fixture @pytest.fixture
def graph_config(self): def graph_config(self):
return init_config(gasstation_complete) return init_config(gasstation_complete)
...@@ -292,6 +288,9 @@ class TestIntegration: ...@@ -292,6 +288,9 @@ class TestIntegration:
final_soc=0 final_soc=0
) )
class TestIntegration(Integration):
def test_contracted_graph(self, contracted_graph): def test_contracted_graph(self, contracted_graph):
G = contracted_graph G = contracted_graph
assert len(G.nodes) == 2 assert len(G.nodes) == 2
...@@ -340,3 +339,13 @@ class TestIntegration: ...@@ -340,3 +339,13 @@ class TestIntegration:
assert set(inserted_t.nodes) == {(0, 4), (2, 3), (1, 1), (2, 1), (1, 0), (2, 0), (3, 0)} 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 len(inserted_t.edges) == 11
assert inserted_t.edges[u, v]['weight'] == weight assert inserted_t.edges[u, v]['weight'] == weight
class TestResult(Integration):
def test_compose_result(self, graph, inserted_t):
path = [(0, 4), (1, 1), (3, 0)]
result = compose_result(graph, inserted_t, path)
assert result == {
'trip_time': 6 + 1 + 4,
'path': [(0, 0), (1, 1), (3, 0)]
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment