-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAtariWrapper.py
373 lines (287 loc) · 11.1 KB
/
AtariWrapper.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
import gym
import numpy
from PIL import Image
class NopOpsEnv(gym.Wrapper):
def __init__(self, env=None, max_count=30):
super(NopOpsEnv, self).__init__(env)
self.max_count = max_count
def reset(self):
self.env.reset()
noops = numpy.random.randint(1, self.max_count + 1)
for _ in range(noops):
obs, _, done, _ = self.env.step(0)
if done:
self.env.reset()
obs, _, _, _ = self.env.step(1)
obs, _, _, _ = self.env.step(2)
return obs
def step(self, action):
obs, reward, done, info = self.env.step(action)
return obs, reward, done, info
class MaxAndSkipEnv(gym.Wrapper):
def __init__(self, env, skip=4):
gym.Wrapper.__init__(self, env)
self._obs_buffer = numpy.zeros((2,) + env.observation_space.shape, dtype=numpy.uint8)
self._skip = skip
def step(self, action):
total_reward = 0.0
done = None
for i in range(self._skip):
obs, reward, done, info = self.env.step(action)
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
max_frame = self._obs_buffer.max(axis=0)
return max_frame, total_reward, done, info
import numpy as np
def create_hollow_square_mask(height, width, thickness):
mask = np.zeros((height, width), dtype=np.uint8)
mask[thickness:-thickness, thickness:-thickness] = 1
mask[:thickness, :] = 0
mask[-thickness:, :] = 0
mask[:, :thickness] = 0
mask[:, -thickness:] = 0
return mask
import matplotlib.pyplot as plt
class ResizeEnv(gym.ObservationWrapper):
def __init__(self, env, height=96, width=96, frame_stacking=4):
super(ResizeEnv, self).__init__(env)
self.height = height
self.width = width
self.frame_stacking = frame_stacking
state_shape = (self.frame_stacking, self.height, self.width)
self.dtype = numpy.float32
self.observation_space = gym.spaces.Box(low=0.0, high=1.0, shape=state_shape, dtype=self.dtype)
self.state = numpy.zeros(state_shape, dtype=self.dtype)
# self.fig, self.ax = plt.subplots()
# plt.ion() # Turn on interactive mode
# def observation(self, state):
# img = Image.fromarray(state)
# img = img.convert('L')
# img = img.resize((self.height, self.width))
# for i in reversed(range(self.frame_stacking - 1)):
# self.state[i + 1] = self.state[i].copy()
# self.state[0] = (numpy.array(img).astype(self.dtype) / 255.0).copy()
# return self.state
def observation(self, state):
import matplotlib.pyplot as plt
img = Image.fromarray(state)
img = img.convert('L')
img = img.resize((self.height, self.width))
# Applying the hollow square mask
# thickness = 2
# mask = create_hollow_square_mask(self.height, self.width, 10)
# fig, ax = plt.subplots()
# plt.ion() # Turn on interactive mode
img_arr = np.array(img)
# img_arr[mask == 0] = 0 # Apply mask to the image array
# cover_height = int(self.height // 4) # Adjust as needed
# img_arr = np.array(img)
# img_arr[:cover_height, :] = 0
# plt.imshow(img_arr, cmap='gray')
# plt.title('Processed Image Array')
# plt.colorbar()
# plt.show()
# self.ax.imshow(img_arr, cmap='gray')
# # self.ax.set_title(f'Processed Image Array - Frame ')
# # plt.colorbar(self.ax.imshow(img_arr, cmap='gray'), ax=self.ax)
# plt.draw()
# plt.pause(0.1)
for i in reversed(range(self.frame_stacking - 1)):
self.state[i + 1] = self.state[i].copy()
self.state[0] = (img_arr.astype(self.dtype) / 255.0).copy()
return self.state
class FireResetEnv(gym.Wrapper):
def __init__(self, env):
gym.Wrapper.__init__(self, env)
def reset(self):
self.env.reset()
obs, _, done, _ = self.env.step(1)
if done:
self.env.reset()
obs, _, done, _ = self.env.step(2)
if done:
self.env.reset()
return obs
class EpisodicLifeEnv(gym.Wrapper):
def __init__(self, env, reward_scale=1.0, dense_rewards=1.0):
gym.Wrapper.__init__(self, env)
self.lives = 0
self.was_real_done = True
self.raw_episodes = 0
self.raw_score = 0.0
self.raw_score_per_episode = 0.0
self.raw_score_total = 0.0
self.reward_scale = reward_scale
self.dense_rewards = dense_rewards
def step(self, action):
obs, reward, done, info = self.env.step(action)
info['raw_score'] = reward
self.was_real_done = done
self.raw_score += reward
self.raw_score_total += reward
if self.was_real_done:
k = 0.1
self.raw_episodes += 1
self.raw_score_per_episode = (1.0 - k) * self.raw_score_per_episode + k * self.raw_score
self.raw_score = 0.0
if self.dense_rewards < numpy.random.rand():
reward = 0.0
lives = self.env.unwrapped.ale.lives()
if lives < self.lives and lives > 0:
done = True
reward = -1.0
if lives == 0 and self.inital_lives > 0:
reward = -1.0
self.lives = lives
reward = numpy.clip(self.reward_scale * reward, -1.0, 1.0)
return obs, reward, done, info
def reset(self, **kwargs):
if self.was_real_done:
obs = self.env.reset(**kwargs)
else:
obs, _, _, _ = self.env.step(1)
obs, _, _, _ = self.env.step(2)
obs, _, _, _ = self.env.step(0)
self.lives = self.env.unwrapped.ale.lives()
self.inital_lives = self.env.unwrapped.ale.lives()
return obs
class StickyActionEnv(gym.Wrapper):
def __init__(self, env, p=0.25):
super(StickyActionEnv, self).__init__(env)
self.p = p
self.last_action = 0
def step(self, action):
if numpy.random.uniform() < self.p:
action = self.last_action
self.last_action = action
return self.env.step(action)
def reset(self):
self.last_action = 0
return self.env.reset()
class RepeatActionEnv(gym.Wrapper):
def __init__(self, env):
gym.Wrapper.__init__(self, env)
self.successive_frame = numpy.zeros((2,) + self.env.observation_space.shape, dtype=numpy.uint8)
def reset(self, **kwargs):
return self.env.reset(**kwargs)
def step(self, action):
reward, done = 0, False
for t in range(4):
state, r, done, info = self.env.step(action)
if t == 2:
self.successive_frame[0] = state
elif t == 3:
self.successive_frame[1] = state
reward += r
if done:
break
state = self.successive_frame.max(axis=0)
return state, reward, done, info
class VisitedRoomInfo(gym.Wrapper):
"""Add number of unique visited rooms to the info dictionary.
For Atari games like MontezumaRevenge and Pitfall.
"""
def __init__(self, env, room_address):
gym.Wrapper.__init__(self, env)
self.room_address = room_address
self.visited_rooms = set()
self.unique_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.unique_rooms = self.unique_rooms.union(self.visited_rooms)
self.visited_rooms.add(self.get_current_room())
if done:
info['episode_visited_rooms'] = len(self.visited_rooms)
info['max_unique_rooms'] = len(self.unique_rooms)
self.visited_rooms.clear()
return obs, rew, done, info
class RawScoreEnv(gym.Wrapper):
def __init__(self, env, max_steps):
gym.Wrapper.__init__(self, env)
self.steps = 0
self.max_steps = max_steps
self.raw_episodes = 0
self.raw_score = 0.0
self.raw_score_per_episode = 0.0
self.raw_score_total = 0.0
def step(self, action):
obs, reward, done, info = self.env.step(action)
info['raw_score'] = reward
self.steps += 1
if self.steps >= self.max_steps:
self.steps = 0
done = True
self.raw_score += reward
self.raw_score_total += reward
if done:
self.steps = 0
self.raw_episodes += 1
k = 0.1
self.raw_score_per_episode = (1.0 - k) * self.raw_score_per_episode + k * self.raw_score
self.raw_score = 0.0
reward = max(0., float(numpy.sign(reward)))
return obs, reward, done, info
def reset(self):
self.env.seed(0)
self.steps = 0
return self.env.reset()
def WrapperAtari(env, height=96, width=96, frame_stacking=4, frame_skipping=4, reward_scale=1.0, dense_rewards=1.0):
env = NopOpsEnv(env)
env = FireResetEnv(env)
env = MaxAndSkipEnv(env, frame_skipping)
env = ResizeEnv(env, height, width, frame_stacking)
env = EpisodicLifeEnv(env, reward_scale, dense_rewards)
return env
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
def WrapperHardAtari(env, height=96, width=96, frame_stacking=1, max_steps=4500):
# env = StickyActionEnv(env)
env = RepeatActionEnv(env)
env = ResizeEnv(env, height, width, frame_stacking)
env = RawScoreEnv(env, max_steps)
env_name = str(env)
env = VisitedRoomInfo(env, room_address=3 if 'Montezuma' in env_name else 1)
return env
def WrapperAtariSparseRewards(env, height=96, width=96, frame_stacking=4, frame_skipping=4):
return WrapperAtari(env, height, width, frame_stacking, dense_rewards=0.2)
import gym
# import gym
# game = 'Pitfall'
# actions = [1, 2, 0, 0, 1, 0, 1, 0, 0, 0, 1, 2, 2, 1, 1, 2, 1, 0, 2, 1, 1, 0, 1, 2, 2, 1, 1, 0, 2, 1, 0, 0, 1, 0, 2, 2, 2, 2, 2, 0, 1, 2, 1, 2, 1, 1, 2, 1, 1, 2, 0, 2, 0, 2, 2, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0]
# final_ob_in_last_game = None
# count = 0
# env = gym.make(game+'NoFrameskip-v4')
# env = WrapperHardAtari(env)
# # env.seed(0)
# env.reset()
# done = False
# while not done:
# count += 1
# print('Game', count)
# # env = gym.make(game+'NoFrameskip-v4')
# # env.seed(0)
# # env.reset()
# # env.seed(0)
# for action in actions:
# for _ in range(4):
# ob, re, done, info = env.step(env.action_space.sample())
# if done:
# print()
# if final_ob_in_last_game is not None and not ((final_ob_in_last_game == ob).all()):
# print('Different observation!')
# final_ob_in_last_game = ob.copy()
# env.close()