Commit 53bbaaa8 authored by Julian Rudolf's avatar Julian Rudolf
Browse files

added plot functionality to experiment.py

parent 6d4fe7bd
......@@ -2,6 +2,7 @@
from Agent.snake_logic import init_game, reset, step
import os
import sys
import matplotlib.pyplot as plt
# calculates mean and worst case over 200 values
......@@ -36,6 +37,83 @@ def run_experiment(max_steps, length, source_filename, target_filename):
string = (str(mean)).ljust(25) + " " + str(worst_case) + "\n"
target_file = open(target_filename, "a")
target_file.write(string)
plot_values(target_filename)
# plots experiment values
def plot_values(filename):
exp = open(filename, "r")
length = 10
t = list(range(10,31))
mean10, mean15, mean20, worst_case10, worst_case15, worst_case20 = list(), list(), list(), list(), list(), list()
values = exp.readlines()
for i in range(len(values)):
if values[i][0] == 'M' or values[i][0] == '-':
continue
elif values[i][0] == 'S' and values[i][16] == '1' and values[i][17] == '0':
continue
elif values[i][0] == 'S' and values[i][17] == '5':
length = 15
continue
elif values[i][0] == 'S' and values[i][16] == '2' and values[i][17] == '0':
length = 20
continue
m = ''
wc = ''
spc = False
if length == 10:
for c in values[i]:
if c != ' ' and not spc:
m = m + c
elif c != ' ' and spc:
wc = wc + c
else:
spc = True
mean10.append(float(m))
worst_case10.append(float(wc))
elif length == 15:
for c in values[i]:
if c != ' ' and not spc:
m = m + c
elif c != ' ' and spc:
wc = wc + c
else:
spc = True
mean15.append(float(m))
worst_case15.append(float(wc))
elif length == 20:
for c in values[i]:
if c != ' ' and not spc:
m = m + c
elif c != ' ' and spc:
wc = wc + c
else:
spc = True
mean20.append(float(m))
worst_case20.append(float(wc))
plt.plot(t, mean10, label="Length = 10")
plt.plot(t, mean15, label="Length = 15")
plt.plot(t, mean20, label="Length = 20")
plt.xlabel('Max Steps')
plt.ylabel('Time (Seconds)')
plt.title("Shield Calculation Times - Mean")
plt.legend()
plt.show()
plt.plot(t, worst_case10, label="Length = 10")
plt.plot(t, worst_case15, label="Length = 15")
plt.plot(t, worst_case20, label="Length = 20")
plt.xlabel('Max Steps')
plt.ylabel('Time (Seconds)')
plt.title("Shield Calculation Times - Worst-Case")
plt.legend()
plt.show()
# run_experiment(30, 10, "test.txt", "test2.txt")
......
......@@ -2,6 +2,7 @@
# in order to run this shell the PYTHONPATH needs to be set to 2psnake directory
# To run an experiment, the python function "run_experiment" in /Experiments/experiment.py is called
# run_experiment plays the snake game with one shield activated and random actions
# the plot is generated automatically. To plot values afterwards, only run "plot_values" function in experiment.py
# Parameters:
# max_steps : for how many steps the SHIELD calculates the corresponding crash probabilities
# length : the length of the snakes
......@@ -34,3 +35,4 @@ for i in 10 15 20
done
done
......@@ -117,8 +117,8 @@ start3 = [{"pos": [[5,6],[5,5],[5,4],[5,3],[5,2],[5,1],[4,1],[3,1],[2,1],[2,2],[
map3 = [[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1],
[1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,0,1],
[1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,1,1,1,1,1,0,1],
[1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,1,1,1,1,1,0,1],
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,1,1,0,1],
[1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,1,1,0,1],
......
No preview for this file type
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