def __find_path_transit(self, origin, destination): origin_node = self.tg.find_closest_node(origin[0], origin[1]) destination_node = self.tg.find_closest_node(destination[0], destination[1]) route = [] distance = 0 for i in dij.shortestPath(self.tg, origin_node, destination_node): route.append(self.tg[i]) if len(route) > 1: distance += calc_distance(route[-1], route[-2]) return (route, distance)
def construct_graph(self): self.G = {} raw_data = [] charge = 0 for i in range(len(self.new_transport)): data = {} dest = {} origin_city = self.new_transport[i][0] dest_city = self.new_transport[i][1] dest[dest_city] = self.new_transport[i][5] data[origin_city] = dest raw_data.append(data) for meta in raw_data: graph.make_link(self.G, meta.keys()[0], meta.values()[0].keys()[0], meta.values()[0].values()[0]) try: self.path = dij.shortestPath(self.G, self.mail.origin, self.mail.destination) except: return -1 for i in range(len(self.path) - 1): charge += self.G[self.path[i]][self.path[i + 1]] #print "charge us=", charge self.mail.costkps = charge
if __name__=="__main__": # load graph with open('graph.pkl', 'r') as f: G = pickle.load(f) # input loop while True: try: input1 = [float(i.strip()) for i in raw_input('lat, lng [origin]: ').split(',')] input2 = [float(i.strip()) for i in raw_input('lat, lng [destination]: ').split(',')] n1 = G.find_closest_node(Node(input1[0], input1[1])) n2 = G.find_closest_node(Node(input2[0], input2[1])) print "orgin node: %s,%s" % (G[n1].lat, G[n1].lng) print "destination node: %s,%s" % (G[n2].lat, G[n2].lng) print "-" * 30 for i in dij.shortestPath(G, n1, n2): print "%s,%s" % (G[i].lat, G[i].lng) print "-" * 30 except Exception as e: print traceback.format_exc()
G[node2] = {} (G[node2])[node1] = value return G if __name__ == "__main__": G = {} raw_data = [ {"Wellington":{"Auckland":5}}, {"Wellington":{"Hamilton":3}}, {"Wellington":{"Rutorua":4}}, {"Wellington":{"Christchurch":6}}, {"Wellington":{"Turanga":8}}, {"Auckland": {"Wellington": 5}}, {"Auckland": {"Hamilton": 2}}, {"Auckland": {"Rutorua":1}}, {"Auckland": {"Turanga":2}} ] for meta in raw_data: make_link(G, ''.join(meta.keys()), meta.values()[0].keys()[0], meta.values()[0].values()[0]) path = dij.shortestPath(G, "Wellington", "Turanga") value = 0 for i in range(len(path) - 1): value += G[path[i]][path[i + 1]] print path