-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
238d3e5
commit 91de829
Showing
4 changed files
with
60 additions
and
0 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
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,3 +1,5 @@ | ||
from .vec_env import VecEnv | ||
from .vec_env import VecEnvWrapper | ||
from .make_vec_env import make_vec_env | ||
|
||
from .record_episode_statistics import RecordEpisodeStatistics |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import time | ||
from collections import deque | ||
|
||
import gym | ||
|
||
|
||
class RecordEpisodeStatistics(gym.Wrapper): | ||
def __init__(self, env, deque_size=100): | ||
super().__init__(env) | ||
self.t0 = time.perf_counter() | ||
self.episode_return = 0.0 | ||
self.episode_horizon = 0 | ||
self.return_queue = deque(maxlen=deque_size) | ||
self.horizon_queue = deque(maxlen=deque_size) | ||
|
||
def reset(self, **kwargs): | ||
observation = super().reset(**kwargs) | ||
self.episode_return = 0.0 | ||
self.episode_horizon = 0 | ||
return observation | ||
|
||
def step(self, action): | ||
observation, reward, done, info = super().step(action) | ||
self.episode_return += reward | ||
self.episode_horizon += 1 | ||
if done: | ||
info['episode'] = {'return': self.episode_return, | ||
'horizon': self.episode_horizon, | ||
'time': round(time.perf_counter() - self.t0, 4)} | ||
self.return_queue.append(self.episode_return) | ||
self.horizon_queue.append(self.episode_horizon) | ||
self.episode_return = 0.0 | ||
self.episode_horizon = 0 | ||
return observation, reward, done, info |
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