-
Notifications
You must be signed in to change notification settings - Fork 9
/
wrappers.py
393 lines (325 loc) · 12.6 KB
/
wrappers.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import itertools
from collections import deque
from copy import copy
import gym
import numpy as np
from PIL import Image
from mpi4py import MPI
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 MaxAndSkipEnv(gym.Wrapper):
def __init__(self, env, 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 = deque(maxlen=2)
self._skip = skip
def step(self, action):
"""Repeat action, sum reward, and max over last observations."""
total_reward = 0.0
done = None
acc_info = {}
for _ in range(self._skip):
obs, reward, done, info = self.env.step(action)
acc_info.update(info)
self._obs_buffer.append(obs)
total_reward += reward
if done:
break
max_frame = np.max(np.stack(self._obs_buffer), axis=0)
return max_frame, total_reward, done, acc_info
def reset(self):
"""Clear past frame buffer and init. to first obs. from inner env."""
self._obs_buffer.clear()
obs = self.env.reset()
self._obs_buffer.append(obs)
return obs
class ProcessFrame84(gym.ObservationWrapper):
def __init__(self, env, crop=True):
self.crop = crop
super(ProcessFrame84, self).__init__(env)
self.observation_space = gym.spaces.Box(low=0, high=255, shape=(84, 84, 1), dtype=np.uint8)
def observation(self, obs):
return ProcessFrame84.process(obs, crop=self.crop)
@staticmethod
def process(frame, crop=True):
if frame.size == 210 * 160 * 3:
img = np.reshape(frame, [210, 160, 3]).astype(np.float32)
elif frame.size == 250 * 160 * 3:
img = np.reshape(frame, [250, 160, 3]).astype(np.float32)
elif frame.size == 224 * 240 * 3: # mario resolution
img = np.reshape(frame, [224, 240, 3]).astype(np.float32)
elif frame.size == 224 * 320 * 3: # sonic
img = np.reshape(frame, [224, 320, 3]).astype(np.float32)
else:
assert False, "Unknown resolution." + str(frame.size)
img = img[:, :, 0] * 0.299 + img[:, :, 1] * 0.587 + img[:, :, 2] * 0.114
size = (84, 110 if crop else 84)
resized_screen = np.array(Image.fromarray(img).resize(size,
resample=Image.BILINEAR), dtype=np.uint8)
x_t = resized_screen[18:102, :] if crop else resized_screen
x_t = np.reshape(x_t, [84, 84, 1])
return x_t.astype(np.uint8)
class ExtraTimeLimit(gym.Wrapper):
def __init__(self, env, max_episode_steps=None):
gym.Wrapper.__init__(self, env)
self._max_episode_steps = max_episode_steps
self._elapsed_steps = 0
def step(self, action):
observation, reward, done, info = self.env.step(action)
self._elapsed_steps += 1
if self._elapsed_steps > self._max_episode_steps:
done = True
return observation, reward, done, info
def reset(self):
self._elapsed_steps = 0
return self.env.reset()
class AddRandomStateToInfo(gym.Wrapper):
def __init__(self, env):
"""Adds the random state to the info field on the first step after reset
"""
gym.Wrapper.__init__(self, env)
def step(self, action):
ob, r, d, info = self.env.step(action)
if self.random_state_copy is not None:
info['random_state'] = self.random_state_copy
self.random_state_copy = None
return ob, r, d, info
def reset(self, **kwargs):
""" Do no-op action for a number of steps in [1, noop_max]."""
self.random_state_copy = copy(self.unwrapped.np_random)
return self.env.reset(**kwargs)
class MontezumaInfoWrapper(gym.Wrapper):
ram_map = {
"room": dict(
index=3,
),
"x": dict(
index=42,
),
"y": dict(
index=43,
),
}
def __init__(self, env):
super(MontezumaInfoWrapper, self).__init__(env)
self.visited = set()
self.visited_rooms = set()
def step(self, action):
obs, rew, done, info = self.env.step(action)
ram_state = unwrap(self.env).ale.getRAM()
for name, properties in MontezumaInfoWrapper.ram_map.items():
info[name] = ram_state[properties['index']]
pos = (info['x'], info['y'], info['room'])
self.visited.add(pos)
self.visited_rooms.add(info["room"])
if done:
info['mz_episode'] = dict(pos_count=len(self.visited),
visited_rooms=copy(self.visited_rooms))
self.visited.clear()
self.visited_rooms.clear()
return obs, rew, done, info
def reset(self):
return self.env.reset()
class MarioXReward(gym.Wrapper):
def __init__(self, env):
gym.Wrapper.__init__(self, env)
self.current_level = [0, 0]
self.visited_levels = set()
self.visited_levels.add(tuple(self.current_level))
self.current_max_x = 0.
def reset(self):
ob = self.env.reset()
self.current_level = [0, 0]
self.visited_levels = set()
self.visited_levels.add(tuple(self.current_level))
self.current_max_x = 0.
return ob
def step(self, action):
ob, reward, done, info = self.env.step(action)
levellow, levelhigh, xscrollHi, xscrollLo = \
info["levelLo"], info["levelHi"], info["xscrollHi"], info["xscrollLo"]
currentx = xscrollHi * 256 + xscrollLo
new_level = [levellow, levelhigh]
if new_level != self.current_level:
self.current_level = new_level
self.current_max_x = 0.
reward = 0.
self.visited_levels.add(tuple(self.current_level))
else:
if currentx > self.current_max_x:
delta = currentx - self.current_max_x
self.current_max_x = currentx
reward = delta
else:
reward = 0.
if done:
info["levels"] = copy(self.visited_levels)
info["retro_episode"] = dict(levels=copy(self.visited_levels))
return ob, reward, done, info
class LimitedDiscreteActions(gym.ActionWrapper):
KNOWN_BUTTONS = {"A", "B"}
KNOWN_SHOULDERS = {"L", "R"}
'''
Reproduces the action space from curiosity paper.
'''
def __init__(self, env, all_buttons, whitelist=KNOWN_BUTTONS | KNOWN_SHOULDERS):
gym.ActionWrapper.__init__(self, env)
self._num_buttons = len(all_buttons)
button_keys = {i for i in range(len(all_buttons)) if all_buttons[i] in whitelist & self.KNOWN_BUTTONS}
buttons = [(), *zip(button_keys), *itertools.combinations(button_keys, 2)]
shoulder_keys = {i for i in range(len(all_buttons)) if all_buttons[i] in whitelist & self.KNOWN_SHOULDERS}
shoulders = [(), *zip(shoulder_keys), *itertools.permutations(shoulder_keys, 2)]
arrows = [(), (4,), (5,), (6,), (7,)] # (), up, down, left, right
acts = []
acts += arrows
acts += buttons[1:]
acts += [a + b for a in arrows[-2:] for b in buttons[1:]]
self._actions = acts
self.action_space = gym.spaces.Discrete(len(self._actions))
def action(self, a):
mask = np.zeros(self._num_buttons)
for i in self._actions[a]:
mask[i] = 1
return mask
class LimitedSonicDiscreteActions(gym.ActionWrapper):
KNOWN_BUTTONS = {"B"}
KNOWN_SHOULDERS = {"L", "R"}
'''
Reproduces the action space from curiosity paper.
'''
def __init__(self, env, all_buttons, whitelist=KNOWN_BUTTONS | KNOWN_SHOULDERS):
gym.ActionWrapper.__init__(self, env)
self._num_buttons = len(all_buttons)
button_keys = {i for i in range(len(all_buttons)) if all_buttons[i] in whitelist & self.KNOWN_BUTTONS}
buttons = [(), *zip(button_keys), *itertools.combinations(button_keys, 2)]
arrows = [(), (5,), (6,), (7,)] # (), down, left, right
acts = []
acts += arrows
acts += buttons[1:]
double_arrows = [ (5,6), (5,7), ]
acts += double_arrows
acts += [a + b for a in arrows[-3:] for b in buttons[1:]]
self._actions = acts
self.action_space = gym.spaces.Discrete(len(self._actions))
def action(self, a):
mask = np.zeros(self._num_buttons)
for i in self._actions[a]:
mask[i] = 1
return mask
class FrameSkip(gym.Wrapper):
def __init__(self, env, n):
gym.Wrapper.__init__(self, env)
self.n = n
def step(self, action):
done = False
totrew = 0
for _ in range(self.n):
ob, rew, done, info = self.env.step(action)
totrew += rew
if done: break
return ob, totrew, done, info
def make_mario_env(crop=True, frame_stack=True, clip_rewards=False):
assert clip_rewards is False
import gym
import retro
import os
from baselines.common.atari_wrappers import FrameStack
# gym.undo_logger_setup()
if MPI.COMM_WORLD.Get_rank() == 0:
if not os.path.isdir('./mario_bk2'):
os.mkdir('./mario_bk2')
env = retro.make('SuperMarioBros-Nes', 'Level1-1', record='./mario_bk2')
else:
env = retro.make('SuperMarioBros-Nes', 'Level1-1')
buttons = env.buttons
env = MarioXReward(env)
env = FrameSkip(env, 4)
env = ProcessFrame84(env, crop=crop)
if frame_stack:
env = FrameStack(env, 4)
env = LimitedDiscreteActions(env, buttons)
return env
class OneChannel(gym.ObservationWrapper):
def __init__(self, env, crop=True):
self.crop = crop
super(OneChannel, self).__init__(env)
assert env.observation_space.dtype == np.uint8
self.observation_space = gym.spaces.Box(low=0, high=255, shape=(84, 84, 1), dtype=np.uint8)
def observation(self, obs):
return obs[:, :, 2:3]
class RetroALEActions(gym.ActionWrapper):
def __init__(self, env, all_buttons, n_players=1):
gym.ActionWrapper.__init__(self, env)
self.n_players = n_players
self._num_buttons = len(all_buttons)
bs = [-1, 0, 4, 5, 6, 7]
actions = []
def update_actions(old_actions, offset=0):
actions = []
for b in old_actions:
for button in bs:
action = []
action.extend(b)
if button != -1:
action.append(button + offset)
actions.append(action)
return actions
current_actions = [[]]
for i in range(self.n_players):
current_actions = update_actions(current_actions, i * self._num_buttons)
self._actions = current_actions
self.action_space = gym.spaces.Discrete(len(self._actions))
def action(self, a):
mask = np.zeros(self._num_buttons * self.n_players)
for i in self._actions[a]:
mask[i] = 1
return mask
class NoReward(gym.Wrapper):
def __init__(self, env):
gym.Wrapper.__init__(self, env)
def step(self, action):
ob, rew, done, info = self.env.step(action)
return ob, 0.0, done, info
def make_multi_pong(frame_stack=True):
import gym
import retro
from baselines.common.atari_wrappers import FrameStack
gym.undo_logger_setup()
game_env = env = retro.make('Pong-Atari2600', players=2)
env = RetroALEActions(env, game_env.BUTTONS, n_players=2)
env = NoReward(env)
env = FrameSkip(env, 4)
env = ProcessFrame84(env, crop=False)
if frame_stack:
env = FrameStack(env, 4)
return env
def make_robo_pong(frame_stack=True):
from baselines.common.atari_wrappers import FrameStack
import roboenvs as robo
env = robo.make_robopong()
env = robo.DiscretizeActionWrapper(env, 2)
env = robo.MultiDiscreteToUsual(env)
env = OneChannel(env)
if frame_stack:
env = FrameStack(env, 4)
env = AddRandomStateToInfo(env)
return env
def make_robo_hockey(frame_stack=True):
from baselines.common.atari_wrappers import FrameStack
import roboenvs as robo
env = robo.make_robohockey()
env = robo.DiscretizeActionWrapper(env, 2)
env = robo.MultiDiscreteToUsual(env)
env = OneChannel(env)
if frame_stack:
env = FrameStack(env, 4)
env = AddRandomStateToInfo(env)
return env