forked from openai/gym
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make RecordEpisodeStatistics work with VectorEnv (openai#2296)
* Make RecordEpisodeStatistics work with VectorEnv * fix test cases * fix lint * add test cases * fix linting * fix tests * fix test cases... * Update gym/wrappers/record_episode_statistics.py Co-authored-by: Tristan Deleu <[email protected]> * fix test cases * fix test cases again Co-authored-by: Tristan Deleu <[email protected]>
- Loading branch information
1 parent
b478717
commit 3b35ac4
Showing
2 changed files
with
54 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,57 @@ | ||
import time | ||
from collections import deque | ||
import numpy as np | ||
import gym | ||
|
||
|
||
class RecordEpisodeStatistics(gym.Wrapper): | ||
def __init__(self, env, deque_size=100): | ||
super(RecordEpisodeStatistics, self).__init__(env) | ||
self.env_is_vec = isinstance(env, gym.vector.VectorEnv) | ||
self.num_envs = getattr(env, "num_envs", 1) | ||
self.t0 = ( | ||
time.time() | ||
) # TODO: use perf_counter when gym removes Python 2 support | ||
self.episode_return = 0.0 | ||
self.episode_length = 0 | ||
self.episode_count = 0 | ||
self.episode_returns = None | ||
self.episode_lengths = None | ||
self.return_queue = deque(maxlen=deque_size) | ||
self.length_queue = deque(maxlen=deque_size) | ||
|
||
def reset(self, **kwargs): | ||
observation = super(RecordEpisodeStatistics, self).reset(**kwargs) | ||
self.episode_return = 0.0 | ||
self.episode_length = 0 | ||
return observation | ||
observations = super(RecordEpisodeStatistics, self).reset(**kwargs) | ||
self.episode_returns = np.zeros(self.num_envs, dtype=np.float32) | ||
self.episode_lengths = np.zeros(self.num_envs, dtype=np.int32) | ||
return observations | ||
|
||
def step(self, action): | ||
observation, reward, done, info = super(RecordEpisodeStatistics, self).step( | ||
observations, rewards, dones, infos = super(RecordEpisodeStatistics, self).step( | ||
action | ||
) | ||
self.episode_return += reward | ||
self.episode_length += 1 | ||
if done: | ||
info["episode"] = { | ||
"r": self.episode_return, | ||
"l": self.episode_length, | ||
"t": round(time.time() - self.t0, 6), | ||
} | ||
self.return_queue.append(self.episode_return) | ||
self.length_queue.append(self.episode_length) | ||
self.episode_return = 0.0 | ||
self.episode_length = 0 | ||
return observation, reward, done, info | ||
self.episode_returns += rewards | ||
self.episode_lengths += 1 | ||
if not self.env_is_vec: | ||
infos = [infos] | ||
dones = [dones] | ||
for i in range(len(dones)): | ||
if dones[i]: | ||
infos[i] = infos[i].copy() | ||
episode_return = self.episode_returns[i] | ||
episode_length = self.episode_lengths[i] | ||
episode_info = { | ||
"r": episode_return, | ||
"l": episode_length, | ||
"t": round(time.time() - self.t0, 6), | ||
} | ||
infos[i]["episode"] = episode_info | ||
self.return_queue.append(episode_return) | ||
self.length_queue.append(episode_length) | ||
self.episode_count += 1 | ||
self.episode_returns[i] = 0 | ||
self.episode_lengths[i] = 0 | ||
return ( | ||
observations, | ||
rewards, | ||
dones if self.env_is_vec else dones[0], | ||
infos if self.env_is_vec else infos[0], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters