Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AI Progamming Exercises
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michael Mutote
AI Progamming Exercises
Commits
b878bbe2
Commit
b878bbe2
authored
1 year ago
by
Michael Mutote
Browse files
Options
Downloads
Patches
Plain Diff
22202956 - touch up on heuristics, N-QUEENS
parent
a4ee4776
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Search_Algorithms/Heuristics.py
+6
-37
6 additions, 37 deletions
Search_Algorithms/Heuristics.py
Search_Algorithms/solution testing.py
+47
-36
47 additions, 36 deletions
Search_Algorithms/solution testing.py
with
53 additions
and
73 deletions
Search_Algorithms/Heuristics.py
+
6
−
37
View file @
b878bbe2
...
...
@@ -3,45 +3,13 @@ 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
]
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
current_queens
=
0
# Count the number of outstanding Queens
for
r
in
state
:
current_queens
=
current_queens
+
r
.
count
(
True
)
return
n
-
current_queens
def
maze_opt
(
path
):
...
...
@@ -64,3 +32,4 @@ def sudoku_opt(path):
Sucessors
.
choose_best_column
(
path
[
-
1
])]
return
max
(
starting_point
,
key
=
lambda
w
:
w
[
2
])[
-
1
]
This diff is collapsed.
Click to expand it.
Search_Algorithms/solution testing.py
+
47
−
36
View file @
b878bbe2
...
...
@@ -41,40 +41,45 @@ import time
# 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),
#
(
Fals
e, False, False, False, False, False, False, False, False, False),
#
(False, False, False, False, False, False, False, False, False, False),
#
(False, False, False, False, False, False, False, False, False, False),
#
(False, False, False, False, False, False, False, False, False, False),
#
(False, False, False, False, False, False, False, False, False,
Fals
e))
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
),
(
Tru
e
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
Tru
e
))
# # ===============================================================================================================
# # BREADTH FIRST SEARCH N QUEENS PROBLEM
# # ===============================================================================================================
# 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 N QUEENS PROBLEM
# ===============================================================================================================
start_time
=
time
.
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
)
end_time
=
time
.
time
()
print
(
end_time
-
start_time
)
# ===============================================================================================================
# 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:
# print(rows)
start_time
=
time
.
time
()
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
)
end_time
=
time
.
time
()
print
(
end_time
-
start_time
)
# # # Sliding Puzzle
# initial_puzzle = (
...
...
@@ -147,20 +152,26 @@ import time
# # # 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
),
(
True
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
False
,
True
))
board
=
((
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
),
(
False
,
False
,
False
,
False
))
start_time
=
time
.
time
()
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
:
print
(
rows
)
\ No newline at end of file
print
(
rows
)
end_time
=
time
.
time
()
print
(
end_time
-
start_time
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment