-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwrappers.py
225 lines (179 loc) · 7.05 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
"""
Basic wrappers, useful for reinforcement learning on gym envs
https://github.com/hill-a/stable-baselines/blob/master/stable_baselines/common/atari_wrappers.py
"""
from collections import deque
import cv2
import gym
import numpy as np
from gym import spaces
from nes_py.wrappers import JoypadSpace
cv2.ocl.setUseOpenCL(False)
class FireResetEnv(gym.Wrapper):
def __init__(self, env):
"""
Take action on reset for environments that are fixed until firing.
:param env: (Gym Environment) the environment to wrap
"""
gym.Wrapper.__init__(self, env)
assert env.unwrapped.get_action_meanings()[1] == 'FIRE'
assert len(env.unwrapped.get_action_meanings()) >= 3
def reset(self, **kwargs):
self.env.reset(**kwargs)
obs, _, done, _ = self.env.step(1)
if done:
self.env.reset(**kwargs)
obs, _, done, _ = self.env.step(2)
if done:
self.env.reset(**kwargs)
return obs
def step(self, action):
return self.env.step(action)
class MaxAndSkipEnv(gym.Wrapper):
def __init__(self, env, skip=4):
"""
Return only every `skip`-th frame (frameskipping)
:param env: (Gym Environment) the environment
:param skip: (int) number of `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=env.observation_space.dtype)
self._skip = skip
def step(self, action):
"""
Step the environment with the given action
Repeat action, sum reward, and max over last observations.
:param action: ([int] or [float]) the action
:return: ([int] or [float], [float], [bool], dict) observation, reward, done, information
"""
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
# 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 FrameDownSample(gym.ObservationWrapper):
def __init__(self, env):
"""
Down sample frames to 84x84 as done in the Nature paper and later work.
:param env: (Gym Environment) the environment
"""
gym.ObservationWrapper.__init__(self, env)
self.width = 84
self.height = 84
self.observation_space = spaces.Box(low=0, high=255, shape=(self.height, self.width, 1),
dtype=env.observation_space.dtype)
def observation(self, frame):
"""
returns the current observation from a frame
:param frame: ([int] or [float]) environment frame
:return: ([int] or [float]) the observation
"""
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
frame = cv2.resize(frame, (self.width, self.height), interpolation=cv2.INTER_AREA)
return frame[:, :, None]
class LazyFrameStack(gym.Wrapper):
def __init__(self, env, n_frames):
"""Stack n_frames last frames.
Returns lazy array, which is much more memory efficient.
See Also
--------
stable_baselines.common.atari_wrappers.LazyFrames
NOTE: Changed high from 255 to 1.0 to match with FrameDownSample observation_space
:param env: (Gym Environment) the environment
:param n_frames: (int) the number of frames to stack
"""
gym.Wrapper.__init__(self, env)
self.n_frames = n_frames
self.frames = deque([], maxlen=n_frames)
shp = env.observation_space.shape
self.observation_space = spaces.Box(low=0, high=1.0, shape=(shp[0], shp[1], shp[2] * n_frames),
dtype=env.observation_space.dtype)
def reset(self, **kwargs):
obs = self.env.reset(**kwargs)
for _ in range(self.n_frames):
self.frames.append(obs)
return self._get_ob()
def step(self, action):
obs, reward, done, info = self.env.step(action)
self.frames.append(obs)
return self._get_ob(), reward, done, info
def _get_ob(self):
assert len(self.frames) == self.n_frames
return LazyFrames(list(self.frames))
class ScaledFloatFrame(gym.ObservationWrapper):
def __init__(self, env):
gym.ObservationWrapper.__init__(self, env)
self.observation_space = spaces.Box(low=0, high=1.0, shape=env.observation_space.shape, dtype=np.float32)
def observation(self, observation):
# careful! This undoes the memory optimization, use
# with smaller replay buffers only.
return np.array(observation).astype(np.float32) / 255.0
class LazyFrames(object):
def __init__(self, frames):
"""
This object ensures that common frames between the observations are only stored once.
It exists purely to optimize memory usage which can be huge for DQN's 1M frames replay
buffers.
This object should only be converted to np.ndarray before being passed to the model.
:param frames: ([int] or [float]) environment frames
"""
self._frames = frames
self._out = None
def _force(self):
if self._out is None:
self._out = np.concatenate(self._frames, axis=2)
self._frames = None
return self._out
def __array__(self, dtype=None):
out = self._force()
if dtype is not None:
out = out.astype(dtype)
return out
def __len__(self):
return len(self._force())
def __getitem__(self, i):
return self._force()[i]
class CustomReward(gym.Wrapper):
def __init__(self, env):
gym.Wrapper.__init__(self, env)
self._current_score = 0
def step(self, action):
state, reward, done, info = self.env.step(action)
reward += (info['score'] - self._current_score) / 40.0
self._current_score = info['score']
if done:
if info['flag_get']:
reward += 350.0
else:
reward -= 50.0
return state, reward / 10.0, done, info
def wrap_nes(env_id, action_space):
"""
Configure environment for NES.
:param env_id: (str) the environment ID
:param action_space: (list) action space
:return: (Gym Environment) the wrapped environment
"""
env = gym.make(env_id)
env = JoypadSpace(env, action_space)
env = MaxAndSkipEnv(env)
if 'FIRE' in env.unwrapped.get_action_meanings():
env = FireResetEnv(env)
env = FrameDownSample(env)
env = ScaledFloatFrame(env)
env = LazyFrameStack(env, 4)
env = CustomReward(env)
return env