def calculated_two_approx(graph): terminal = np.where(graph.ndata['t'].view(1, -1)[0] == 1)[0] graph = graph.to_networkx(edge_attrs=['weight']).to_undirected() T = steiner_tree(graph, terminal) T_weight = 0 for _, _, edge_weight in T.edges.data('weight', default=1): T_weight += int(edge_weight) return T_weight
def steiner_find(list_of_locations, list_of_homes, starting_car_location, adjacency_matrix): G = nx.Graph(incoming_graph_data=adjacency_matrix, cutoff=1000) predecessors, distances = nx.floyd_warshall_predecessor_and_distance(G) homeIndices = [] for h in list_of_homes: homeIndices.append(list_of_locations.index(h)) # homeIndices.append(list_of_locations.index(starting_car_location)) tree = apxa.steiner_tree( G, homeIndices + [list_of_locations.index(starting_car_location)]) # print(list(tree.nodes)) # print(homeIndices) # print(starting_car_location) # treematrix = nx.adjacency_matrix(tree) # return greedyAllPairs(list(tree.nodes), homeIndices, int(starting_car_location), treematrix) nodelist = list(tree.nodes()) currIndex = list_of_locations.index(starting_car_location) visitedNodes = [] path = [list_of_locations.index(starting_car_location)] dropoff_mapping = {} added = False # print(currIndex) nodelist = nodelist[ nodelist.index(currIndex):] + nodelist[:nodelist.index(currIndex)] # print(nodelist) dropped = False allHomes = set(homeIndices) homeSet = set(homeIndices) length = len(nodelist) - 1 i = 0 while i in range(0, length): dropped = False homeNeighbors = list( homeSet.intersection(list(tree.neighbors(nodelist[i])))) if len(homeNeighbors) >= 3: dropped = True homeNeighbors = [j for j in homeNeighbors if j in homeSet] if (nodelist[i] in homeSet): homeNeighbors = homeNeighbors + [nodelist[i]] dropoff_mapping[nodelist[i]] = homeNeighbors homeSet = set([j for j in homeSet if j not in homeNeighbors]) n = 1 while i + n in range(0, length) and nodelist[ i + n] in allHomes and nodelist[i + n] not in homeSet: n = n + 1 path_to_next = nx.reconstruct_path(nodelist[i], nodelist[i + n], predecessors) path.extend(path_to_next[1:]) if nodelist[i] in homeSet and not dropped: dropoff_mapping[nodelist[i]] = [nodelist[i]] homeSet.remove(nodelist[i]) i = i + n if nodelist[-1] in homeSet: dropoff_mapping[nodelist[-1]] = [nodelist[-1]] path_to_start = nx.reconstruct_path(nodelist[-1], nodelist[0], predecessors) path.extend(path_to_start[1:]) return path, dropoff_mapping
def steiner_path(self): #returns a list of remotes s = approximation.steiner_tree(self.g, self.bot_locations + [self.home]) unop = self.build_remote(s) rem = [] for i in range(len(unop)): if unop[i] not in unop[i + 1:]: rem.append(unop[i]) return rem
def _gen_ER(self, n): """ Input: g: networkx graph, node_list: steiner tree terminal, edge_list: steiner tree solution ----------------------------------------------------------------------------------- Output: tuple of items (a,b) -> (DGLGraph(), torch.tensor(m,1)) a. graph that follow DGL graph with node features(is_terminal) and edge features(weight) b. the label list indicate whether the edge in solution (as torch tensor that label 1 to in_solution edge, 0 otherwise) """ def __convert_g(g, node_list, edge_list): dgl_g = dgl.DGLGraph() # dgl_g.from_networkx(g, edge_attrs=['weight']) dgl_g.from_networkx(g) dgl_g.nodes[node_list].data['p'] = th.ones((len(node_list), 1)) if self.node_task: label = th.zeros([g.number_of_nodes(), 1]) for u, v in edge_list: label[u] = 1 label[v] = 1 # give lable as input feature, this should give 100% accuracy # g.ndata['h'] = label return dgl_g, label for v in self.v_list: for nums in range(math.ceil(n / len(self.v_list))): while True: g = nx.fast_gnp_random_graph(v, (4) * math.log(v) / v) if nx.is_connected(g): break copy_g = g.copy() node_list = list(g.nodes) weight = {} for e in g.edges(): # random set edge weight from 1 to 10 weight[e] = {'weight': np.random.randint(low=1, high=10)} nx.set_edge_attributes(copy_g, weight) # print(g.edges.data()) # assign terminal randomly to nodes np.random.shuffle(node_list) terminal_node = node_list[:int(0.1 * len(node_list))] # calculate steiner tree ST_f = steiner_tree(copy_g, terminal_node) if self.save_file: write_file( "./ER_4logn_task/4logn_" + str(v) + '_' + str(nums), copy_g, terminal_node) self.graphs.append( __convert_g(copy_g, terminal_node, ST_f.edges()))
def shared_path(self): #returns a list of remotes s = approximation.steiner_tree(self.g, self.bot_locations + [self.home]) unop = [] for vert in self.bot_locations: sh = nx.shortest_path(s, vert, self.home, weight="weight") for i in range(len(sh) - 1): unop.append([sh[i], sh[i + 1]]) rem = [] for i in range(len(unop)): if unop[i] not in unop[i + 1:]: rem.append(unop[i]) return rem
def steiner_tree_approx(self, locationMap): G = nx.DiGraph() terminalNodes = ["E"] for loc in locationMap: for rl in locationMap[loc]['expansions'].values(): #eW = (2 - FEATURE_WEIGHTS.get(rl.featureCode, 0.00)) eW = FEATURE_WEIGHTS.get(rl.featureCode, 12) nodename = unidecode(loc + u"T0") if rl.ltype == 'country': edges = [(nodename, rl.geonameid, eW), (rl.geonameid, rl.country, eW), (rl.country, 'E', eW)] elif rl.ltype == 'admin1': edges = [(nodename, rl.geonameid, eW), (rl.geonameid, rl.admin1, eW), (rl.admin1, rl.country, eW), (rl.country, 'E', eW)] else: #edges = [(loc + "T0", rl.geonameid, eW), (rl.geonameid, rl.name, eW), (rl.name, rl.admin1, eW), (rl.admin1, rl.country, eW), (rl.country, 'E', eW)] edges = [(nodename, rl.geonameid, eW), (rl.geonameid, rl.admin1, eW), (rl.admin1, rl.country, eW), (rl.country, 'E', eW)] G.add_weighted_edges_from(edges) terminalNodes.append(nodename) if G.number_of_nodes() == 0: return G, [] stG = approximation.steiner_tree(G.to_undirected(), terminalNodes) def ego_nw_degree(degree, node): return sum((degree(p) for p in nx.descendants(G, node))) G = G.subgraph(stG) degree = G.degree() geofocus = sorted([(t, ego_nw_degree(degree, t)) for t in terminalNodes[1:]], reverse=True) return G, geofocus[0] if geofocus else []
def best_method_sofar(c): pos_bots = find_robot_position(c) pos_bots = list(set(pos_bots + [c.home])) if len(pos_bots) == 1: return st_tree_sb = approximation.steiner_tree(c.G, pos_bots) t_mst = nx.Graph.copy(st_tree_sb) non_home = list(range(1, c.home)) + list(range(c.home + 1, c.v + 1)) non_home = [n for n in non_home if n in list(nx.nodes(t_mst))] nodes_depth = nx.shortest_path_length(t_mst, c.home) while t_mst.number_of_nodes() > 1: max_depth = max(nodes_depth.values()) for x in non_home: # remote if the depth of x is max and it has bots if nodes_depth[x] == max_depth: # remote only when there's a bot in the vertex if x in pos_bots: # remote every MST edge neigh_iter = nx.neighbors(t_mst, x) neghi = neigh_iter.__next__() tmp_bots = c.remote(x, neghi) pos_bots.remove(x) pos_bots.append(neghi) # delete the n ode from MST t_mst.remove_node(x) nodes_depth.pop(x) non_home.remove(x)
def get_redundant_multicast_trees(self, source, destinations, k=2, algorithm='steiner', weight_metric='weight', heur_args=None): """Builds k redundant multicast trees: trees should not share any edges unless necessary. Supports various algorithms, several of which may not work for k>2.""" # Need to sanitize the input to ensure that we know about all of the given # destinations or else we'll cause an exception. old_dests = destinations destinations = [] for d in old_dests: if d not in self.topo: log.warning( "Skipping unknown destination %s in requested multicast tree" % d) else: destinations.append(d) if algorithm == 'steiner': """Default algorithm implemented by networkx that uses sum of shortest paths 2*D approximation. Currently not available in latest release of networkx, so see README if this import doesn't work.""" try: from networkx.algorithms.approximation import steiner_tree except ImportError: raise NotImplementedError( "Steiner Tree algorithm not found! See README") # we don't care about directionality of the mcast tree here, # so we can treat the source as yet another destination destinations = destinations + [source] # Skip over graph modifications if we only want one tree if k == 1: return [steiner_tree(self.topo, destinations)] # Naive heuristic: generate a multicast tree, increase the # weights on the edges to discourage them, generate another... # So we need to add a temporary attribute to the edges for # the heuristic to use or else we'd overwrite the weights. # TODO: generalize this residual graph approach? for u, v in self.topo.edges(): self.topo[u][v]['_temp_mcast_weight'] = self.topo[u][v].get( weight_metric, 1.0) # Disjoint trees heuristic: we have the choice of two penalties that we # add to an edge's weight to prevent it from being chosen next round: # 1) args[0] == 'max' --> the max weight of all edges # 2) args[0] == 'double' --> double the weight of the edge penalty_heuristic = 'max' if heur_args is not None and len(heur_args) >= 1: if heur_args[0] not in ('max', 'double'): log.warn( "Unknown steiner tree edge penalty heuristic (args[0]): %s. Using max instead" % heur_args[0]) else: penalty_heuristic = heur_args[0] max_weight = max((e[2]['_temp_mcast_weight'] for e in self.topo.edges(data=True))) trees = [] for i in range(k): new_tree = steiner_tree(self.topo, destinations, weight='_temp_mcast_weight') for u, v in new_tree.edges(): if penalty_heuristic == 'max': self.topo[u][v]['_temp_mcast_weight'] += max_weight else: # must be double self.topo[u][v]['_temp_mcast_weight'] *= 2 trees.append(new_tree) for u, v in self.topo.edges(): del self.topo[u][v]['_temp_mcast_weight'] results = trees elif algorithm == 'diverse-paths': """This algorithm builds multiple trees by getting multiple paths to each terminal (destination) and selectively adding these paths together to create each tree. The heuristic chooses destinations in increasing order of shortest path from source. It adds a given path to the tree with the most components in common so as to create somewhat minimally-sized multicast trees.""" destinations = set(destinations) shortest_paths = nx.shortest_path_length(self.topo, source, weight=weight_metric) shortest_paths = ((l, d) for d, l in shortest_paths if d in destinations) sorted_destinations = sorted(shortest_paths) # Track trees as sets of edges to make checking overlap faster # NOTE: if the path overlaps with the tree in terms of a node # but not an edge incident with that node, we have a cycle! trees = [set() for i in range(k)] for _, d in sorted_destinations: paths = self.get_redundant_paths(source, d, k) # ensure each tree receives a path trees_left = set(range(k)) for i, p in enumerate(paths): # Add this path to the tree with most components in common edges = self.get_edges_for_path(p) overlaps = ((len(trees[j].intersection(edges)), j) for j in trees_left) best_tree = max(overlaps)[1] trees_left.remove(best_tree) trees[best_tree].update(edges) # Subgraph the topology with the trees' edges to maintain attributes results = [self.topo.edge_subgraph(t) for t in trees] # Sanity check that we're generating actual trees for i, t in enumerate(results): # If it isn't a tree for some reason, trim it down until it is # by first getting a spanning tree of it and then trimming off # any leaf nodes that aren't terminals (destinations). if not nx.is_tree(t): log.info("non-tree mcast tree generated!") new_t = t.edge_subgraph( nx.minimum_spanning_edges(t, data=False, weight=weight_metric)) non_terminal_leaves = [n for n in new_t.nodes() if\ (new_t.degree(n) == 1 and n not in destinations and n != source)] while len(non_terminal_leaves) > 0: log.info("trimming tree leaves: %s" % non_terminal_leaves) new_t.remove_nodes_from(non_terminal_leaves) non_terminal_leaves = [n for n in new_t.nodes() if\ (new_t.degree(n) == 1 and n not in destinations and n != source)] results[i] = new_t elif algorithm == 'red-blue': """SkeletonList red-blue paths construction based off 2013 Bejerano and Koppol (Bell Labs) paper entitled 'Link-Coloring Based Scheme for Multicast and Unicast Protection'. This only gives us two redundant yet maximally disjoint subgraphs so we need to apply some other heuristic to further partition them. Currently, we recursively apply this procedure to the pair of graphs in order to generate a number of maximally disjoint trees numbering a power of 2""" # TODO: determine how to better support k not powers of 2 if k != (2**int(math.log(k, 2))): log.warn( "Requested %d redundant red-blue trees, but we currently only fully support powers of 2 for k! Slicing off tail end of results..." % k) from redundant_multicast_algorithms import SkeletonList # Repeatedly apply the procedure over everything currently in the results, # which doubles the number of maximally disjoint spanning DAGs each time results = [self.topo] for i in range(int(math.ceil(math.log(k, 2)))): this_round = [] for t in results: sl = SkeletonList(t, source) red_dag = sl.get_red_graph() blue_dag = sl.get_blue_graph() this_round.append(red_dag) this_round.append(blue_dag) results = this_round assert len(results) >= k # Now we need to turn the results into multicast trees try: from networkx.algorithms.approximation import steiner_tree except ImportError: raise NotImplementedError( "Steiner Tree algorithm not found! See README") assert all(all(d in g for d in destinations) for g in results) # Slice off unrequested results, and then convert to undirected # graphs results = results[:k] results = [ steiner_tree(t, destinations, root=source, weight=weight_metric).to_undirected() for t in results ] assert not any(r.is_directed() for r in results) elif algorithm == 'ilp': """Our (UCI-DSM group) proposed ILP-based heuristic.""" from redundant_multicast_algorithms import ilp_redundant_multicast results = ilp_redundant_multicast(self.topo, source, destinations, k) else: raise ValueError("Unkown multicast tree generation algorithm %s" % algorithm) # Finally, we need to make a new graph copy for each of the trees since we used # subgraph to build them, which means they share the same 'graph' object, which # means they will overwrite each other's attributes when doing e.g. g.graph['address'] = ip_addr results = [nx.Graph(t) for t in results] # Some sanity checks to verify that they're all trees and all subscribers are reachable from the root (connected) if __debug__: for tree in results: if not nx.is_tree(tree): log.warn( "Non-tree multicast tree generated by %s algorithm!" % algorithm) log.debug("Edges are: %s" % list(tree.edges())) if not nx.is_connected(tree): log.warn( "disconnected multicast tree generated by %s algorithm!" % algorithm) log.debug("Edges are: %s" % list(tree.edges())) log.debug("Nodes are: %s" % list(tree.nodes())) return results
def steiner(list_of_locations, list_of_homes, starting_car_location, adjacency_matrix): G = adjacency_matrix_to_graph(adjacency_matrix)[0] Gcopy = adjacency_matrix_to_graph(adjacency_matrix)[0] homeNums = [list_of_locations.index(x) for x in list_of_homes ] + [list_of_locations.index(starting_car_location)] #G = nx.steiner_tree(nx.from_numpy_matrix(adjacency_matrix), list_of_homes + [starting_car_location]); #choose x random nodes from the steiner tree as dropoff locations, then for each TA in list_of_homes, #add that TA to the output dictionary at the dropoff location they're closest to #find shortest cycle from start point that passes through the intermediate nodes by converting graph to metric closure #and finding shortest path greedily G1 = nx1.steiner_tree( adjacency_matrix_to_graph(adjacency_matrix)[0], homeNums) #network x graph w mst output2 = {} list2 = [] dropList = G1.nodes dropList = random.sample(dropList, k=(len(dropList) // 2)) #print(len(dropList)) #decides which TAs go to which dropoff loc for home in list_of_homes: #strings lowestWeight = float('inf') chosenDropoff = dropList[0] for dropLoc in dropList: #numbers sp = nx.shortest_path_length(G1, dropLoc, list_of_locations.index(home)) if (sp < lowestWeight): chosenDropoff = dropLoc lowestWeight = sp if (chosenDropoff in output2): output2[chosenDropoff] += [list_of_locations.index(home)] else: output2[chosenDropoff] = [list_of_locations.index(home)] #decides the shortest cycle going through start to all dropoff locs dropListcopy = dropList for drop in dropList: if (not drop in output2): dropListcopy.remove(drop) dropList = dropListcopy lowestDropoff = dropList[0] lowestDropoffWeight = float( 'inf' ) #var to store lowest home number, var to store lowest length too dropoffsLeft = dropList source = list_of_locations.index(starting_car_location) finalDropOff = "" ####GREEDY STEINER TREE SOL#### while dropoffsLeft != []: dict = nx.single_source_shortest_path_length(G1, source) for drop in dropoffsLeft: if (dict[drop] < lowestDropoffWeight): lowestDropoff = drop lowestDropoffWeight = dict[drop] path2 = nx.shortest_path(G1, source, lowestDropoff, weight="weight") if (len(dropoffsLeft) == 1): finalDropOff = lowestDropoff path2.pop() list2.extend(path2) #print(list) #decrement homes by 1 dropoffsLeft.remove(lowestDropoff) source = lowestDropoff if dropoffsLeft: lowestDropoff = dropoffsLeft[0] lowestDropoffWeight = float( 'inf' ) #var to store lowest home number, var to store lowest length too path2 = nx.shortest_path(G, finalDropOff, list_of_locations.index(starting_car_location), weight="weight") list2.extend(path2) ###GREEDY STEINER TREE SOL### ####VISIT DROPOFF LOCS IN ORDER OF PREORDER VISIT OF MST list3 = [] prevI = list_of_locations.index(starting_car_location) for i in dfs.dfs_preorder_nodes( G1, list_of_locations.index(starting_car_location)): if (i == list_of_locations.index(starting_car_location)): continue else: list3 += nx.shortest_path(G, prevI, i, weight="weight") list3.pop() prevI = i list3 += nx.shortest_path(G, prevI, list_of_locations.index(starting_car_location), weight="weight") return list2, list3, output2
circuit.add_edge(10, 13) circuit.add_edge(11, 13) circuit.add_edge(11, 14) circuit.add_edge(12, 14) circuit.add_edge(13, 15) # Convert the circuit to an equivalent formula. formula = circuit_to_formula(circuit) print(formula_to_string(formula)) labels = nx.get_node_attributes(circuit, "label") options = { "node_size": 600, "alpha": 0.5, "node_color": "blue", "labels": labels, "font_size": 22, } plt.figure(figsize=(8, 8)) pos = nx.multipartite_layout(circuit, subset_key="layer") nx.draw_networkx(circuit, pos, **options) plt.title(formula_to_string(formula)) plt.axis("equal") plt.show() print(approximate.steiner_tree(G=circuit, terminal_nodes=[14, 15]))
map_file = "experiment_2/id_to_file.csv" f = open(map_file, 'r') while True: l = f.readline() arr = l.split(';') if len(arr)<2: break CODE_FILE = arr[1] ROOT_FOLDER = arr[2] FILE_NAME = arr[3] OUTPUT_FILE = arr[4] print(ROOT_FOLDER, FILE_NAME) filename = ROOT_FOLDER + '/' + FILE_NAME +'.txt' G, Ts = build_networkx_graph(filename) ST = steiner_tree(G, Ts[0]) print(ST.nodes(), ST.edges()) f2 = open(ROOT_FOLDER+'/'+FILE_NAME+"_node_weighted.txt", 'w') for n in ST.nodes(): node_str = "" node_str += str(n) + ' ' node_str += '0' + ' ' if n in Ts[0]: node_str += "terminal" + ' ' + "in_solution" else: fnd = False for u, v in ST.edges(): if n==u or n==v: fnd = True node_str += "non_terminal" + ' ' + "in_solution" break
def Single_Source(netwrok, location, Weight, lemda): #Function for SS_Sofda """ Take network,demands weight (which should be 'Cost', 'Delay' or 'Cost_Lemda') and value of lemda (int) as arrguments Returns: Final forest and Cost of it """ G = read_network(netwrok) for a in G.nodes(): G.nodes[a]['Cost'] = 1 C = float('Inf') weight = Weight Destinations, Source_id, u, G_dash = rand_demands() list_1 = [] Final_walk = [] destination = Destinations[1] D_in_string = [] for a in Destinations: D_in_string.append(str(a)) Lemda = lemda for vm in u: ax = [] ax.append(str(vm)) cost_stiner = 0 cost_temp = float('inf') Walk1, InfessileWalk_flag1 = Walk(vm, weight, Lemda) terminal_nodes = ax + D_in_string Walk_Cost = cost_cal(Walk1) if InfessileWalk_flag1 == 0: F_temp = approximation.steiner_tree(G, terminal_nodes, 'Cost') stiner_path = {} for a, b in F_temp.edges(): if a in Destinations: list_1 = [] list_1.append(a) list_1.append(b) stiner_path[a] = list_1 destination = a else: list_1.append(a) list_1.append(b) stiner_path[destination] = list_1 list_1 = [] stiner = [] for a in stiner_path: stiner += stiner_path[a] cost_stiner = stiner_Cost(stiner) cost_temp = cost_stiner + Walk_Cost if cost_temp < C: stiner1 = stiner C = cost_temp F = F_temp.edges() Final_Walk = Walk1 last_vm = vm else: pass print list(set(stiner)), last_vm, Final_Walk return C
def mst(image_path, N_runs, new_size = 'automatic',union_type = None, reversed_colors = True, noise_on=True, rseed = None, t2 =.1, ds_interpolation = 'nearest'): #cluster inputs cluster_flag = True G_filtered = 0 if rseed is None: rseed = list(range(N_runs)) if len(rseed)!= N_runs: print('not enough seeds') else: s = time.process_time() # STEP 1: pre extraction # parameters: #t2 = .5 number_of_colors = 0 number_of_cc = 1 graph_type = '1' t1 = 0.0 print(ds_interpolation) G_pre_extracted, color_dict, partition_dict, folder_path = pre_extraction.pre_extraction_from_image(image_path, new_size, t2, number_of_colors, number_of_cc, graph_type, t1, reversed_colors, ds_interpolation = ds_interpolation) Gpe_nodes = len(G_pre_extracted.nodes()) Gpe_edges = len(G_pre_extracted.edges()) print('Gpe: n_nodes,n_edges:',len(G_pre_extracted.nodes()),len(G_pre_extracted.edges())) #G_pre_extracted = filtering.bifurcation_paths(G_pre_extracted,[]) ## STEP 2: tree approximation #sys.exit() # bfs_Graph = steiner_tree(G_pre_extracted, list(G_pre_extracted.nodes())) mst = tree.maximum_spanning_edges(G_pre_extracted, algorithm='kruskal', data='weight') bfs_Graph = nx.Graph() bfs_Graph.add_edges_from(list(mst)) # bfs_Graph = pre_extraction.tree_approximation(G_pre_extracted, color_dict, partition_dict, folder_path) with open(folder_path + '/bfs_extracted_graph.pkl', 'wb') as file: pkl.dump(bfs_Graph, file) with open(folder_path + '/other_par.pkl', 'wb') as file: pkl.dump([color_dict, partition_dict, folder_path], file) print('info stored') if cluster_flag: folder_path_cluster = './runs/'+image_path.split('/')[-2]+'/'+(image_path.split('/')[-1]).split('.')[0] ''' print('fpc',folder_path_cluster) with open(folder_path_cluster + '/other_par.pkl', 'rb') as file: [color_dict, partition_dict, folder_path] = pkl.load(file) with open(folder_path_cluster + '/extracted_graph.pkl', 'rb') as file: G_pre_extracted = pkl.load(file) with open(folder_path_cluster + '/bfs_extracted_graph.pkl', 'rb') as file: bfs_Graph = pkl.load(file) ''' # STEP #3: filtering (via MST) # computing the leaves deg = nx.degree_centrality(bfs_Graph) N = len(bfs_Graph.nodes) for node in deg.keys(): deg[node] = round(deg[node] * (N - 1)) terminal_candidates= [node for node in bfs_Graph.nodes() if deg[node] == 1] print('leaves',len(terminal_candidates)) Gf = {} terminals = {} for i in range(N_runs): print('steiner tree n=',i) # getting a random terminals #terminal_list = [random.choice(terminal_candidates) for i in range(int(.025 * len(terminal_candidates)))] terminal_list = [] print('random seed',rseed[i]) rng = np.random.RandomState(seed=rseed[i]) node = rng.choice(terminal_candidates) terminal_list.append(node) print('number of terminals',int(.025 * len(terminal_candidates))) while len(terminal_list)<int(.025 * len(terminal_candidates)): #print('i',i) node = rng.choice(terminal_candidates) #terminal_list.append(node) #print('node',node) node_pos = G_pre_extracted.nodes[node]['pos'] dist_bool = [] for elem in terminal_list: if elem != node: #print('elem!= node') elem_pos = G_pre_extracted.nodes[elem]['pos'] val = distance.euclidean(elem_pos,node_pos)>.2 dist_bool.append(val) #print('db',dist_bool) #print('db',dist_bool) if sum(dist_bool)==len(dist_bool): terminal_list.append(node) print('terminal_list',terminal_list) print('computing steiner tree:') G_filtered = steiner_tree(G_pre_extracted, terminal_list) G_filtered = quality_measure.relabeling(G_filtered, G_pre_extracted) Gf[i] = G_filtered terminals[i] = terminal_list #print(terminal_list) for i in range(N_runs): graph = Gf[i] terminal_list = terminals[i] #print('terminals',terminal_list) print('------random n=:'+str(i)+'----------: n_nodes:',len(graph.nodes()),', n_edges:',len(graph.edges())) pre_extraction.plt_graph_plots([graph], partition_dict, color_dict, folder_path_cluster, '/G_filtered' + str(i) + '.png', alpha=.6, width_list=[3], color_list=['black'], highlighted_nodes=[terminal_list]) #with open(folder_path + '/G_filtered' + str(i) + '.pkl', 'wb') as file: # pkl.dump(graph, file) G_filtered = img2net.superimposing_graphs(Gf.values(), G_pre_extracted, union_type) weights = [8*G_filtered.edges[edge]['weight'] for edge in G_filtered.edges()] pre_extraction.plt_graph_plots([G_filtered], partition_dict, color_dict, folder_path, '/G_filtered.png', alpha=.5, width_list=[weights], color_list=['black'] ) with open(folder_path_cluster + '/G_filtered.pkl', 'wb') as file: pkl.dump(G_filtered, file) print('storing results at:',folder_path_cluster) ''' try: os.system('rm -r '+ '../../data/output/test/'+folder_path.split('/')[-1]+'/mst/') except: pass ''' #img2net.move_files(folder_path_cluster,'../../data/output/test/'+folder_path.split('/')[-1]+'/mst/') #os.system('rm -r '+folder_path) e = time.process_time() execution_time = e-s ''' with open( '../../data/output/test/'+folder_path.split('/')[-1]+'/mst/ex_time.pkl', 'wb') as file: pkl.dump(execution_time, file) ''' return G_filtered, Gpe_nodes, Gpe_edges
def solve(client): client.end() client.start() non_home = list(range(1, client.home)) + list( range(client.home + 1, client.v + 1)) all_students = list(range(1, client.students + 1)) dictionary = dict() prob = PQ() remoted = set() havebots = {} # initialize the confidence with 10 confidence = {student: 10 for student in all_students} for v in non_home: dictionary[v] = client.scout(v, all_students) # initialize prob for v in non_home: score = sum([confidence[s] for s, res in dictionary[v].items() if res is True]) + \ sum([-confidence[s] for s, res in dictionary[v].items() if res is False]) prob.push(v, score) bot_at_home = 0 while bot_at_home < client.bots: toberemoted = set() newadd = set() numfound = 0 for v, n in havebots.items(): if n > 0: numfound += n toberemoted.add(v) else: prob.update(v, float('-inf')) while bot_at_home + numfound < client.bots: a, b = prob.pop() numfound += 1 toberemoted.add(a) newadd.add(a) toberemoted.add(client.home) order, path = bfs( approximation.steiner_tree(client.G, list(toberemoted)), client.home) if bot_at_home + sum(havebots.values()) == client.bots: remotelist = order else: remotelist = [] for (me, you) in order: if me in newadd: remotelist.append((me, you)) while remotelist: if bot_at_home >= client.bots: break (frm, to) = remotelist.pop() num = client.remote(frm, to) remoted.add(frm) if num != havebots.get(frm, num): update(True, confidence, prob, dictionary, v, len(all_students), remoted) else: update(False, confidence, prob, dictionary, v, len(all_students), remoted) havebots[frm] = 0 prob.update(frm, float('-inf')) if to == client.home: bot_at_home += num else: havebots[to] = havebots.get(to, 0) + num if bot_at_home >= client.bots: break client.end()
print("Initial Graph G") print("Edges: ") print(G.edges(data=True)) print("Nodes: ") print(G.nodes(data=True)) print("Range of k: " + str(list(powerset(list(range(n - 1, -1, -1)))))) sol_found = False for k in list(powerset(list(range(n - 1, -1, -1)))): Gp = G.copy() print("Terminal nodes: " + str(list(k))) for node in G.nodes(data=True): if node[0] not in list(k): Gp.remove_node(node[0]) Tp = apxalgo.steiner_tree(Gp, list(k), weight="length") print("Tp nodes: ") print(Tp.nodes(data=True)) print("Tp edges: ") print(Tp.edges(data=True)) Tp.pos = G.pos # plotting the steiner tree Tp plt.clf plt.figure(figsize=(20, 14)) plt.title("Steiner tree Tp") pos = Tp.pos colorvalues = [ "green" if node[1].get("sensornode") else "red" for node in Tp.nodes(data=True) ] edge_labels = nx.get_edge_attributes(Tp, "length")
return pset if __name__ == '__main__': g = nx.Graph() g.add_nodes_from([0, 1, 2, 3, 4, 5]) g.add_edges_from([(0, 1, { 'weight': 100 }), (2, 1, { 'weight': 410 }), (2, 5, { 'weight': 100 }), (2, 3, { 'weight': 50 }), (3, 5, { 'weight': 400 }), (1, 3, { 'weight': 105 }), (3, 4, { 'weight': 100 }), (4, 5, { 'weight': 210 }), (0, 3, { 'weight': 100 })]) a, b = bfs(approximation.steiner_tree(g, [0, 2, 5]), 0) while a: print(a.pop()) print(b)
def solve(client): client.end() client.start() non_home = list(range(1, client.home)) + list( range(client.home + 1, client.v + 1)) all_students = list(range(1, client.students + 1)) dictionary = dict() prob = PQ() remoted = set() havebots = [] # initialize the confidence with 10 confidence = {student: 10 for student in all_students} for v in non_home: dictionary[v] = client.scout(v, all_students) # initialize prob for v in non_home: score = sum([confidence[s] for s, res in dictionary[v].items() if res is True]) + \ sum([-confidence[s] for s, res in dictionary[v].items() if res is False]) prob.push(v, score) bot_at_home = 0 path, dist = dijkstra(client) while bot_at_home < client.bots: #pop the nodes with highest probability of having bots, find the shortest dijstras remote of temp = [] v, priority = prob.pop() temp.append(v) while True: if prob.isEmpty(): break nex, nprio = prob.pop() if nprio != priority: prob.push(nex, nprio) break temp.append(nex) minv = float('inf') minnode = None for x in temp: comp = client.G[x][path[x]]['weight'] if comp < minv: minv = comp minnode = x for x in temp: if x != minnode: prob.push(x, priority) v = minnode # pop the nodes with highest probability of having bots, find the shortest dijstras remote ABOVE num = client.remote(v, path[v]) remoted.add(v) v = path[v] if num != 0: update(True, confidence, prob, dictionary, v, len(all_students), remoted) if v == client.home: bot_at_home += num else: havebots.append(v) print(havebots) if bot_at_home + len(havebots) >= client.bots: if len(havebots) == 0: break havebots.append(client.home) order, b = bfs(approximation.steiner_tree(client.G, havebots), client.home) while order: if bot_at_home >= client.bots: break (frm, to) = order.pop() num = client.remote(frm, to) if to == client.home: bot_at_home += num else: update(False, confidence, prob, dictionary, v, len(all_students), remoted) client.end()