-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.py
executable file
·411 lines (361 loc) · 13 KB
/
environment.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
from __future__ import division
import gym
import gym_super_mario_bros
from gym import Wrapper, ObservationWrapper
import numpy as np
import torch
from collections import deque
from gym.spaces.box import Box
from cv2 import resize, INTER_AREA, INTER_NEAREST
import random
import gym_super_mario_bros
from nes_py.wrappers import JoypadSpace
import cv2
import random
from random import randint, randrange
from math import sqrt
import pickle
STAGE_LIST = [
"1-1",
"1-2",
"1-3",
"1-4",
"2-1",
"2-2",
"2-3",
"2-4",
"3-1",
"3-2",
"3-3",
"3-4",
"4-1",
"4-2",
"4-3",
"4-4",
"5-1",
"5-2",
"5-3",
"5-4",
"6-1",
"6-2",
"6-3",
"6-4",
"7-1",
"7-2",
"7-3",
"7-4",
"8-1",
"8-2",
"8-3",
"8-4",
]
CUSTOM_MOVEMENT = [
["NOOP"],
["right"],
["right", "A"],
["right", "B"],
["right", "A", "B"],
["A"],
["left", "A"],
["left", "B"],
["left", "A", "B"],
["down"],
]
word_stage_time = {
1: {1: 400, 2: 400, 3: 300, 4: 300},
2: {1: 400, 2: 400, 3: 300, 4: 300},
3: {1: 400, 2: 300, 3: 300, 4: 300},
4: {1: 400, 2: 400, 3: 300, 4: 400},
5: {1: 300, 2: 400, 3: 300, 4: 300},
6: {1: 400, 2: 400, 3: 300, 4: 300},
7: {1: 400, 2: 400, 3: 300, 4: 400},
8: {1: 300, 2: 400, 3: 300, 4: 400},
}
def mario_env(env_id, args):
env = gym.make(env_id)
env = EpisodicLifeEnv(env, args.time_per_stage)
env = NoopResetEnv(env, noop_max=30)
env = MaxAndSkipEnv(env, skip=args.skip_rate)
env = RewardWrapper(env)
env._max_episode_steps = args.max_episode_length
env = MarioRescale(env)
env = frame_stack(env)
env = NormalizedEnv(env)
env = JoypadSpace(env, CUSTOM_MOVEMENT)
return env
def process_frame(frame):
frame = frame[15:-25, 43:-13]
frame = resize(frame, (100, 100), INTER_AREA)
frame = resize(frame, (80, 80), INTER_AREA)
frame = 0.2989 * frame[:, :, 0] + 0.587 * frame[:, :, 1] + 0.114 * frame[:, :, 2]
return frame.astype(np.float32)
class MarioRescale(ObservationWrapper):
def __init__(self, env):
ObservationWrapper.__init__(self, env)
self.observation_space = Box(0.0, 1.0, [4, 80, 80], dtype=np.uint8)
self.obs_keep = None
def observation(self, observation):
return process_frame(observation)
class NoopResetEnv(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.
"""
Wrapper.__init__(self, env)
self.env = env
self.noop_max = noop_max
self.override_num_noops = None
self.noop_action = 0
def reset(self, **kwargs):
"""Do no-op action for a number of steps in [0, noop_max]."""
obs = self.env.reset(**kwargs)
if self.override_num_noops is not None:
noops = self.override_num_noops
else:
noops = randint(0, self.noop_max)
for _ in range(noops):
obs, _, done, _ = self.env.step(self.noop_action)
if done:
obs = self.env.reset(**kwargs)
return obs
class EpisodicLifeEnv(Wrapper):
def __init__(self, env, tps):
"""Make end-of-life == end-of-episode, make end-of-stage == end-of-episode, and make end-of-time == end-of-episode, but only reset on true game over."""
super().__init__(env)
self.env = env
self.lives = 0
self.time_per_stage = tps
self.was_real_done = True
self.time_limit = False
self.world_num = self.env.unwrapped._world
self.stage_num = self.env.unwrapped._stage
self.singleStage = self.env.unwrapped.is_single_stage_env
self.world_stage_time = word_stage_time
self.time_level = max(
self.world_stage_time[self.world_num][self.stage_num] - self.time_per_stage,
0,
)
self.pen_flag = False
self.rew_flag = False
def step(self, action):
obs, reward, done, info = self.env.step(action)
self.obs_last = obs
self.was_real_done = done
world = self.env.unwrapped._world
stage = self.env.unwrapped._stage
lives = self.env.unwrapped._life
if lives < self.lives and lives != 255:
done = True
self.pen_flag = True
if world > self.world_num or stage > self.stage_num and not done:
self.world_num = world
self.stage_num = stage
self.time_level = max(
self.world_stage_time[self.world_num][self.stage_num]
- self.time_per_stage,
0,
)
done = True
self.rew_flag = True
if info["time"] < self.time_level and not done:
if lives == 0 or self.singleStage:
self.was_real_done = True
else:
self.time_limit = True
done = True
self.pen_flag = True
self.lives = lives
if self.lives == 255:
self.pen_flag = True
return obs, reward, done, info
def reset(self, **kwargs):
if self.was_real_done:
obs = self.env.reset(**kwargs)
else:
if self.time_limit:
self.env.unwrapped._kill_mario()
obs = self.obs_last
self.time_limit = False
self.lives = self.env.unwrapped._life
self.world_num = self.env.unwrapped._world
self.stage_num = self.env.unwrapped._stage
self.singleStage = self.env.unwrapped.is_single_stage_env
self.time_level = max(
self.world_stage_time[self.world_num][self.stage_num] - self.time_per_stage,
0,
)
return obs
class RewardWrapper(Wrapper):
def __init__(self, env=None):
"""Reward Mario for each new point further right he achieves"""
super().__init__(env)
self.env = env
self.old_position = self.env.unwrapped._x_position
self.singleStage = self.env.unwrapped.is_single_stage_env
self.buzzer_list = []
def step(self, action):
self.old_position = max(self.old_position, self.env.unwrapped._x_position)
total_reward = 0
obs, reward, done, info = self.env.step(action)
if info["x_pos"] > 60000: # In case of glitch with position on some levels
info["x_pos"] = 1
reward = min(max(info["x_pos"] - self.old_position, 0.0), 30.0)
if (
info["world"] == 7 and info["stage"] == 4
): # To simulate game reward sound when proper path achieved on level. Agent still needs to figure out what that path is to achieve reward
if info["x_pos"] < 1700:
self.buzzer_list = []
if len(self.buzzer_list) > 0:
if self.buzzer_list[0] <= (self.env.env.env.env._elapsed_steps - 400):
self.buzzer_list = []
if ((1700 < info["x_pos"] < 1846) and info["y_pos"] >= 191) and len(
self.buzzer_list
) == 0:
self.buzzer_list.append(self.env.env.env.env._elapsed_steps)
elif (
((1903 <= info["x_pos"] <= 1927) and info["y_pos"] == 191)
and len(self.buzzer_list) == 1
and self.buzzer_list[0] > (self.env.env.env.env._elapsed_steps - 400)
):
self.buzzer_list.append("First_Spot")
elif (1984 <= info["x_pos"] <= 2080) and len(self.buzzer_list) >= 2:
if info["y_pos"] == 127:
if "Main_Spot" not in set(self.buzzer_list):
self.buzzer_list.append("Main_Spot")
else:
self.buzzer_list = []
elif (
(2114 <= info["x_pos"] <= 2164)
and info["y_pos"] == 127
and len(self.buzzer_list) == 3
):
self.buzzer_list.append("Last_Spot")
elif (
(2200 < info["x_pos"] <= 2440)
and info["y_pos"] >= 191
and len(self.buzzer_list) == 4
and self.buzzer_list[0] > (self.env.env.env.env._elapsed_steps - 400)
):
if set(["First_Spot", "Main_Spot", "Last_Spot"]) < set(
self.buzzer_list
):
reward = 30
self.old_position = info["x_pos"]
self.buzzer_list = []
# Below is to deter agent from using warp zone to skip to other worlds. If you do want agent to learn discover warp zones and use them just comment out two lines below
elif (
(info["world"] == 1 and info["stage"] == 2)
or (info["world"] == 4 and info["stage"] == 2)
) and info["y_pos"] > 254:
reward = -1.0
if info["y_pos"] < 70: # Mario is falling and about to die so penalize
reward = -1.0
total_reward += reward
if done and not self.singleStage: # Rewards for passing stage or dying
if self.env.env.env.pen_flag:
total_reward = -30.0
if self.env.env.env.rew_flag:
total_reward = 30.0
elif done: # Rewards for passing stage or dying for single stage environment
if info["flag_get"]:
total_reward = 30.0
else:
total_reward = -30.0
return obs, total_reward, done, info
def reset(self, **kwargs):
obs = self.env.reset(**kwargs)
self.old_position = self.env.unwrapped._x_position
self.buzzer_list = []
return obs
class MaxAndSkipEnv(Wrapper):
def __init__(self, env=None, skip=4):
"""Return only every `skip`-th frame"""
super().__init__(env)
# most recent raw observations (for max pooling across time steps)
self.env = env
self._obs_buffer = deque(maxlen=1)
self._skip = skip
self.buzzer_list = []
def step(self, action):
done = None
for _ in range(self._skip):
obs, reward, done, info = self.env.step(action)
self._obs_buffer.append(obs)
if done:
break
max_frame = np.max(np.stack(self._obs_buffer), axis=0)
return max_frame, reward, done, info
def reset(self, **kwargs):
"""Clear past frame buffer and init. to first obs. from inner env."""
self._obs_buffer.clear()
obs = self.env.reset(**kwargs)
self.buzzer_list = []
self._obs_buffer.append(obs)
return obs
class frame_stack(Wrapper):
def __init__(self, env, stack_frames=4):
Wrapper.__init__(self, env)
self.stack_frames = stack_frames
self.frames = deque([], maxlen=self.stack_frames)
def reset(self):
ob = self.env.reset()
for _ in range(self.stack_frames):
self.frames.append(ob)
return self.observation_stack()
def step(self, action):
ob, rew, done, info = self.env.step(action)
self.frames.append(ob)
return self.observation_stack(), rew, done, info
def observation_stack(self):
assert len(self.frames) == self.stack_frames
return np.stack(self.frames, axis=0).reshape((1, 4, 80, 80))
class RunningMeanStd(object):
def __init__(self, epsilon=1e-4, shape=()):
self.mean = np.zeros(shape, "float32")
self.var = np.ones(shape, "float32")
self.count = epsilon
def update(self, arr):
batch_mean = np.mean(arr)
batch_var = np.var(arr)
batch_count = arr.shape[0]
self.update_from_moments(batch_mean, batch_var, batch_count)
def update_from_moments(self, batch_mean, batch_var, batch_count):
delta = batch_mean - self.mean
tot_count = self.count + batch_count
new_mean = self.mean + delta * batch_count / tot_count
m_a = self.var * self.count
m_b = batch_var * batch_count
m_2 = (
m_a
+ m_b
+ np.square(delta) * self.count * batch_count / (self.count + batch_count)
)
new_var = m_2 / (self.count + batch_count)
new_count = batch_count + self.count
self.mean = new_mean
self.var = new_var
self.count = new_count
class NormalizedEnv(ObservationWrapper):
def __init__(self, env):
ObservationWrapper.__init__(self, env)
self.obs_rms = RunningMeanStd(shape=())
self.is_training = False
def set_training_on(self):
self.is_training = True
def set_training_off(self):
self.is_training = False
def observation(self, obs):
if self.is_training:
self.obs_rms.update(obs)
obs = np.clip(
(obs - self.obs_rms.mean) / np.sqrt(self.obs_rms.var + 1e-8), -10.0, 10.0
)
return obs
def load(self, path):
with open(f"{path}/obs_rms.pkl", "rb") as file_handler:
vec_normalize = pickle.load(file_handler)
self.obs_rms = vec_normalize
return
def save(self, path):
with open(f"{path}/obs_rms.pkl", "wb") as file_handler:
pickle.dump(self.obs_rms, file_handler)