def solve(self, starting_board: Board) -> any: self.open_queue: PriorityQueue = PriorityQueue() self.open_dict = dict() self.closed_list = [] self.closed_dict = dict() start_time = time.time() current_board = starting_board current_board.parent = None h = self.heuristic_func(current_board) g = current_board.g current_f = g + h current_board.h = h current_board.f = current_f self.open_queue.put((current_f, current_board)) self.open_dict[current_board] = (g, h) goal_1 = get_goal_1(rows=starting_board.rows, columns=starting_board.columns) goal_2 = get_goal_2(rows=starting_board.rows, columns=starting_board.columns) while not (current_board.equals(goal_1) or current_board.equals(goal_2)): if (time.time() - start_time) > self.time_out: return None current_board = self.search(current_board) if current_board is None: return None current_board.open_length_solved = len(self.open_dict) current_board.closed_length_solved = len(self.closed_list) return current_board
def solve(self, starting_board: Board) -> any: self.open_queue: PriorityQueue = PriorityQueue() self.open_set = set() self.closed_list = [] self.closed_set = set() start_time = time.time() current_board = starting_board current_board.parent = None h = self.heuristic_func(current_board) current_board.h = h self.open_queue.put((h, time.time(), current_board)) self.open_set.add(current_board) # if h(n) are equal, arbitrary selection instead of comparing # cost (here, we use time as that arbitrary comparator) goal_1 = get_goal_1(rows=starting_board.rows, columns=starting_board.columns) goal_2 = get_goal_2(rows=starting_board.rows, columns=starting_board.columns) while not (current_board.equals(goal_1) or current_board.equals(goal_2)): if (time.time() - start_time) > self.time_out: return None current_board = self.search(current_board) if current_board is None: return None current_board.open_length_solved = len(self.open_set) current_board.closed_length_solved = len(self.closed_list) return current_board
def h2(board: Board) -> int: manhattan_distance_goal_1 = modified_manhattan_distance( board, get_goal_1(rows=board.rows, columns=board.columns)) manhattan_distance_goal_2 = modified_manhattan_distance( board, get_goal_2(rows=board.rows, columns=board.columns)) if manhattan_distance_goal_1 <= manhattan_distance_goal_2: return manhattan_distance_goal_1 return manhattan_distance_goal_2
def h1(board: Board) -> int: hamming_distance_goal_1 = hamming_distance( board, get_goal_1(rows=board.rows, columns=board.columns)) hamming_distance_goal_2 = hamming_distance( board, get_goal_2(rows=board.rows, columns=board.columns)) if hamming_distance_goal_1 <= hamming_distance_goal_2: return hamming_distance_goal_1 return hamming_distance_goal_2