def get_route(start_point,end_point): #16,17 start_stations = vc_landmarks[start_point] end_stations = vc_landmarks[end_point] #44 metro_system = get_active_stations() if stations_under_construction else vc_metro #45 if stations_under_construction: #46 possible_route = dfs(metro_system,start_staton,end_station) #47 if not possible_route: return None #18, routes = [] #19 for start_station in start_stations: for end_station in end_stations: #20, 48 changed vc metro to metro station to get the shortest route for whichever metro graph is currently in place route = bfs(metro_system,start_station,end_station) #21 if route: routes.append(route) #22 shortest_route = min(routes, key=len) return shortest_route
def get_route(start_point, end_point): #start_stations will be looking at all stations we could possibly start at given the landmark start_stations = vc_landmarks[start_point] #end_stations will be looking at all the end stations we could possibly end given the landmark end_stations = vc_landmarks[end_point] #list collecting the shortest routes routes = [] #looping through every combination of start and end stations for start_station in start_stations: for end_station in end_stations: #looking at how many stops it takes from start to finish #landmarks are being passed in #print("Iterating over {0} and {1}".format(start_station, end_station))#USE for Testing only metro_system = get_active_stations( ) if stations_under_construction else vc_metro #check if stations_under_construction is empty #Checks to see if a route EXISTS using Depth First Search if len(stations_under_construction) > 0: possible_route = dfs(metro_system, start_station, end_station) if possible_route is None: return None #Breath First Search used to find the shortest route route = bfs(metro_system, start_station, end_station) #if there is a route we append the shortest route to routes list we created above if route: routes.append(route) #print("Routes list consists of : " + str(routes)) shortest_route = min(routes, key=len) return shortest_route
def run_bfs(map_path): g = graph_search.GridMap(map_path) res = graph_search.bfs(g.init_pos, g.transition, g.is_goal, graph_search._ACTIONS) #res = graph_search.bfs(g.init_pos, g.transition, g.is_goal, graph_search._ACTIONS_2) #actionset = res[0]; print("Plan: " + str(res[0])) print("Visited: " + str(res[1])) #print res; #g.display_map(res[0],res[1]) y = len(res[0]) res1 = g.init_pos list = [] for x in range(0, y): i = 0 actionset = res[0][i] i = +1 res1 = g.bfs_simulator(res1, res[0][x], graph_search._ACTIONS, action_probability=0.8, neighbour_probability=0.1, ortho_probability=0, simulate=False) #res1 = g.bfs_simulator(res1, res[0][x], graph_search._ACTIONS_2, action_probability=0.8, neighbour_probability=0.1, ortho_probability=0.05, simulate=False) #res1= graph_search.GridMap.bfs_simulator() list.append(res1) #print res1; print(list) g.display_map(list, res[1])
def test_breadth_first(build_graph, start_end, excepted_path): graph = build_graph start_vertex_value = start_end[0] target_vertex_value = start_end[1] path = bfs(graph, start_vertex_value, target_vertex_value) assert path == excepted_path
def test_breadth_first_directed(build_graph): graph = build_graph start_vertex = "vertex_2" target_vertex = "vertex_1" path = bfs(graph, start_vertex, target_vertex) if graph.directed: assert path is None else: assert path == ["vertex_2", "vertex_3", "vertex_1"]
def get_route(start_point, end_point): start_stations = vc_landmarks[start_point] end_stations = vc_landmarks[end_point] routes = [] for start_station in start_stations: for end_station in end_stations: route = bfs(vc_metro, start_station, end_station) if route: routes.append(route) shortest_route = min(routes, key=len) return shortest_route
def get_route(start_point, end_point): start_stations = vc_landmarks[start_point] end_stations = vc_landmarks[end_point] routes = [] for start_station in start_stations: for end_station in end_stations: metro_system = get_active_stations() if stations_under_construction else vc_metro if len(stations_under_construction) > 0: possible_route = dfs(metro_system, start_station, end_station) if possible_route is None: return None route = bfs(metro_system, start_station, end_station) if route is not None: routes.append(route)
def get_route(start_point, end_point): start_stations = vc_landmarks[start_point] end_stations = vc_landmarks[end_point] routes = list() for start_station in start_stations: for end_station in end_stations: metro_system = get_active_stations() if stations_under_construction else vc_metro if stations_under_construction: if not dfs(metro_system, start_station, end_station): return None route = bfs(metro_system, start_station, end_station) if route: routes.append(route) return min(routes, key=len)
def get_route(start_point, end_point): start_stations=vc_landmarks[start_point] end_stations=vc_landmarks[end_point] routes=[] for start_station in start_stations: for end_station in end_stations: metro_system= get_active_stations() if stations_under_construction else vc_metro if stations_under_construction: possible_route = dfs(metro_system, start_station, end_station) if not possible_route: return None route=bfs(metro_system, start_station, end_station) if route: routes.append(route) shortest_route=min(routes, key=len) return shortest_route
def get_route(start_point, end_point): start_stations = vc_landmarks[start_point] # retrieves list of stations corresponding to startpoint end_stations = vc_landmarks[end_point] # retrieves list of stations corresponding to endpoint routes = [] for start_station in start_stations: for end_station in end_stations: metro_system = get_active_stations() if stations_under_construction else vc_metro # set metro_system to graph of all open stations if stations_under_construction: possible_route = dfs(metro_system, start_station, end_station) # check if a route exists if possible_route == []: return None route = bfs(metro_system, start_station, end_station) if route: routes.append(route) shortest_route = min(routes, key=len) if routes else None return shortest_route
def get_route(start_point,end_point): if start_point == end_point: print("ERROR: Start point and end point cannot be the same!\nPlease try again") new_route() else: start_stations = vc_landmarks[start_point] end_stations = vc_landmarks[end_point] routes = [] for start_station in start_stations: for end_station in end_stations: metro_system = get_active_stations() if stations_under_construction else vc_metro if stations_under_construction: possible_route = dfs(metro_system,start_station,end_station) if not possible_route: return None route = bfs(metro_system,start_station,end_station) if route: routes.append(route) shortest_route = min(routes,key=len) return shortest_route
def test_breadth_first_cycles(build_graph): graph = build_graph graph.add_vertex("vertex_5") graph.add_edge("vertex_2", "vertex_1") graph.add_edge("vertex_3", "vertex_4") graph.add_edge("vertex_4", "vertex_5") start_vertex = "vertex_2" end_vertex = "vertex_5" path = bfs(graph, start_vertex, end_vertex) if graph.directed: except_path = [ "vertex_2", "vertex_1", "vertex_3", "vertex_4", "vertex_5" ] else: except_path = ["vertex_2", "vertex_3", "vertex_4", "vertex_5"] print(path) assert path == except_path
def get_route(start_point, end_point): start_stations = vc_landmarks[start_point] end_stations = vc_landmarks[end_point] routes = [] for start_station in start_stations: for end_station in end_stations: # if there is construction metro_system becomes new updated graph. Else vc_metro graph is used metro_system = get_active_stations( ) if stations_under_construction else vc_metro # checks if there is a possible route with a depth first search if len(stations_under_construction) > 0: possible_route = dfs(metro_system, start_station, end_station) if possible_route is None: return None # creates routes with the different start and end stations route = bfs(metro_system, start_station, end_station) if route: routes.append(route) # selects shortest route from list of routes. shortest_route = min(routes, key=len) return shortest_route
def get_route(start_point, end_point): start_stations = vc_landmarks[start_point] end_stations = vc_landmarks[end_point] routes = [] for start_station in start_stations: for end_station in end_stations: metro_system = get_active_stations( ) if stations_under_construction else vc_metro if stations_under_construction: possible_route = dfs(metro_system, start_station, end_station) if not possible_route: print( "There is not route between station {0} and station {1}." .format(start_station, end_station)) return None route = bfs(metro_system, start_station, end_station) if route: routes += [route] shortest_route = min(routes, key=len) return shortest_route
def get_route(start_point, end_point): start_stations = vc_landmarks[start_point] end_stations = vc_landmarks[end_point] routes = [] for start_station in start_stations: for end_station in end_stations: # accommodats ournew updated_metro graph if there are stations under construction metro_system = get_active_stations( ) if stations_under_construction else vc_metro # makes sure stations_under_constructions isn't empty if len(stations_under_construction) > 0: # using depth-first function to help find shortest route between points possible_route = dfs(metro_system, start_station, end_station) if possible_route is None: return None route = bfs(metro_system, start_station, end_station) if route is not None: routes.append(route) # find shortest route with little trick, 2D list shortest_route = min(routes, key=len) return shortest_route
def get_route(start_point, end_point): # we grab the sets of stations connected to the start_point and end_point landmarks start_stations = vc_landmarks[start_point] end_stations = vc_landmarks[end_point] # because some landmarks have more than one station, there is sometimes more than one route between landmarks # it’s our job to collect ALL of the shortest routes between stations and then compare them based on path length # to collect our shortest routes for comparison, create an empty list routes = [] # we loop through each station in start_stations for start_station in start_stations: # and each station in end_stations for end_station in end_stations: ## add changes to accommodate our new updated_metro graph if there are stations under construction ## if stations_under_construction isn't empty we use the updated metro graph returned with the helper function ## if there are no stations_under_construction we can keep using the original graph metro_system = get_active_stations() if stations_under_construction else vc_metro ## if there are stations_under_construction it means we might not find a route at all if stations_under_construction: ## so it is worth to check if a route even exists using the dfs() function possible_route = dfs(metro_system, start_station, end_station) ## if there is no possible_route we return None, we need this step because ## then shortest_route will be None in new_route() and we have some control flow set up to handle that condition if not possible_route: return None ## update the graph used to metro_system so that we can get the shortest route for whichever metro graph is currently in place # we call bfs() on each combination of start and end station to find all the shortest routes route = bfs(metro_system, start_station, end_station) # check if a route exists between the two stations looked at if route is not None: # the route returned from bfs() is a list that represents the path # add this new shortest route to the routes list routes.append(route) # after the loops finished we have all the shortest route options saved in routes # we can now get our shortest route from the list based on list length shortest_route = min(routes, key = len) return shortest_route
def get_route(start_point, end_point): #each landmark could have several start stations and several end stations start_stations = vc_landmarks[start_point] end_stations = vc_landmarks[end_point] routes = [] #for each start station, and for each end station, see if there is a route for sstation in start_stations: for estation in end_stations: metro_system = get_active_stations( ) if stations_under_construction else vc_metro if stations_under_construction != []: possible_route = dfs(metro_system, sstation, estation) if not possible_route: return None route = bfs(metro_system, sstation, estation) #if the route exists, add to list if route: routes.append(route) #find the shortest route shortest_route = min(routes, key=len) return shortest_route
def get_route(start_point, end_point): # define start and end locations start_stations = vc_landmarks[start_point] end_stations = vc_landmarks[end_point] # initiate route list all_routes = [] # updated metro system with stations under construction metro_system = get_active_stations( ) if stations_under_construction else vc_metro # check to see if stations_under construction # make it immposible for travel from one # location to anothe for ss in start_stations: for es in end_stations: possible_route = dfs(metro_system, ss, es) if possible_route: if possible_route not in all_routes: all_routes.append(possible_route) if possible_route == None: return None # itterating through all posible start stations # nearest landmarks for start_station in start_stations: for end_station in end_stations: route = bfs(vc_metro, start_station, end_station) if route not in all_routes: all_routes.append(route) return all_routes
def get_route(start_point, end_point): start_stations = vc_landmarks[start_point] end_stations = vc_landmarks[end_point] routes = [] for start_station in start_stations: for end_station in end_stations: if len(stations_under_construction) != 0: metro_system = get_active_stations() else: metro_system = vc_metro if len(stations_under_construction) != 0: #checking if route exists possible_route = dfs(metro_system, start_station, end_station) if not possible_route: return None route = bfs(metro_system, start_station, end_station) if route: routes += [route] shortest_route = min(routes, key=len) return shortest_route
def get_route(start_point, end_point): start_stations = vc_landmarks[start_point] end_stations = vc_landmarks[end_point] routes = [] for start in start_stations: for end in end_stations: metro_system = get_active_stations( ) if stations_under_construction else vc_metro if stations_under_construction: possible_route = dfs(metro_system, start, end) if not possible_route: return None route = bfs(metro_system, start, end) if route: routes.append(route) try: shortest_route = min(routes, key=len) return shortest_route except ValueError: print("You are already at the closest station to your destination!") new_destination = input( "Would you like to input a new destination? y/n") if new_destination.lower() == "y": new_route(start_point, end_point)
def distance(self, a, b): from graph_search import bfs return len(bfs(self, a, b))
# start, goal = 1310, 1320 start, goal = 1111, 2121 # goal -= 120 # 10 worked well cache = DefaultBear(dict) logger.configure() path = [*range(start, goal + 1)] ds = [pairwise[i, j] for i, j in zip(path[:-1], path[1:])] plot_trajectory_rope(path, ds, all_images, title=f'Ground-Truth', key=f"../figures/rope_plans/bfs.png") queries.clear() path = bfs(G, start, goal) ds = [pairwise[i, j] for i, j in zip(path[:-1], path[1:])] cache.cost['bfs'] = len(queries.keys()) cache.len['bfs'] = len(path) print(f" bfs len: {len(path)}", *path) print(f"# of queries {len(queries.keys())}") plot_trajectory_rope(path, ds, all_images, title=f'Breath-first', key=f"../figures/rope_plans/bfs.png") queries.clear() path = heuristic_search(G, start, goal, heuristic) ds = [pairwise[i, j] for i, j in zip(path[:-1], path[1:])] cache.cost.update(heuristic=len(queries.keys()))
print(action_path) print(visited) g.display_map(path,visited) file.close() import graph_search g = graph_search.GridMap('./map1.txt') file = open('output1.txt', 'a') [[path, action_path],visited]=graph_search.bfs(g.init_pos , g.transition , g.is_goal, graph_search._ACTIONS) file.write("DFS on map1: path = "+str(path) + "\n") file.write("DFS on map1: action_path = "+str(action_path) + "\n") file.write("DFS on map1: visited = "+str(visited) + "\n") print(path) print(action_path) print(visited) g.display_map(path,visited) file.close() ''' import graph_search g = graph_search.GridMap('./map2.txt') file = open('output2.txt', 'a') [[path, action_path], visited] = graph_search.bfs(g.init_pos, g.transition, g.is_goal, graph_search._ACTIONS) file.write("DFS on map2: path = " + str(path) + "\n") file.write("DFS on map2: action_path = " + str(action_path) + "\n") file.write("DFS on map2: visited = " + str(visited) + "\n") g.display_map(path, visited) file.close()