Skip to content
Snippets Groups Projects
Commit 5d796d50 authored by tnbeats's avatar tnbeats
Browse files

22211572

parent 68c2830b
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
File added
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment