def main(): """Use this program to compare and contrast four graph searching algorithms.""" map_40 = load_map_40() map_10 = load_map_10() map_simple = load_map_simple() algorithms = [a_star, uniform_cost, breath_first, depth_first] # The loop below will execute the same search using each of the four algorithms. # Experiment with different maps and searches to understand the attributes, # performance characteristics, and code of each algorithm. graph = map_40 start_node = 5 goal_node = 34 print(f"Nodes in Map: {len(graph.intersections)} \n") for algo in algorithms: method = algo.SearchAlgo(graph, start_node, goal_node) method.search() print(f"Algorithm: {method.algorithm_name}") print(f"Path: {method.path}") print(f"Path length: {method.path_length}") print(f"Number of nodes visited: {len(method.visited)} ({len(method.visited) / len(method.map.intersections) * 100}%)") print(f"Nodes visited:", ', '.join(map(str, method.visited))) print("")
def test(shortest_path_function): map_40 = load_map_40() correct = 0 for start, goal, answer_path in MAP_40_ANSWERS: path = shortest_path_function(map_40, start, goal).path if path == answer_path: correct += 1 else: print("For start:", start, "Goal: ", goal, "Your path:", path, "Correct: ", answer_path) if correct == len(MAP_40_ANSWERS): print("All tests pass! Congratulations!") else: print("You passed", correct, "/", len(MAP_40_ANSWERS), "test cases")
def main(): # interactive test map_10 = load_map_10() map_40 = load_map_40() print(f"Testing path planner...") start_node = 5 goal_node = 34 planner = PathPlanner(map_40, start_node, goal_node) path = planner.path print(f"Start Node: {start_node}") print(f"Goal Node: {goal_node}") print(f"Resulting path: {path}") if path == [5, 16, 37, 12, 34]: print(f"Great! Your code works for these inputs!\n") else: print("Something is off, your code produced the following:") print(path, "\n") # course test for validation of project from test import test print(f"Running project validation tests from Udacity...") test(PathPlanner)
from helpers import load_map_40 from student_code import shortest_path from test import test map_40 = load_map_40() path = shortest_path(map_40, 8, 24) if path == [8, 14, 16, 37, 12, 17, 10, 24]: print("great! Your code works for these inputs!") else: print("something is off, your code produced the following:") print(path) test(shortest_path)
y2 = intersections[node_2][1] return math.sqrt((x1 - x2)**2 + (y1 - y2)**2) def get_tentative_gScore(self, current, neighbor): """Returns the tentative g Score of a node""" return self.get_gScore(current) + self.distance(current, neighbor) def heuristic_cost_estimate(self, node): """ Returns the heuristic cost estimate of a node """ return self.distance(node, self.goal) def calculate_fscore(self, node): """Calculate the f score of a node. """ return self.get_gScore(node) + self.heuristic_cost_estimate(node) def record_best_path_to(self, current, neighbor): """Record the best path to a node """ self.cameFrom[neighbor] = current self.gScore[neighbor] = self.get_tentative_gScore(current, neighbor) self.fScore[neighbor] = self.calculate_fscore(neighbor) ## Calculates shortest route from start to goal # Modifiy start and goal to see different results! start = 1 goal = 8 show_map(load_map_40(), start=start, goal=goal, path=PathPlanner(load_map_40(), start, goal).path)