Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
scos
scos.projects
2PSnake
Commits
63e9c4d8
Commit
63e9c4d8
authored
Nov 09, 2020
by
Julian Rudolf
Browse files
started implementing functions for automated experiments
parent
31699b20
Changes
1
Hide whitespace changes
Inline
Side-by-side
Experiments/experiment.py
0 → 100644
View file @
63e9c4d8
from
Agent.snake_logic
import
init_game
,
reset
,
step
import
os
# calculates mean and worst case over 200 values
def
calculate_times
(
filename
):
time_sum
=
0
time_file
=
open
(
"../Experiments/"
+
filename
,
"r"
)
times
=
(
time_file
.
readlines
())[:
200
]
for
time
in
times
:
time
=
float
(
time
)
time_sum
+=
time
mean_time
=
time_sum
/
len
(
times
)
worst_case
=
max
(
times
)
print
(
mean_time
)
print
(
worst_case
)
return
mean_time
,
worst_case
# runs one experiment
# max_steps : for how many steps the shield calculates crash probabilities
# length : length of the snake
# source_filename : filename of times file in which mean and worst case is calculated
# target_filename : filename of final times file in which all means and worst cases are saved
def
run_experiment
(
max_steps
,
length
,
source_filename
,
target_filename
):
init_game
(
max_steps
,
length
,
True
,
source_filename
)
for
i
in
range
(
10
):
game_over
=
step
(
"left"
,
True
)
if
game_over
:
reset
(
True
,
length
)
mean
,
worst_case
=
calculate_times
(
source_filename
)
string
=
"Max Steps: "
+
str
(
max_steps
)
+
" | Snake Length: "
+
str
(
length
)
+
"
\n
"
+
"Mean = "
+
str
(
mean
)
+
"
\n
Worst Case = "
+
str
(
worst_case
)
+
"
\n
"
target_file
=
open
(
target_filename
,
"a"
)
target_file
.
write
(
string
)
os
.
remove
(
"../Experiments/"
+
source_filename
)
run_experiment
(
15
,
20
,
"test.txt"
,
"test_target.txt"
)
run_experiment
(
10
,
15
,
"test2.txt"
,
"test_target.txt"
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment