def __calc_route__(self): node_dict = {} map_dict = {} for item in self.ms.all_doors(): node = Node(item) node_dict[item] = node if not item.map_name in map_dict: map_dict[item.map_name] = [] map_dict[item.map_name].append(node) from_node = Node(wx.GetApp().GetNavFrom()) map_dict[wx.GetApp().GetNavFrom().map_name].append(from_node) to_node = Node(wx.GetApp().GetNavTo()) map_dict[wx.GetApp().GetNavTo().map_name].append(to_node) #beam beam_node = Node(self.ms.beam_location()) log.debug("beam_node: %s" % beam_node) from_node.edges.append((1, beam_node)) map_dict[beam_node.payload.map_name].append(beam_node) for map_name, map_nodes in map_dict.items(): for index, item_a in enumerate(map_nodes): for item_b in map_nodes[index + 1:]: cost = self.ds.in_map_distance(item_a.payload, item_b.payload) add_edge(cost, item_a, item_b) for item, node in node_dict.items(): other_item = self.ms.other_side(item) cost = self.ds.cost(item, other_item) cost = 1 node.edges.append((cost, node_dict[other_item])) node_dict[wx.GetApp().GetNavFrom()] = from_node node_dict[wx.GetApp().GetNavTo()] = to_node node_dict[self.ms.beam_location()] = beam_node nodes = node_dict.values() for item in nodes: log.debug(item) for item_b in item.edges: log.debug(" %s" % str(item_b)) from_node.cost = 0 solve(nodes) for item in nodes: log.debug(item) log.debug("ROUTE:") self.__route__ = get_route(nodes, node_dict[wx.GetApp().GetNavTo()])
def createsolver(kind, pq_impl=None): if kind == "leftturn": import leftturn return ["Left turn only", leftturn.solve] elif kind == "depthfirst": import depthfirst return ["Depth first search", depthfirst.solve] elif kind == "dijkstra": import dijkstra description = "Dijkstra's Algorithm" pq = None if pq_impl: pq = priority_queue(pq_impl) description += " (using %s)" % pq_impl else: pq = priority_queue(DefaultPriorityQueue) description += " (using default %s)" % DefaultPriorityQueue return [description, lambda maze: dijkstra.solve(maze, pq)] elif kind == "astar": import astar description = "A-star Search" pq = None if pq_impl: pq = priority_queue(pq_impl) description += " (using %s)" % pq_impl else: pq = priority_queue(DefaultPriorityQueue) description += " (using default %s)" % DefaultPriorityQueue return [description, lambda maze: astar.solve(maze, pq)] else: import breadthfirst return ["Breadth first search", breadthfirst.solve]
def main(): pygame.init() win = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) clock = pygame.time.Clock() grid = Grid(WINDOW_WIDTH, WINDOW_HEIGHT, BLOCK_SIZE) grid.initGrid() initial = grid.getRandomCell() initial.startFlag = True initial.distance = 0 destination = grid.getRandomCell() destination.destFlag = True current = initial grid.makeNoise() running = True wallLoop = True motion = False while running: clock.tick(60) # optional setting walls before path finding while wallLoop: drawDisplay(win, grid.getCellList()) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() running = False elif event.type == pygame.MOUSEBUTTONDOWN: motion = True elif event.type == pygame.MOUSEBUTTONUP: motion = False elif event.type == pygame.MOUSEMOTION: if motion: mouseX, mouseY = pygame.mouse.get_pos() grid.cellList[grid.getIndex( int(mouseX / BLOCK_SIZE), int(mouseY / BLOCK_SIZE))].setWallFlag() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: wallLoop = False for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() running = False # end condition if destination.visited: print(destination.distance) drawPath(win, getPath(destination)) running = False # use dijkstra algorithm to get next cell current, grid.cellList = d.solve(current, grid) drawDisplay(win, grid.getCellList())
while length < 27: path_option = goal_state.get_actions() opt = [ initial_state.get_manhattan_distance(goal_state.apply_action(act)) for act in path_option ] if np.random.rand(1)[0] <= 0.1: goal_state = goal_state.apply_action( np.random.permutation(path_option)[0]) else: goal_state = goal_state.apply_action(path_option[np.argmax(opt)]) puzzle = Puzzle(initial_state, goal_state) length = len(As.solve(puzzle)[0]) - 1 print(goal_state.to_string()) solution_start_time = datetime.datetime.now() [plane_A_star, states_vis_A] = As.solve(puzzle) print('time to solve A_star{}'.format(datetime.datetime.now() - solution_start_time)) print('length of plan A_star {}'.format(len(plane_A_star) - 1)) print('states visited A_star {}'.format(states_vis_A)) print("----------------------------") solution_start_time = datetime.datetime.now() [plane_dijkstra, states_vis_dijkstra] = dij.solve(puzzle) print('time to solve dijkstra {}'.format(datetime.datetime.now() - solution_start_time)) print('length of plan dijkstra {}'.format(len(plane_dijkstra) - 1)) print('states visited dijkstra {}'.format(states_vis_dijkstra))
near_locations.sort() print "\nnear_locations" pprint(near_locations) where_am_i = near_locations[0][1] print "\nnear_location: %s" % where_am_i #~ where_am_i = find_location_obj(ex.map_name, ex.loc) #~ where_am_i = find_location_obj('White Stone', (707,162)) #~ where_am_i = find_location_obj('Desert Pines', (166,100)) #sto #~ where_am_i = find_location_obj('Nordcarn', (51,184)) #~ where_am_i = find_location_obj('Desert Pines', (44,302)) #~ where_am_i = find_location_obj('Desert Pines', (172,12)) #South Exit to Portland where_am_i.cost = 0 solve(all_nodes) #~ for item in all_nodes: #~ print item #~ destination_location = find_location_obj('Desert Pines', (44,302)) #~ destination_location = find_location_obj('Nordcarn', (51,184)) destination_location = find_location_obj('Desert Pines', (166,100)) #sto print "\nRoute" for item in get_route(all_nodes, destination_location): print item #~ print beam_location_xml