def get_shortest_path(self, player, target, avoid): """ avoid ([nodes]): nodes to temporarily avoid, (y,x) """ temp_edge_storage = [] dbout("AVOID LIST:") dbout(avoid) temp_edge_storage = [] path = [] for node in avoid: neighbors = list(nx.all_neighbors(self.G, node)) for neighbor in neighbors: self.G.remove_edge(node, neighbor) temp_edge_storage.extend([(node, neighbor) for neighbor in neighbors]) try: path = nx.shortest_path(self.G, (player.y, player.x), (target.y, target.x)) except: dbout("EXCEPTION: UNABLE TO FIND SHORTEST PATH") for edge in temp_edge_storage: self.G.add_edge(edge[0], edge[1]) raise Exception("Except") for edge in temp_edge_storage: self.G.add_edge(edge[0], edge[1]) dbout("PATH FROM PLAYER TO TARGET GIVEN AVOID LIST:") dbout(path) return path
def choose_best_conversion_path(conversion_graph, input_format, output_format): try: graph_path = nx.shortest_path( conversion_graph, input_format, output_format) except nx.NetworkXNoPath: raise Exception(""" No combination of plugins available in doconv can convert from {0} to {1}""".format(input_format, output_format)) return graph_path if conversion_graph is not None else None
def choose_best_conversion_path(self): """Select as path the one that requires less conversions. """ graph = self.plugin_manager.graph try: conversion_path = nx.shortest_path( graph, self.input_format, self.output_format) except nx.NetworkXNoPath: raise Exception(""" No combination of plugins available in doconv can convert from {0} to {1}""".format(self.input_format, self.output_format)) conversion_path = conversion_path if graph is not None else None logger.debug( "Chosen chain of transformations: {0}".format(conversion_path)) return conversion_path
def get_transfers(graph): ugraph = graph.to_undirected() path = nx.shortest_path(ugraph, 'YOU', 'SAN') # subtract 2 to account for YOU, SAN - gives number of nodes on transfer path # subtract another one because we want number of edges (= number of nodes - 1) return len(path) - 3
def count_orbits(graph: nx.DiGraph): paths = nx.shortest_path(graph, 'COM') lengths = [len(path) - 1 for path in paths.values()] return sum(lengths)