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
7e1a6d58
Commit
7e1a6d58
authored
1 year ago
by
DannyAbdi
Browse files
Options
Downloads
Patches
Plain Diff
Implemented expectimax algorithm
parent
4cb5059f
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!1
Single and Multi-Agent Pathfinding in Maze games
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
expectimax.py
+43
-0
43 additions, 0 deletions
expectimax.py
with
43 additions
and
0 deletions
expectimax.py
0 → 100644
+
43
−
0
View file @
7e1a6d58
from
minmax
import
MinMax
class
Expectimax
(
MinMax
):
"""
Initializes an Expectimax object.
:param maze: The maze used for navigation and solving.
"""
def
__init__
(
self
,
maze
):
super
().
__init__
(
maze
)
"""
Performs the Expectimax 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 Expectimax search tree.
:param maximising_player: True if maximising player (enemy), False if minimising player (player).
:return: The best move for the enemy agent.
"""
def
expectimax
(
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
)
for
move
in
valid_moves
:
if
self
.
is_valid_move
(
move
):
eval
=
self
.
expectimax
(
player_position
,
move
,
depth
-
1
,
False
)
max_eval
=
max
(
max_eval
,
eval
)
return
max_eval
else
:
total_eval
=
0
valid_moves
=
self
.
get_valid_moves
(
player_position
)
num_moves
=
len
(
valid_moves
)
for
move
in
valid_moves
:
if
self
.
is_valid_move
(
move
):
eval
=
self
.
expectimax
(
move
,
enemy_position
,
depth
-
1
,
True
)
total_eval
+=
eval
return
total_eval
/
num_moves
\ 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