-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
247 lines (197 loc) · 9.35 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
import sys, random, threading, time, gzip, traceback
import gym, gym_ple
import numpy as np
import os
from args import *
from utils import StopWatch, timeF
from Queue import Queue
from EC_agent import EpisodicControlAgent, make_buffers
arg_group = arg_parser.add_argument_group('env arguments')
arg_group.add_argument('--max_episodes', type=int, default=1000000)
arg_group.add_argument('--max_frames', type=int, default=5000000)
arg_group.add_argument('--num_repeat', type=int, default=1)
arg_group.add_argument('--force_overwrite', type=str2bool, default=False)
arg_group.add_argument('--headless', type=str2bool, default=False)
arg_group.add_argument('--checkpoint_frame_spacing', type=int, default=-1)
arg_group.add_argument('--eval_frame_spacing', type=int, default=-1)
arg_group.add_argument('--eval_seeds', type=str2list(int), default=range(100,105))
arg_group.add_argument('--multi_eval', type=str2bool, default=False)
arg_group.add_argument('--seed', type=int, default=5)
arg_group.add_argument('--atari_greyscale','--atari_grayscale', type=str2bool, default=True)
arg_group.add_argument('env')
arg_group.add_argument('log_dir')
class AgentProcess(object):
def __init__(self, env_name, buffers, projection, seed, num_repeat):
self.seed = seed
self.num_repeat = num_repeat
self.frame_count = 0
self.env = gym.make(env_name)
self.agent = EpisodicControlAgent(buffers, projection)
np.random.seed(self.seed)
self.env.seed(self.seed)
random.seed(self.seed)
def run_episode(self):
act_time, (return_, frames) = timeF(self.act_episode)
wrapup_time, _ = timeF(self.wrapup_episode)
self.frame_count += frames
return return_, frames, self.frame_count, act_time, wrapup_time
def act_episode(self):
obs = self.env.reset()
self.agent.init_episode(obs)
frames = 0
return_ = 0
do_render = os.path.isfile('/tmp/gymrender')
while True:
action, meta = self.agent.choose_action(obs)
obs_pre = obs
reward = 0
for _ in range(self.num_repeat):
obs, sub_reward, done, info = self.env.step(action)
if do_render:
self.env.render()
frames += 1
reward += sub_reward
if done:
break
self.agent.observe_action(action, reward, obs_pre, obs, meta)
return_ += reward
if done:
return (return_, frames)
def wrapup_episode(self):
self.agent.wrapup_episode()
def ensure_log_dir():
if os.path.exists(args.log_dir):
if not args.force_overwrite:
raise ValueError('log_dir exists (and --force_overwrite as False)')
else:
os.makedirs(args.log_dir)
def write_arg_file():
arg_file = open(os.path.join(args.log_dir, 'args'), 'w')
arg_file.write(`vars(args)`)
arg_file.close()
def get_train_file():
return open(os.path.join(args.log_dir, 'train.csv'), 'w')
def get_eval_file():
return open(os.path.join(args.log_dir, 'eval.csv'), 'w')
def write_buff_files(agent_process):
def get_forest_data(forest, action_buffer):
datas, labels, drift_hists = [], [], []
for i in range(forest.get_memory_size()):
if forest.is_active(i):
datas.append(forest.get_data(i))
labels.append(forest.get_label(i))
if action_buffer.drift_strategy.drift_hist is not None:
drift_hists.append(action_buffer.drift_strategy.drift_hist[i,:])
return datas, labels, drift_hists
all_data = {}
for ndx, buf in enumerate(agent_process.agent.action_buffers):
datas, labels, drift_hists = get_forest_data(buf.forest, buf)
all_data['data_%d'%ndx] = datas
all_data['labels_%d'%ndx] = labels
all_data['driftHist_%d'%ndx] = drift_hists
fname = os.path.join(args.log_dir, 'data.npz')
np.savez_compressed(fname, **all_data)
def log_init(log_file, buffers):
log_file.write('episode,totalBufferSize,totalFrameCount,walltime,return,epFrames,actTime,wrapupTime')
for i in range(len(buffers)):
log_file.write(',size%d'%i)
log_file.write(',seed,evictions\n')
log_file.flush()
def log_episode(log_file, buffers, episode_num, time, return_, ep_frames, total_frame_count, act_time, wrapup_time, seed):
buff_sizes = [buff.size() for buff in buffers]
total_buffer_size = sum(buff_sizes)
outputs = [episode_num, total_buffer_size, total_frame_count, time]
outputs.extend([return_, ep_frames, act_time, wrapup_time])
outputs.extend(buff_sizes)
outputs.append(seed)
outputs.append(sum([buff.evict_count for buff in buffers]))
log_file.write(','.join(map(str, outputs))+'\n')
log_file.flush()
#TODO: This is really ugly monkeypatching
# A proper solution would be to properly subclass, but this gets the job done for now...
def monkeypatch_atari_greyscale():
from gym.envs.atari import AtariEnv
AtariEnv._get_image = lambda slf: slf.ale.getScreenGrayscale().squeeze()
old_init = AtariEnv.__init__
def new_init(slf, *args, **kwargs):
old_init(slf, *args, **kwargs)
old_space = slf.observation_space
if len(old_space.shape) == 3:
slf.observation_space = gym.spaces.Box(low=old_space.low[:,:,0], high=old_space.high[:,:,0])
AtariEnv.__init__ = new_init
if not args.headless:
old_render = AtariEnv._render
def new_render(slf, mode='human', close=False):
if close or mode != 'human':
old_render(slf, mode, close)
if slf.viewer is None:
from gym.envs.classic_control import rendering
slf.viewer = rendering.SimpleImageViewer()
slf.viewer.imshow(slf._get_image()[:,:,np.newaxis].repeat(3,axis=2))
AtariEnv._render = new_render
if __name__ == '__main__':
parse_args()
if args.headless: # Fix for gym_ple, which likes to needlessly spawn windows on construction...
os.putenv('SDL_VIDEODRIVER', 'fbcon')
os.environ['SDL_VIDEODRIVER'] = 'dummy'
if args.atari_greyscale:
monkeypatch_atari_greyscale()
env_name = args.env
buffers, projection = make_buffers(env_name=env_name, seed=args.seed)
agent_process = AgentProcess(env_name=env_name, buffers=buffers, projection=projection, seed=args.seed, num_repeat=args.num_repeat)
eval_process = AgentProcess(env_name=env_name, buffers=buffers, projection=projection, seed=args.seed, num_repeat=args.num_repeat)
ensure_log_dir()
write_arg_file()
train_file, eval_file = get_train_file(), get_eval_file()
log_init(train_file, buffers)
log_init(eval_file, buffers)
next_checkpoint, next_eval = args.checkpoint_frame_spacing, args.eval_frame_spacing
eval_num = 0
stopwatch = StopWatch()
stopwatch.start()
try:
for episode_num in range(args.max_episodes):
return_, ep_frames, total_frame_count, act_time, wrapup_time = agent_process.run_episode()
log_episode(train_file, buffers, episode_num, stopwatch.time(), return_, ep_frames, total_frame_count, act_time, wrapup_time, args.seed)
if total_frame_count >= args.max_frames:
break
if args.checkpoint_frame_spacing > 0 and total_frame_count >= next_checkpoint:
stopwatch.pause()
write_buff_files(agent_process)
while next_checkpoint < total_frame_count:
next_checkpoint += args.checkpoint_frame_spacing
stopwatch.start()
if args.eval_frame_spacing > 0 and total_frame_count >= next_eval:
stopwatch.pause()
eval_num += 1
for seed in args.eval_seeds:
wrapup_time = 0
eval_process.env.seed(seed)
act_time, (return_, ep_frames) = timeF(eval_process.act_episode)
log_episode(eval_file, buffers, eval_num, stopwatch.time(), return_, ep_frames, total_frame_count, act_time, wrapup_time, seed)
if args.multi_eval:
eval_process.env.seed(seed)
old_eps = eval_process.agent.eps
eval_process.agent.eps = -1
act_time, (return_, ep_frames) = timeF(eval_process.act_episode)
log_episode(eval_file, buffers, eval_num, stopwatch.time(), return_, ep_frames, total_frame_count, act_time, wrapup_time, -seed)
eval_process.agent.eps = old_eps
while next_eval < total_frame_count:
next_eval += args.eval_frame_spacing
stopwatch.start()
except:
traceback.print_exc()
finally:
stopwatch.pause()
#eval_num += 1
#total_frame_count = agent_process.frame_count
#for seed in args.eval_seeds:
# eval_process.env.seed(seed)
# act_time, (return_, ep_frames) = timeF(agent_process.act_episode)
# wrapup_time = 0
# log_episode(eval_file, buffers, eval_num, stopwatch.time(), return_, ep_frames, total_frame_count, act_time, wrapup_time, seed)
train_file.close()
eval_file.close()
write_buff_files(agent_process)
#episodeNum, return, totalBufferSize, totalFrameCount, walltime, epFrames, actTimer, wrapupTimer
# per-buffer: size