-
Notifications
You must be signed in to change notification settings - Fork 8.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make RecordEpisodeStatistics work with VectorEnv #2296
Merged
jkterry1
merged 11 commits into
openai:master
from
vwxyzjn:make-RecordEpisodeStatistics-work-with-vec-env
Aug 5, 2021
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
edd8b72
Make RecordEpisodeStatistics work with VectorEnv
vwxyzjn 1419822
fix test cases
vwxyzjn aed31bf
fix lint
vwxyzjn 0f25b42
add test cases
vwxyzjn 225e130
fix linting
vwxyzjn d48114b
fix tests
vwxyzjn a3e60dd
fix test cases...
vwxyzjn e6df452
Update gym/wrappers/record_episode_statistics.py
vwxyzjn c97d946
fix test cases
vwxyzjn d51328c
Merge branch 'make-RecordEpisodeStatistics-work-with-vec-env' of http…
vwxyzjn 72ac78d
fix test cases again
vwxyzjn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.num_envs
is not defined here ifenv
is aVectorEnv
instance.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t follow.
VectorEnv
has anum_envs
attribute, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh right the wrapper inherits the properties from
env
, sorry my bad!