ray/rllib/examples/env/transformed_action_space_env.py
Avnish Narayan 026bf01071
[RLlib] Upgrade gym version to 0.21 and deprecate pendulum-v0. (#19535)
* Fix QMix, SAC, and MADDPA too.

* Unpin gym and deprecate pendulum v0

Many tests in rllib depended on pendulum v0,
however in gym 0.21, pendulum v0 was deprecated
in favor of pendulum v1. This may change reward
thresholds, so will have to potentially rerun
all of the pendulum v1 benchmarks, or use another
environment in favor. The same applies to frozen
lake v0 and frozen lake v1

Lastly, all of the RLlib tests and have
been moved to python 3.7

* Add gym installation based on python version.

Pin python<= 3.6 to gym 0.19 due to install
issues with atari roms in gym 0.20

* Reformatting

* Fixing tests

* Move atari-py install conditional to req.txt

* migrate to new ale install method

* Fix QMix, SAC, and MADDPA too.

* Unpin gym and deprecate pendulum v0

Many tests in rllib depended on pendulum v0,
however in gym 0.21, pendulum v0 was deprecated
in favor of pendulum v1. This may change reward
thresholds, so will have to potentially rerun
all of the pendulum v1 benchmarks, or use another
environment in favor. The same applies to frozen
lake v0 and frozen lake v1

Lastly, all of the RLlib tests and have
been moved to python 3.7
* Add gym installation based on python version.

Pin python<= 3.6 to gym 0.19 due to install
issues with atari roms in gym 0.20

Move atari-py install conditional to req.txt

migrate to new ale install method

Make parametric_actions_cartpole return float32 actions/obs

Adding type conversions if obs/actions don't match space

Add utils to make elements match gym space dtypes

Co-authored-by: Jun Gong <jungong@anyscale.com>
Co-authored-by: sven1977 <svenmika1977@gmail.com>
2021-11-03 16:24:00 +01:00

74 lines
2.6 KiB
Python

import gym
from typing import Type
from ray.rllib.utils.annotations import override
class ActionTransform(gym.ActionWrapper):
def __init__(self, env, low, high):
super().__init__(env)
self._low = low
self._high = high
self.action_space = type(env.action_space)(self._low, self._high,
env.action_space.shape,
env.action_space.dtype)
def action(self, action):
return (action - self._low) / (self._high - self._low) * (
self.env.action_space.high -
self.env.action_space.low) + self.env.action_space.low
def transform_action_space(env_name_or_creator) -> Type[gym.Env]:
"""Wrapper for gym.Envs to have their action space transformed.
Args:
env_name_or_creator (Union[str, Callable[]]: String specifier or
env_maker function.
Returns:
New TransformedActionSpaceEnv class
to be used as env. The constructor takes a config dict with `_low`
and `_high` keys specifying the new action range
(default -1.0 to 1.0). The reset of the config dict will be
passed on to the underlying/wrapped env's constructor.
Examples:
>>> # By gym string:
>>> pendulum_300_to_500_cls = transform_action_space("Pendulum-v1")
>>> # Create a transformed pendulum env.
>>> pendulum_300_to_500 = pendulum_300_to_500_cls({"_low": -15.0})
>>> pendulum_300_to_500.action_space
... gym.spaces.Box(-15.0, 1.0, (1, ), "float32")
"""
class TransformedActionSpaceEnv(gym.Env):
"""PendulumEnv w/ an action space of range 300.0 to 500.0."""
def __init__(self, config):
self._low = config.pop("low", -1.0)
self._high = config.pop("high", 1.0)
if isinstance(env_name_or_creator, str):
self.env = gym.make(env_name_or_creator)
else:
self.env = env_name_or_creator(config)
self.env = ActionTransform(self.env, self._low, self._high)
self.observation_space = self.env.observation_space
self.action_space = self.env.action_space
@override(gym.Env)
def reset(self):
return self.env.reset()
@override(gym.Env)
def step(self, actions):
return self.env.step(actions)
@override(gym.Env)
def render(self, mode=None):
return self.env.render(mode)
return TransformedActionSpaceEnv
TransformedActionPendulum = transform_action_space("Pendulum-v1")