2021-06-30 12:32:11 +02:00
|
|
|
import gym
|
2021-10-29 10:46:52 +02:00
|
|
|
from typing import Type
|
2021-06-30 12:32:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ActionTransform(gym.ActionWrapper):
|
|
|
|
def __init__(self, env, low, high):
|
|
|
|
super().__init__(env)
|
|
|
|
self._low = low
|
|
|
|
self._high = high
|
2022-01-29 18:41:57 -08:00
|
|
|
self.action_space = type(env.action_space)(
|
|
|
|
self._low, self._high, env.action_space.shape, env.action_space.dtype
|
|
|
|
)
|
2021-06-30 12:32:11 +02:00
|
|
|
|
|
|
|
def action(self, action):
|
|
|
|
return (action - self._low) / (self._high - self._low) * (
|
2022-01-29 18:41:57 -08:00
|
|
|
self.env.action_space.high - self.env.action_space.low
|
|
|
|
) + self.env.action_space.low
|
2021-06-30 12:32:11 +02:00
|
|
|
|
|
|
|
|
2021-10-29 10:46:52 +02:00
|
|
|
def transform_action_space(env_name_or_creator) -> Type[gym.Env]:
|
2021-06-30 12:32:11 +02:00
|
|
|
"""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:
|
2022-02-04 05:59:56 -08:00
|
|
|
New transformed_action_space_env function that returns an environment
|
|
|
|
wrapped by the ActionTransform wrapper. 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.
|
2021-06-30 12:32:11 +02:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
>>> # By gym string:
|
[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 08:24:00 -07:00
|
|
|
>>> pendulum_300_to_500_cls = transform_action_space("Pendulum-v1")
|
2021-06-30 12:32:11 +02:00
|
|
|
>>> # 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")
|
|
|
|
"""
|
|
|
|
|
2022-02-04 05:59:56 -08:00
|
|
|
def transformed_action_space_env(config):
|
|
|
|
if isinstance(env_name_or_creator, str):
|
|
|
|
inner_env = gym.make(env_name_or_creator)
|
|
|
|
else:
|
|
|
|
inner_env = env_name_or_creator(config)
|
|
|
|
_low = config.pop("low", -1.0)
|
|
|
|
_high = config.pop("high", 1.0)
|
|
|
|
env = ActionTransform(inner_env, _low, _high)
|
|
|
|
return env
|
|
|
|
|
|
|
|
return transformed_action_space_env
|
2021-06-30 12:32:11 +02:00
|
|
|
|
|
|
|
|
[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 08:24:00 -07:00
|
|
|
TransformedActionPendulum = transform_action_space("Pendulum-v1")
|