[Wrappers]: add TransformObservation (#1670)

* Create transform_observation.py

* Create test_transform_observation.py

* Update __init__.py
This commit is contained in:
Xingdong Zuo
2019-10-11 23:58:04 +02:00
committed by pzhokhov
parent 5e62533d2c
commit ed8b13e11a
3 changed files with 54 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
import pytest
import numpy as np
import gym
from gym.wrappers import TransformObservation
@pytest.mark.parametrize('env_id', ['CartPole-v1', 'Pendulum-v0'])
def test_transform_observation(env_id):
affine_transform = lambda x: 3*x + 2
env = gym.make(env_id)
wrapped_env = TransformObservation(gym.make(env_id), lambda obs: affine_transform(obs))
env.seed(0)
wrapped_env.seed(0)
obs = env.reset()
wrapped_obs = wrapped_env.reset()
assert np.allclose(wrapped_obs, affine_transform(obs))
action = env.action_space.sample()
obs, reward, done, _ = env.step(action)
wrapped_obs, wrapped_reward, wrapped_done, _ = wrapped_env.step(action)
assert np.allclose(wrapped_obs, affine_transform(obs))
assert np.allclose(wrapped_reward, reward)
assert wrapped_done == done