-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom episode stats wrapper to minimize data passed through infos
- Loading branch information
1 parent
8eb0d5c
commit 3cc3b2e
Showing
7 changed files
with
63 additions
and
34 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
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
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,36 @@ | ||
import numpy as np | ||
import gymnasium | ||
|
||
import pufferlib.utils | ||
|
||
|
||
class EpisodeStatsWrapper(gymnasium.Wrapper): | ||
def __init__(self, env, *args, **kwargs): | ||
self.env = env | ||
self.observation_space = env.observation_space | ||
self.action_space = env.action_space | ||
self.reset() | ||
|
||
# TODO: Fix options. Maybe reimplement gymnasium.Wrapper with better compatibility | ||
def reset(self, seed=None): | ||
self.info = dict(episode_return=0, episode_length=0) | ||
return super().reset(seed=seed) | ||
|
||
def step(self, action): | ||
observation, reward, terminated, truncated, info = super().step(action) | ||
|
||
for k, v in pufferlib.utils.unroll_nested_dict(info): | ||
if "exploration_map" in k: | ||
self.info[k] = self.info.get(k, np.zeros_like(v)) + v | ||
else: | ||
self.info[k] = v | ||
|
||
# self.info['episode_return'].append(reward) | ||
self.info["episode_return"] += reward | ||
self.info["episode_length"] += 1 | ||
|
||
info = {} | ||
if terminated or truncated or self.info["episode_length"] % self.env.log_frequency == 0: | ||
info = self.info | ||
|
||
return observation, reward, terminated, truncated, 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
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