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) 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: return 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): 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 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: # 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): 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): # 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 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 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
import graph_search g = graph_search.GridMap('./map0.txt') file = open('output0.txt', 'a') [[path, action_path],visited]=graph_search.dfs(g.init_pos , g.transition , g.is_goal, graph_search._ACTIONS) file.write("BFS on map0: path = "+str(path) + "\n") file.write("BFS on map0: action_path = "+str(action_path) + "\n") file.write("BFS on map0: visited = "+str(visited) + "\n") 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.dfs(g.init_pos , g.transition , g.is_goal, graph_search._ACTIONS) file.write("BFS on map1: path = "+str(path) + "\n") file.write("BFS on map1: action_path = "+str(action_path) + "\n") file.write("BFS on map2: visited = "+str(visited) + "\n") 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.dfs(g.init_pos , g.transition , g.is_goal, graph_search._ACTIONS) file.write("BFS on map2: path = "+str(path) + "\n") file.write("BFS on map2: action_path = "+str(action_path) + "\n") file.write("BFS on map2: visited = "+str(visited) + "\n") g.display_map(path,visited) file.close()
import graph_search g = graph_search.GridMap('./map0.txt') file = open('output0.txt', 'a') [[path, action_path], visited] = graph_search.dfs(g.init_pos, g.transition, g.is_goal, graph_search._ACTIONS) print(path) print(action_path) print(visited) file.write("BFS on map0: path = " + str(path) + "\n") file.write("BFS on map0: action_path = " + str(action_path) + "\n") file.write("BFS on map0: visited = " + str(visited) + "\n") g.display_map(path, visited, "Project-1_1.1_DFS on map0") file.close() import graph_search g = graph_search.GridMap('./map1.txt') file = open('output1.txt', 'a') [[path, action_path], visited] = graph_search.dfs(g.init_pos, g.transition, g.is_goal, graph_search._ACTIONS) print(path) print(action_path) print(visited) file.write("BFS on map1: path = " + str(path) + "\n") file.write("BFS on map1: action_path = " + str(action_path) + "\n") file.write("BFS on map2: visited = " + str(visited) + "\n") g.display_map(path, visited, "Project-1_1_1.2_DFS on map1") file.close() import graph_search
def run_dfs(map_path): g = graph_search.GridMap(map_path) res = graph_search.dfs(g.init_pos, g.transition, g.is_goal, graph_search._ACTIONS) g.display_map(res[0][0], res[1])