Commit 63e9c4d8 authored by Julian Rudolf's avatar Julian Rudolf
Browse files

started implementing functions for automated experiments

parent 31699b20
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) + "\nWorst 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")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment