def topological_distance(power_lines, power_points, name, weight, voltage, output_workspace): """ Calculation of electrical network centrality as a number of shortest paths between each substation and topologically closest generation points. Parameters ---------- power_lines: str path to the polyline shapefile with all power lines power_points: str path to the point shapefile with all power points (substations, generation) with attribute 'Point_Type', all generation points have value 'ЭС', all substations have values 'ПС' name: str name field for power lines as a third key for multigraph weight: str weight field name for power lines (inverted capacity) voltage: str voltage field name for power lines output_workspace: str path to the output directory Returns ------- number of nodes (original power points without orphan links), number of generation points, number of substation points""" G_network = aux_ie.convert_shp_to_graph(power_lines, "false", "true", name) G_points = nx.read_shp(power_points) number_nodes = int(G_points.number_of_nodes()) dict_point_type = {} t1 = nx.get_node_attributes(G_points, 'Point_Type') nodes_from_points = G_points.nodes for node_p in nodes_from_points: dict_point_type[node_p] = t1[node_p] nx.set_node_attributes(G_network, dict_point_type, 'type') nodes_from_network = G_network.nodes generation = set() node_dict = nx.get_node_attributes(G_network, 'type') for node in nodes_from_network: if node in node_dict: if node_dict[node] == 'ЭС': generation.add(node) generation_count = len(generation) substation_count = number_nodes - generation_count # G_network, trace_dict = trace_lines(G_network, voltage) shortest_path = nx.multi_source_dijkstra_path(G_network, generation, weight=weight) aux_ie.export_path_to_shp(G_network, "true", output_workspace, trace_dict + [shortest_path]) return number_nodes, generation_count, substation_count
def test_simple_paths(self): G = nx.path_graph(4) lengths = nx.multi_source_dijkstra_path_length(G, [0]) assert lengths == {n: n for n in G} paths = nx.multi_source_dijkstra_path(G, [0]) assert paths == {n: list(range(n + 1)) for n in G}
def test_path_no_sources(self): with pytest.raises(ValueError): nx.multi_source_dijkstra_path(nx.Graph(), {})
import networkx as nx import matplotlib.pyplot as plt G=nx.read_gml('./football.gml') nx.draw(G,with_labels=True) #plt.show() print(nx.shortest_path(G, source='Buffalo', target='Kent')) print(nx.shortest_path(G, source='Buffalo', target='Rice')) # Dijkstra算法 print(nx.single_source_dijkstra_path(G, 'Buffalo')) print(nx.multi_source_dijkstra_path(G, {'Buffalo', 'Rice'})) # Flody算法 print(nx.floyd_warshall(G, weight='weight'))
def voronoi_cells(G, center_nodes, weight='weight'): """Returns the Voronoi cells centered at `center_nodes` with respect to the shortest-path distance metric. If *C* is a set of nodes in the graph and *c* is an element of *C*, the *Voronoi cell* centered at a node *c* is the set of all nodes *v* that are closer to *c* than to any other center node in *C* with respect to the shortest-path distance metric. [1]_ For directed graphs, this will compute the "outward" Voronoi cells, as defined in [1]_, in which distance is measured from the center nodes to the target node. For the "inward" Voronoi cells, use the :meth:`DiGraph.reverse` method to reverse the orientation of the edges before invoking this function on the directed graph. Parameters ---------- G : NetworkX graph center_nodes : set A nonempty set of nodes in the graph `G` that represent the center of the Voronoi cells. weight : string or function The edge attribute (or an arbitrary function) representing the weight of an edge. This keyword argument is as described in the documentation for :func:`~networkx.multi_source_dijkstra_path`, for example. Returns ------- dictionary A mapping from center node to set of all nodes in the graph closer to that center node than to any other center node. The keys of the dictionary are the element of `center_nodes`, and the values of the dictionary form a partition of the nodes of `G`. Examples -------- To get only the partition of the graph induced by the Voronoi cells, take the collection of all values in the returned dictionary:: >>> G = nx.path_graph(6) >>> center_nodes = {0, 3} >>> cells = nx.voronoi_cells(G, center_nodes) >>> partition = set(map(frozenset, cells.values())) >>> sorted(map(sorted, partition)) [[0, 1], [2, 3, 4, 5]] Raises ------ ValueError If `center_nodes` is empty. References ---------- .. [1] Erwig, Martin. (2000), "The graph Voronoi diagram with applications." *Networks*, 36: 156--163. <dx.doi.org/10.1002/1097-0037(200010)36:3<156::AID-NET2>3.0.CO;2-L> """ # Determine the shortest paths from any one of the center nodes to # every node in the graph. # # This raises `ValueError` if `center_nodes` is an empty set. paths = nx.multi_source_dijkstra_path(G, center_nodes, weight=weight) # Determine the center node from which the shortest path originates. nearest = {v: p[0] for v, p in paths.items()} # Get the mapping from center node to all nodes closer to it than to # any other center node. cells = groups(nearest) # We collect all unreachable nodes under a special key, if there are any. unreachable = set(G) - set(nearest) if unreachable: cells['unreachable'] = unreachable return cells
def test_simple_paths(self): G = nx.path_graph(4) lengths = nx.multi_source_dijkstra_path_length(G, [0]) assert_equal(lengths, {n: n for n in G}) paths = nx.multi_source_dijkstra_path(G, [0]) assert_equal(paths, {n: list(range(n + 1)) for n in G})
def test_path_no_sources(self): nx.multi_source_dijkstra_path(nx.Graph(), {})
G1 = G1.to_undirected() dictionary_a = {} nodes_a = G1.nodes for node1 in nodes_a: t1 = nx.get_node_attributes(G1, 'Point_Type') dictionary_a[node1] = t1[node1] nx.set_node_attributes(G, dictionary_a, 'type') nodes_g = nx.nodes(G) gen = set() node_dict = nx.get_node_attributes(G, 'type') for node in nodes_g: if node in node_dict: if node_dict[node] == 'ЭС': print(node, ' is generation') gen.add(node) path = nx.multi_source_dijkstra_path(G, gen) export_path_to_shp(path, "true", 'Name', path_e, G) create_cpg(file_path) driver = ogr.GetDriverByName('ESRI Shapefile') dataSource = driver.Open('{}.shp'.format(file_path)) src_layer = dataSource.GetLayer() records = process_layer(src_layer) data_source = driver.CreateDataSource(os.path.join(path_e, 'el_centrality{}.shp'.format(year))) dst_layer = data_source.CreateLayer(file_path, None, ogr.wkbMultiLineString, options=["ENCODING=CP1251"]) field_name = ogr.FieldDefn('name', ogr.OFTString) field_name.SetWidth(80) dst_layer.CreateField(field_name) dst_layer.CreateField(ogr.FieldDefn('count', ogr.OFTInteger))
def test_simple_paths(self): G = nx.path_graph(4) lengths = nx.multi_source_dijkstra_path_length(G, [0]) assert_equal(lengths, dict((n, n) for n in G)) paths = nx.multi_source_dijkstra_path(G, [0]) assert_equal(paths, dict((n, list(range(n + 1))) for n in G))