Back to Repositories

Validating Action Space Rescaling in OpenAI Gym

This test suite validates the RescaleAction wrapper functionality in OpenAI Gym, ensuring proper action space scaling between environments. It verifies the wrapper’s behavior with different environments and validates error handling for invalid action ranges.

Test Coverage Overview

The test suite provides comprehensive coverage of the RescaleAction wrapper functionality.

Key areas tested include:
  • Action space validation for incompatible environments (CartPole)
  • Proper scaling of actions in compatible environments (Pendulum)
  • State observation consistency before and after wrapping
  • Reward preservation across wrapped environments
  • Error handling for out-of-bounds actions

Implementation Analysis

The testing approach utilizes pytest fixtures and numpy assertions to validate action space transformations. The implementation employs direct comparison testing with both wrapped and unwrapped environments, ensuring consistent behavior across different action ranges.

Key patterns include:
  • Environment state synchronization using fixed seeds
  • Numerical comparison using numpy’s allclose()
  • Exception handling validation for invalid scenarios

Technical Details

Testing tools and configuration:
  • pytest framework for test organization
  • numpy for numerical comparisons
  • gym.make() for environment instantiation
  • RescaleAction wrapper for action space transformation
  • CartPole-v1 and Pendulum-v1 environments for testing
  • Environment checker disabled for testing purposes

Best Practices Demonstrated

The test suite exemplifies several testing best practices in reinforcement learning environment validation.

Notable practices include:
  • Isolated environment creation and cleanup
  • Deterministic testing through seed control
  • Explicit error case validation
  • Numerical precision handling
  • Comprehensive state and reward verification

openai/gym

tests/wrappers/test_rescale_action.py

            
import numpy as np
import pytest

import gym
from gym.wrappers import RescaleAction


def test_rescale_action():
    env = gym.make("CartPole-v1", disable_env_checker=True)
    with pytest.raises(AssertionError):
        env = RescaleAction(env, -1, 1)
    del env

    env = gym.make("Pendulum-v1", disable_env_checker=True)
    wrapped_env = RescaleAction(
        gym.make("Pendulum-v1", disable_env_checker=True), -1, 1
    )

    seed = 0

    obs, info = env.reset(seed=seed)
    wrapped_obs, wrapped_obs_info = wrapped_env.reset(seed=seed)
    assert np.allclose(obs, wrapped_obs)

    obs, reward, _, _, _ = env.step([1.5])
    with pytest.raises(AssertionError):
        wrapped_env.step([1.5])
    wrapped_obs, wrapped_reward, _, _, _ = wrapped_env.step([0.75])

    assert np.allclose(obs, wrapped_obs)
    assert np.allclose(reward, wrapped_reward)