-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.py
More file actions
358 lines (286 loc) · 13.2 KB
/
analysis.py
File metadata and controls
358 lines (286 loc) · 13.2 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import glob
import os
from dataclasses import dataclass
from pathlib import Path
import numpy as np
from agents.PPOAtariAgent import PPOAtariCNDAgent, PPOAtariRNDAgent, PPOAtariFEDRefAgent, PPOAtariICMAgent, PPOAtariFWDAgent
from agents.PPOProcgenAgent import PPOProcgenRNDAgent, PPOProcgenCNDAgent, PPOProcgenICMAgent, PPOProcgenFWDAgent
from analytic.FeatureAnalysis import FeatureAnalysis
from analytic.MetricTensor import initialize
from analytic.StateCollector import collect_states, collect_samples, save_states
from plots.dataloader import parse_text_line, load_text_files
from plots.paths import models_root, states_root, results_path, plot_root
def generate_analytic_data(descriptor, gen_ref_states=True, gen_features=True):
print('Generating data for {0:s}'.format(descriptor.env))
ref_reward = -1
for i in range(len(descriptor.models)):
instance_id, max_reward, file = find_best_agent(descriptor.env, descriptor.models[i], descriptor.config_ids[i])
descriptor.instance_ids.append(instance_id)
descriptor.result_files.append(file)
if ref_reward < max_reward:
descriptor.ref_model = descriptor.models[i]
descriptor.ref_agent = descriptor.agents[i]
descriptor.ref_config_id = descriptor.config_ids[i]
descriptor.ref_instance_id = instance_id
ref_reward = max_reward
if gen_ref_states:
generate_ref_states(descriptor.env, descriptor.env_id, descriptor.ref_model, descriptor.ref_agent, descriptor.ref_config_id, [descriptor.ref_instance_id])
if gen_features:
for i in range(len(descriptor.models)):
generate_features(descriptor.env, descriptor.env_id, descriptor.models[i], descriptor.agents[i], descriptor.config_ids[i], descriptor.instance_ids[i])
config = []
for i in range(len(descriptor.models)):
config.append(generate_config(descriptor.env, descriptor.models[i], descriptor.labels[i], descriptor.result_files[i], descriptor.config_ids[i], descriptor.instance_ids[i]))
return config
def generate_ref_states(env_name, env_id, model, agent, config_id, instance_id):
print('Generating reference states')
states = []
next_states = []
for i in instance_id:
path = os.path.join(models_root, '{0:s}_{1:d}_{2:s}_{3:d}'.format(env_name, config_id, model, i))
print('Loading {0:s}'.format(path))
agent, env, _ = initialize(env_name, env_id, path, str(config_id), agent)
s0, s1 = collect_states(agent, env, 10000)
states.append(s0)
next_states.append(s1)
save_states(Path(states_root) / '{0:s}.npy'.format(env_name), states, next_states)
def generate_features(env_name, env_id, model, agent, config_id, instance_id):
print('Generating features for {0:s}'.format(model))
agent, _, _ = initialize(env_name, env_id, os.path.join(models_root, '{0:s}_{1:d}_{2:s}_{3:d}'.format(env_name, config_id, model, instance_id)), str(config_id), agent)
collect_samples(agent, Path(states_root) / '{0:s}.npy'.format(env_name), Path(states_root) / '{0:s}_{1:d}_{2:s}_{3:d}'.format(env_name, config_id, model, instance_id), model)
def generate_config(env, model, label, result_file, config_id, instance_id):
config = {'samples': Path(states_root) / '{0:s}_{1:d}_{2:s}_{3:d}.npy'.format(env, config_id, model, instance_id),
# 'results': os.path.join(results_path, '{0:s}/{1:s}/{2:d}/ppo_{3:s}_{4:d}_{5:s}_{6:d}.npy'.format(model, env, config_id, env, config_id, model, instance_id)),
'results': result_file,
'id': '{0:s}{1:d}_{2:d}'.format(model, config_id, instance_id),
'label': label,
}
return config
def find_best_agent(env, model, config_id):
folder = os.path.join(results_path, '{0:s}/{1:s}/{2:d}/'.format(model, env, config_id))
results = []
for file in glob.glob(str(folder) + '/*.npy'):
path = Path(file)
instance_id = int(path.stem.split('_')[-1])
data = np.load(file, allow_pickle=True).item()
if 're' in data:
key = 're'
else:
key = 'ext_reward'
max_reward = np.max(data[key]['sum']).item()
results.append((instance_id, max_reward, file))
data = load_text_files(folder)
if len(data) > 0:
for i, file in enumerate(glob.glob(str(folder) + '/*.log')):
path = Path(file)
instance_id = int(path.stem.split('_')[-1])
max_reward = int(np.max(data[i]['re']['sum']).item())
results.append((instance_id, max_reward, file))
results.sort(key=lambda a: a[1])
return results[-1][0], results[-1][1], results[-1][2]
class Descriptor:
def __init__(self):
self.env = None
self.models = []
self.agents = []
self.config_ids = []
self.instance_ids = []
self.result_files = []
self.labels = []
self.ref_model = None
self.ref_agent = None
self.ref_config_id = None
self.ref_instance_id = None
class MontezumaDescriptor(Descriptor):
def __init__(self):
super().__init__()
self.env = 'montezuma'
self.env_id = 'MontezumaRevengeNoFrameskip-v4'
self.models = ['rnd', 'cnd', 'cnd', 'cnd']
self.agents = [PPOAtariRNDAgent, PPOAtariCNDAgent, PPOAtariCNDAgent, PPOAtariCNDAgent]
self.config_ids = [2, 49, 42, 44]
self.labels = ['RND', 'SND-V', 'SND-STD', 'SND-VIC']
class GravitarDescriptor(Descriptor):
def __init__(self):
super().__init__()
self.env = 'gravitar'
self.env_id = 'GravitarNoFrameskip-v4'
self.models = ['rnd', 'cnd', 'cnd', 'cnd']
self.agents = [PPOAtariRNDAgent, PPOAtariCNDAgent, PPOAtariCNDAgent, PPOAtariCNDAgent]
self.config_ids = [2, 14, 11, 13]
self.labels = ['RND', 'SND-V', 'SND-STD', 'SND-VIC']
class PrivateEyeDescriptor(Descriptor):
def __init__(self):
super().__init__()
self.env = 'private_eye'
self.env_id = 'PrivateEyeNoFrameskip-v4'
self.models = ['rnd', 'cnd', 'cnd', 'cnd']
self.agents = [PPOAtariRNDAgent, PPOAtariCNDAgent, PPOAtariCNDAgent, PPOAtariCNDAgent]
self.config_ids = [2, 8, 4, 7]
self.labels = ['RND', 'SND-V', 'SND-STD', 'SND-VIC']
class PitfallDescriptor(Descriptor):
def __init__(self):
super().__init__()
self.env = 'pitfall'
self.env_id = 'PitfallNoFrameskip-v4'
self.models = ['rnd', 'cnd', 'cnd', 'cnd']
self.agents = [PPOAtariRNDAgent, PPOAtariCNDAgent, PPOAtariCNDAgent, PPOAtariCNDAgent]
self.config_ids = [2, 8, 4, 7]
self.labels = ['RND', 'SND-V', 'SND-STD', 'SND-VIC']
class SolarisDescriptor(Descriptor):
def __init__(self):
super().__init__()
self.env = 'solaris'
self.env_id = 'SolarisNoFrameskip-v4'
self.models = ['rnd', 'cnd', 'cnd', 'cnd']
self.agents = [PPOAtariRNDAgent, PPOAtariCNDAgent, PPOAtariCNDAgent, PPOAtariCNDAgent]
self.config_ids = [2, 8, 4, 7]
self.labels = ['RND', 'SND-V', 'SND-STD', 'SND-VIC']
class VentureDescriptor(Descriptor):
def __init__(self):
super().__init__()
self.env = 'venture'
self.env_id = 'VentureNoFrameskip-v4'
self.models = ['rnd', 'cnd', 'cnd', 'cnd']
self.agents = [PPOAtariRNDAgent, PPOAtariCNDAgent, PPOAtariCNDAgent, PPOAtariCNDAgent]
self.config_ids = [2, 10, 4, 8]
self.labels = ['RND', 'SND-V', 'SND-STD', 'SND-VIC']
class CaveflyerDescriptor(Descriptor):
def __init__(self):
super().__init__()
self.env = 'caveflyer'
self.env_id = 'procgen-caveflyer-v0'
self.models = ['rnd', 'cnd', 'cnd', 'cnd']
self.agents = [PPOProcgenRNDAgent, PPOProcgenCNDAgent, PPOProcgenCNDAgent, PPOProcgenCNDAgent]
self.config_ids = [2, 8, 4, 7]
self.labels = ['RND', 'SND-V', 'SND-STD', 'SND-VIC']
class ClimberDescriptor(Descriptor):
def __init__(self):
super().__init__()
self.env = 'climber'
self.env_id = 'procgen-climber-v0'
self.models = ['rnd', 'cnd', 'cnd', 'cnd']
self.agents = [PPOProcgenRNDAgent, PPOProcgenCNDAgent, PPOProcgenCNDAgent, PPOProcgenCNDAgent]
self.config_ids = [2, 8, 4, 7]
self.labels = ['RND', 'SND-V', 'SND-STD', 'SND-VIC']
class CoinrunDescriptor(Descriptor):
def __init__(self):
super().__init__()
self.env = 'coinrun'
self.env_id = 'procgen-coinrun-v0'
self.models = ['rnd', 'cnd', 'cnd', 'cnd']
self.agents = [PPOProcgenRNDAgent, PPOProcgenCNDAgent, PPOProcgenCNDAgent, PPOProcgenCNDAgent]
self.config_ids = [2, 8, 4, 7]
self.labels = ['RND', 'SND-V', 'SND-STD', 'SND-VIC']
class JumperDescriptor(Descriptor):
def __init__(self):
super().__init__()
self.env = 'jumper'
self.env_id = 'procgen-jumper-v0'
self.models = ['rnd', 'cnd', 'cnd', 'cnd']
self.agents = [PPOProcgenRNDAgent, PPOProcgenCNDAgent, PPOProcgenCNDAgent, PPOProcgenCNDAgent]
self.config_ids = [2, 8, 4, 7]
self.labels = ['RND', 'SND-V', 'SND-STD', 'SND-VIC']
class MontezumaSPDescriptor(Descriptor):
def __init__(self):
super().__init__()
self.env = 'montezuma'
self.env_id = 'MontezumaRevengeNoFrameskip-v4'
self.models = ['icm', 'fwd']
self.agents = [PPOAtariICMAgent, PPOAtariFWDAgent]
self.config_ids = [30, 45]
self.labels = ['ICM', 'SP']
class GravitarSPDescriptor(Descriptor):
def __init__(self):
super().__init__()
self.env = 'gravitar'
self.env_id = 'GravitarNoFrameskip-v4'
self.models = ['icm', 'fwd']
self.agents = [PPOAtariICMAgent, PPOAtariFWDAgent]
self.config_ids = [10, 12]
self.labels = ['ICM', 'SP']
class PrivateEyeSPDescriptor(Descriptor):
def __init__(self):
super().__init__()
self.env = 'private_eye'
self.env_id = 'PrivateEyeNoFrameskip-v4'
self.models = ['icm', 'fwd']
self.agents = [PPOAtariICMAgent, PPOAtariFWDAgent]
self.config_ids = [5, 6]
self.labels = ['ICM', 'SP']
class PitfallSPDescriptor(Descriptor):
def __init__(self):
super().__init__()
self.env = 'pitfall'
self.env_id = 'PitfallNoFrameskip-v4'
self.models = ['icm', 'fwd']
self.agents = [PPOAtariICMAgent, PPOAtariFWDAgent]
self.config_ids = [5, 6]
self.labels = ['ICM', 'SP']
class SolarisSPDescriptor(Descriptor):
def __init__(self):
super().__init__()
self.env = 'solaris'
self.env_id = 'SolarisNoFrameskip-v4'
self.models = ['icm', 'fwd']
self.agents = [PPOAtariICMAgent, PPOAtariFWDAgent]
self.config_ids = [5, 6]
self.labels = ['ICM', 'SP']
class VentureSPDescriptor(Descriptor):
def __init__(self):
super().__init__()
self.env = 'venture'
self.env_id = 'VentureNoFrameskip-v4'
self.models = ['icm', 'fwd']
self.agents = [PPOAtariICMAgent, PPOAtariFWDAgent]
self.config_ids = [6, 7]
self.labels = ['ICM', 'SP']
class CaveflyerSPDescriptor(Descriptor):
def __init__(self):
super().__init__()
self.env = 'caveflyer'
self.env_id = 'procgen-caveflyer-v0'
self.models = ['icm', 'fwd']
self.agents = [PPOProcgenICMAgent, PPOProcgenFWDAgent]
self.config_ids = [6, 5]
self.labels = ['ICM', 'SP']
class ClimberSPDescriptor(Descriptor):
def __init__(self):
super().__init__()
self.env = 'climber'
self.env_id = 'procgen-climber-v0'
self.models = ['icm', 'fwd']
self.agents = [PPOProcgenICMAgent, PPOProcgenFWDAgent]
self.config_ids = [6, 5]
self.labels = ['ICM', 'SP']
class CoinrunSPDescriptor(Descriptor):
def __init__(self):
super().__init__()
self.env = 'coinrun'
self.env_id = 'procgen-coinrun-v0'
self.models = ['icm', 'fwd']
self.agents = [PPOProcgenICMAgent, PPOProcgenFWDAgent]
self.config_ids = [6, 5]
self.labels = ['ICM', 'SP']
class JumperSPDescriptor(Descriptor):
def __init__(self):
super().__init__()
self.env = 'jumper'
self.env_id = 'procgen-jumper-v0'
self.models = ['icm', 'fwd']
self.agents = [PPOProcgenICMAgent, PPOProcgenFWDAgent]
self.config_ids = [6, 5]
self.labels = ['ICM', 'SP']
if __name__ == '__main__':
descriptors = [MontezumaDescriptor, GravitarDescriptor, PrivateEyeDescriptor, SolarisDescriptor, VentureDescriptor]
descriptors = [CaveflyerDescriptor, ClimberDescriptor, CoinrunDescriptor, JumperDescriptor]
# descriptors = [PitfallSPDescriptor]
# descriptors = [GravitarSPDescriptor, PrivateEyeSPDescriptor, SolarisSPDescriptor, VentureSPDescriptor]
# descriptors = [CaveflyerSPDescriptor, ClimberSPDescriptor, CoinrunSPDescriptor, JumperSPDescriptor]
for desc in descriptors:
desc_instance = desc()
config = generate_analytic_data(desc_instance, gen_ref_states=False, gen_features=False)
analysis = FeatureAnalysis(config)
analysis.plot(str(Path(plot_root) / desc_instance.env))
# analysis.table(filename='features_cnd{0:d}_table'.format(config_id))
# analysis.plot_feature_boxplot('features_cnd{0:d}_boxplot'.format(config_id))