diff --git a/Search_Algorithms/Heuristics.py b/Search_Algorithms/Heuristics.py index 1350d1e0f565a7aedd629ac6f26fac18d194d707..ec2ebf62dad123532f667c5bec6f913bbc6dff5c 100644 --- a/Search_Algorithms/Heuristics.py +++ b/Search_Algorithms/Heuristics.py @@ -2,6 +2,45 @@ import is_goal 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. + + Returns: + int: The heuristic value, which is the sum of the minimum moves for all queens. + """ + state = path[-1] # Extracting the current state from the path + n = len(state) # Size of the board (N x N) + total_moves = 0 + + for y in range(n): + for x in range(n): + if state[y][x] == 1: # If there's a queen at (y, x) + min_moves = n # Max possible moves + # Check for a safe spot in each column + for col in range(n): + if col != x: + conflict = False + # Check row and diagonals for conflict + for k in range(n): + if state[y][k] == 1 and k != x: + conflict = True + break + if y + (col - x) < n and y + (col - x) >= 0 and state[y + (col - x)][col] == 1: + conflict = True + break + if y - (col - x) < n and y - (col - x) >= 0 and state[y - (col - x)][col] == 1: + conflict = True + break + if not conflict: + min_moves = min(min_moves, abs(col - x)) + total_moves += min_moves + + return total_moves diff --git a/Search_Algorithms/__pycache__/Heuristics.cpython-311.pyc b/Search_Algorithms/__pycache__/Heuristics.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..3e2187da28fe6988c4e889be760f43a6d10b9340 Binary files /dev/null and b/Search_Algorithms/__pycache__/Heuristics.cpython-311.pyc differ diff --git a/Search_Algorithms/__pycache__/Search_Algorithms.cpython-311.pyc b/Search_Algorithms/__pycache__/Search_Algorithms.cpython-311.pyc index 324b9ebb3b744554f17659a85e3d7296e59c750e..7d99d25b16a36ce1e9bbd9ae1d0c2e57180074bb 100644 Binary files a/Search_Algorithms/__pycache__/Search_Algorithms.cpython-311.pyc and b/Search_Algorithms/__pycache__/Search_Algorithms.cpython-311.pyc differ diff --git a/Search_Algorithms/__pycache__/Sucessors.cpython-311.pyc b/Search_Algorithms/__pycache__/Sucessors.cpython-311.pyc index 65583285d0a03c9ebcb42aede0f6c20d30783620..0ea166c9a329969edcaded3aef280e16853e2255 100644 Binary files a/Search_Algorithms/__pycache__/Sucessors.cpython-311.pyc and b/Search_Algorithms/__pycache__/Sucessors.cpython-311.pyc differ diff --git a/Search_Algorithms/__pycache__/is_goal.cpython-311.pyc b/Search_Algorithms/__pycache__/is_goal.cpython-311.pyc index 75ad5ea3b3f97d945274f3e03bcf33ac0edb7069..212d690a6df9d909015a425c7ff6948e7d455c7d 100644 Binary files a/Search_Algorithms/__pycache__/is_goal.cpython-311.pyc and b/Search_Algorithms/__pycache__/is_goal.cpython-311.pyc differ diff --git a/Search_Algorithms/solution testing.py b/Search_Algorithms/solution testing.py index cb98f7f3ec2e89919763a5e2ddb8ba4e47c91e4d..60914e7b3715cef12b22e96cdf201ef2d40b052c 100644 --- a/Search_Algorithms/solution testing.py +++ b/Search_Algorithms/solution testing.py @@ -4,140 +4,168 @@ import is_goal import Heuristics import time -# =============================================================================================================== -# BREADTH FIRST SEARCH MAZE -# =============================================================================================================== -# Maze Problem, note, the maze is fixed in the allowed state. -print(Search_Algorithms.BreadthFirstSearch((4, 0), Sucessors.maze_successor, is_goal.is_goal_maze)) - -# Sudoku -grid = ((5, 3, 0, 0, 7, 0, 0, 0, 0), - (6, 0, 0, 1, 9, 5, 0, 0, 0), - (0, 9, 8, 0, 0, 0, 0, 6, 0), - (8, 0, 0, 0, 6, 0, 0, 0, 3), - (4, 0, 0, 8, 0, 3, 0, 0, 1), - (7, 0, 0, 0, 2, 0, 0, 0, 6), - (0, 6, 0, 0, 0, 0, 2, 8, 0), - (0, 0, 0, 4, 1, 9, 0, 0, 5), - (0, 0, 0, 0, 8, 0, 0, 0, 0)) - -# =============================================================================================================== -# BREADTH FIRST SEARCH SUDOKU -# =============================================================================================================== -sln = (Search_Algorithms.BreadthFirstSearch(grid, Sucessors.sudoku_successor, is_goal.is_goal_sudoku)[-1]) - -for rows in sln: - print(rows) - -# =============================================================================================================== -# DEPTH FIRST SEARCH SUDOKU -# =============================================================================================================== -sln = (Search_Algorithms.DepthFirstSearch( - grid, Sucessors.sudoku_successor, is_goal.is_goal_sudoku)[-1]) -# -for rows in sln: - print(rows) - +# # =============================================================================================================== +# # BREADTH FIRST SEARCH MAZE +# # =============================================================================================================== +# # Maze Problem, note, the maze is fixed in the allowed state. +# print(Search_Algorithms.BreadthFirstSearch( +# (4, 0), Sucessors.maze_successor, is_goal.is_goal_maze)) + +# # Sudoku +# grid = ((5, 3, 0, 0, 7, 0, 0, 0, 0), +# (6, 0, 0, 1, 9, 5, 0, 0, 0), +# (0, 9, 8, 0, 0, 0, 0, 6, 0), +# (8, 0, 0, 0, 6, 0, 0, 0, 3), +# (4, 0, 0, 8, 0, 3, 0, 0, 1), +# (7, 0, 0, 0, 2, 0, 0, 0, 6), +# (0, 6, 0, 0, 0, 0, 2, 8, 0), +# (0, 0, 0, 4, 1, 9, 0, 0, 5), +# (0, 0, 0, 0, 8, 0, 0, 0, 0)) -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)) +# # =============================================================================================================== +# # BREADTH FIRST SEARCH SUDOKU +# # =============================================================================================================== +# sln = (Search_Algorithms.BreadthFirstSearch( +# grid, Sucessors.sudoku_successor, is_goal.is_goal_sudoku)[-1]) -# =============================================================================================================== -# BREADTH FIRST SEARCH N QUEENS PROBLEM -# =============================================================================================================== -sln = (Search_Algorithms.BreadthFirstSearch(board, Sucessors.queens_successor, is_goal.is_goal_queens)[-1]) +# for rows in sln: +# print(rows) -output_tuple = tuple(tuple("Q" if value else "." for value in sln) - for sln in sln) -for rows in output_tuple: - print(rows) +# # =============================================================================================================== +# # DEPTH FIRST SEARCH SUDOKU +# # =============================================================================================================== +# sln = (Search_Algorithms.DepthFirstSearch( +# grid, Sucessors.sudoku_successor, is_goal.is_goal_sudoku)[-1]) +# # +# 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), +# (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)) +# # =============================================================================================================== +# # BREADTH FIRST SEARCH N QUEENS PROBLEM +# # =============================================================================================================== +# sln = (Search_Algorithms.BreadthFirstSearch( +# board, Sucessors.queens_successor, is_goal.is_goal_queens)[-1]) -# =============================================================================================================== -# BREADTH FIRST SEARCH PUZZLE PROBLEM -# =============================================================================================================== -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: +# print(rows) -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 +# # =============================================================================================================== +# sln = (Search_Algorithms.DepthFirstSearch( +# board, Sucessors.queens_successor, is_goal.is_goal_queens)[-1]) -# # Sliding Puzzle -initial_puzzle = ( - (7, 2, 4), - (5, 0, 6), - (8, 3, 1) -) -# =============================================================================================================== -# BREADTH FIRST SEARCH PUZZLE PROBLEM -# =============================================================================================================== -sln = (Search_Algorithms.BreadthFirstSearch(initial_puzzle, Sucessors.puzzle_successor, is_goal.is_goal_puzzle)) +# output_tuple = tuple(tuple("Q" if value else "." for value in sln) +# for sln in sln) +# for rows in output_tuple: +# print(rows) -if sln: - for rows in sln: - for r in rows: - print(r) -else: - print("No solution found") +# # # Sliding Puzzle +# initial_puzzle = ( +# (7, 2, 4), +# (5, 0, 6), +# (8, 3, 1) +# ) +# # =============================================================================================================== +# # BREADTH FIRST SEARCH PUZZLE PROBLEM +# # =============================================================================================================== +# sln = (Search_Algorithms.BreadthFirstSearch(initial_puzzle, +# Sucessors.puzzle_successor, is_goal.is_goal_puzzle)) -# =============================================================================================================== -# DEPTH FIRST SEARCH PUZZLE PROBLEM -# search was timed on Michael's computer and took 211 seconds, Please be patient -# =============================================================================================================== -# start_time = time.time() -sln = (Search_Algorithms.DepthFirstSearch(initial_puzzle, Sucessors.puzzle_successor, is_goal.is_goal_puzzle)[-1]) -# end_time = time.time() -# print(end_time - start_time) +# if sln: +# for rows in sln: +# for r in rows: +# print(r) +# else: +# print("No solution found") -if sln: - for rows in sln: - print(rows) -else: - print("No solution found") +# # =============================================================================================================== +# # DEPTH FIRST SEARCH PUZZLE PROBLEM +# # search was timed on Michael's computer and took 211 seconds, Please be patient +# # =============================================================================================================== +# # start_time = time.time() +# sln = (Search_Algorithms.DepthFirstSearch(initial_puzzle, +# Sucessors.puzzle_successor, is_goal.is_goal_puzzle)[-1]) +# # end_time = time.time() +# # print(end_time - start_time) -# =============================================================================================================== -# A* SEARCH MAZE PROBLEM -# =============================================================================================================== -print(Search_Algorithms.A_StarSearch((4, 0), Sucessors.maze_successor, is_goal.is_goal_maze, Heuristics.maze_opt)) +# if sln: +# for rows in sln: +# print(rows) +# else: +# print("No solution found") # # =============================================================================================================== -# # A* SEARCH SUDOKU +# # A* SEARCH MAZE PROBLEM # # =============================================================================================================== -sln = (Search_Algorithms.A_StarSearch(grid, Sucessors.sudoku_successor, - is_goal.is_goal_sudoku, Heuristics.sudoku_opt))[-1] +# print(Search_Algorithms.A_StarSearch((4, 0), Sucessors.maze_successor, +# is_goal.is_goal_maze, Heuristics.maze_opt)) -for rows in sln: - print(rows) +# # # =============================================================================================================== +# # # A* SEARCH SUDOKU +# # # =============================================================================================================== +# sln = (Search_Algorithms.A_StarSearch(grid, Sucessors.sudoku_successor, +# is_goal.is_goal_sudoku, Heuristics.sudoku_opt))[-1] -# ================================================================================================================= -# A* SEARCH PUZZLE -# ================================================================================================================= -sln = (Search_Algorithms.A_StarSearch(initial_puzzle, Sucessors.puzzle_successor, - is_goal.is_goal_puzzle, Heuristics.puzzle_opt))[-1] +# for rows in sln: +# print(rows) -if sln: - for rows in sln: - print(rows) -else: - print("No solution found") +# # ================================================================================================================= +# # A* SEARCH PUZZLE +# # ================================================================================================================= +# sln = (Search_Algorithms.A_StarSearch(initial_puzzle, Sucessors.puzzle_successor, +# is_goal.is_goal_puzzle, Heuristics.puzzle_opt))[-1] + +# if sln: +# for rows in sln: +# print(rows) +# else: +# print("No solution found") -# # =============================================================================================================== -# # A* SEARCH QUEENS -# # =============================================================================================================== +# # # =============================================================================================================== +# # # A* SEARCH QUEENS +# # # =============================================================================================================== + + + + + +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)) + +sln = (Search_Algorithms.A_StarSearch(board, Sucessors.queens_successor, + is_goal.is_goal_queens, Heuristics.queens_opt))[-1] + +output_tuple = tuple(tuple("Q" if value else "." for value in sln) + for sln in sln) +for rows in output_tuple: + print(rows) \ No newline at end of file