def a_star_shortest_path(start_node, dest_node): """ Test AStar's shortest path in comparison to networkx's shortest_path. Parameters: ----------- start_node: networkx Node The starting node. dest_node: networkx Node The ending node. Passing Criteria: -------- Both path lengths must be the same. """ context = Context(strategies.StrategyAStar(graph, 0, 'vanilla')) path = context.run_strategy_route(start_node, dest_node) print("A star shortest path", path) regular_path_length = graph_utils.get_path_length(graph, path) nx_shortest_path = shortest_path(start_node, dest_node) nx_shortest_path_length = graph_utils.get_path_length( graph, nx_shortest_path) if nx_shortest_path_length == regular_path_length: print("Test Passed") else: print("Test Failed") print("\n")
def a_star_max_elevation_grade(start_node, dest_node): """ Test AStar's min elevation path using grade as the weight. Parameters: ----------- start_node: networkx Node The starting node. dest_node: networkx Node The ending node. Passing Criteria: -------- max_elevation path must have the smaller path length than the limit and grade is less than regular grade. """ print("A star max elevation using grade as weight") context = Context(strategies.StrategyAStar(graph, limit, 'max grade')) path = context.run_strategy_route(start_node, dest_node) print("Elevation path", path) grade = graph_utils.get_average_grade(graph, path) max_path_length = graph_utils.get_path_length(graph, path) context = Context(strategies.StrategyAStar(graph, 0, 'vanilla')) path = context.run_strategy_route(start_node, dest_node) print("Vanilla path", path) regular_grade = graph_utils.get_average_grade(graph, path) regular_path_length = graph_utils.get_path_length(graph, path) max_length = regular_path_length * (1 + limit) print("Vanilla path length:", regular_path_length, "\nMax Possible Path length:", max_length, "\nLength of elevation path:", max_path_length) print("Vanilla path Grade:", round(regular_grade, 2), "\nNew Grade:", round(grade, 2)) if max_path_length <= max_length: if grade >= regular_grade: print("Test Passed") else: print("Test Passed: Path length. A* wasn't able to maximize grade") else: print("Test Failed") print("\n")
def a_star_max_elevation(start_node, dest_node): """ Test AStar's max elevation path using elevation as the weight. Parameters: ----------- start_node: networkx Node The starting node. dest_node: networkx Node The ending node. Passing Criteria: -------- max_elevation path must have the smaller path length than the limit and max_elevation is greater than regular elevation. """ print("A star max elevation using elevation change as weight") context = Context( strategies.StrategyAStar(graph, limit, 'max elevation_change')) path = context.run_strategy_route(start_node, dest_node) print("Elevation path", path) elevation = graph_utils.get_path_elevation(graph, path) max_path_length = graph_utils.get_path_length(graph, path) context = Context(strategies.StrategyAStar(graph, 0, 'vanilla')) path = context.run_strategy_route(start_node, dest_node) print("Vanilla path", path) regular_elevation = graph_utils.get_path_elevation(graph, path) regular_path_length = graph_utils.get_path_length(graph, path) max_length = regular_path_length * (1 + limit) print("Vanilla path length:", regular_path_length, "\nMax Possible Path length:", max_length, "\nLength of elevation path:", max_path_length) print("Vanilla path Elevation:", regular_elevation, "\nNew Elevation:", elevation) if max_path_length <= max_length and elevation >= regular_elevation: print("Test Passed") else: print("Test Failed") print("\n")
def all_shortest_path(start, dest): """ Run all the shortest path algorithms: BFS, Dijkstra, A* Parameters: ----------- start: String The starting address. dest: String The ending address. Returns: -------- times: An array of the time (as float values) it took each algorithm to run. """ try: start_node = get_node_from_address(graph, start) dest_node = get_node_from_address(graph, dest) except Exception as e: print(e) print("Could not map address to node") times = [] print("Running Dijkstra") context = Context(strategies.StrategyDijkstra(graph, 0, 'vanilla')) start_time = time() path = context.run_strategy_route(start_node, dest_node) end_time = time() times.append(end_time - start_time) print("Running BFS") context = Context(strategies.StrategyBFS(graph, 0, 'vanilla')) start_time = time() path = context.run_strategy_route(start_node, dest_node) end_time = time() times.append(end_time - start_time) print("Running A*") context = Context(strategies.StrategyAStar(graph, 0, 'vanilla')) start_time = time() path = context.run_strategy_route(start_node, dest_node) end_time = time() times.append(end_time - start_time) return times
def max_elevation_path_performance(start, dest, method): """ Run a performance test on the maximum elevation path of Dijkstra and A*. Parameters: ----------- start: String The starting address. dest: String The ending address. method: String The method to use: Maximize Elevation Gain or Maximize Steepness. Returns: -------- times: An array of the time (as float values) it took each algorithm to run. """ try: start_node = get_node_from_address(graph, start) dest_node = get_node_from_address(graph, dest) except Exception as e: print(e) print("Could not map address to node") times = [] print("Running Dijkstra") context = Context(strategies.StrategyDijkstra(graph, 0, method)) start_time = time() path = context.run_strategy_route(start_node, dest_node) end_time = time() times.append(end_time - start_time) print("Running A*") context = Context(strategies.StrategyAStar(graph, 0, method)) start_time = time() path = context.run_strategy_route(start_node, dest_node) end_time = time() times.append(end_time - start_time) return times