def _get_full_path(self, point, another_point, cur_time=12 * 60): # O(n*logn) dijk_d = dijkstra.DijkstraSPF(self.d_graph, point) dijk_ft = dijkstra.DijkstraSPF(self.full_t_graph, point) dijk_ht = dijkstra.DijkstraSPF(self.half_t_graph, point) timie = 0 if cur_time < 8 * 60 or cur_time > 20 * 60: timie = dijk_ht.get_path(another_point) else: timie = dijk_ft.get_path(another_point) return (dijk_d.get_path(another_point), timie)
def pose(): SIZE_X = 15 SIZE_Y = 15 obs = [ 2, 17, 32, 33, 34, 38, 39, 40, 42, 47, 48, 49, 53, 54, 55, 62, 63, 64, 68, 69, 70, 77, 78, 79, 86, 105, 106, 107, 108, 109, 110, 112, 114, 115, 116, 117, 118, 119, 144, 151, 153, 156, 159, 161, 163, 174, 181, 186, 189, 204, 206, 208, 219 ] graph = create_map(15, 15, obs) print("Type location of starting node") xi = int(input("X = ")) yi = int(input("Y = ")) start = SIZE_X * yi + xi paths = dij.DijkstraSPF(graph, start) # print("%-5s %-5s" % ("destination", "cost")) # for u in range(0,SIZE_X*SIZE_Y): # if not u in obs: # print("%-5s %8d" % (u, paths.get_distance(u))) print("Type location of destination node") xf = int(input("X = ")) yf = int(input("Y = ")) dest = SIZE_X * yf + xf if not dest in obs: print(paths.get_path(dest)) return paths.get_path(dest) else: print("Destination cannot be reached.") return 0
def get_path(self, starting_waypoint, destination_id): """Given starting way point id and destination id, find the shortest path between them in the graph, return list of waypoints between to path.""" # Run dijkstra's algorithm dijkstra_output = dijkstra.DijkstraSPF(self.graph, starting_waypoint) # Get waypoint_id of destination cur = armaps.model.get_db() cur.execute( "SELECT waypoint_id FROM waypoints WHERE destination_id = %s", (destination_id, )) cur_output = cur.fetchone() dest_waypoint_id = int(cur_output["waypoint_id"]) # Get the path from starting waypoint to destination's waypoint path = dijkstra_output.get_path(dest_waypoint_id) # Excluding starting point in path, add coords of each waypoint to data data = [] for i in range(1, len(path)): data.append({ "lat": self.waypoints[path[i]]["lat"], "lon": self.waypoints[path[i]]["lon"], "id": self.waypoints[path[i]]["waypoint_id"] }) return data
def _get_dist(self, point, another_point, cur_time=12 * 60, easy=False): # O(n*logn) if easy: dijk_d = dijkstra.DijkstraSPF(self.d_graph, point) e = dijk_d.get_distance(another_point) return (e, e) dijk_d = dijkstra.DijkstraSPF(self.d_graph, point) timie = 0 if cur_time < 8 * 60 or cur_time > 20 * 60: dijk_ht = dijkstra.DijkstraSPF(self.half_t_graph, point) timie = dijk_ht.get_distance(another_point) else: dijk_ft = dijkstra.DijkstraSPF(self.full_t_graph, point) timie = dijk_ft.get_distance(another_point) return (dijk_d.get_distance(another_point), timie)
def estimate_by_nodes(self, ori_nodes, dest_nodes): assert len(ori_nodes) == len( dest_nodes), "ori_nodes, dest_nodes must have same length" dist_arr = np.zeros(len(ori_nodes)) for i in range(len(ori_nodes)): ori_node = ori_nodes[i] dest_node = dest_nodes[i] dijkstra3 = dijkstra.DijkstraSPF(self.graph3, ori_node) dist = dijkstra3.get_distance(dest_node) if dist < math.inf: dist_arr[i] = dist else: dijkstra1 = dijkstra.DijkstraSPF(self.graph1, ori_node) dist = dijkstra1.get_distance(dest_node) if dist < math.inf: dist_arr[i] = dist return dist_arr
def estimate_by_node(self, ori_node, dest_node): dijkstra3 = dijkstra.DijkstraSPF(self.graph3, ori_node) dist = dijkstra3.get_distance(dest_node) if dist < math.inf: path = dijkstra3.get_path(dest_node) print("\n".join(path)) print("dist: {}".format(round(dist, 2))) return dist, path else: dijkstra1 = dijkstra.DijkstraSPF(self.graph1, ori_node) dist = dijkstra1.get_distance(dest_node) if dist < math.inf: path = dijkstra1.get_path(dest_node) print("\n".join(path)) print("dist (min1): {}".format(round(dist, 2))) return dist, path else: print("fail") return 0.0, None
def get_shortest_paths(self, max_jump): paths = [] for i in range(satellite_number): paths_sourced_from_i = [] dijk_instance = dijkstra.DijkstraSPF(self.__graph, i) for j in range(satellite_number): distance = dijk_instance.get_distance(j) path = tuple(dijk_instance.get_path(j)) if len(path) <= max_jump: paths_sourced_from_i.append((path, distance)) else: paths_sourced_from_i.append((None, None)) paths.append(paths_sourced_from_i) return paths
data = {"address": get_router_address_for_host(num)} requests.post(url=make_router_url(num), json=data) if exist_link(num, i): data = {"address": get_router_address(num, i, prefix=True)} requests.post(url=make_router_url(num), json=data) def router_add_route(src, by, dst): data = { "gateway": get_router_address(by, src), "destination": get_router_host_address(dst) } requests.post(url=make_router_url(src), json=data) graph = dijkstra.Graph() for i in node_list: router_add_address(i) for i in distance_list: graph.add_edge(i[0], i[1], i[2]) graph.add_edge(i[1], i[0], i[2]) for i in node_list: dij = dijkstra.DijkstraSPF(graph, i) for j in network_list: path = dij.get_path(j) if len(path) >= 2: router_add_route(i, path[1], j)