Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Abdi Danny 2019 ZHAC232 100938755 FINAL YEAR PROJECT
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
Abdi Danny (2019) ZHAC232
Abdi Danny 2019 ZHAC232 100938755 FINAL YEAR PROJECT
Commits
4cb5059f
Commit
4cb5059f
authored
1 year ago
by
DannyAbdi
Browse files
Options
Downloads
Patches
Plain Diff
Modified minmax algorithm to incorporate alpha beta pruning
parent
d53d2a20
No related branches found
Branches containing commit
No related tags found
Tags containing commit
Loading
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
minmax.py
+16
-11
16 additions, 11 deletions
minmax.py
with
16 additions
and
11 deletions
minmax.py
+
16
−
11
View file @
4cb5059f
...
...
@@ -34,37 +34,42 @@ class MinMax:
self
.
maze
.
current_level
[
y
][
x
]
!=
1
)
"""
Performs the MinMax algorithm to determine the best move the enemy can take.
Performs the MinMax algorithm
with Alpha-beta pruning
to determine the best move the enemy can take.
:param player_position: The current position of the player agent.
:param enemy_position: The current position of the enemy agent.
:param depth: The depth of the MinMax search tree.
:param maximising_player: True if maximising player (enemy), False if minimising player (player).
:param alpha: The best value that the maximizing player currently can guarantee.
:param beta: The best value that the minimizing player currently can guarantee.
:param maximizing_player: True if maximizing player (enemy), False if minimizing player (player).
:return: The best move for the enemy agent.
"""
def
minmax
(
self
,
player_position
,
enemy_position
,
depth
,
maximi
s
ing_player
):
def
minmax
(
self
,
player_position
,
enemy_position
,
depth
,
alpha
,
beta
,
maximi
z
ing_player
):
if
depth
==
0
or
self
.
game_over
():
# Evaluate the current game state
return
self
.
evaluate
(
player_position
,
enemy_position
)
if
maximi
s
ing_player
:
if
maximi
z
ing_player
:
max_eval
=
float
(
'
-inf
'
)
valid_moves
=
self
.
get_valid_moves
(
enemy_position
)
best_move
=
None
for
move
in
valid_moves
:
if
self
.
is_valid_move
(
move
):
eval
=
self
.
minmax
(
player_position
,
move
,
depth
-
1
,
False
)
if
eval
>
max_eval
:
max_eval
=
eval
best_move
=
move
return
max_eval
if
depth
==
1
else
best_move
eval
=
self
.
minmax
(
player_position
,
move
,
depth
-
1
,
alpha
,
beta
,
False
)
max_eval
=
max
(
max_eval
,
eval
)
alpha
=
max
(
alpha
,
eval
)
if
beta
<=
alpha
:
break
return
max_eval
else
:
min_eval
=
float
(
'
inf
'
)
valid_moves
=
self
.
get_valid_moves
(
player_position
)
for
move
in
valid_moves
:
if
self
.
is_valid_move
(
move
):
eval
=
self
.
minmax
(
move
,
enemy_position
,
depth
-
1
,
True
)
eval
=
self
.
minmax
(
move
,
enemy_position
,
depth
-
1
,
alpha
,
beta
,
True
)
min_eval
=
min
(
min_eval
,
eval
)
beta
=
min
(
beta
,
eval
)
if
beta
<=
alpha
:
break
return
min_eval
"""
...
...
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