| 167 | |
| 168 | |
| 169 | class FrameStack(gym.Wrapper): |
| 170 | def __init__(self, env, k): |
| 171 | """Stack k last frames. |
| 172 | |
| 173 | Returns lazy array, which is much more memory efficient. |
| 174 | |
| 175 | See Also |
| 176 | -------- |
| 177 | baselines.common.atari_wrappers.LazyFrames |
| 178 | """ |
| 179 | gym.Wrapper.__init__(self, env) |
| 180 | self.k = k |
| 181 | self.frames = deque([], maxlen=k) |
| 182 | shp = env.observation_space.shape |
| 183 | self.observation_space = spaces.Box(low=0, high=255, shape=(shp[0], shp[1], shp[2] * k)) |
| 184 | |
| 185 | def reset(self): |
| 186 | ob = self.env.reset() |
| 187 | for _ in range(self.k): |
| 188 | self.frames.append(ob) |
| 189 | return self._get_ob() |
| 190 | |
| 191 | def step(self, action): |
| 192 | ob, reward, done, info = self.env.step(action) |
| 193 | self.frames.append(ob) |
| 194 | return self._get_ob(), reward, done, info |
| 195 | |
| 196 | def _get_ob(self): |
| 197 | assert len(self.frames) == self.k |
| 198 | return LazyFrames(list(self.frames)) |
| 199 | |
| 200 | |
| 201 | class LazyFrames: |