Skip to content
Snippets Groups Projects
Commit a4ee4776 authored by Michael Mutote's avatar Michael Mutote
Browse files

22202956 - touch up on testing functions

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