2022-05-13 13:58:19 +01:00
|
|
|
"""Wrapper for transforming observations."""
|
|
|
|
from typing import Any, Callable
|
2019-10-11 23:58:04 +02:00
|
|
|
|
2022-05-13 13:58:19 +01:00
|
|
|
import gym
|
2019-10-11 23:58:04 +02:00
|
|
|
|
|
|
|
|
2022-05-13 13:58:19 +01:00
|
|
|
class TransformObservation(gym.ObservationWrapper):
|
|
|
|
"""Transform the observation via an arbitrary function :attr:`f`.
|
2019-10-11 23:58:04 +02:00
|
|
|
|
2022-05-13 13:58:19 +01:00
|
|
|
The function :attr:`f` should be defined on the observation space of the base environment, ``env``, and should, ideally, return values in the same space.
|
|
|
|
|
|
|
|
If the transformation you wish to apply to observations returns values in a *different* space, you should subclass :class:`ObservationWrapper`, implement the transformation, and set the new observation space accordingly. If you were to use this wrapper instead, the observation space would be set incorrectly.
|
|
|
|
|
|
|
|
Example:
|
2019-10-11 23:58:04 +02:00
|
|
|
>>> import gym
|
2022-05-13 13:58:19 +01:00
|
|
|
>>> import numpy as np
|
2019-10-11 23:58:04 +02:00
|
|
|
>>> env = gym.make('CartPole-v1')
|
|
|
|
>>> env = TransformObservation(env, lambda obs: obs + 0.1*np.random.randn(*obs.shape))
|
|
|
|
>>> env.reset()
|
|
|
|
array([-0.08319338, 0.04635121, -0.07394746, 0.20877492])
|
|
|
|
"""
|
2021-07-29 02:26:34 +02:00
|
|
|
|
2022-05-13 13:58:19 +01:00
|
|
|
def __init__(self, env: gym.Env, f: Callable[[Any], Any]):
|
|
|
|
"""Initialize the :class:`TransformObservation` wrapper with an environment and a transform function :param:`f`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
env: The environment to apply the wrapper
|
|
|
|
f: A function that transforms the observation
|
|
|
|
"""
|
2022-08-30 19:41:59 +05:30
|
|
|
super().__init__(env)
|
2019-10-11 23:58:04 +02:00
|
|
|
assert callable(f)
|
|
|
|
self.f = f
|
|
|
|
|
|
|
|
def observation(self, observation):
|
2022-05-13 13:58:19 +01:00
|
|
|
"""Transforms the observations with callable :attr:`f`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
observation: The observation to transform
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The transformed observation
|
|
|
|
"""
|
2019-10-11 23:58:04 +02:00
|
|
|
return self.f(observation)
|