def Ai(self, k, r, X, i): T = SuitabilityGraph() while k > 0: TBEST = SuitabilityGraph() for kprime in range(1, k + 1): for v in self.__nodes: if i > 1: Tprime = self.Ai(kprime, v, X, i - 1) p = self.__paths[tuple(sorted([r, v]))] Tprime.append_path(p, self.__graph) else: dists = {} for t in self.__terminals: dists[t] = self.__dist[tuple(sorted([v, t]))] ord_term = sorted(dists.iteritems(), key=operator.itemgetter(1)) Tprime = SuitabilityGraph() for j in range(kprime): p = self.__paths[tuple(sorted([v, ord_term[j][0]]))] Tprime.append_path(p, self.__graph) if self.d(TBEST) > self.d(Tprime): TBEST = Tprime T.append_graph(TBEST) k -= len(set(TBEST.keys()).intersection(X)) X = set(X).difference(TBEST.keys()) return T
def steiner_tree(self): subtrees = [] _, paths = dijkstra(self.__graph, self.__poi, self.__terminals) for t in self.__terminals: path = paths[t] subtree = SuitabilityGraph() # j = 0 # for i in range(len(path_to_poi)): # if path_to_poi[i] in self.__graph.contracted_regions: # region_id = path_to_poi[i] # subtree.append_from_path(path_to_poi[j:i], self.__original_graph) # closest_node = self.__find_closest_node_to_poi_within_region(region_id) # path_endpoint_1 = self.__dist_paths_node_within_region_node[closest_node][1][path_to_poi[i - 1]] # subtree.append_from_path(path_endpoint_1, self.__original_graph) # path_endpoint_2 = self.__dist_paths_node_within_region_node[closest_node][1][path_to_poi[i + 1]] # subtree.append_from_path(path_endpoint_2, self.__original_graph) # j = i + 1 # subtree.append_from_path(path_to_poi[j:], self.__original_graph) subtree.append_path(path, self.__graph) subtrees.append(subtree) steiner_tree = self.__merge_subtrees(subtrees) self.__prune_steiner_tree(steiner_tree) if self.__contract_graph: self.__decontract_steiner_tree(steiner_tree) return steiner_tree
def __build_steiner_forest(self): forest = SuitabilityGraph() dist = cost = occupancy = 0 for t, dest in self.__confirmed.iteritems(): if dest is None: dest = self.__medoids[t] self.__confirmed[t] = dest for t_ in self.__term_tree[t]: self.__detour[t_] += self.__graph.dist[tuple( sorted([t, dest]))] # # Print groups of terminals that travel together. # if dest in self.__pois: # print self.__term_tree[t] # print t, dest self.__graph.compute_dist_paths(origins=[t], destinations=[dest], recompute=True) try: forest.append_path( self.__graph.paths[tuple(sorted([t, dest]))], self.__graph) dist = self.__graph.dist[tuple(sorted([t, dest]))] cost += dist except KeyError: # pdb.set_trace() print 'key error:', t, dest # Who is driver? # if t in self.__terminals and (dist > max_wd or dest == self.__medoids[t]): # print "Driver:", t, "Meeting point:", dest occupancy += len(self.__term_tree[t]) * dist # print "Num. terms:", len(self.__term_tree[t]), "Shared distance:", dist return forest, cost, occupancy / cost
def enclosing_region(self): enclosing = SuitabilityGraph() paths = [] paths.append(self.__paths[tuple(sorted([323287670, 2392803740]))]) paths.append(self.__paths[tuple(sorted([2392803740, 127578100]))]) paths.append(self.__paths[tuple(sorted([127578100, 3109398450]))]) paths.append(self.__paths[tuple(sorted([3109398450, 342909685]))]) paths.append(self.__paths[tuple(sorted([342909685, 323287670]))]) for path in paths: enclosing.append_path(path, self.__original_graph) return enclosing
def __build_steiner_tree_bactracking(self, node, subset): steiner_tree = SuitabilityGraph() next_node = self.__s_d[node][tuple(subset)][1] if self.__contract_graph: print(node, self.__s_d[node][tuple(subset)]) # pdb.set_trace() if next_node is not None: steiner_tree.append_path(self.__paths[tuple(sorted([node, next_node]))], self.__graph) (best_e, best_f) = self.__s_d[node][tuple(subset)][2] # pdb.set_trace() steiner_branch_e = SuitabilityGraph() if best_e is not None and best_e != [next_node]: steiner_branch_e = self.__build_steiner_tree_bactracking(next_node, best_e) steiner_branch_f = SuitabilityGraph() if best_f is not None and best_f != [next_node] and len(best_f) > 0: steiner_branch_f = self.__build_steiner_tree_bactracking(next_node, best_f) steiner_tree.append_graph(steiner_branch_e) steiner_tree.append_graph(steiner_branch_f) return steiner_tree
def __build_steiner_tree(self, node, subset): steiner_tree = SuitabilityGraph() next_node = self.__s_d[node][subset][0][3] print(node, self.__s_d[node][subset]) # pdb.set_trace() if next_node is not None: try: steiner_tree.append_path( self.__paths[tuple(sorted([node, next_node]))], self.__graph) except KeyError: _, paths = dijkstra(self.__graph, node, [next_node]) steiner_tree.append_path(paths[next_node], self.__graph) (set_e, set_f) = self.__s_d[node][subset][0][4] steiner_branch_e = SuitabilityGraph() if set_e is not None and set_e != [next_node]: steiner_branch_e = self.__build_steiner_tree(next_node, set_e) steiner_branch_f = SuitabilityGraph() if set_f is not None and set_f != [next_node] and len(set_f) > 0: steiner_branch_f = self.__build_steiner_tree(next_node, set_f) steiner_tree.append_graph(steiner_branch_e) steiner_tree.append_graph(steiner_branch_f) return steiner_tree
def __build_steiner_tree_bactracking(self, node, subset): steiner_tree = SuitabilityGraph() next_node = self.__steiner_distances[node][tuple(subset)][3] # pdb.set_trace() if next_node is not None: try: steiner_tree.append_path( self.__paths[tuple(sorted([node, next_node]))], self.__graph) except KeyError: _, paths = dijkstra(self.__graph, node, [next_node]) steiner_tree.append_path(paths[next_node], self.__graph) (best_e, best_f) = self.__steiner_distances[node][tuple(subset)][4] steiner_branch_e = SuitabilityGraph() if best_e is not None and best_e != [next_node]: steiner_branch_e = self.__build_steiner_tree_bactracking( next_node, best_e) steiner_branch_f = SuitabilityGraph() if best_f is not None and best_f != [next_node] and len(best_f) > 0: steiner_branch_f = self.__build_steiner_tree_bactracking( next_node, best_f) steiner_tree.append_graph(steiner_branch_e) steiner_tree.append_graph(steiner_branch_f) return steiner_tree
suitability_graph.append_graph(graph) suitability_graph.extend_suitable_regions(seed, generator) suitability_graph.extend_suitable_regions(seed, generator) regions = suitability_graph.get_suitable_regions(generator) # suitability_graph.contract_suitable_regions(generator) # paths = [] # for t in terminals: # _, paths_t = dijkstra(suitability_graph, t, [poi], consider_node_weights=False) # paths.append(paths_t[poi]) # _, paths_poi = suitability_graph.compute_shortest(poi, terminals) suitability_graph.compute_dist_paths(origins=[poi], destinations=terminals) paths = [ suitability_graph.paths[tuple(sorted([t, poi]))] for t in terminals ] tree = SuitabilityGraph() for p in paths: tree.append_path(p, suitability_graph) ngh = NetworkXGraphHelper(suitability_graph) ngh.draw_graph( special_nodes=[(terminals, None, None), ([poi], None, None)], # subgraphs_1=[r for _, (r, _, _, _, _, _) in regions.iteritems()], special_subgraphs=[(tree, None)], print_node_labels=False)