def compute_path(labyrinth): children_gen = lab_module.NeighborsGeneratorJPS(labyrinth) heur_goal = lab_module.heur_diag(labyrinth.goal, labyrinth.start) eq_to_goal = lambda p: p == labyrinth.goal cur_time = time.perf_counter() path, *_, info = searching.a_star(labyrinth.start, eq_to_goal, heur_goal, children_gen) cur_time = time.perf_counter() - cur_time if path: path = reconstruct_path(path, labyrinth) return path, cur_time, info
def compute(self): if not self.labyrinth.start or not self.labyrinth.goal: raise ValueError("Start or goal not found") self.children_gen = NeighborsGeneratorJPS(self.labyrinth) self.heur = heur_diag self.heur_goal = self.heur(self.labyrinth.goal, self.labyrinth.start) self.max_heur = int(self.heur_goal(self.labyrinth.start)) print("Start detected:\t{}".format(self.labyrinth.start)) print("Goal detected:\t{}".format(self.labyrinth.goal)) print("Starting search...") eq_to_goal = lambda p: np.array_equal(p, self.labyrinth.goal) c_time = time.perf_counter() path, *_, info = a_star(self.labyrinth.start, eq_to_goal, self.heur_goal, self.children_gen, self.gui_callback) c_time = (time.perf_counter() - c_time) if path: path, cost = self.reconstruct_path(path) else: cost = float("inf") # print(path) print("Search ended") print("Time:", round(c_time, 2), "s") print("Nodes searched:", info.nodes) print("Maximum list size:", info.maxl) print("Path cost:", cost) if path is None: print("Path not found") else: print("Found path of", len(path), "nodes") print("Path generation completed") print("*" * 100) if PATH_DELAY_TIME: for i in range(len(path)): self.gui_callback(None, path[:i]) time.sleep(PATH_DELAY_TIME) else: self.gui_callback(None, path)
def test_a_star(self): """Test and visualize A* search""" start = 'a' goal = 'u' node_positions = { n: self.romania.node[n]['pos'] for n in self.romania.node.keys() } self.romania.reset_search() path = a_star(self.romania, start, goal) self.draw_graph(self.romania, node_positions=node_positions, start=start, goal=goal, path=path)