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

rusable state graph

parent d9ab9974
No related branches found
No related tags found
No related merge requests found
...@@ -20,10 +20,16 @@ def insert_start_node(s: Node, ...@@ -20,10 +20,16 @@ def insert_start_node(s: Node,
graph_extended: nx.DiGraph, graph_extended: nx.DiGraph,
capacity: SoC, capacity: SoC,
initial_soc: SoC, initial_soc: SoC,
f: AccessFunctions = AccessFunctions() f: AccessFunctions = AccessFunctions(),
added_nodes=None
) -> nx.DiGraph: ) -> nx.DiGraph:
"""Insert s into extended graph an create states and edges as necessary.""" """Insert s into extended graph an create states and edges as necessary."""
if added_nodes is None:
added_nodes = []
added_nodes.append((s, initial_soc))
graph_extended.add_node((s, initial_soc)) graph_extended.add_node((s, initial_soc))
v: Node v: Node
for v in gas_stations: for v in gas_stations:
try: try:
...@@ -38,7 +44,8 @@ def insert_start_node(s: Node, ...@@ -38,7 +44,8 @@ def insert_start_node(s: Node,
d = f.path_distance(graph_core, shortest_p) d = f.path_distance(graph_core, shortest_p)
c_v = f.charging_coefficient(graph_core, v) c_v = f.charging_coefficient(graph_core, v)
g = initial_soc - w g = initial_soc - w
if (v, g) not in graph_extended.nodes:
added_nodes.append((v, g))
graph_extended.add_edge((s, initial_soc), (v, g), weight=d) graph_extended.add_edge((s, initial_soc), (v, g), weight=d)
for u in graph_contracted.neighbors(v): for u in graph_contracted.neighbors(v):
c_u = f.charging_coefficient(graph_contracted, u) c_u = f.charging_coefficient(graph_contracted, u)
...@@ -66,10 +73,16 @@ def insert_final_node(t: Node, ...@@ -66,10 +73,16 @@ def insert_final_node(t: Node,
graph_extended: nx.DiGraph, graph_extended: nx.DiGraph,
capacity: SoC, capacity: SoC,
final_soc: SoC, final_soc: SoC,
f: AccessFunctions = AccessFunctions() f: AccessFunctions = AccessFunctions(),
added_nodes=None
) -> nx.DiGraph: ) -> nx.DiGraph:
"""Insert terminal node into extended graph an create states and edges as necessary.""" """Insert terminal node into extended graph an create states and edges as necessary."""
if added_nodes is None:
added_nodes = []
graph_extended.add_node((t, final_soc)) graph_extended.add_node((t, final_soc))
added_nodes.append((t, final_soc))
u: Node u: Node
for u in gas_stations: for u in gas_stations:
try: try:
...@@ -242,6 +255,8 @@ def shortest_path(G: nx.Graph, ...@@ -242,6 +255,8 @@ def shortest_path(G: nx.Graph,
charge_path=[(n, 0) for n in _path] charge_path=[(n, 0) for n in _path]
) )
added_nodes = []
contracted_graph: nx.Graph = contracted_graph or contract_graph(G, charging_stations, capacity, f) contracted_graph: nx.Graph = contracted_graph or contract_graph(G, charging_stations, capacity, f)
extended_graph = extended_graph or state_graph(contracted_graph, capacity, f) extended_graph = extended_graph or state_graph(contracted_graph, capacity, f)
...@@ -253,7 +268,8 @@ def shortest_path(G: nx.Graph, ...@@ -253,7 +268,8 @@ def shortest_path(G: nx.Graph,
graph_extended=extended_graph, graph_extended=extended_graph,
capacity=capacity, capacity=capacity,
initial_soc=initial_soc, initial_soc=initial_soc,
f=f f=f,
added_nodes=added_nodes
) )
extended_graph = insert_final_node( extended_graph = insert_final_node(
...@@ -263,17 +279,21 @@ def shortest_path(G: nx.Graph, ...@@ -263,17 +279,21 @@ def shortest_path(G: nx.Graph,
graph_extended=extended_graph, graph_extended=extended_graph,
capacity=capacity, capacity=capacity,
final_soc=final_soc, final_soc=final_soc,
f=f f=f,
added_nodes=added_nodes
) )
try: try:
path: List[State] = nx.shortest_path(extended_graph, (s, initial_soc), (t, final_soc)) path: List[State] = nx.shortest_path(extended_graph, (s, initial_soc), (t, final_soc))
except nx.NetworkXNoPath: except nx.NetworkXNoPath:
return EmptyResult() res = EmptyResult()
else:
res = compose_result(
graph_core=G,
extended_graph=extended_graph,
path=path,
f=f
)
return compose_result( extended_graph.remove_nodes_from(added_nodes)
graph_core=G, return res
extended_graph=extended_graph,
path=path,
f=f
)
...@@ -7,7 +7,8 @@ from evrouting.gasstation.routing import ( ...@@ -7,7 +7,8 @@ from evrouting.gasstation.routing import (
state_graph, state_graph,
insert_final_node, insert_final_node,
insert_start_node, insert_start_node,
compose_result compose_result,
shortest_path
) )
from evrouting.graph_tools import ( from evrouting.graph_tools import (
label, label,
...@@ -325,6 +326,21 @@ class TestIntegration(Integration): ...@@ -325,6 +326,21 @@ class TestIntegration(Integration):
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
def test_shortest_path_reset_state_graph(self, graph, graph_config, extended_graph, contracted_graph):
before = len(extended_graph.nodes)
shortest_path(G=graph,
charging_stations=graph_config['charging_stations'],
s=graph_config['s'],
t=graph_config['t'],
initial_soc=graph_config['initial_soc'],
final_soc=graph_config['final_soc'],
capacity=graph_config['capacity'],
extended_graph=extended_graph,
contracted_graph=contracted_graph
)
assert before == len(extended_graph.nodes)
class TestResult(Integration): class TestResult(Integration):
def test_compose_result(self, graph, inserted_t): def test_compose_result(self, graph, inserted_t):
......
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