def start_solving(start_state, goal_state, verbose=False): solver = AStar(start_state, goal_state, verbose) solution = solver.solve() if solution: print("Total movements: ", solution.cost) print("Steps:") ll = solution.get_path() for idx, x in enumerate(reversed(ll)): print("#{}".format(idx)) pretty_print_state(x.state)
def solve(self): super().solve() solved = False current_node = None i = 0 while not self.queue.is_empty(): current_node = self.queue.pop() current_node.set_cost(current_node.distance) if self.verbose: print() print("Iteration: ", i) print("empty", self.queue.is_empty()) print("Current state: ") pretty_print_state(current_node.state) print("Visited: ", self.visited_state) print("Queue: ") self.queue.print() self.visited_state.add(current_node.state) if is_goal(current_node.state, self.goal_state): solved = True if self.verbose: print("Found: ", current_node.state) break expansion = [] for adj in current_node.expand(): if adj in self.visited_state: continue expanding_node = StateNode(adj, self.goal_state, depth=current_node.depth + 1, parent=current_node) expanding_node.set_cost(expanding_node.distance + expanding_node.depth) expansion.append(expanding_node) self.queue.add(expanding_node) i += 1 if solved: return Solution(current_node) raise Exception("Error can not solve the puzzle")