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
02d08804
Commit
02d08804
authored
1 year ago
by
DannyAbdi
Browse files
Options
Downloads
Patches
Plain Diff
Implemented minmax algorithm for multi-agent navigation
parent
288e40ff
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!1
Single and Multi-Agent Pathfinding in Maze games
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
minmax.py
+88
-0
88 additions, 0 deletions
minmax.py
with
88 additions
and
0 deletions
minmax.py
0 → 100644
+
88
−
0
View file @
02d08804
class
MinMax
:
"""
Initializes a MinMax object.
:param maze: The maze used for navigation and solving.
"""
def
__init__
(
self
,
maze
):
self
.
maze
=
maze
"""
Gets the valid moves that an enemy agent can take.
:param player_position: The current position of the player agent.
:return: A list of valid moves for the enemy agent.
"""
def
get_valid_moves
(
self
,
player_position
):
valid_moves
=
[]
for
dx
,
dy
in
[(
1
,
0
),
(
-
1
,
0
),
(
0
,
1
),
(
0
,
-
1
)]:
x
,
y
=
player_position
[
1
]
+
dx
,
player_position
[
0
]
+
dy
if
(
0
<=
x
<
len
(
self
.
maze
.
current_level
[
0
])
and
0
<=
y
<
len
(
self
.
maze
.
current_level
)
and
self
.
maze
.
current_level
[
y
][
x
]
!=
1
):
valid_moves
.
append
((
y
,
x
))
return
valid_moves
"""
Checks if a move to a given position is valid.
:param position: The position to move to.
:return: True if the move is valid, False otherwise.
"""
def
is_valid_move
(
self
,
position
):
x
,
y
=
position
[
1
],
position
[
0
]
return
(
0
<=
x
<
len
(
self
.
maze
.
current_level
[
0
])
and
0
<=
y
<
len
(
self
.
maze
.
current_level
)
and
self
.
maze
.
current_level
[
y
][
x
]
!=
1
)
"""
Performs the MinMax algorithm 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).
:return: The best move for the enemy agent.
"""
def
minmax
(
self
,
player_position
,
enemy_position
,
depth
,
maximising_player
):
if
depth
==
0
or
self
.
game_over
():
# Evaluate the current game state
return
self
.
evaluate
(
player_position
,
enemy_position
)
if
maximising_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
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
)
min_eval
=
min
(
min_eval
,
eval
)
return
min_eval
"""
Evaluate the current game state.
:param player_position: The current position of the player agent.
:param enemy_position: The current position of the enemy agent.
:return: The evaluation of the current game state.
"""
def
evaluate
(
self
,
player_position
,
enemy_position
):
# Implement the evaluation logic here
pass
"""
Check if the game is over.
:return: True if the game is over, False otherwise.
"""
def
game_over
(
self
):
# Implement the game over condition here
pass
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