-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
285 lines (225 loc) · 9.41 KB
/
main.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
################################################################################
# Copyright (c) 2017. Vincenzo Lomonaco, Karan Desai, Eugenio Culurciello, #
# Davide Maltoni. All rights reserved. #
# See the accompanying LICENSE file for terms. #
# #
# Date: 27-05-2019 #
# Authors: Vincenzo Lomonaco, Karan Desai, Eugenio Culurciello, Davide Maltoni #
# E-mail: vincenzo.lomonaco@unibo.it #
# Website: vincenzolomonaco.com #
################################################################################
import os.path
import numpy as np
import random
from model_utils import get_model
from doom_env import init_doom_env
import pprint
import vizdoom
import logging
from collections import OrderedDict
# Sacred dependecies
from sacred import Experiment
from sacred.observers import MongoObserver
# Creating the experiment
ex = Experiment('CL4RL')
# Setting custom logger
logger = logging.getLogger('mylogger')
logger.handlers = []
ch = logging.StreamHandler()
formatter = logging.Formatter('[%(levelname).1s] > %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
ex.logger = logger
# We add the observer (if you don't have a configured DB
# then simply comment the line below line).
ex.observers.append(MongoObserver.create(db_name='experiments_db'))
@ex.config
def cfg():
""" Default configuration parameters. Overwritten by specific exps
configurations. """
# train or test
mode = None
# number of experiments runs
num_runs = 1
# list of learning rates for each map
learning_rate = None
# number of steps in an episode
episode_size = None
# number of game instances running in parallel
batch_size = None
# number of episodes for training
episode_num = None
# number of full-lenght episodes for testing
num_test_ep = None
# number of steps per epoch
epoch_game_steps = None
# save best model based on epoch test results
save_best = None
# discount factor
episode_discount = None
# learning rate multiplier at each epoch
lr_step = None
# lambda (memory strength co-efficient) for EWC, zero lambda means no EWC
ewc_lambda = 0
# clip value for the fisher matrix
clip_value = 0.00000001
# number of game samples to be drawn for fisher matrix calculation in EWC
fisher_sample_size = 100
# number of episodes between two computations of the fisher matrix
fisher_ep_freq = None
# reward threshold for computing fisher
fisher_threshold = None
# long moving average time window
long_tw_size = 50
# short moving average time window
short_tw_size = 6
# random generator seed
seed = None
# Model type among 'aac' or 'aac_map'
model = None
# path to base model file and action set (to remove?)
base_model = None
action_set = None
# path to model file (string in train, list in test)
load = None
# vizdoom config path
vizdoom_config = None
# list of maps to use in the wad file
vizdoom_maps = None
# vizdoom window visibility for the test
vizdoom_test_visible = None
# path to vizdoom
vizdoom_path = os.path.dirname(vizdoom.__file__)
# vizdoom basic utilities wad
wad_path = 'cfgs/freedoom2.wad'
# skiprate
skiprate = None
# number of frames per input
frame_num = None
# checkpoint file name
checkpoint_file = None
# after how many epoch to save the model
checkpoint_rate = None
# command to launch a bot (to remove?)
bot_cmd = None
# pkl file containing results to plot
results_file = None
# log level
log_level = 'DEBUG'
# backend: 'GPU' or 'CPU'
backend = 'GPU'
# ex.add_config('cfgs/light/naive/train.json')
@ex.automain
def main(_config):
""" Main script which for running a non-stationary doom environments
composed of multiple maps. """
# Setting logger
log = ex.logger
log.setLevel(_config['log_level'].upper())
# Printing the conf for visual check
log.debug(
'Exp. configurations:\n'
'----------------------------------------------------------------\n'
+ pprint.pformat(_config) + '\n' +
'----------------------------------------------------------------\n'
)
init_doom_env(_config)
random.seed(_config['seed'])
np.random.seed(_config['seed'])
model = get_model(_config)
if _config['mode'] == 'train':
test_crew = [[] for j in range(_config['num_runs'])]
test_std = [[] for j in range(_config['num_runs'])]
runs_avg_crew = [[[] for i in range(len(_config['vizdoom_maps']))]
for j in range(len(_config['vizdoom_maps']))]
# train with multiple runs
for run_id in range(_config['num_runs']):
ex.info[str(run_id)] = {'test_crew': [], 'test_std': []}
print("\n------------------ RUN {0} -------------------"
.format(run_id))
for task_id in range(len(_config['vizdoom_maps'])):
crew, std, movavg_stats = model.run_train(task_id=task_id)
test_crew[run_id].append(crew[-1])
test_std[run_id].append(std[-1])
# save results into mongodb
ex.info[str(run_id)]['test_crew'].append(crew[-1])
ex.info[str(run_id)]['test_std'].append(std[-1])
ex.info[str(run_id)]['mov_avg_stats'] = movavg_stats
ex.info[str(run_id)]['tot_crew'] = crew
ex.info[str(run_id)]['tot_std'] = std
# prepare for next run
del model
model = get_model(_config)
# printing results
print(
'----------------------------------------------------------------\n'
' RESULTS\n' +
'----------------------------------------------------------------\n'
)
for run_id in range(_config['num_runs']):
print("\n------------------ RUN {0} -------------------"
.format(run_id))
for task_id in range(len(_config['vizdoom_maps'])):
avg_crew = 0
avg_std = 0
for map_id in range(len(_config['vizdoom_maps'])):
# print(test_crew[run_id][task_id])
# print(test_std[run_id][task_id])
print(
"[run %d], [task: %d], [map: %d], "
"[avg. cumulated reward: %f.2f], "
"[dev.std: %.2f]" % (run_id, task_id, map_id,
test_crew[run_id][task_id][map_id],
test_std[run_id][task_id][map_id])
)
avg_crew += test_crew[run_id][task_id][map_id]
avg_std += test_std[run_id][task_id][map_id]
runs_avg_crew[task_id][map_id].append(
test_crew[run_id][task_id][map_id]
)
avg_crew /= len(_config['vizdoom_maps'])
avg_std /= len(_config['vizdoom_maps'])
print("[run %d] [avg_maps_crew %.3f], [avg_maps_std %.3f]\n" %
(run_id, avg_crew, avg_std))
print(
'----------------------------------------------------------------\n'
' AVG RUNS RESULTS\n' +
'----------------------------------------------------------------\n'
)
for task_id in range(len(_config['vizdoom_maps'])):
tot_avg_crew = []
for map_id in range(len(_config['vizdoom_maps'])):
print("[task %d], [map: %d], [runs avg_crew %.3f],"
" [runs std %.3f]" %
(task_id, map_id,
float(np.mean(runs_avg_crew[task_id][map_id])),
float(np.std(runs_avg_crew[task_id][map_id]))))
tot_avg_crew.append(np.mean(runs_avg_crew[task_id][map_id]))
print("[avg_runs_maps_crew %.3f], [std %.3f]\n" %
(float(np.mean(tot_avg_crew)), float(np.std(tot_avg_crew))))
else:
# we assume to have a list in load for test
res = {}
for model_path in _config['load']:
res[model_path] = []
for task_id in range(len(_config['vizdoom_maps'])):
crew, std = model.run_test(
num_test_ep=_config['num_test_ep'], load=model_path,
seed=_config['seed'], task_id=task_id)
res[model_path].append((crew, std))
res = OrderedDict(sorted(res.items(), key=lambda t: t[0]))
for model_name, vec in res.items():
avg_crew = 0
avg_std = 0
for i, (crew, std) in enumerate(vec):
print(
"[name %s], [map: %d], [avg. cumulated reward: %f.2f],"
"[dev.std: %.2f]" % (model_name, i, crew, std)
)
avg_crew += crew
avg_std += std
avg_crew /= len(res[model_name])
avg_std /= len(res[model_name])
print("[avg_crew %.3f], [avg_std %.3f]\n" % (avg_crew, avg_std))