-
Notifications
You must be signed in to change notification settings - Fork 31
/
envs.py
330 lines (268 loc) · 9.57 KB
/
envs.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
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
import gym
import cv2
import numpy as np
from abc import abstractmethod
from collections import deque
from copy import copy
import gym_super_mario_bros
from nes_py.wrappers import BinarySpaceToDiscreteSpaceEnv
from gym_super_mario_bros.actions import SIMPLE_MOVEMENT, COMPLEX_MOVEMENT
from torch.multiprocessing import Pipe, Process
from model import *
from config import *
from PIL import Image
train_method = default_config['TrainMethod']
max_step_per_episode = int(default_config['MaxStepPerEpisode'])
class Environment(Process):
@abstractmethod
def run(self):
pass
@abstractmethod
def reset(self):
pass
@abstractmethod
def pre_proc(self, x):
pass
@abstractmethod
def get_init_state(self, x):
pass
def unwrap(env):
if hasattr(env, "unwrapped"):
return env.unwrapped
elif hasattr(env, "env"):
return unwrap(env.env)
elif hasattr(env, "leg_env"):
return unwrap(env.leg_env)
else:
return env
class NoopResetEnv(gym.Wrapper):
def __init__(self, env, noop_max=30):
"""Sample initial states by taking random number of no-ops on reset.
No-op is assumed to be action 0.
"""
gym.Wrapper.__init__(self, env)
self.noop_max = noop_max
self.override_num_noops = None
self.noop_action = 0
assert env.unwrapped.get_action_meanings()[0] == 'NOOP'
def reset(self, **kwargs):
""" Do no-op action for a number of steps in [1, noop_max]."""
self.env.reset(**kwargs)
if self.override_num_noops is not None:
noops = self.override_num_noops
else:
noops = self.unwrapped.np_random.randint(1, self.noop_max + 1) #pylint: disable=E1101
assert noops > 0
obs = None
for _ in range(noops):
obs, _, done, _ = self.env.step(self.noop_action)
if done:
obs = self.env.reset(**kwargs)
return obs
def step(self, ac):
return self.env.step(ac)
class MaxAndSkipEnv(gym.Wrapper):
def __init__(self, env, is_render, skip=4):
"""Return only every `skip`-th frame"""
gym.Wrapper.__init__(self, env)
# most recent raw observations (for max pooling across time steps)
self._obs_buffer = np.zeros((2,) + env.observation_space.shape, dtype=np.uint8)
self._skip = skip
self.is_render = is_render
def step(self, action):
"""Repeat action, sum reward, and max over last observations."""
total_reward = 0.0
done = None
for i in range(self._skip):
obs, reward, done, info = self.env.step(action)
if self.is_render:
self.env.render()
if i == self._skip - 2:
self._obs_buffer[0] = obs
if i == self._skip - 1:
self._obs_buffer[1] = obs
total_reward += reward
if done:
break
# Note that the observation on the done=True frame
# doesn't matter
max_frame = self._obs_buffer.max(axis=0)
return max_frame, total_reward, done, info
def reset(self, **kwargs):
return self.env.reset(**kwargs)
class MontezumaInfoWrapper(gym.Wrapper):
def __init__(self, env, room_address):
super(MontezumaInfoWrapper, self).__init__(env)
self.room_address = room_address
self.visited_rooms = set()
def get_current_room(self):
ram = unwrap(self.env).ale.getRAM()
assert len(ram) == 128
return int(ram[self.room_address])
def step(self, action):
obs, rew, done, info = self.env.step(action)
self.visited_rooms.add(self.get_current_room())
if done:
if 'episode' not in info:
info['episode'] = {}
info['episode'].update(visited_rooms=copy(self.visited_rooms))
self.visited_rooms.clear()
return obs, rew, done, info
def reset(self):
return self.env.reset()
class AtariEnvironment(Environment):
def __init__(
self,
env_id,
is_render,
env_idx,
child_conn,
history_size=4,
h=84,
w=84,
life_done=True):
super(AtariEnvironment, self).__init__()
self.daemon = True
self.env = MaxAndSkipEnv(NoopResetEnv(gym.make(env_id)), is_render)
if 'Montezuma' in env_id:
self.env = MontezumaInfoWrapper(self.env, room_address=3 if 'Montezuma' in env_id else 1)
self.env_id = env_id
self.is_render = is_render
self.env_idx = env_idx
self.steps = 0
self.episode = 0
self.rall = 0
self.recent_rlist = deque(maxlen=100)
self.child_conn = child_conn
self.history_size = history_size
self.history = np.zeros([history_size, h, w])
self.h = h
self.w = w
self.reset()
def run(self):
super(AtariEnvironment, self).run()
while True:
action = self.child_conn.recv()
if 'Breakout' in self.env_id:
action += 1
s, reward, done, info = self.env.step(action)
if max_step_per_episode < self.steps:
done = True
log_reward = reward
force_done = done
self.history[:3, :, :] = self.history[1:, :, :]
self.history[3, :, :] = self.pre_proc(s)
self.rall += reward
self.steps += 1
if done:
self.recent_rlist.append(self.rall)
print("[Episode {}({})] Step: {} Reward: {} Recent Reward: {} Visited Room: [{}]".format(
self.episode, self.env_idx, self.steps, self.rall, np.mean(self.recent_rlist),
info.get('episode', {}).get('visited_rooms', {})))
self.history = self.reset()
self.child_conn.send(
[self.history[:, :, :], reward, force_done, done, log_reward])
def reset(self):
self.last_action = 0
self.steps = 0
self.episode += 1
self.rall = 0
s = self.env.reset()
self.get_init_state(
self.pre_proc(s))
return self.history[:, :, :]
def pre_proc(self, X):
X = np.array(Image.fromarray(X).convert('L')).astype('float32')
x = cv2.resize(X, (self.h, self.w))
return x
def get_init_state(self, s):
for i in range(self.history_size):
self.history[i, :, :] = self.pre_proc(s)
class MarioEnvironment(Process):
def __init__(
self,
env_id,
is_render,
env_idx,
child_conn,
history_size=4,
life_done=True,
h=84,
w=84, movement=COMPLEX_MOVEMENT, sticky_action=True,
p=0.25):
super(MarioEnvironment, self).__init__()
self.daemon = True
self.env = BinarySpaceToDiscreteSpaceEnv(
gym_super_mario_bros.make(env_id), COMPLEX_MOVEMENT)
self.is_render = is_render
self.env_idx = env_idx
self.steps = 0
self.episode = 0
self.rall = 0
self.recent_rlist = deque(maxlen=100)
self.child_conn = child_conn
self.life_done = life_done
self.history_size = history_size
self.history = np.zeros([history_size, h, w])
self.h = h
self.w = w
self.reset()
def run(self):
super(MarioEnvironment, self).run()
while True:
action = self.child_conn.recv()
if self.is_render:
self.env.render()
obs, reward, done, info = self.env.step(action)
# when Mario loses life, changes the state to the terminal
# state.
if self.life_done:
if self.lives > info['life'] and info['life'] > 0:
force_done = True
self.lives = info['life']
else:
force_done = done
self.lives = info['life']
else:
force_done = done
# reward range -15 ~ 15
log_reward = reward / 15
self.rall += log_reward
r = log_reward
self.history[:3, :, :] = self.history[1:, :, :]
self.history[3, :, :] = self.pre_proc(obs)
self.steps += 1
if done:
self.recent_rlist.append(self.rall)
print(
"[Episode {}({})] Step: {} Reward: {} Recent Reward: {} Stage: {} current x:{} max x:{}".format(
self.episode,
self.env_idx,
self.steps,
self.rall,
np.mean(
self.recent_rlist),
info['stage'],
info['x_pos'],
self.max_pos))
self.history = self.reset()
self.child_conn.send([self.history[:, :, :], r, force_done, done, log_reward])
def reset(self):
self.last_action = 0
self.steps = 0
self.episode += 1
self.rall = 0
self.lives = 3
self.stage = 1
self.max_pos = 0
self.get_init_state(self.env.reset())
return self.history[:, :, :]
def pre_proc(self, X):
# grayscaling
x = cv2.cvtColor(X, cv2.COLOR_RGB2GRAY)
# resize
x = cv2.resize(x, (self.h, self.w))
return x
def get_init_state(self, s):
for i in range(self.history_size):
self.history[i, :, :] = self.pre_proc(s)