def test_dijkstra(self): (D, P) = nx.single_source_dijkstra(self.XG, 's') validate_path(self.XG, 's', 'v', 9, P['v']) assert_equal(D['v'], 9) validate_path( self.XG, 's', 'v', 9, nx.single_source_dijkstra_path(self.XG, 's')['v']) assert_equal( nx.single_source_dijkstra_path_length(self.XG, 's')['v'], 9) validate_path( self.XG, 's', 'v', 9, nx.single_source_dijkstra(self.XG, 's')[1]['v']) validate_path( self.MXG, 's', 'v', 9, nx.single_source_dijkstra_path(self.MXG, 's')['v']) GG = self.XG.to_undirected() # make sure we get lower weight # to_undirected might choose either edge with weight 2 or weight 3 GG['u']['x']['weight'] = 2 (D, P) = nx.single_source_dijkstra(GG, 's') validate_path(GG, 's', 'v', 8, P['v']) assert_equal(D['v'], 8) # uses lower weight of 2 on u<->x edge validate_path(GG, 's', 'v', 8, nx.dijkstra_path(GG, 's', 'v')) assert_equal(nx.dijkstra_path_length(GG, 's', 'v'), 8) validate_path(self.XG2, 1, 3, 4, nx.dijkstra_path(self.XG2, 1, 3)) validate_path(self.XG3, 0, 3, 15, nx.dijkstra_path(self.XG3, 0, 3)) assert_equal(nx.dijkstra_path_length(self.XG3, 0, 3), 15) validate_path(self.XG4, 0, 2, 4, nx.dijkstra_path(self.XG4, 0, 2)) assert_equal(nx.dijkstra_path_length(self.XG4, 0, 2), 4) validate_path(self.MXG4, 0, 2, 4, nx.dijkstra_path(self.MXG4, 0, 2)) validate_path( self.G, 's', 'v', 2, nx.single_source_dijkstra(self.G, 's', 'v')[1]['v']) validate_path( self.G, 's', 'v', 2, nx.single_source_dijkstra(self.G, 's')[1]['v']) validate_path(self.G, 's', 'v', 2, nx.dijkstra_path(self.G, 's', 'v')) assert_equal(nx.dijkstra_path_length(self.G, 's', 'v'), 2) # NetworkXError: node s not reachable from moon assert_raises(nx.NetworkXNoPath, nx.dijkstra_path, self.G, 's', 'moon') assert_raises( nx.NetworkXNoPath, nx.dijkstra_path_length, self.G, 's', 'moon') validate_path(self.cycle, 0, 3, 3, nx.dijkstra_path(self.cycle, 0, 3)) validate_path(self.cycle, 0, 4, 3, nx.dijkstra_path(self.cycle, 0, 4)) assert_equal( nx.single_source_dijkstra(self.cycle, 0, 0), ({0: 0}, {0: [0]}))
def test_dijkstra(self): (D, P) = nx.single_source_dijkstra(self.XG, 's') validate_path(self.XG, 's', 'v', 9, P['v']) assert_equal(D['v'], 9) validate_path( self.XG, 's', 'v', 9, nx.single_source_dijkstra_path(self.XG, 's')['v']) assert_equal(dict( nx.single_source_dijkstra_path_length(self.XG, 's'))['v'], 9) validate_path( self.XG, 's', 'v', 9, nx.single_source_dijkstra(self.XG, 's')[1]['v']) validate_path( self.MXG, 's', 'v', 9, nx.single_source_dijkstra_path(self.MXG, 's')['v']) GG = self.XG.to_undirected() # make sure we get lower weight # to_undirected might choose either edge with weight 2 or weight 3 GG['u']['x']['weight'] = 2 (D, P) = nx.single_source_dijkstra(GG, 's') validate_path(GG, 's', 'v', 8, P['v']) assert_equal(D['v'], 8) # uses lower weight of 2 on u<->x edge validate_path(GG, 's', 'v', 8, nx.dijkstra_path(GG, 's', 'v')) assert_equal(nx.dijkstra_path_length(GG, 's', 'v'), 8) validate_path(self.XG2, 1, 3, 4, nx.dijkstra_path(self.XG2, 1, 3)) validate_path(self.XG3, 0, 3, 15, nx.dijkstra_path(self.XG3, 0, 3)) assert_equal(nx.dijkstra_path_length(self.XG3, 0, 3), 15) validate_path(self.XG4, 0, 2, 4, nx.dijkstra_path(self.XG4, 0, 2)) assert_equal(nx.dijkstra_path_length(self.XG4, 0, 2), 4) validate_path(self.MXG4, 0, 2, 4, nx.dijkstra_path(self.MXG4, 0, 2)) validate_path( self.G, 's', 'v', 2, nx.single_source_dijkstra(self.G, 's', 'v')[1]['v']) validate_path( self.G, 's', 'v', 2, nx.single_source_dijkstra(self.G, 's')[1]['v']) validate_path(self.G, 's', 'v', 2, nx.dijkstra_path(self.G, 's', 'v')) assert_equal(nx.dijkstra_path_length(self.G, 's', 'v'), 2) # NetworkXError: node s not reachable from moon assert_raises(nx.NetworkXNoPath, nx.dijkstra_path, self.G, 's', 'moon') assert_raises( nx.NetworkXNoPath, nx.dijkstra_path_length, self.G, 's', 'moon') validate_path(self.cycle, 0, 3, 3, nx.dijkstra_path(self.cycle, 0, 3)) validate_path(self.cycle, 0, 4, 3, nx.dijkstra_path(self.cycle, 0, 4)) assert_equal( nx.single_source_dijkstra(self.cycle, 0, 0), ({0: 0}, {0: [0]}))
def test_dijkstra(self): (D, P) = nx.single_source_dijkstra(self.XG, 's') assert_equal(P['v'], ['s', 'x', 'u', 'v']) assert_equal(D['v'], 9) assert_equal( nx.single_source_dijkstra_path(self.XG, 's')['v'], ['s', 'x', 'u', 'v']) assert_equal( nx.single_source_dijkstra_path_length(self.XG, 's')['v'], 9) assert_equal( nx.single_source_dijkstra(self.XG, 's')[1]['v'], ['s', 'x', 'u', 'v']) assert_equal( nx.single_source_dijkstra_path(self.MXG, 's')['v'], ['s', 'x', 'u', 'v']) GG = self.XG.to_undirected() (D, P) = nx.single_source_dijkstra(GG, 's') assert_equal(P['v'], ['s', 'x', 'u', 'v']) assert_equal(D['v'], 8) # uses lower weight of 2 on u<->x edge assert_equal(nx.dijkstra_path(GG, 's', 'v'), ['s', 'x', 'u', 'v']) assert_equal(nx.dijkstra_path_length(GG, 's', 'v'), 8) assert_equal(nx.dijkstra_path(self.XG2, 1, 3), [1, 4, 5, 6, 3]) assert_equal(nx.dijkstra_path(self.XG3, 0, 3), [0, 1, 2, 3]) assert_equal(nx.dijkstra_path_length(self.XG3, 0, 3), 15) assert_equal(nx.dijkstra_path(self.XG4, 0, 2), [0, 1, 2]) assert_equal(nx.dijkstra_path_length(self.XG4, 0, 2), 4) assert_equal(nx.dijkstra_path(self.MXG4, 0, 2), [0, 1, 2]) assert_equal( nx.single_source_dijkstra(self.G, 's', 'v')[1]['v'], ['s', 'u', 'v']) assert_equal( nx.single_source_dijkstra(self.G, 's')[1]['v'], ['s', 'u', 'v']) assert_equal(nx.dijkstra_path(self.G, 's', 'v'), ['s', 'u', 'v']) assert_equal(nx.dijkstra_path_length(self.G, 's', 'v'), 2) # NetworkXError: node s not reachable from moon assert_raises(nx.NetworkXError, nx.dijkstra_path, self.G, 's', 'moon') assert_raises(nx.NetworkXError, nx.dijkstra_path_length, self.G, 's', 'moon') assert_equal(nx.dijkstra_path(self.cycle, 0, 3), [0, 1, 2, 3]) assert_equal(nx.dijkstra_path(self.cycle, 0, 4), [0, 6, 5, 4])
def test_dijkstra(self): (D, P) = nx.single_source_dijkstra(self.XG, 's') validate_path(self.XG, 's', 'v', 9, P['v']) assert D['v'] == 9 validate_path(self.XG, 's', 'v', 9, nx.single_source_dijkstra_path(self.XG, 's')['v']) assert dict(nx.single_source_dijkstra_path_length(self.XG, 's'))['v'] == 9 validate_path(self.XG, 's', 'v', 9, nx.single_source_dijkstra(self.XG, 's')[1]['v']) validate_path(self.MXG, 's', 'v', 9, nx.single_source_dijkstra_path(self.MXG, 's')['v']) GG = self.XG.to_undirected() # make sure we get lower weight # to_undirected might choose either edge with weight 2 or weight 3 GG['u']['x']['weight'] = 2 (D, P) = nx.single_source_dijkstra(GG, 's') validate_path(GG, 's', 'v', 8, P['v']) assert D['v'] == 8 # uses lower weight of 2 on u<->x edge validate_path(GG, 's', 'v', 8, nx.dijkstra_path(GG, 's', 'v')) assert nx.dijkstra_path_length(GG, 's', 'v') == 8 validate_path(self.XG2, 1, 3, 4, nx.dijkstra_path(self.XG2, 1, 3)) validate_path(self.XG3, 0, 3, 15, nx.dijkstra_path(self.XG3, 0, 3)) assert nx.dijkstra_path_length(self.XG3, 0, 3) == 15 validate_path(self.XG4, 0, 2, 4, nx.dijkstra_path(self.XG4, 0, 2)) assert nx.dijkstra_path_length(self.XG4, 0, 2) == 4 validate_path(self.MXG4, 0, 2, 4, nx.dijkstra_path(self.MXG4, 0, 2)) validate_path(self.G, 's', 'v', 2, nx.single_source_dijkstra(self.G, 's', 'v')[1]) validate_path(self.G, 's', 'v', 2, nx.single_source_dijkstra(self.G, 's')[1]['v']) validate_path(self.G, 's', 'v', 2, nx.dijkstra_path(self.G, 's', 'v')) assert nx.dijkstra_path_length(self.G, 's', 'v') == 2 # NetworkXError: node s not reachable from moon pytest.raises(nx.NetworkXNoPath, nx.dijkstra_path, self.G, 's', 'moon') pytest.raises(nx.NetworkXNoPath, nx.dijkstra_path_length, self.G, 's', 'moon') validate_path(self.cycle, 0, 3, 3, nx.dijkstra_path(self.cycle, 0, 3)) validate_path(self.cycle, 0, 4, 3, nx.dijkstra_path(self.cycle, 0, 4)) assert nx.single_source_dijkstra(self.cycle, 0, 0) == (0, [0])
def failure(graph, node): if str(type(node)) == "<class 'list'>": return failure_list(graph, node) if node not in graph.nodes: print("The node:", node, "is not in the network!") return -1 # Get the node informations info = graph.nodes[node] info.pop('table') info.pop('BIFT') if 1 <= node and node <= 128: info.pop('match_table') # edge(node, 8) edge(node, 9) edge(node, 2) node_adj = graph.adj[ node] # info: node -> adj e.g {8: {'weight': 1.0}, 9: {'weight': 1.0}, 2: {'weight': 1.0}} # edge(8, node) edge(8, node) edge(2, node) adj_node = { } # info: adj -> node e.g {8: {'weight': 1.0}, 9: {'weight': 1.0}, 2: {'weight': 1.0}} for adj in node_adj: adj_node[adj] = graph.adj[adj][node] if len(node_adj) != len(adj_node): print("WARNING !!! Error in edges information.") node_info = { 'node': node, 'node_adj': node_adj, 'adj_node': adj_node, 'info': info } # Remove the node graph.remove_node(node) # Update 'table' list_nodes = list(graph.nodes) for u_node in list_nodes: table = nx.single_source_dijkstra_path(graph, u_node) # for weighed graphs graph.nodes[u_node]['table'] = table # Update 'BIFT' and 'match_table' match_table = {} ingress_egress = [] for node_e in list_nodes: if 1 <= node_e and node_e <= 128: ingress_egress.append(node_e) for ie_node in ingress_egress: match_table[ie_node] = 2**(ie_node - 1) for _node in list_nodes: if _node in ingress_egress: # if ingress/egress graph.nodes[_node]['match_table'] = match_table bift = {} for e in graph.adj[_node]: bift[e] = 0 for e in ingress_egress: if e != _node: if e in graph.nodes[_node]['table']: n_hop = graph.nodes[_node]['table'][e][1] bift[n_hop] |= match_table[e] graph.nodes[_node]['BIFT'] = bift return node_info
def stop_walking_area(walk_graph, walk_dis, start_node, link_near_stop): cur_path = dict( nx.single_source_dijkstra_path(walk_graph, start_node, cutoff=walk_dis, weight='weight')) del cur_path[start_node] reach_links = {} for key in cur_path: sub_path = list(zip(cur_path[key][:-1], cur_path[key][1:])) for each_link in sub_path: if each_link in reach_links: next else: reach_links[each_link] = 1 reach_links_df = pd.DataFrame.from_dict(reach_links, orient='index', columns=['accessed' ]).reset_index() reach_links_df.rename(columns={'index': 'b_e'}, inplace=True) streets_access = streets[(streets['b_e'].isin(reach_links_df['b_e'])) | (streets['e_b'].isin(reach_links_df['b_e'])) | (streets['LinkID'] == link_near_stop)] geom = [x for x in streets_access.geometry] multi_line = geometry.MultiLineString(geom) multi_line_polygon = multi_line.convex_hull if multi_line_polygon.geom_type != 'Polygon': multi_line_polygon = multi_line_polygon.envelope return multi_line_polygon
def PTO_diam_path_branches(MST, diam_path): #1. Find branches MST_copy = MST.copy() branches = {} for node in diam_path: branches[node] = [] for subnode in MST.neighbors(node): if subnode not in diam_path: MST_copy.remove_edge(node, subnode) branches[node].append(nx.single_source_dijkstra_path(MST_copy, subnode).keys()) #2. Find subgraphs subgraphs = {} for root in branches.keys(): subgraphs[root] = [] for branch in branches[root]: subg_tmp = MST.subgraph(branch) subgraphs[root].append(subg_tmp) #3. Return results class Results: def __init__(self, branches, subgraphs): self.branches, self.subgraphs = branches, subgraphs return Results(branches, subgraphs)
def failure(graph, node): if str(type(node))=="<class 'list'>": return failure_list(graph, node) if node not in graph.nodes: print("The node:", node, "is not in the network!") return -1 # Get the node informations info = graph.nodes[node] info.pop('table') # edge(node, 8) edge(node, 9) edge(node, 2) node_adj = graph.adj[node] # info: node -> adj e.g {8: {'weight': 1.0}, 9: {'weight': 1.0}, 2: {'weight': 1.0}} # edge(8, node) edge(8, node) edge(2, node) adj_node = {} # info: adj -> node e.g {8: {'weight': 1.0}, 9: {'weight': 1.0}, 2: {'weight': 1.0}} for adj in node_adj: adj_node[adj] = graph.adj[adj][node] if len(node_adj) != len(adj_node): print("WARNING !!! Error in edges information.") node_info = {'node': node, 'node_adj': node_adj, 'adj_node': adj_node, 'info': info} # Remove the node graph.remove_node(node) # Update tables list_nodes = list(graph.nodes) for u_node in list_nodes: table = nx.single_source_dijkstra_path(graph, u_node) # for weighed graphs graph.nodes[u_node]['table'] = table return node_info
def caminhos_minimos_um_no(nx_grafo, no, file_name_prefix): lista_dijkstra_path = [] dijkstra_dict_path = dict( nx.single_source_dijkstra_path(nx_grafo, no, weight='weight')) for key, value in dijkstra_dict_path.items(): lista_dijkstra_path.append([no, key, value]) dijkstra_df_path = pd.DataFrame(lista_dijkstra_path, columns=['origem', 'destino', 'caminho']) dijkstra_df_path.to_csv( 'D:\\repos\\study\\mestrado\\artigos\\UBS\\resultados\\' + str(file_name_prefix) + str(no) + '_dijkstra_path_singlesource.csv', sep=';') # Executando função para pegar as distâncias dos caminhos # mínimos entre todos os nós lista_dijkstra_length = [] dijkstra_dict_length = dict( nx.single_source_dijkstra_path_length(nx_grafo, no, weight='weight')) for key, value in dijkstra_dict_length.items(): lista_dijkstra_length.append([no, key, value]) dijkstra_df_length = pd.DataFrame( lista_dijkstra_length, columns=['origem', 'destino', 'distancia']) # Arredondando valores das distâncias para duas casas decimais dijkstra_df_length = dijkstra_df_length.round(2) dijkstra_df_length.to_csv( 'D:\\repos\\study\\mestrado\\artigos\\UBS\\resultados\\' + str(file_name_prefix) + str(no) + '_dijkstra_length_singlesource.csv', sep=';')
def failure_list(graph, nodes): nodes_info = [] for node in nodes: if node not in graph.nodes: print("The node:", node, "is not in the network!") continue # Get the node informations info = graph.nodes[node] info.pop('table') # edge(node, 8) edge(node, 9) edge(node, 2) node_adj = graph.adj[node] # info: node -> adj e.g {8: {'weight': 1.0}, 9: {'weight': 1.0}, 2: {'weight': 1.0}} # edge(8, node) edge(8, node) edge(2, node) adj_node = {} # info: adj -> node e.g {8: {'weight': 1.0}, 9: {'weight': 1.0}, 2: {'weight': 1.0}} for adj in node_adj: adj_node[adj] = graph.adj[adj][node] if len(node_adj) != len(adj_node): print("WARNING !!! Error in edges information.") node_info = {'node': node, 'node_adj': node_adj, 'adj_node': adj_node, 'info': info} nodes_info.append(node_info) # Remove the node graph.remove_node(node) # Update tables startTime = time.perf_counter() # start time list_nodes = list(graph.nodes) for u_node in list_nodes: table = nx.single_source_dijkstra_path(graph, u_node) # for weighed graphs graph.nodes[u_node]['table'] = table endTime = time.perf_counter() # end time return nodes_info, endTime-startTime
def solve_fiber_path(row, owner, fiber_nodes_owner, G1): current_node = str(int(row['node_id'])) paths = nx.single_source_dijkstra_path(G1, current_node) lengths = nx.single_source_dijkstra_path_length(G1, current_node) all_paths_from_fiber = {node: paths[node] for node in fiber_nodes_owner} all_lengths_from_fiber = { node: lengths[node] for node in fiber_nodes_owner } if (len(all_paths_from_fiber) > 0): optimal_node = min(all_lengths_from_fiber, key=all_lengths_from_fiber.get) optimal_path = paths[optimal_node] optimal_path_length = lengths[optimal_node] else: optimal_path = [None] optimal_path_length = None output = pd.Series({ ('length_' + owner): optimal_path_length, ('path_' + owner): optimal_path, ('fiber_node_' + owner): optimal_path[-1] }) return output
def create_directed_graphs(self): ''' :return: ''' self.directed_graphs = np.empty( (self.no_of_atoms, self.no_of_atoms - 1, 3), dtype=int) # parse all the atoms one by one and get directed graph to that atom # as the sink node for idx in xrange(self.no_of_atoms): # get shortest path from the root to all the other atoms and then reverse the edges. path = nx.single_source_dijkstra_path(self.graph, idx) G = nx.DiGraph() for i in xrange(self.no_of_atoms): temp = path[i] temp.reverse() G.add_path(temp) # do a topological sort to get a order of atoms with all edges pointing to the root topological_order = nx.topological_sort(G) sorted_path = np.empty((self.no_of_atoms - 1, 3)) no_of_incoming_edges = {} for i in xrange(self.no_of_atoms - 1): node = topological_order[i] edge = (nx.edges(G, node))[0] if edge[1] in no_of_incoming_edges: index = no_of_incoming_edges[edge[1]] no_of_incoming_edges[edge[1]] += 1 else: index = 0 no_of_incoming_edges[edge[1]] = 1 sorted_path[i, :] = [node, edge[1], index] self.directed_graphs[idx, :, :] = sorted_path
def context_splitting_graph_many(obj: Union[HybridCloud, CloudEnsemble], sources: Iterable[int], max_dist: float) -> List[list]: """ Performs a dijkstra shortest paths on the obj's weighted graph to retrieve the skeleton nodes within `max_dist` for every source node ID in `sources`. Args: obj: The HybridCloud/CloudEnsemble with the graph, nodes and vertices. sources: The source nodes. max_dist: The maximum distance to the source node (along the graph). Returns: The nodes within the requested context for every source node - same ordering as `sources`. """ g = obj.graph() if isinstance(sources, list) and len(sources) == 1: path = nx.single_source_dijkstra_path(g, sources[0], weight='weight', cutoff=max_dist) return [list(path.keys())] else: paths = dict( nx.all_pairs_dijkstra_path(g, weight='weight', cutoff=max_dist)) return [list(paths[s].keys()) for s in sources]
def test_bidirectional_dijkstra(self): validate_length_path(self.XG, "s", "v", 9, *nx.bidirectional_dijkstra(self.XG, "s", "v")) validate_length_path(self.G, "s", "v", 2, *nx.bidirectional_dijkstra(self.G, "s", "v")) validate_length_path(self.cycle, 0, 3, 3, *nx.bidirectional_dijkstra(self.cycle, 0, 3)) validate_length_path(self.cycle, 0, 4, 3, *nx.bidirectional_dijkstra(self.cycle, 0, 4)) validate_length_path(self.XG3, 0, 3, 15, *nx.bidirectional_dijkstra(self.XG3, 0, 3)) validate_length_path(self.XG4, 0, 2, 4, *nx.bidirectional_dijkstra(self.XG4, 0, 2)) # need more tests here P = nx.single_source_dijkstra_path(self.XG, "s")["v"] validate_path( self.XG, "s", "v", sum(self.XG[u][v]["weight"] for u, v in zip(P[:-1], P[1:])), nx.dijkstra_path(self.XG, "s", "v"), ) # check absent source G = nx.path_graph(2) pytest.raises(nx.NodeNotFound, nx.bidirectional_dijkstra, G, 3, 0)
def calculate_green_path(graph, gn_id): ''' print 'Green path from green node [%d] = ' % (gn_id) print nx.single_source_dijkstra_path(graph, source=gn_id, weight='weight') ''' return nx.single_source_dijkstra_path(graph, source=gn_id, weight='weight')
def compute_all_shorted_path(graph, voxels_size, neighbor_size=45): """ Compute all the shorted path from the base position of the graph position. Parameters ---------- graph : networkx.Graph Graph of 3d voxel center point position voxels_size : int Voxels diameter size Returns ------- all_shorted_path_to_stem_base : dict List of all the shorted path of the graph from the base """ # ========================================================================== # Get the high points in the matrix and the supposed base plant points x_stem, y_stem, z_stem = find_base_stem_position( graph.nodes(), voxels_size, neighbor_size=neighbor_size) # ========================================================================== # Compute the shorted path all_shorted_path_to_stem_base = networkx.single_source_dijkstra_path( graph, (x_stem, y_stem, z_stem), weight="weight") return all_shorted_path_to_stem_base
def worker(taskq, resultq, G, pois): for srcid in iter(taskq.get, 'STOP'): # dists = nx.single_source_dijkstra_path_length(G, srcid) # d = {} # for dstid in dists: # if dstid in pois: d[dstid] = dists[dstid] path = nx.single_source_dijkstra_path(G, srcid) d = {} for dstid in path: if dstid not in pois: continue l, j = [], [] p = path[dstid] if len(p) <= 1: l.append(5.0) j.append(0.0) else: for i in xrange(len(p) - 1): e = G.edge[p[i]][p[i + 1]] l.append(float(e['latency'])) j.append(float(e['jitter'])) d[dstid] = { 'latency': float(sum(l)), 'jitter': float(sum(j) / float(len(j))) } resultq.put((srcid, d))
def hirarcy(self, c, files_dict): edges = [] interfaces = 'select path, superClass from classes' files_Names = [x.split(".")[-1] for x in files_dict] pathNames = {} for row in c.execute(interfaces): nameClass = (row[0]).split(".")[-1] pathNames[nameClass] = row[0] nameSuper = (row[1]).split(".")[-1] if (nameClass in files_Names): sup = 'root' if (nameSuper in files_Names): sup = nameSuper edges.append((sup, nameClass)) g = networkx.DiGraph() g.add_edges_from(edges) degs = g.out_degree() degsIN = g.in_degree() succ = dict(networkx.bfs_successors(g, 'root')) for s in succ: succ[s] = len(succ[s]) paths = networkx.single_source_dijkstra_path(g, 'root') depth = {} for n in g.nodes(): depth[n] = 2 if (n in paths): depth[n] = len(paths[n]) self.addFromDict(files_dict, degs, pathNames) self.addFromDict(files_dict, degsIN, pathNames) self.addFromDict(files_dict, succ, pathNames) self.addFromDict(files_dict, depth, pathNames) return files_Names,
def hirarcy(self, c, files_dict): edges = [] interfaces = 'select path, superClass from classes' files_Names = [x.split(".")[-1] for x in files_dict] pathNames = {} for row in c.execute(interfaces): nameClass = (row[0]).split(".")[-1] pathNames[nameClass] = row[0] nameSuper = (row[1]).split(".")[-1] if (nameClass in files_Names): sup = 'root' if (nameSuper in files_Names): sup = nameSuper edges.append((sup, nameClass)) g = networkx.DiGraph() g.add_node('root') g.add_edges_from(edges) degs = g.out_degree() degsIN = g.in_degree() succ = dict(networkx.bfs_successors(g, 'root')) for s in succ: succ[s] = len(succ[s]) paths = networkx.single_source_dijkstra_path(g, 'root') depth = {} for n in g.nodes(): depth[n] = 2 if (n in paths): depth[n] = len(paths[n]) self.addFromDict(files_dict,degs,pathNames) self.addFromDict(files_dict,degsIN,pathNames) self.addFromDict(files_dict,succ,pathNames) self.addFromDict(files_dict,depth,pathNames) return files_Names,
def test_dijkstra(self): (D,P)= nx.single_source_dijkstra(self.XG,'s') assert_equal(P['v'], ['s', 'x', 'u', 'v']) assert_equal(D['v'],9) assert_equal(nx.single_source_dijkstra_path(self.XG,'s')['v'], ['s', 'x', 'u', 'v']) assert_equal(nx.single_source_dijkstra_path_length(self.XG,'s')['v'],9) assert_equal(nx.single_source_dijkstra(self.XG,'s')[1]['v'], ['s', 'x', 'u', 'v']) assert_equal(nx.single_source_dijkstra_path(self.MXG,'s')['v'], ['s', 'x', 'u', 'v']) GG=self.XG.to_undirected() # make sure we get lower weight # to_undirected might choose either edge with weight 2 or weight 3 GG['u']['x']['weight']=2 (D,P)= nx.single_source_dijkstra(GG,'s') assert_equal(P['v'] , ['s', 'x', 'u', 'v']) assert_equal(D['v'],8) # uses lower weight of 2 on u<->x edge assert_equal(nx.dijkstra_path(GG,'s','v'), ['s', 'x', 'u', 'v']) assert_equal(nx.dijkstra_path_length(GG,'s','v'),8) assert_equal(nx.dijkstra_path(self.XG2,1,3), [1, 4, 5, 6, 3]) assert_equal(nx.dijkstra_path(self.XG3,0,3), [0, 1, 2, 3]) assert_equal(nx.dijkstra_path_length(self.XG3,0,3),15) assert_equal(nx.dijkstra_path(self.XG4,0,2), [0, 1, 2]) assert_equal(nx.dijkstra_path_length(self.XG4,0,2), 4) assert_equal(nx.dijkstra_path(self.MXG4,0,2), [0, 1, 2]) assert_equal(nx.single_source_dijkstra(self.G,'s','v')[1]['v'], ['s', 'u', 'v']) assert_equal(nx.single_source_dijkstra(self.G,'s')[1]['v'], ['s', 'u', 'v']) assert_equal(nx.dijkstra_path(self.G,'s','v'), ['s', 'u', 'v']) assert_equal(nx.dijkstra_path_length(self.G,'s','v'), 2) # NetworkXError: node s not reachable from moon assert_raises(nx.NetworkXNoPath,nx.dijkstra_path,self.G,'s','moon') assert_raises(nx.NetworkXNoPath,nx.dijkstra_path_length,self.G,'s','moon') assert_equal(nx.dijkstra_path(self.cycle,0,3),[0, 1, 2, 3]) assert_equal(nx.dijkstra_path(self.cycle,0,4), [0, 6, 5, 4]) assert_equal(nx.single_source_dijkstra(self.cycle,0,0),({0:0}, {0:[0]}) )
def cont_filt(wood, leaf, dist_threshold=0.05): arr = np.vstack((wood, leaf)) wood_ids = np.arange(wood.shape[0]) center = base_center(arr, base_length=0.3)[0] G = create_graph_iter(arr, n_neighbors=5, nn_step=2, dist_threshold=np.inf, maxiter=20) print('Graph created') nbrs = NearestNeighbors(leaf_size=15, n_jobs=-1).fit(arr) base_id = nbrs.kneighbors(center.reshape(1, -1), 1, return_distance=False)[0][0] mask = np.zeros(G.number_of_nodes()).astype(bool) # Calculating the shortest path shortpath = nx.single_source_dijkstra_path_length(G, base_id) # Obtaining the node coordinates and their respective distance from # the base point. nodes_ids = shortpath.keys() dist = shortpath.values() # Obtaining path list for every node. path = nx.single_source_dijkstra_path(G, base_id) # Obtaining nodes coordinates. nodes = arr[nodes_ids] dist = np.array(dist) path = path.values() path_nodes = [i for j in path for i in j] mask[path_nodes] = True mask[wood_ids] = True e = np.inf threshold = 1 print('Starting region growing') while e > threshold: nbrs = NearestNeighbors(leaf_size=15, n_jobs=-1).fit(arr[~mask]) e1 = np.sum(mask) nbrs_dist, nbrs_ids = nbrs.kneighbors(arr[mask], 1) for i, nbr_i in enumerate(nbrs_ids[nbrs_dist <= dist_threshold]): if dist[nbr_i] <= dist[mask][i]: mask[nbr_i] = True e2 = np.sum(mask) e = e2 - e1 # e = nbrs_ids.shape[0] print e return nodes[mask], nodes[~mask]
def dijkstra(self): self.routing_table = {} dijkstra = nx.single_source_dijkstra_path(self.LSDB, self.id, weight='weight') for k, v in dijkstra.items(): if type(k) == str: self.routing_table[k] = v[1]
def compute_shortest_paths(self, ydim): start = time.time() self.source = self.N.nodes()[argmin(self.P[self.N.nodes(), ydim])] self.shortest_paths = nx.single_source_dijkstra_path(self.N, self.source) self.max_path_len = max(nx.single_source_dijkstra_path_length(self.N, self.source).values()) print 'G compute time: %f seconds' % (time.time() - start)
def __configure_routing(self): for n in self.graph: self.routing[n] = single_source_dijkstra_path(self.graph, n) self.ipdestlpm = PyTricia() for n, d in self.graph.nodes_iter(data=True): dlist = d.get('ipdests', '').split() for destipstr in dlist: ipnet = ipaddr.IPNetwork(destipstr) xnode = {} self.ipdestlpm[str(ipnet)] = xnode if 'dests' in xnode: xnode['dests'].append(n) else: xnode['net'] = ipnet xnode['dests'] = [n] # install static forwarding table entries to each node # FIXME: there's a problematic bit of code here that triggers # pytricia-related (iterator) core dump for nodename, nodeobj in self.nodes.iteritems(): if isinstance(nodeobj, Router): for prefix in self.ipdestlpm.keys(): lpmnode = self.ipdestlpm.get(prefix) if nodename not in lpmnode['dests']: routes = self.routing[nodename] for d in lpmnode['dests']: try: path = routes[d] except KeyError: self.logger.warn( "No route from {} to {}".format( nodename, d)) continue nexthop = path[1] nodeobj.addForwardingEntry(prefix, nexthop) self.owdhash = {} for a in self.graph: for b in self.graph: key = a + ':' + b rlist = [a] while rlist[-1] != b: nh = self.nexthop(rlist[-1], b) if not nh: self.logger.debug( 'No route from %s to %s (in owd; ignoring)' % (a, b)) return None rlist.append(nh) owd = 0.0 for i in xrange(len(rlist) - 1): owd += self.delay(rlist[i], rlist[i + 1]) self.owdhash[key] = owd
def extract_fragments_for_cc(group_cc, cc_df, bois): """ Construct a graph from the given edge table and find all paths that connect one BOI to another BOI (with no BOIs in between). Each path is called a "fragment", stored as a tuple of the path's nodes, starting with a BOI node and ending with a BOI node. The path from A->B is returned once (where node id A < B). The corresponding reverse path B->A is not returned. Args: group_cc: Arbitrary group ID cc_df: Edge table with columns 'label_a', 'label_b', Returns: """ g = prepare_graph(cc_df, bois) nodes = pd.unique(cc_df[['label_a', 'label_b']].values.reshape(-1)) boi_nodes = {*filter(lambda n: g.nodes[n]['boi'], nodes)} fragments = [] for n in boi_nodes: # Find shortest paths to all other BOIs paths = nx.single_source_dijkstra_path(g, n, weight='distance') frags = [ paths[target] for target in paths.keys() if target in boi_nodes ] # Drop the 1-node (0-edge) path, (i.e. the starting node itself) frags = [*filter(lambda frag: len(frag) > 1, frags)] # Drop all paths that contain an intermediate BOI. # (Keep only the paths with no BOIs along the way.) frags = [ *filter(lambda frag: not boi_nodes.intersection(frag[1:-1]), frags) ] # The first and last nodes are BOIs. assert all( [frag[0] in boi_nodes and frag[-1] in boi_nodes for frag in frags]) fragments.extend(frags) # Reverse order if necessary so the first node ID # is less than the last node ID. # That way paths and their reverse counterparts can be deduplicated below. for f in fragments: assert f[0] in boi_nodes and f[-1] in boi_nodes if f[0] > f[-1]: f[:] = f[::-1] # Store as a set to deduplicate. fragments = {tuple(frag) for frag in fragments} return group_cc, fragments
def dijkstraPathExample(fileName="./" + SUBDIRNAME + "/dijkstraSP.png"): print() G = nx.DiGraph() G.add_weighted_edges_from([ (0, 1, 5.0), (0, 4, 9.0), (0, 7, 8.0), (1, 2, 12.0), (1, 3, 15.0), (1, 7, 4.0), (2, 3, 3.0), (2, 6, 11.0), (3, 6, 9.0), (4, 5, 4.0), (4, 6, 20.0), (4, 7, 5.0), (5, 2, 1.0), (5, 6, 13.0), (7, 5, 6.0), (7, 2, 7.0), ]) sourceNode = 0 sp = nx.single_source_dijkstra_path(G, sourceNode) spL = nx.single_source_dijkstra_path_length(G, sourceNode) finalEdgeTo = {} print('Shortest paths and weights') print('Node\tedgeTo\tWeight') for node in sorted(sp): # if (node != sourceNode): print(str(node) + '\t' + str(sp[node]) + '\t\t' + str(spL[node])) if (node != sourceNode): finalEdgeTo[(sp[node][-2], sp[node][-1])] = { "sp" + str(sourceNode): 'sp' + str(sourceNode) } # print (str(node) + '\t' + str((sp[node][-2], sp[node][-1]))+'\t'+str(spL[node])) nx.set_edge_attributes(G, finalEdgeTo) print('updated graph') print(G.edges.data()) print('Calculating longest path using Topological sort if DAG') print('Is DAG: ' + str(nx.is_directed_acyclic_graph(G))) if (nx.is_directed_acyclic_graph(G)): print('Longest Path') print(nx.dag_longest_path(G)) print('Longest Path Weight') print(nx.dag_longest_path_length(G)) labels = nx.get_edge_attributes(G, 'sp0') pos = nx.spring_layout(G) nx.draw_networkx_edge_labels(G, pos, edge_labels=labels) nx.draw(G, pos, with_labels=True, font_weight='bold') print("Saving file to " + str(fileName)) plt.savefig(fileName) print()
def compute_shortest_paths(self, ydim): start = time.time() self.source = self.N.nodes()[argmin(self.P[self.N.nodes(), ydim])] self.shortest_paths = nx.single_source_dijkstra_path( self.N, self.source) self.max_path_len = max( nx.single_source_dijkstra_path_length(self.N, self.source).values()) print 'G compute time: %f seconds' % (time.time() - start)
def get_shortest_paths(self, source_node, target_node=None): if target_node: path_lengths, paths = nx.single_source_dijkstra( self, source_node.id, target_node.id) else: paths = nx.single_source_dijkstra_path(self, source_node.id) path_lengths = nx.single_source_dijkstra_path_length( self, source_node.id) return paths, path_lengths
def calcRoute(G, start, destL, colL): node = start['node'] try: # calculate the route from the OSM node to all nodes in the network route_dij = nx.single_source_dijkstra_path(G=G, source=node, weight='weight') except: print("Start node " + str(node) + " not found") nearest_node = getNearestNode(node, start['y'], start['x']) print("Using nearest node instead, nodeid: " + str(nearest_node)) route_dij = nx.single_source_dijkstra_path(G=G, source=nearest_node, weight='weight') routeL = [x for i, x in route_dij.items() if i in destL] pairL = [] for route in routeL: status, link = calcPair(G, route) if status: pairL.append(link) pairL = pd.DataFrame(pairL, columns=colL) return pairL, len(routeL) / len(destL)
def calculate_shortest_path(graph, node_id): ''' print 'Shortest path from node [%d] = ' % (node_id) print nx.single_source_dijkstra_path(graph, source=node_id, weight='weight') ''' return nx.single_source_dijkstra_path(graph, source=node_id, weight='weight')
def test_dijkstra(self): (D,P)= nx.single_source_dijkstra(self.XG,'s') assert_equal(P['v'], ['s', 'x', 'u', 'v']) assert_equal(D['v'],9) assert_equal(nx.single_source_dijkstra_path(self.XG,'s')['v'], ['s', 'x', 'u', 'v']) assert_equal(nx.single_source_dijkstra_path_length(self.XG,'s')['v'],9) assert_equal(nx.single_source_dijkstra(self.XG,'s')[1]['v'], ['s', 'x', 'u', 'v']) assert_equal(nx.single_source_dijkstra_path(self.MXG,'s')['v'], ['s', 'x', 'u', 'v']) GG=self.XG.to_undirected() (D,P)= nx.single_source_dijkstra(GG,'s') assert_equal(P['v'] , ['s', 'x', 'u', 'v']) assert_equal(D['v'],8) # uses lower weight of 2 on u<->x edge assert_equal(nx.dijkstra_path(GG,'s','v'), ['s', 'x', 'u', 'v']) assert_equal(nx.dijkstra_path_length(GG,'s','v'),8) assert_equal(nx.dijkstra_path(self.XG2,1,3), [1, 4, 5, 6, 3]) assert_equal(nx.dijkstra_path(self.XG3,0,3), [0, 1, 2, 3]) assert_equal(nx.dijkstra_path_length(self.XG3,0,3),15) assert_equal(nx.dijkstra_path(self.XG4,0,2), [0, 1, 2]) assert_equal(nx.dijkstra_path_length(self.XG4,0,2), 4) assert_equal(nx.dijkstra_path(self.MXG4,0,2), [0, 1, 2]) assert_equal(nx.single_source_dijkstra(self.G,'s','v')[1]['v'], ['s', 'u', 'v']) assert_equal(nx.single_source_dijkstra(self.G,'s')[1]['v'], ['s', 'u', 'v']) assert_equal(nx.dijkstra_path(self.G,'s','v'), ['s', 'u', 'v']) assert_equal(nx.dijkstra_path_length(self.G,'s','v'), 2) # NetworkXError: node s not reachable from moon assert_raises(nx.NetworkXNoPath,nx.dijkstra_path,self.G,'s','moon') assert_raises(nx.NetworkXNoPath,nx.dijkstra_path_length,self.G,'s','moon') assert_equal(nx.dijkstra_path(self.cycle,0,3),[0, 1, 2, 3]) assert_equal(nx.dijkstra_path(self.cycle,0,4), [0, 6, 5, 4]) assert_equal(nx.single_source_dijkstra(self.cycle,0,0),(0, [0]) )
def test_bidirectional_dijkstra(self): assert_equal(nx.bidirectional_dijkstra(self.XG, "s", "v"), (9, ["s", "x", "u", "v"])) assert_equal(nx.bidirectional_dijkstra(self.G, "s", "v"), (2, ["s", "x", "v"])) assert_equal(nx.bidirectional_dijkstra(self.cycle, 0, 3), (3, [0, 1, 2, 3])) assert_equal(nx.bidirectional_dijkstra(self.cycle, 0, 4), (3, [0, 6, 5, 4])) assert_equal(nx.bidirectional_dijkstra(self.XG3, 0, 3), (15, [0, 1, 2, 3])) assert_equal(nx.bidirectional_dijkstra(self.XG4, 0, 2), (4, [0, 1, 2])) # need more tests here assert_equal(nx.dijkstra_path(self.XG, "s", "v"), nx.single_source_dijkstra_path(self.XG, "s")["v"])
def print_path(graph, weight): if weight not in ("length", "time_by_car", "time_by_flight"): # so we don't need to check in each branch later raise ValueError(f"weight not supported: {weight}") print(f"{nx.single_source_dijkstra(graph, 'Taif', weight=weight)}") print( f"{nx.single_source_dijkstra_path_length(graph, 'Taif', weight=weight)}" ) path = nx.single_source_dijkstra_path(graph, 'Taif', weight=weight) print(f"{path}")
def __configure_routing(self): for n in self.graph: self.routing[n] = single_source_dijkstra_path(self.graph, n) self.ipdestlpm = PyTricia() for n,d in self.graph.nodes_iter(data=True): dlist = d.get('ipdests','').split() for destipstr in dlist: ipnet = ipaddr.IPNetwork(destipstr) xnode = {} self.ipdestlpm[str(ipnet)] = xnode if 'dests' in xnode: xnode['dests'].append(n) else: xnode['net'] = ipnet xnode['dests'] = [ n ] # install static forwarding table entries to each node # FIXME: there's a problematic bit of code here that triggers # pytricia-related (iterator) core dump for nodename,nodeobj in self.nodes.iteritems(): if isinstance(nodeobj, Router): for prefix in self.ipdestlpm.keys(): lpmnode = self.ipdestlpm.get(prefix) if nodename not in lpmnode['dests']: routes = self.routing[nodename] for d in lpmnode['dests']: try: path = routes[d] except KeyError: self.logger.warn("No route from {} to {}".format(nodename, d)) continue nexthop = path[1] nodeobj.addForwardingEntry(prefix, nexthop) self.owdhash = {} for a in self.graph: for b in self.graph: key = a + ':' + b rlist = [ a ] while rlist[-1] != b: nh = self.nexthop(rlist[-1], b) if not nh: self.logger.debug('No route from %s to %s (in owd; ignoring)' % (a,b)) return None rlist.append(nh) owd = 0.0 for i in xrange(len(rlist)-1): owd += self.delay(rlist[i],rlist[i+1]) self.owdhash[key] = owd
def get_all_shortest_paths_from_source(self, source_node): """ Get all paths from one source node using Dijkstra :param source_node: :return: """ if source_node not in self.nodes.geometry: raise NetworkError("Source node is invalid") source_node = (source_node.x, source_node.y) return nx.single_source_dijkstra_path(self.graph, source_node)
def test_single_source_shortest_path(self): p=nx.shortest_path(self.cycle,0) assert_equal(p[3],[0,1,2,3]) assert_equal(p,nx.single_source_shortest_path(self.cycle,0)) p=nx.shortest_path(self.grid,1) assert_equal(p[12],[1, 2, 3, 4, 8, 12]) # now with weights p=nx.shortest_path(self.cycle,0,weight=True) assert_equal(p[3],[0,1,2,3]) assert_equal(p,nx.single_source_dijkstra_path(self.cycle,0)) p=nx.shortest_path(self.grid,1,weight=True) assert_equal(p[12],[1, 2, 3, 4, 8, 12])
def test_single_source_shortest_path(self): p=nx.shortest_path(self.cycle,0) assert_equal(p[3],[0,1,2,3]) assert_equal(p,nx.single_source_shortest_path(self.cycle,0)) p=nx.shortest_path(self.grid,1) assert_equal(p[12],[1, 2, 3, 4, 8, 12]) # now with weights p=nx.shortest_path(self.cycle,0,weighted=True) assert_equal(p[3],[0,1,2,3]) assert_equal(p,nx.single_source_dijkstra_path(self.cycle,0)) p=nx.shortest_path(self.grid,1,weighted=True) assert_equal(p[12],[1, 2, 3, 4, 8, 12])
def test_single_source_shortest_path(self): p = nx.shortest_path(self.cycle, 0) assert_equal(p[3], [0, 1, 2, 3]) assert_equal(p, nx.single_source_shortest_path(self.cycle, 0)) p = nx.shortest_path(self.grid, 1) validate_grid_path(4, 4, 1, 12, p[12]) # now with weights p = nx.shortest_path(self.cycle, 0, weight='weight') assert_equal(p[3], [0, 1, 2, 3]) assert_equal(p, nx.single_source_dijkstra_path(self.cycle, 0)) p = nx.shortest_path(self.grid, 1, weight='weight') validate_grid_path(4, 4, 1, 12, p[12])
def test_single_source_shortest_path(self): p=nx.shortest_path(self.cycle,0) assert_equal(p[3],[0,1,2,3]) assert_equal(p,nx.single_source_shortest_path(self.cycle,0)) p=nx.shortest_path(self.grid,1) validate_grid_path(4, 4, 1, 12, p[12]) # now with weights p=nx.shortest_path(self.cycle,0,weight='weight') assert_equal(p[3],[0,1,2,3]) assert_equal(p,nx.single_source_dijkstra_path(self.cycle,0)) p=nx.shortest_path(self.grid,1,weight='weight') validate_grid_path(4, 4, 1, 12, p[12])
def test_SPF(): edgeLen = 50 G = tNetworkX.createGridTopo(edgeLen=edgeLen) # print "G: "+str(G.edges(data=True)) s = time.clock() s1 = time.time() path = nx.single_source_dijkstra_path(G,'0000.0000.0000') e = time.clock() e1 = time.time() print "time.clock() consume time: "+str(e-s) print "time.time() consume time: "+str(e1-s1) pass
def allpairs(graph_file=None,wt_attr=None): """ Print the shortest path for all nodes, using the attribute named <b>wt_attr</b> as the weighting function. """ if graph_file is None and wt_attr is None: parser = argparse.ArgumentParser() parser.add_argument("-w", help="Attribute to use for shortest path weight", metavar="<weight attribute>") parser.add_argument("graph_file",help="Modelnet Graph File") args = parser.parse_args() graph_file = args.graph_file wt_attr = args.w gr = load_graph(graph_file) print '<?xml version="1.0" encoding="ISO-8859-1"?>' print '<allpairs>' numdone = 0 sys.stderr.write("Routing Node %s" % str(numdone)) for src in gr.nodes(): if gr.node[src]['vn'] == -1: continue if wt_attr: sp = nx.single_source_dijkstra_path(gr,src,wt_attr) else: sp = nx.single_source_shortest_path(gr,src) for dst in sp: if gr.node[dst]['vn'] == -1: continue if dst == src: continue path = sp[dst] hops = [gr[x][y]['int_idx'] for x,y in pairwise(path)] print ('<path int_vndst="%d" int_vnsrc="%d" hops="%s"/>' % (gr.node[dst]['vn'], gr.node[src]['vn'], " ".join(map(str,hops)))) sys.stderr.write('\b'*len(str(numdone))) sys.stderr.write("%d" % int(numdone+1)) numdone += 1 print '</allpairs>'
def test_dijkstra(self): (D, P) = nx.single_source_dijkstra(self.XG, "s") assert_equal(P["v"], ["s", "x", "u", "v"]) assert_equal(D["v"], 9) assert_equal(nx.single_source_dijkstra_path(self.XG, "s")["v"], ["s", "x", "u", "v"]) assert_equal(nx.single_source_dijkstra_path_length(self.XG, "s")["v"], 9) assert_equal(nx.single_source_dijkstra(self.XG, "s")[1]["v"], ["s", "x", "u", "v"]) assert_equal(nx.single_source_dijkstra_path(self.MXG, "s")["v"], ["s", "x", "u", "v"]) GG = self.XG.to_undirected() (D, P) = nx.single_source_dijkstra(GG, "s") assert_equal(P["v"], ["s", "x", "u", "v"]) assert_equal(D["v"], 8) # uses lower weight of 2 on u<->x edge assert_equal(nx.dijkstra_path(GG, "s", "v"), ["s", "x", "u", "v"]) assert_equal(nx.dijkstra_path_length(GG, "s", "v"), 8) assert_equal(nx.dijkstra_path(self.XG2, 1, 3), [1, 4, 5, 6, 3]) assert_equal(nx.dijkstra_path(self.XG3, 0, 3), [0, 1, 2, 3]) assert_equal(nx.dijkstra_path_length(self.XG3, 0, 3), 15) assert_equal(nx.dijkstra_path(self.XG4, 0, 2), [0, 1, 2]) assert_equal(nx.dijkstra_path_length(self.XG4, 0, 2), 4) assert_equal(nx.dijkstra_path(self.MXG4, 0, 2), [0, 1, 2]) assert_equal(nx.single_source_dijkstra(self.G, "s", "v")[1]["v"], ["s", "u", "v"]) assert_equal(nx.single_source_dijkstra(self.G, "s")[1]["v"], ["s", "u", "v"]) assert_equal(nx.dijkstra_path(self.G, "s", "v"), ["s", "u", "v"]) assert_equal(nx.dijkstra_path_length(self.G, "s", "v"), 2) # NetworkXError: node s not reachable from moon assert_raises(nx.NetworkXNoPath, nx.dijkstra_path, self.G, "s", "moon") assert_raises(nx.NetworkXNoPath, nx.dijkstra_path_length, self.G, "s", "moon") assert_equal(nx.dijkstra_path(self.cycle, 0, 3), [0, 1, 2, 3]) assert_equal(nx.dijkstra_path(self.cycle, 0, 4), [0, 6, 5, 4])
def test_bidirectional_dijkstra(self): assert_equal(nx.bidirectional_dijkstra(self.XG, "s", "v"), (9, ["s", "x", "u", "v"])) (dist, path) = nx.bidirectional_dijkstra(self.G, "s", "v") assert_equal(dist, 2) # skip this test, correct path could also be ['s','u','v'] # assert_equal(nx.bidirectional_dijkstra(self.G,'s','v'), # (2, ['s', 'x', 'v'])) assert_equal(nx.bidirectional_dijkstra(self.cycle, 0, 3), (3, [0, 1, 2, 3])) assert_equal(nx.bidirectional_dijkstra(self.cycle, 0, 4), (3, [0, 6, 5, 4])) assert_equal(nx.bidirectional_dijkstra(self.XG3, 0, 3), (15, [0, 1, 2, 3])) assert_equal(nx.bidirectional_dijkstra(self.XG4, 0, 2), (4, [0, 1, 2])) # need more tests here assert_equal(nx.dijkstra_path(self.XG, "s", "v"), nx.single_source_dijkstra_path(self.XG, "s")["v"])
def test_dijkstra_vs_networkx_single_source_all_lenghts_and_paths(): """ Test Dijkstra: Rion's implementation vs Networkx implementation > Single Source """ # NX Version G = nx.from_edgelist(edgelist_james) nx.set_edge_attributes(G, 'weight', edgelist_james) nx_lenghts = nx.single_source_dijkstra_path_length(G, source='s', weight='weight') nx_paths = nx.single_source_dijkstra_path(G, source='s', weight='weight') # My Version d = Dijkstra() d.from_edgelist(edgelist_james, directed=False) dc_lenghts, dc_paths = d.single_source_shortest_paths('s', kind='metric') assert (nx_lenghts == dc_lenghts) assert (nx_paths == dc_paths)
def clip(self, d): self.G = nx.single_source_dijkstra_path(self.N, self.source) g = nx.Graph() P = set() print 'geodesic clipping..' for path in pbar(self.G.values()): if len(path) < 2: continue curr_dist = 0 for p in range(1, len(path)): # from source (level 0) to i curr_dist += self.N[path[p-1]][path[p]]['weight'] if curr_dist <= d: g.add_edge(path[p-1], path[p]) P.add(harray(self.P[path[p-1]])) P.add(harray(self.P[path[p]])) else: break return vstack(P)
def test_bidirectional_dijkstra(self): assert_equal(nx.bidirectional_dijkstra(self.XG, 's', 'v'), (9, ['s', 'x', 'u', 'v'])) assert_equal(nx.bidirectional_dijkstra(self.G,'s','v'), (2, ['s', 'x', 'v'])) assert_equal(nx.bidirectional_dijkstra(self.cycle,0,3), (3, [0, 1, 2, 3])) assert_equal(nx.bidirectional_dijkstra(self.cycle,0,4), (3, [0, 6, 5, 4])) assert_equal(nx.bidirectional_dijkstra(self.XG3,0,3), (15, [0, 1, 2, 3])) assert_equal(nx.bidirectional_dijkstra(self.XG4,0,2), (4, [0, 1, 2])) # need more tests here assert_equal(nx.dijkstra_path(self.XG,'s','v'), nx.single_source_dijkstra_path(self.XG,'s')['v'])
def test_bidirectional_dijkstra(self): validate_length_path( self.XG, 's', 'v', 9, *nx.bidirectional_dijkstra(self.XG, 's', 'v')) validate_length_path( self.G, 's', 'v', 2, *nx.bidirectional_dijkstra(self.G, 's', 'v')) validate_length_path( self.cycle, 0, 3, 3, *nx.bidirectional_dijkstra(self.cycle, 0, 3)) validate_length_path( self.cycle, 0, 4, 3, *nx.bidirectional_dijkstra(self.cycle, 0, 4)) validate_length_path( self.XG3, 0, 3, 15, *nx.bidirectional_dijkstra(self.XG3, 0, 3)) validate_length_path( self.XG4, 0, 2, 4, *nx.bidirectional_dijkstra(self.XG4, 0, 2)) # need more tests here P = nx.single_source_dijkstra_path(self.XG, 's')['v'] validate_path(self.XG, 's', 'v', sum(self.XG[u][v]['weight'] for u, v in zip( P[:-1], P[1:])), nx.dijkstra_path(self.XG, 's', 'v'))
def maketree(self, prune=False): try: self.tree = tree = networkx.minimum_spanning_tree(self.graph) path = networkx.single_source_dijkstra_path(tree, self.uid) except KeyError: self.route = self.via = {} return self.route = route = dict((node, path[1]) for node, path in path.iteritems() if len(path) > 1) self.via = via = {} for k, v in route.iteritems(): try: via[v].append(k) except KeyError: via[v] = [k] if prune: gr = self.graph prune = set(gr.nodes()) - set(path) prune.discard(self.uid) gr.remove_nodes_from(prune)
def assign(self): #does 4 iterations in which it assigns od, updates t_a. find paths to minimize travel time and we record route for each od pair pdb.set_trace() for i in range(4): #do 4 iterations for origin in self.demand.keys(): #find the shortest paths from this origin to each destination print origin paths_dict = nx.single_source_dijkstra_path(self.G, origin, cutoff = None, weight = 't_a') #Compute shortest path between source and all other reachable nodes for a weighted graph. Returns dict keyed by by target with the value being a list of node ids of the shortest path for destination in self.demand[origin].keys(): print destination od_flow = iteration_vals[i] * self.demand[origin][destination]*0.053 #to get morning flows, take 5.3% of daily driver values. 11.5/(4.5*6+11.5*10+14*4+4.5*4) from Figure S10 of http://www.nature.com/srep/2012/121220/srep01001/extref/srep01001-s1.pdf #get path path_list = paths_dict[destination] #list of nodes #increment flow on the paths and update t_a for index in range(0, len(path_list) - 1): u = path_list[index] v = path_list[index + 1] num_multi_edges = len( self.G[u][v]) if num_multi_edges >1: #multi-edge print 'multiedge: ', num_multi_edges #identify multi edge with lowest t_a best = 0 best_t_a = float('inf') for multi_edge in self.G[u][v].keys(): new_t_a = self.G[u][v][multi_edge]['t_a'] if (new_t_a < best_t_a) and (self.G[u][v][multi_edge]['capacity']>0): best = multi_edge best_t_a = new_t_a else: best = 0 if (self.G[u][v][best]['capacity']>0): print 'adding flow' self.G[u][v][best]['flow'] += od_flow t = util.TravelTime(self.G[u][v][best]['t_0'], self.G[u][v][best]['capacity']) travel_time= t.get_new_travel_time(od_flow) #TODO #min(t.get_new_travel_time(od_flow), self.G[u][v][best]['distance_0']*1.0/3600.0) #distance in miles, t_a in seconds!! So we are saying that the minimum of the t_a and distance (in miles) * (1 hr/ 1 mile) * (1hr / 3600s) if travel_time > self.G[u][v][best]['distance_0']*3600: print travel_time print 'and 1mph: ', self.G[u][v][best]['distance_0']*3600 self.G[u][v][best]['t_a'] = travel_time return self.G
def connect_shortest_v2(weigthed_graph,memb,tf,weighted=True,cutoff=None): #Trying with manual shortest_paths between pairs of memb and tf res=model.AnnotatedGraph() for m in memb: if m not in weigthed_graph: continue if weighted: # spaths=nx.algorithms.shortest_paths.single_source_dijkstra_path(weigthed_graph, m, weight='weight') spaths=nx.single_source_dijkstra_path(weigthed_graph, m, weight='weight',cutoff=cutoff) else: spaths=nx.single_source_shortest_path(weigthed_graph, m,cutoff=cutoff) for t in tf: if t not in spaths: continue if cutoff and (len(spaths[t])>cutoff): continue res.add_path(spaths[t]) copy_attributes_from_g(res,weigthed_graph) return res
def test_single_source_shortest_path(self): p = nx.shortest_path(self.cycle, 0) assert_equal(p[3], [0, 1, 2, 3]) assert_equal(p, nx.single_source_shortest_path(self.cycle, 0)) p = nx.shortest_path(self.grid, 1) validate_grid_path(4, 4, 1, 12, p[12]) # now with weights p = nx.shortest_path(self.cycle, 0, weight='weight') assert_equal(p[3], [0, 1, 2, 3]) assert_equal(p, nx.single_source_dijkstra_path(self.cycle, 0)) p = nx.shortest_path(self.grid, 1, weight='weight') validate_grid_path(4, 4, 1, 12, p[12]) # weights and method specified p = nx.shortest_path(self.cycle, 0, method='dijkstra', weight='weight') assert_equal(p[3], [0, 1, 2, 3]) assert_equal(p, nx.single_source_shortest_path(self.cycle, 0)) p = nx.shortest_path(self.cycle, 0, method='bellman-ford', weight='weight') assert_equal(p[3], [0, 1, 2, 3]) assert_equal(p, nx.single_source_shortest_path(self.cycle, 0))
def create_directed_graphs(self): self.directed_graphs = np.empty((self.no_of_atoms,self.no_of_atoms-1,2),dtype=int) #parse all the atoms one by one and get directed graph to that atom for idx in xrange(self.no_of_atoms): #get shortest path from the root to all the other atoms and then reverse the edges. path = nx.single_source_dijkstra_path(self.graph,idx) G = nx.DiGraph() for i in xrange(self.no_of_atoms): temp = path[i] temp.reverse() G.add_path(temp) # do a topological sort to get a order of atoms with all edges pointing to the root topological_order = nx.topological_sort(G) sorted_path = np.empty((self.no_of_atoms-1,2)) for i in xrange(self.no_of_atoms-1): node = topological_order[i] edge = (nx.edges(G,node))[0] sorted_path[i,:] = [edge[0],edge[1]] # sorted_path[self.no_of_atoms-1, :] = [idx,self.no_of_atoms] self.directed_graphs[idx,:,:] = sorted_path
def test_bidirectional_dijkstra(self): validate_length_path( self.XG, 's', 'v', 9, *nx.bidirectional_dijkstra(self.XG, 's', 'v')) validate_length_path( self.G, 's', 'v', 2, *nx.bidirectional_dijkstra(self.G, 's', 'v')) validate_length_path( self.cycle, 0, 3, 3, *nx.bidirectional_dijkstra(self.cycle, 0, 3)) validate_length_path( self.cycle, 0, 4, 3, *nx.bidirectional_dijkstra(self.cycle, 0, 4)) validate_length_path( self.XG3, 0, 3, 15, *nx.bidirectional_dijkstra(self.XG3, 0, 3)) validate_length_path( self.XG4, 0, 2, 4, *nx.bidirectional_dijkstra(self.XG4, 0, 2)) # need more tests here P = nx.single_source_dijkstra_path(self.XG, 's')['v'] validate_path(self.XG, 's', 'v', sum(self.XG[u][v]['weight'] for u, v in zip( P[:-1], P[1:])), nx.dijkstra_path(self.XG, 's', 'v')) # check absent source G = nx.path_graph(2) assert_raises(nx.NodeNotFound, nx.bidirectional_dijkstra, G, 3, 0)
def __init__(self): self.graph0 = nx.Graph([(0, 1), (1, 2), (1, 3), (3, 4), (3, 5), (4, 5), (5, 6), (6, 7), (6, 8), (7, 9), (8, 9), (8, 10), (9, 10)]) nx.draw_circular(self.graph0) plt.show() self.cycles0 = nx.cycle_basis(self.graph0) display(self.cycles0) self.graph1 = nx.DiGraph([(0, 1), (1, 2), (2, 0), (0, 3), (3, 4), (3, 5), (4, 5)]) nx.draw_circular(self.graph1) plt.show() self.cycles1 = nx.simple_cycles(self.graph1) display(list(self.cycles1)) self.graph2 = nx.Graph() self.graph2.add_edge(0, 1, weight=2) self.graph2.add_edge(0, 2, weight=3) self.graph2.add_edge(0, 3, weight=1) self.graph2.add_edge(1, 3, weight=4) self.graph2.add_edge(2, 3, weight=1) self.graph2.add_edge(2, 4, weight=1) self.graph2.add_edge(3, 4, weight=3) pos = nx.circular_layout(self.graph2) labels = nx.get_edge_attributes(self.graph2, 'weight') nx.draw(self.graph2, pos=pos) nx.draw_networkx_edge_labels(self.graph2, pos=pos, edge_labels=labels) plt.show() print('Minimum Spanning Tree (MST): ') nx.draw(self.graph2, pos=pos, style='dashed') mst = nx.minimum_spanning_tree(self.graph2) nx.draw(mst, pos=pos, width=3) nx.draw_networkx_edge_labels(self.graph2, pos=pos, edge_labels=labels) plt.show() print('Single Source Dijkstra Path: ') ssdp = nx.single_source_dijkstra_path(self.graph2, 0) display(ssdp) print('Single Source Dijkstra Path Length: ') ssdpl = nx.single_source_dijkstra_path_length(self.graph2, 0) display(ssdpl)
def _subgraph(self, root): import networkx as nx targets = self._closest(root)[0:self.max_size] # Get a directed tree g = nx.DiGraph() #t = nx.dfs_tree(self._g) # Add each path to target #print type(g) #print type(root) paths = nx.single_source_dijkstra_path(self._g,root) for target in targets: path = paths[target] edges_in_path = set() for i,n1 in enumerate(path[:-1]): n2=path[i+1] weight = self._g.get_edge_data(n1,n2)['weight'] if weight==0: weight=1 edges_in_path.add((n1,n2,weight)) # Add the new path and all edge weights for node in path: g.add_weighted_edges_from(edges_in_path) return g
def worker(taskq, resultq, G, pois): for srcid in iter(taskq.get, 'STOP'): # dists = nx.single_source_dijkstra_path_length(G, srcid) # d = {} # for dstid in dists: # if dstid in pois: d[dstid] = dists[dstid] path = nx.single_source_dijkstra_path(G, srcid) d = {} for dstid in path: if dstid not in pois: continue l, j = [], [] p = path[dstid] if len(p) <= 1: l.append(5.0) j.append(0.0) else: for i in xrange(len(p)-1): e = G.edge[p[i]][p[i+1]] l.append(float(e['latency'])) j.append(float(e['jitter'])) d[dstid] = {'latency':float(sum(l)), 'jitter':float(sum(j)/float(len(j)))} resultq.put((srcid, d))
def shortest_path(G, source=None, target=None, weight=None): """Compute shortest paths in the graph. Parameters ---------- G : NetworkX graph source : node, optional Starting node for path. If not specified, compute shortest paths using all nodes as source nodes. target : node, optional Ending node for path. If not specified, compute shortest paths using all nodes as target nodes. weight : None or string, optional (default = None) If None, every edge has weight/distance/cost 1. If a string, use this edge attribute as the edge weight. Any edge attribute not present defaults to 1. Returns ------- path: list or dictionary All returned paths include both the source and target in the path. If the source and target are both specified, return a single list of nodes in a shortest path from the source to the target. If only the source is specified, return a dictionary keyed by targets with a list of nodes in a shortest path from the source to one of the targets. If only the target is specified, return a dictionary keyed by sources with a list of nodes in a shortest path from one of the sources to the target. If neither the source nor target are specified return a dictionary of dictionaries with path[source][target]=[list of nodes in path]. Examples -------- >>> G=nx.path_graph(5) >>> print(nx.shortest_path(G,source=0,target=4)) [0, 1, 2, 3, 4] >>> p=nx.shortest_path(G,source=0) # target not specified >>> p[4] [0, 1, 2, 3, 4] >>> p=nx.shortest_path(G,target=4) # source not specified >>> p[0] [0, 1, 2, 3, 4] >>> p=nx.shortest_path(G) # source,target not specified >>> p[0][4] [0, 1, 2, 3, 4] Notes ----- There may be more than one shortest path between a source and target. This returns only one of them. For digraphs this returns a shortest directed path. To find paths in the reverse direction first use G.reverse(copy=False) to flip the edge orientation. See Also -------- all_pairs_shortest_path() all_pairs_dijkstra_path() single_source_shortest_path() single_source_dijkstra_path() """ if source is None: if target is None: ## Find paths between all pairs. if weight is None: paths=nx.all_pairs_shortest_path(G) else: paths=nx.all_pairs_dijkstra_path(G,weight=weight) else: ## Find paths from all nodes co-accessible to the target. directed = G.is_directed() if directed: G.reverse(copy=False) if weight is None: paths=nx.single_source_shortest_path(G,target) else: paths=nx.single_source_dijkstra_path(G,target,weight=weight) # Now flip the paths so they go from a source to the target. for target in paths: paths[target] = list(reversed(paths[target])) if directed: G.reverse(copy=False) else: if target is None: ## Find paths to all nodes accessible from the source. if weight is None: paths=nx.single_source_shortest_path(G,source) else: paths=nx.single_source_dijkstra_path(G,source,weight=weight) else: ## Find shortest source-target path. if weight is None: paths=nx.bidirectional_shortest_path(G,source,target) else: paths=nx.dijkstra_path(G,source,target,weight) return paths
def sin_cyclostationary_traffic_matrix(topology, mean, stddev, gamma, log_psi, delta=0.2, n=24, periods=1, max_u=0.9, origin_nodes=None, destination_nodes=None): """ Return a cyclostationary sequence of traffic matrices, where traffic volumes evolve over time as sin waves. The sequence is generated by first generating a single matrix assigning traffic volumes drawn from a lognormal distribution and assigned to specific origin-destination pairs using the Ranking Metrics Heuristic method proposed by Nucci et al. [3]_. Then, all matrices of the sequence are generated by adding zero-mean normal fluctuation in the traffic volumes. Finally, traffic volumes are multiplied by a sin function with unitary mean to model periodic fluctuations. This process was originally proposed by [3]_. Cyclostationary sequences of traffic matrices are generally suitable for modeling real network traffic over long periods, up to several days. In fact, real traffic exhibits diurnal patterns well modelled by cyclostationary sequences. Parameters ---------- topology : topology The topology for which the traffic matrix is calculated. This topology can either be directed or undirected. If it is undirected, this function assumes that all links are full-duplex. mean : float The mean volume of traffic among all origin-destination pairs stddev : float The standard deviation of volumes among all origin-destination pairs. gamma : float Parameter expressing relation between mean and standard deviation of traffic volumes of a specific flow over the time log_psi : float Parameter expressing relation between mean and standard deviation of traffic volumes of a specific flow over the time delta : float [0, 1] A parameter indicating the intensity of variation of traffic volumes over a period. Specifically, let x be the mean volume over a specific OD pair, the minimum and maximum traffic volumes for that OD pair (excluding random fluctuations) are respectively :math:`x*(1 - delta)` and :math:`x*(1 + delta)` n : int Number of traffic matrices per period. For example, if it is desired to model traffic varying cyclically over a 24 hour period, and n is set to 24, therefore, the time interval between subsequent traffic matrices is is 1 hour. periods : int Number of periods. In total the sequence is composed of :math:`n * periods` traffic matrices. max_u : float, optional Represent the max link utilization. If specified, traffic volumes are scaled so that the most utilized link of the network has an utilization equal to max_u. If None, volumes are not scaled, but in this case links may end up with an utilization factor greater than 1.0 origin_nodes : list, optional A list of all nodes which can be traffic sources. If not specified all nodes of the topology are traffic sources destination_nodes : list, optional A list of all nodes which can be traffic destinations. If not specified all nodes of the topology are traffic destinations Returns ------- tms : TrafficMatrixSequence References ---------- .. [3] Nucci et al., The problem of synthetically generating IP traffic matrices: initial recommendations, ACM SIGCOMM Computer Communication Review, 35(3), 2005 """ tm_sequence = TrafficMatrixSequence() static_tm = static_traffic_matrix(topology, mean, stddev, max_u=None, origin_nodes=origin_nodes, destination_nodes=destination_nodes) volume_unit = static_tm.attrib['volume_unit'] mean_dict = static_tm.flows() psi = exp(log_psi) if psi == 0.0: raise ValueError("The value of log_psi provided is too small and " "causes psi=0.0, which makes the standard deviation " "of random fluctuation to become infinite. Try with " "a greater value of log_psi") std_dict = dict(((o, d), (m/psi)**(1.0/gamma)) for (o, d), m in mean_dict.items()) print(std_dict.values()) if any(isinf(std) for std in std_dict.values()): raise ValueError("The value of log_psi or gamma provided are too " "small and causes the standard deviation of random " "fluctuations to become infinite. Try with a greater " "value of log_psi and/or gamma") od_pairs = static_tm.od_pairs() for _ in range(periods): for i in range(n): tm = TrafficMatrix(volume_unit=volume_unit) for o, d in od_pairs: volume = static_tm[(o, d)] * (1 + delta * sin((2*pi*i)/n)) # Implementation without Numpy # volume = max([0, normalvariate(volume, std_dict[(o, d)])]) volume = max((0, normal(volume, std_dict[(o, d)]))) tm.add_flow(o, d, volume) tm_sequence.append(tm) if max_u is not None: if origin_nodes is not None: shortest_path = dict( (node, nx.single_source_dijkstra_path(topology, node, weight='weight')) for node in origin_nodes) else: shortest_path = nx.all_pairs_dijkstra_path(topology, weight='weight') current_max_u = max((max(link_loads(topology, tm_sequence.get(i), shortest_path ).values()) for i in range(n*periods))) norm_factor = max_u/current_max_u for i in range(n*periods): for o, d in mean_dict: tm_sequence.matrix[i].flow[o][d] *= norm_factor return tm_sequence
def stationary_traffic_matrix(topology, mean, stddev, gamma, log_psi, n, max_u=0.9, origin_nodes=None, destination_nodes=None): """ Return a stationary sequence of traffic matrices. The sequence is generated by first generating a single matrix assigning traffic volumes drawn from a lognormal distribution and assigned to specific origin-destination pairs using the Ranking Metrics Heuristic method proposed by Nucci et al. [2]_. Then, all matrices of the sequence are generated by adding zero-mean normal fluctuation in the traffic volumes. This process was originally proposed by [2]_ Stationary sequences of traffic matrices are generally suitable for modeling network traffic over short periods (up to 1.5 hours). Over longer periods, real traffic exhibits diurnal patterns and they are better modelled by cyclostationary sequences Parameters ---------- topology : topology The topology for which the traffic matrix is calculated. This topology can either be directed or undirected. If it is undirected, this function assumes that all links are full-duplex. mean : float The mean volume of traffic among all origin-destination pairs stddev : float The standard deviation of volumes among all origin-destination pairs. gamma : float Parameter expressing relation between mean and standard deviation of traffic volumes of a specific flow over the time log_psi : float Parameter expressing relation between mean and standard deviation of traffic volumes of a specific flow over the time n : int Number of matrices in the sequence max_u : float, optional Represent the max link utilization. If specified, traffic volumes are scaled so that the most utilized link of the network has an utilization equal to max_u. If None, volumes are not scaled, but in this case links may end up with an utilization factor greater than 1.0 origin_nodes : list, optional A list of all nodes which can be traffic sources. If not specified all nodes of the topology are traffic sources destination_nodes : list, optional A list of all nodes which can be traffic destinations. If not specified all nodes of the topology are traffic destinations Returns ------- tms : TrafficMatrixSequence References ---------- .. [2] Nucci et al., The problem of synthetically generating IP traffic matrices: initial recommendations, ACM SIGCOMM Computer Communication Review, 35(3), 2005 """ tm_sequence = TrafficMatrixSequence() static_tm = static_traffic_matrix(topology, mean, stddev, max_u=None, origin_nodes=origin_nodes, destination_nodes=destination_nodes) volume_unit = static_tm.attrib['volume_unit'] mean_dict = static_tm.flows() psi = exp(log_psi) if psi == 0.0: raise ValueError("The value of log_psi provided is too small and " "causes psi=0.0, which makes the standard deviation " "of random fluctuation to become infinite. Try with " "a greater value of log_psi") std_dict = dict(((o, d), (m/psi)**(1.0/gamma)) for (o, d), m in mean_dict.items()) if any(isinf(std) for std in std_dict.values()): raise ValueError("The value of log_psi or gamma provided are too " "small and causes the standard deviation of random " "fluctuations to become infinite. Try with a greater " "value of log_psi and/or gamma") flows = {} for o, d in mean_dict: # Implementation without Numpy: # flows[(o, d)] = [max([0, normalvariate(mean_dict[(o, d)], # std_dict[(o, d)])]) for _ in range(n)] flows[(o, d)] = [max((0, normal(mean_dict[(o, d)], std_dict[(o, d)])))\ for _ in range(n)] for i in range(n): traffic_marix = TrafficMatrix(volume_unit=volume_unit) for o, d in mean_dict: traffic_marix.add_flow(o, d, flows[(o, d)][i]) tm_sequence.append(traffic_marix) if max_u is not None: if origin_nodes is not None: shortest_path = dict( (node, nx.single_source_dijkstra_path(topology, node, weight='weight')) for node in origin_nodes) else: shortest_path = nx.all_pairs_dijkstra_path(topology, weight='weight') current_max_u = max((max(link_loads(topology, tm_sequence.get(i), shortest_path ).values()) for i in range(n))) norm_factor = max_u/current_max_u for i in range(n): for o, d in mean_dict: tm_sequence.matrix[i].flow[o][d] *= norm_factor return tm_sequence
def static_traffic_matrix(topology, mean, stddev, max_u=0.9, origin_nodes=None, destination_nodes=None): """ Return a TrafficMatrix object, i.e. a single traffic matrix, representing the traffic volume exchanged over a network at a specific point in time This matrix is generated by assigning traffic volumes drawn from a lognormal distribution and assigned to specific origin-destination pairs using the Ranking Metrics Heuristic method proposed by Nucci et al. [1]_ Parameters ---------- topology : topology The topology for which the traffic matrix is calculated. This topology can either be directed or undirected. If it is undirected, this function assumes that all links are full-duplex. mean : float The mean volume of traffic among all origin-destination pairs stddev : float The standard deviation of volumes among all origin-destination pairs. max_u : float, optional Represent the max link utilization. If specified, traffic volumes are scaled so that the most utilized link of the network has an utilization equal to max_u. If None, volumes are not scaled, but in this case links may end up with an utilization factor greater than 1.0 origin_nodes : list, optional A list of all nodes which can be traffic sources. If not specified, all nodes of the topology are traffic sources destination_nodes : list, optional A list of all nodes which can be traffic destinations. If not specified, all nodes of the topology are traffic destinations Returns ------- tm : TrafficMatrix References ---------- .. [1] Nucci et al., The problem of synthetically generating IP traffic matrices: initial recommendations, ACM SIGCOMM Computer Communication Review, 35(3), 2005 """ try: mean = float(mean) stddev = float(stddev) except ValueError: raise ValueError('mean and stddev must be of type float') if mean < 0 or stddev < 0: raise ValueError('mean and stddev must be not negative') topology = topology.copy() if topology.is_directed() \ else topology.to_directed() volume_unit = topology.graph['capacity_unit'] mu = log(mean**2/sqrt(stddev**2 + mean**2)) sigma = sqrt(log((stddev**2/mean**2) + 1)) if origin_nodes is None and destination_nodes is None: od_pairs = od_pairs_from_topology(topology) else: all_nodes = topology.nodes() origins = origin_nodes if origin_nodes is not None \ else all_nodes destinations = destination_nodes if destination_nodes is not None \ else all_nodes od_pairs = [(o, d) for o in origins for d in destinations if o != d] nr_pairs = len(od_pairs) volumes = sorted(lognormal(mu, sigma, size=nr_pairs)) #volumes = sorted([lognormvariate(mu, sigma) for _ in range(nr_pairs)]) if any(isinf(vol) for vol in volumes): raise ValueError('Some volumes are too large to be handled by a '\ 'float type. Set a lower value of mu and try again.') sorted_od_pairs = __ranking_metrics_heuristic(topology, od_pairs) # check if the matrix matches and scale if needed assignments = dict(zip(sorted_od_pairs, volumes)) if max_u is not None: if origin_nodes is not None: shortest_path = dict( (node, nx.single_source_dijkstra_path(topology, node, weight='weight')) for node in origin_nodes) # remove OD pairs not connected for o in shortest_path: for d in destinations: if o != d and d not in shortest_path[o]: od_pairs.remove((o, d)) else: shortest_path = nx.all_pairs_dijkstra_path(topology, weight='weight') for u, v in topology.edges_iter(): topology.edge[u][v]['load'] = 0.0 # Find max u for o, d in od_pairs: path = shortest_path[o][d] if len(path) > 1: for hop in range(len(path) - 1): topology.edge[path[hop]][path[hop + 1]]['load'] \ += assignments[(o, d)] # Calculate scaling current_max_u = max((float(topology.edge[u][v]['load']) \ /float(topology.edge[u][v]['capacity']) for u, v in topology.edges_iter())) norm_factor = max_u/current_max_u for od_pair in assignments: assignments[od_pair] *= norm_factor # write to traffic matrix traffic_matrix = TrafficMatrix(volume_unit=volume_unit) for (o, d), flow in assignments.items(): traffic_matrix.add_flow(o, d, flow) return traffic_matrix