def cost_to_all_cells(filename, src_waypoint, output_filename): """ Loads a level, calculates the cost to all reachable cells from src_waypoint, then saves the result in a csv file with name output_filename. Args: filename: The name of the text file containing the level. src_waypoint: The character associated with the initial waypoint. output_filename: The filename for the output csv file. """ # Load and display the level. level = load_level(filename) show_level(level) # Retrieve the source coordinates from the level. src = level['waypoints'][src_waypoint] # Calculate the cost to all reachable cells from src and save to a csv file. costs_to_all_cells = dijkstras_shortest_path_to_all( src, level, navigation_edges) save_level_costs(level, costs_to_all_cells, output_filename)
def test_route(filename, src_waypoint, dst_waypoint, output_filename): """ Loads a level, searches for a path between the given waypoints, and displays the result. Args: filename: The name of the text file containing the level. src_waypoint: The character associated with the initial waypoint. dst_waypoint: The character associated with the destination waypoint. """ # Load and display the level. level = load_level(filename) show_level(level) # Retrieve the source and destination coordinates from the level. src = level['waypoints'][src_waypoint] dst = level['waypoints'][dst_waypoint] # Search for and display the path from src to dst. #haphazardly edited to output to file aswell as stdout path = dijkstras_shortest_path(src, dst, level, navigation_edges) if path: show_level(level, path) original_out = sys.stdout f = open(output_filename, "w+") sys.stdout = f show_level(level, path) sys.stdout = original_out f.close print("Saved file:", output_filename) else: print("No path possible!")
def write_path(filename, src_waypoint, dst_waypoint, ofname): """ Loads a level, searches for a path between the given waypoints, and displays the result. Args: filename: The name of the text file containing the level. src_waypoint: The character associated with the initial waypoint. dst_waypoint: The character associated with the destination waypoint. """ # Load and display the level. level = load_level(filename) #print("finding path from ", src_waypoint, " to ", dst_waypoint, " of this graph:") #show_level(level) #print("writing output to file: ", ofname) # Retrieve the source and destination coordinates from the level. src = level['waypoints'][src_waypoint] dst = level['waypoints'][dst_waypoint] #save stdout tempstdout = sys.stdout stdout = open(ofname, "w") # Search for and display the path from src to dst. path = dijkstras_shortest_path(src, dst, level, navigation_edges) if path: show_level(level, path) else: print("No path possible!") stdout = tempstdout
def test_route(filename, src_waypoint, dst_waypoint): """ Loads a level, searches for a path between the given waypoints, and displays the result. Args: filename: The name of the text file containing the level. src_waypoint: The character associated with the initial waypoint. dst_waypoint: The character associated with the destination waypoint. """ # Load and display the level. level = load_level(filename) show_level(level) # Retrieve the source and destination coordinates from the level. src = level['waypoints'][src_waypoint] # if ((dst_waypoint.isalnum())): # print("No destination!") # else: dst = level['waypoints'][dst_waypoint] # Search for and display the path from src to dst. path = dijkstras_shortest_path(src, dst, level, navigation_edges) if path: show_level(level, path) else: print("No path possible!")
def test_route(filename, src_waypoint, dst_waypoint): level = load_level(filename) src = level['waypoints'][src_waypoint] dst = level['waypoints'][dst_waypoint] path = dfs(src, dst, level, get_steps) show_level(level, path)
def test_route(filename, src_waypoint, dst_waypoint): level = load_level(filename) show_level(level) src = level['waypoints'][src_waypoint] dst = level['waypoints'][dst_waypoint] path = dijkstras_shortest_path(src, dst, level, navigation_edges) if path: show_level(level, path) else: print("No path possible!")
def test_route(filename, src_waypoint, dst_waypoint): level = load_level(filename) src = level['waypoints'][src_waypoint] dst = level['waypoints'][dst_waypoint] path = dijk(src, dst, level, get_steps) if len(path) is not 0: show_level(level, path) else: print("There is no path from " + src_waypoint + " to " + dst_waypoint + ".")
def test_route(filename, src_waypoint, dst_waypoint): level = load_level(filename) show_level(level) src = level['waypoints'][src_waypoint] dst = level['waypoints'][dst_waypoint] path = dijkstras_shortest_path(src, dst, level, navigation_edges) if path: show_level(level, path) else: print "No path possible!"
def test_route(filename, src_waypoint, dst_waypoint, keyFound): level = load_level(filename, keyFound) print("source = " + src_waypoint + ",", "destination = " + dst_waypoint + ",", "Key found =", keyFound, "\n") src = level['waypoints'][src_waypoint] dst = level['waypoints'][dst_waypoint] path = dijkstra(src, dst, level, get_steps) if path: show_level(level, path) else: show_level(level, path) print("No path possible!!!")
def test_route(filename, src_waypoint, dst_waypoint): #Level gets stored level = load_level(filename) #Level gets drawn on screen show_level(level) #src and dst are set. src = level['waypoints'][src_waypoint] dst = level['waypoints'][dst_waypoint] #Path is found. path = dijkstras_shortest_path(src, dst, level, navigation_edges) if path: show_level(level, path) else: print "No path possible!"
def export_new_maze(filename, destinationfile, src_waypoint, dst_waypoint): level = load_level(filename) show_level(level) # Retrieve the source and destination coordinates from the level. src = level['waypoints'][src_waypoint] dst = level['waypoints'][dst_waypoint] # Search for and display the path from src to dst. path = dijkstras_shortest_path(src, dst, level, navigation_edges) if path: show_level(level, path) with open(destinationfile, "w") as f: with redirect_stdout(f): show_level(level, path) print("Wrote to " + destinationfile) else: print("No path possible!")
def test_route(filename, src_waypoint, dst_waypoint): level = load_level(filename) if VERBOSE: print("Level layout:") show_level(level) src = level['waypoints'][src_waypoint] dst = level['waypoints'][dst_waypoint] path = dijkstras_shortest_path(src, dst, level, navigation_edges) if path: show_level(level, path) else: print "No path possible!" # Show the level if the user hasn't already seen it if not VERBOSE: show_level(level, [])
def cost_to_all_cells(filename, src_waypoint, output_filename): """ Loads a level, calculates the cost to all reachable cells from src_waypoint, then saves the result in a csv file with name output_filename. Args: filename: The name of the text file containing the level. src_waypoint: The character associated with the initial waypoint. output_filename: The filename for the output csv file. """ # Load and display the level. level = load_level(filename) show_level(level) # Retrieve the source coordinates from the level. src = level['waypoints'][src_waypoint] # Calculate the cost to all reachable cells from src and save to a csv file. costs_to_all_cells = dijkstras_shortest_path_to_all(src, level, navigation_edges) save_level_costs(level, costs_to_all_cells, output_filename)
def __init__(self, filename): self.filename = filename self.level = load_level(filename)
# Calculate the cost to all reachable cells from src and save to a csv file. costs_to_all_cells = dijkstras_shortest_path_to_all( src, level, navigation_edges) save_level_costs(level, costs_to_all_cells, output_filename) def get_cell_type(level, coord): # finds the cell type of the particular coordinate if coord in level["walls"]: return "walls" elif coord in level[SPACES]: return SPACES return "invalid coord or level" if __name__ == '__main__': filename, src_waypoint, dst_waypoint = 'my_maze.txt', 'a', 'd' #file_directory = "../input/" level = load_level(filename) # Use this function call to find the route between two waypoints. test_route(filename, src_waypoint, dst_waypoint) # Use this function to calculate the cost to all reachable cells from an origin point. cost_to_all_cells(filename, src_waypoint, 'my_maze_costs.csv')
costs_to_all_cells = dijkstras_shortest_path_to_all( src, level, navigation_edges) save_level_costs(level, costs_to_all_cells, output_filename) if __name__ == '__main__': filename, src_waypoint, dst_waypoint = 'example.txt', 'a', 'e' # Use this function call to find the route between two waypoints. test_route(filename, src_waypoint, dst_waypoint) # Use this function to calculate the cost to all reachable cells from an origin point. cost_to_all_cells(filename, src_waypoint, 'my_costs.csv') f = open("test_maze_path.txt", "w+") level = load_level("test_maze.txt") # we also did the path with a* star #dijkstras_shortest_path_a_star(level['waypoints']['a'], level['waypoints']['d'], level, navigation_edges) the_path = dijkstras_shortest_path(level['waypoints']['a'], level['waypoints']['d'], level, navigation_edges) path = [] for coord in the_path: path.append(coord) xs, ys = zip(*(list(level['spaces'].keys()) + list(level['walls']))) x_lo, x_hi = min(xs), max(xs) y_lo, y_hi = min(ys), max(ys) path_cells = set(path)