diff --git a/Search_Algorithms/Heuristics.py b/Search_Algorithms/Heuristics.py index ec2ebf62dad123532f667c5bec6f913bbc6dff5c..6351cccdedee3ca08a88b2e58b5a614e40e19679 100644 --- a/Search_Algorithms/Heuristics.py +++ b/Search_Algorithms/Heuristics.py @@ -3,10 +3,11 @@ import Sucessors def queens_opt(path): + """ Heuristic for the N-Queens problem that calculates the minimum number of moves required to move each queen to a position where it is not in conflict with any other queen. - + Args: path (list): The path taken so far, where the last element is the current state of the N-Queens board. @@ -27,7 +28,7 @@ def queens_opt(path): conflict = False # Check row and diagonals for conflict for k in range(n): - if state[y][k] == 1 and k != x: + if state[y][k] and k != x: conflict = True break if y + (col - x) < n and y + (col - x) >= 0 and state[y + (col - x)][col] == 1: @@ -43,12 +44,10 @@ def queens_opt(path): return total_moves - - def maze_opt(path): """maze search heuristic going to have to use the euclidian distance, so it works for any maze""" state = path[-1] - return (is_goal.MAZE_GOAL[0] - state[0])**2 + (is_goal.MAZE_GOAL[1] - state[1])**2 + return (is_goal.MAZE_GOAL[0] - state[0]) ** 2 + (is_goal.MAZE_GOAL[1] - state[1]) ** 2 def puzzle_opt(path): @@ -65,7 +64,3 @@ def sudoku_opt(path): Sucessors.choose_best_column(path[-1])] return max(starting_point, key=lambda w: w[2])[-1] - -def queens_opt(path): - pass - diff --git a/Search_Algorithms/solution testing.py b/Search_Algorithms/solution testing.py index 60914e7b3715cef12b22e96cdf201ef2d40b052c..88e09d4759589ec711a09930dce9c4bec99728fc 100644 --- a/Search_Algorithms/solution testing.py +++ b/Search_Algorithms/solution testing.py @@ -40,7 +40,7 @@ import time # for rows in sln: # print(rows) - +# # board = ((False, False, False, False, False, False, False, False, False, False), # (False, False, False, False, False, False, False, False, False, False), # (False, False, False, False, False, False, False, False, False, False), @@ -57,19 +57,19 @@ import time # # =============================================================================================================== # sln = (Search_Algorithms.BreadthFirstSearch( # board, Sucessors.queens_successor, is_goal.is_goal_queens)[-1]) - +# # output_tuple = tuple(tuple("Q" if value else "." for value in sln) # for sln in sln) # for rows in output_tuple: # print(rows) -# # =============================================================================================================== -# # BREADTH FIRST SEARCH PUZZLE PROBLEM -# # =============================================================================================================== +# =============================================================================================================== +# DEPTH FIRST SEARCH N-QUEENS +# =============================================================================================================== # sln = (Search_Algorithms.DepthFirstSearch( # board, Sucessors.queens_successor, is_goal.is_goal_queens)[-1]) - +# # output_tuple = tuple(tuple("Q" if value else "." for value in sln) # for sln in sln) # for rows in output_tuple: @@ -151,20 +151,15 @@ import time -board = ((False, False, False, False, False, False, False, False, False, False), - (False, False, False, False, False, False, False, False, False, False), - (False, False, False, False, False, False, False, False, False, False), - (False, False, False, False, False, False, False, False, False, False), - (False, False, False, False, False, False, False, False, False, False), - (False, False, False, False, False, False, False, False, False, False), - (False, False, False, False, False, False, False, False, False, False), - (False, False, False, False, False, False, False, False, False, False), - (False, False, False, False, False, False, False, False, False, False), - (False, False, False, False, False, False, False, False, False, False)) +board = ((False, False, False, False), + (False, False, False, False), + (False, False, False, False), + (False, False, False, False)) sln = (Search_Algorithms.A_StarSearch(board, Sucessors.queens_successor, is_goal.is_goal_queens, Heuristics.queens_opt))[-1] +print(sln) output_tuple = tuple(tuple("Q" if value else "." for value in sln) for sln in sln) for rows in output_tuple: