Skip to content

Commit

Permalink
mv bugfixes: fix several key-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
piaulous committed Sep 6, 2023
1 parent d854f17 commit 34734b0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 34 deletions.
4 changes: 1 addition & 3 deletions ding0/grid/lv_grid/graph_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,9 +650,7 @@ def remove_parallels_and_loops(graph):
to_remove = []

# identify all the parallel edges in the MultiDiGraph
parallels = [(u, v) for u, v, k in graph.edges(keys=True) if k > 0 and
graph.number_of_edges(u, v) > 1]

parallels = ((u, v) for u, v, k in graph.edges(keys=True) if graph.number_of_edges(u, v) > 1)
# remove the parallel edge with greater "weight" attribute value
for u, v in set(parallels):
k, _ = max(graph.get_edge_data(u, v).items(), key=lambda x: x[1]['length'])
Expand Down
24 changes: 18 additions & 6 deletions ding0/grid/mv_grid/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ def get_shortest_path_shp_single_target(osm_graph, node1, node2, return_path=Fal
line_shp = linemerge([osm_graph.edges[edge]['geometry'] for edge in edge_path])

if line_shp.is_empty: # in case of route_length == 1
line_shp = osm_graph.edges[node1, node2, next(iter(osm_graph.get_edge_data(node1, node2).keys()))]['geometry'] # TODO make universal
edge_key = next(iter(osm_graph.get_edge_data(node1, node2).keys()))
line_shp = osm_graph.edges[node1, node2, edge_key]['geometry']

line_length = line_shp.length

Expand Down Expand Up @@ -312,7 +313,7 @@ def reduce_graph_for_dist_matrix_calc(graph, nodes_to_keep):
post_graph_no = len(graph)

# ensure graph bidirectionality
bidir_edges = [(v, u, data) for u, v, data in graph.edges(data=True) if (v, u) not in graph.edges]
bidir_edges = [(v, u, k, data) for u, v, k, data in graph.edges(keys=True, data=True) if (v, u, k) not in graph.edges]
graph.add_edges_from(bidir_edges)

return graph
Expand Down Expand Up @@ -519,8 +520,13 @@ def check_stub_criterion(stub_dict, stub_graph):
load_nodes = [load_nodes[1:] for n in load_nodes if n not in stub_data['load']][0]

for n in load_nodes:

cum_load = sum([stub_data['load'][n] for n in load_nodes])
cum_load = 0
for inner_n in load_nodes:
try:
cum_load += stub_data['load'][inner_n]
except KeyError:
# ToDo: Find origin of problem that in load_nodes is not only loads
logger.error(f"{inner_n} not in stub_data['load']")
if cum_load <= cfg_ding0.get('mv_connect', 'load_area_sat_string_load_threshold'):
comp = [root] + load_nodes
mod_stubs_list.append(comp)
Expand Down Expand Up @@ -554,8 +560,14 @@ def update_stub_dict(stub_dict, mod_stubs_list, node_list):
i = max(stub_dict.keys()) + 1

stub_dict[i] = {}
stub_dict[i]['load'] = {node: int(node_list[node].peak_load / cfg_ding0.get('assumptions', 'cos_phi_load'))
for node in load_node_set} #{node: demand}
try:
stub_dict[i]['load'] = {node: int(node_list[node].peak_load / cfg_ding0.get('assumptions', 'cos_phi_load'))
for node in load_node_set} #{node: demand}
except KeyError:
stub_dict[i]['load'] = {node: int(node_list[node].peak_load / cfg_ding0.get('assumptions', 'cos_phi_load'))
for node in load_node_set if node in node_list.keys()}
# ToDo: Find origin of problem that in node_list is not only ding0_ids, see check_stub_criterion ToDo
logger.error(f"Key not in node_list, should not be an osm_id.")
stub_dict[i]['root'] = root
stub_dict[i]['comp'] = set(comp)
stub_dict[i]['dist'] = set()
Expand Down
45 changes: 20 additions & 25 deletions ding0/grid/mv_grid/urban_mv_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def mv_urban_connect(mv_grid, osm_graph_red, core_graph, stub_graph, stub_dict,
cabledist_node_set = stub_data['dist']
root_node = stub_data['root']
comp = load_node_set.union({root_node}, cabledist_node_set)
# comp = set(nx.dfs_preorder_nodes(stub_graph, source=root_node))

# stub root node does not intersect with routed branches
if not root_node in osmid_branch_dict:
Expand All @@ -57,33 +58,33 @@ def mv_urban_connect(mv_grid, osm_graph_red, core_graph, stub_graph, stub_dict,
# root node has branching, old root node will be cable dist, new root node intersect with routed branches
if root_deg > 1 and len(comp) > 2:
# in case root node has deg > 1, build new edge between old and new root
node = root_node
node = root_node
if not node in node_list.keys():
cabledist_node_set.add(node)

line_shp, line_length, path = get_shortest_path_shp_multi_target(osm_graph_red,
node, routed_graph_node_set)

root_node = path[0]
stub_graph.add_node(root_node, x=osm_graph_red.nodes[root_node]['x'], y=osm_graph_red.nodes[root_node]['y'])
stub_graph.add_edge(root_node, node, geometry=line_shp)
comp.add(root_node)

else:
# in case deg = 1, delete old root and compute new edge from root neignbor to graph
# compute new edge geometry to ding0 graph
root_nb = list({n for n in stub_graph.neighbors(root_node)} & set(comp))[0]
# compute new edge geometry to ding0 graph
if len(comp) == 2:
root_nb = next(iter(load_node_set))
else:
root_nb = list({n for n in stub_graph.neighbors(root_node)} & set(comp))[0]

line_shp, line_length, path = get_shortest_path_shp_multi_target(osm_graph_red,
root_nb, routed_graph_node_set)
#remove old root node from graph and component
stub_graph.remove_edge(root_node, root_nb)
#remove old root node from component
comp.discard(root_node)
# introduce new root node and update edge
root_node = path[0]
stub_graph.add_node(root_node, x=osm_graph_red.nodes[root_node]['x'], y=osm_graph_red.nodes[root_node]['y'])
stub_graph.add_edge(root_node, root_nb, geometry=line_shp)
#print(root_node, root_nb)
comp.add(root_node)
node = root_nb

# introduce new root node and update edge
root_node = path[0]
comp.add(root_node)
stub_graph.add_node(root_node, x=osm_graph_red.nodes[root_node]['x'], y=osm_graph_red.nodes[root_node]['y'])
stub_graph.add_edge(root_node, node, geometry=line_shp)
stub_graph.add_edge(node, root_node, geometry=line_shp)

#### SPLIT MAIN ROUTE EDGE INTO 2 SEGMENTS

Expand Down Expand Up @@ -195,10 +196,9 @@ def mv_urban_connect(mv_grid, osm_graph_red, core_graph, stub_graph, stub_dict,
# for simple stubs, there is just one load (one edge)
if len(comp) == 2:

load_node = list(load_node_set)[0] #next(iter(
#edge_key = next(iter(stub_graph.get_edge_data(root_node, load_node).keys())) #TODO uncomment
#print(root_node, load_node)
branch_shp = stub_graph.edges[root_node, load_node, 0]['geometry']
load_node = list(load_node_set)[0]
edge_key = next(iter(stub_graph.get_edge_data(root_node, load_node).keys()))
branch_shp = stub_graph.edges[root_node, load_node, edge_key]['geometry']
branch_length = branch_shp.length

mv_grid.graph.add_edge(root_node_ding0, node_list[load_node], branch=BranchDing0(geometry=branch_shp,
Expand All @@ -216,11 +216,6 @@ def mv_urban_connect(mv_grid, osm_graph_red, core_graph, stub_graph, stub_dict,
dist_mapping = {} #for converting osm names to ding0 names in comp_graph
dist_mapping[root_node] = str(root_node_ding0)

# add cable distributor for root, if is not station or load
# add cable distributor for branchings if necessary
#if not root_node in node_list.keys():
# cabledist_node_set.add(root_node)

if len(cabledist_node_set):

for node in cabledist_node_set:
Expand Down

0 comments on commit 34734b0

Please sign in to comment.