Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added vector env support to StepAPICompatibility wrapper. #238

Merged
merged 1 commit into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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