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
c80ce556
Commit
c80ce556
authored
1 year ago
by
Michael Mutote
Browse files
Options
Downloads
Patches
Plain Diff
22202956 - A* SEARCH PUZZLE
parent
7a8d0032
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
4_9_Exercises/Question 3.py
+22
-2
22 additions, 2 deletions
4_9_Exercises/Question 3.py
Search_Algorithms/Heuristics.py
+6
-1
6 additions, 1 deletion
Search_Algorithms/Heuristics.py
Search_Algorithms/solution testing.py
+68
-18
68 additions, 18 deletions
Search_Algorithms/solution testing.py
with
96 additions
and
21 deletions
4_9_Exercises/Question 3.py
+
22
−
2
View file @
c80ce556
def
nsp
(
x
,
y
):
"""
had to be redone with back tracking although recursion is better down there
"""
trace
=
[[(
0
,
0
)]]
sln
=
[]
while
trace
:
current
=
trace
.
pop
(
-
1
)
last_position
=
current
[
-
1
]
if
last_position
==
(
x
,
y
):
sln
.
append
(
current
)
else
:
up
=
(
last_position
[
0
]
+
1
,
last_position
[
1
])
right
=
(
last_position
[
0
],
last_position
[
1
]
+
1
)
if
(
up
not
in
current
)
and
((
up
[
0
]
<=
x
)
and
(
up
[
1
]
<=
y
)):
trace
.
append
(
current
+
[
up
])
if
(
right
not
in
current
)
and
((
right
[
0
]
<=
x
)
and
(
right
[
1
]
<=
y
)):
trace
.
append
(
current
+
[
right
])
return
len
(
sln
)
def
nsp2
(
x
,
y
):
"""
Same solution but with recursion and shorter
"""
if
x
<
0
or
y
<
0
:
return
0
if
(
0
,
0
)
==
(
x
,
y
):
return
1
else
:
return
nsp
(
x
,
y
-
1
)
+
nsp
(
x
-
1
,
y
)
return
nsp2
(
x
,
y
-
1
)
+
nsp2
(
x
-
1
,
y
)
This diff is collapsed.
Click to expand it.
Search_Algorithms/Heuristics.py
+
6
−
1
View file @
c80ce556
...
...
@@ -13,7 +13,12 @@ def maze_opt(path):
def
puzzle_opt
(
path
):
pass
current_state
=
path
[
-
1
]
total
=
0
for
i
in
range
(
len
(
current_state
)):
for
j
in
range
(
len
(
current_state
[
i
])):
total
=
total
+
abs
(
is_goal
.
PUZZLE_GOAL
[
i
][
j
]
-
current_state
[
i
][
j
])
return
total
def
sudoku_opt
(
path
):
...
...
This diff is collapsed.
Click to expand it.
Search_Algorithms/solution testing.py
+
68
−
18
View file @
c80ce556
...
...
@@ -2,8 +2,11 @@ import Search_Algorithms
import
Sucessors
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
))
...
...
@@ -18,18 +21,24 @@ grid = ((5, 3, 0, 0, 7, 0, 0, 0, 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
)
print
(
"
--------------------------------
"
)
# ===============================================================================================================
# DEPTH FIRST SEARCH SUDOKU
# ===============================================================================================================
sln
=
(
Search_Algorithms
.
DepthFirstSearch
(
grid
,
Sucessors
.
sudoku_successor
,
is_goal
.
is_goal_sudoku
)[
-
1
])
#
for
rows
in
sln
:
print
(
rows
)
# N 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
),
...
...
@@ -41,11 +50,22 @@ 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
))
# ===============================================================================================================
# BREADTH FIRST SEARCH N QUEENS PROBLEM
# ===============================================================================================================
sln
=
(
Search_Algorithms
.
BreadthFirstSearch
(
board
,
Sucessors
.
queens_successor
,
is_goal
.
is_goal_queens
)[
-
1
])
# 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
)
# ===============================================================================================================
# 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
)
...
...
@@ -53,14 +73,33 @@ for rows in output_tuple:
print
(
rows
)
# Sliding Puzzle
#
# 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
))
if
sln
:
for
rows
in
sln
:
for
r
in
rows
:
print
(
r
)
else
:
print
(
"
No solution found
"
)
sln
=
(
Search_Algorithms
.
BreadthFirstSearch
(
initial_puzzle
,
Sucessors
.
puzzle_successor
,
is_goal
.
is_goal_puzzle
)[
-
1
])
# ===============================================================================================================
# 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
:
...
...
@@ -68,17 +107,16 @@ if sln:
else
:
print
(
"
No solution found
"
)
#
# sln = (Search_Algorithms.DepthFirstSearch(initial_puzzle, Sucessors.puzzle_successor, is_goal.is_goal_puzzle)[-1])
#
# if sln:
# for rows in sln:
# print(rows)
# else:
# print("No solution found")
# print(Search_Algorithms.A_StarSearch((4, 0), Sucessors.maze_successor, is_goal.is_goal_maze, Heuristics.maze_opt))
# ===============================================================================================================
# A* SEARCH MAZE PROBLEM
# ===============================================================================================================
print
(
Search_Algorithms
.
A_StarSearch
((
4
,
0
),
Sucessors
.
maze_successor
,
is_goal
.
is_goal_maze
,
Heuristics
.
maze_opt
))
# # ===============================================================================================================
# # A* SEARCH SUDOKU
# # ===============================================================================================================
sln
=
(
Search_Algorithms
.
A_StarSearch
(
grid
,
Sucessors
.
sudoku_successor
,
is_goal
.
is_goal_sudoku
,
Heuristics
.
sudoku_opt
))[
-
1
]
...
...
@@ -86,8 +124,20 @@ for rows in sln:
print
(
rows
)
# =================================================================================================================
# 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
# # ===============================================================================================================
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