Files
Gymnasium/gymnasium/wrappers/clip_action.py

44 lines
1.3 KiB
Python
Raw Normal View History

2022-05-13 13:58:19 +01:00
"""Wrapper for clipping actions within a valid bound."""
import numpy as np
import gymnasium as gym
2022-09-08 10:10:07 +01:00
from gymnasium.spaces import Box
class ClipAction(gym.ActionWrapper, gym.utils.RecordConstructorArgs):
2022-05-13 13:58:19 +01:00
"""Clip the continuous action within the valid :class:`Box` observation space bound.
2021-07-29 02:26:34 +02:00
2022-05-13 13:58:19 +01:00
Example:
>>> import gymnasium as gym
>>> from gymnasium.wrappers import ClipAction
>>> env = gym.make("Hopper-v4")
2022-05-13 13:58:19 +01:00
>>> env = ClipAction(env)
>>> env.action_space
Box(-1.0, 1.0, (3,), float32)
>>> _ = env.reset(seed=42)
>>> _ = env.step(np.array([5.0, -2.0, 0.0]))
... # Executes the action np.array([1.0, -1.0, 0]) in the base environment
2022-05-13 13:58:19 +01:00
"""
def __init__(self, env: gym.Env):
2022-05-13 13:58:19 +01:00
"""A wrapper for clipping continuous actions within the valid bound.
Args:
env: The environment to apply the wrapper
"""
assert isinstance(env.action_space, Box)
gym.utils.RecordConstructorArgs.__init__(self)
gym.ActionWrapper.__init__(self, env)
def action(self, action):
2022-05-13 13:58:19 +01:00
"""Clips the action within the valid bounds.
Args:
action: The action to clip
Returns:
The clipped action
"""
return np.clip(action, self.action_space.low, self.action_space.high)