Skip to content

Commit

Permalink
Added vector env support to StepAPICompatibility wrapper. (openai#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhishs authored Jan 5, 2023
1 parent 300b383 commit e8897ff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
13 changes: 5 additions & 8 deletions gymnasium/wrappers/step_api_compatibility.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
"""Implementation of StepAPICompatibility wrapper class for transforming envs between new and old step API."""
import gymnasium as gym
from gymnasium.logger import deprecation
from gymnasium.utils.step_api_compatibility import (
convert_to_done_step_api,
convert_to_terminated_truncated_step_api,
)
from gymnasium.utils.step_api_compatibility import step_api_compatibility


class StepAPICompatibility(gym.Wrapper):
Expand Down Expand Up @@ -36,6 +33,7 @@ def __init__(self, env: gym.Env, output_truncation_bool: bool = True):
output_truncation_bool (bool): Whether the wrapper's step method outputs two booleans (new API) or one boolean (old API)
"""
super().__init__(env)
self.is_vector_env = isinstance(env.unwrapped, gym.vector.VectorEnv)
self.output_truncation_bool = output_truncation_bool
if not self.output_truncation_bool:
deprecation(
Expand All @@ -52,7 +50,6 @@ def step(self, action):
(observation, reward, terminated, truncated, info) or (observation, reward, done, info)
"""
step_returns = self.env.step(action)
if self.output_truncation_bool:
return convert_to_terminated_truncated_step_api(step_returns)
else:
return convert_to_done_step_api(step_returns)
return step_api_compatibility(
step_returns, self.output_truncation_bool, self.is_vector_env
)
16 changes: 16 additions & 0 deletions tests/wrappers/test_step_compatibility.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import numpy as np
import pytest

import gymnasium as gym
from gymnasium.spaces import Discrete
from gymnasium.vector import AsyncVectorEnv, SyncVectorEnv
from gymnasium.wrappers import StepAPICompatibility


Expand Down Expand Up @@ -54,6 +56,20 @@ def test_step_compatibility_to_old_api(env):
assert isinstance(done, bool)


@pytest.mark.parametrize("vector_env", [SyncVectorEnv, AsyncVectorEnv])
def test_vector_env_step_compatibility_to_old_api(vector_env):
num_envs = 2
env = vector_env([NewStepEnv for _ in range(num_envs)])
old_env = StepAPICompatibility(env, False)

step_returns = old_env.step([0] * num_envs)
assert len(step_returns) == 4
_, _, dones, _ = step_returns
assert isinstance(dones, np.ndarray)
for done in dones:
assert isinstance(done, np.bool_)


@pytest.mark.parametrize("apply_api_compatibility", [None, True, False])
def test_step_compatibility_in_make(apply_api_compatibility):
gym.register("OldStepEnv-v0", entry_point=OldStepEnv)
Expand Down

0 comments on commit e8897ff

Please sign in to comment.