-
Notifications
You must be signed in to change notification settings - Fork 1
/
average_plotter.py
134 lines (107 loc) · 3.31 KB
/
average_plotter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import glob
import json
import matplotlib.pyplot as plt
import sys
from game.game import Game
from evo.evo import EvoAlg
from agents.rulebased import RuleBasedAgent0, RuleBasedAgent1, RuleBasedAgent2, RuleBasedAgent3, RandomAgent
agent_types = ["R01", "R02", "R03", "R04"]
agent_classes = {
'R01' : RuleBasedAgent0,
'R02' : RuleBasedAgent1,
'R03' : RuleBasedAgent2,
'R04' : RuleBasedAgent3,
'RR' : RandomAgent
}
agent_plot_files = []
for agent in agent_types:
agent_plot_files.append(glob.glob("./Plots/" + agent + "*log.txt"))
print(agent_plot_files)
for i in range(len(agent_types)):
print("\n\nLevels Generated for " + agent_types[i] + ": \n\n\n")
for j in range(len(agent_types)):
print("Fitness of " + agent_types[j] + ": \n")
total_reward = 0
for plot_file_num in range(len(agent_plot_files[i])):
with open(agent_plot_files[i][plot_file_num]) as f:
data = json.load(f)
sum_reward = 0
for level in data['population_history'][-1]['population']:
gen_param_specs = {
'initial_rock_density' : {
'dtype' : float,
'min' : level['initial_rock_density'],
'max' : level['initial_rock_density']
},
'initial_tree_density' : {
'dtype' : float,
'min' : level['initial_tree_density'],
'max' : level['initial_tree_density']
},
'rock_refinement_runs' : {
'dtype' : int,
'min' : level['rock_refinement_runs'],
'max' : level['rock_refinement_runs']
},
'tree_refinement_runs' : {
'dtype' : int,
'min' : level['tree_refinement_runs'],
'max' : level['tree_refinement_runs']
},
'rock_neighbour_depth' : {
'dtype' : int,
'min' : level['rock_neighbour_depth'],
'max' : level['rock_neighbour_depth']
},
'tree_neighbour_depth' : {
'dtype' : int,
'min' : level['tree_neighbour_depth'],
'max' : level['tree_neighbour_depth']
},
'rock_neighbour_number' : {
'dtype' : int,
'min' : level['rock_neighbour_number'],
'max' : level['rock_neighbour_number']
},
'tree_neighbour_number' : {
'dtype' : int,
'min' : level['tree_neighbour_number'],
'max' : level['tree_neighbour_number']
},
'base_clear_depth' : {
'dtype' : int,
'min' : level['base_clear_depth'],
'max' : level['base_clear_depth']
},
'enemies_crush_trees' : {
'dtype' : bool
},
'random_seed' : {
'dtype' : int,
'min' : level['random_seed'],
'max' : level['random_seed']
},
'flee_distance' : {
'dtype' : int,
'min' : level['flee_distance'],
'max' : level['flee_distance']
}
}
ea = EvoAlg(gen_param_specs)
env = Game(evo_system=ea)
state = env.reset()
run_limit = 1
agent_class = agent_classes[agent_types[j]]
agent = agent_class(env)
reward_count = 0
while True:
state, reward, done, _ = env.step(agent.act(state))
reward_count += reward
if done:
#print(reward_count)
break
sum_reward += reward_count
print("Average reward for run ", plot_file_num, " : ", sum_reward/100)
total_reward += sum_reward/100
print("Total average reward over the ", len(agent_plot_files[i]) ," runs: ", total_reward/len(agent_plot_files[i]))
#We need to average this so we have the average for each agent type