Action Wrappers#
Base Class#
- class gymnasium.ActionWrapper(env: Env[ObsType, ActType])#
Superclass of wrappers that can modify the action before
env.step()
.If you would like to apply a function to the action before passing it to the base environment, you can simply inherit from
ActionWrapper
and overwrite the methodaction()
to implement that transformation. The transformation defined in that method must take values in the base environment’s action space. However, its domain might differ from the original action space. In that case, you need to specify the new action space of the wrapper by settingself.action_space
in the__init__()
method of your wrapper.Among others, Gymnasium provides the action wrappers
ClipAction
andRescaleAction
for clipping and rescaling actions.Constructor for the action wrapper.
- action(action: WrapperActType) ActType #
Returns a modified action before
env.step()
is called.- Parameters:
action – The original
step()
actions- Returns:
The modified actions
Available Action Wrappers#
- class gymnasium.wrappers.ClipAction(env: Env)#
Clip the continuous action within the valid
Box
observation space bound.Example
>>> import gymnasium as gym >>> from gymnasium.wrappers import ClipAction >>> env = gym.make("Hopper-v4") >>> 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
- Parameters:
env – The environment to apply the wrapper
- class gymnasium.wrappers.RescaleAction(env: Env, min_action: float | int | ndarray, max_action: float | int | ndarray)#
Affinely rescales the continuous action space of the environment to the range [min_action, max_action].
The base environment
env
must have an action space of typespaces.Box
. Ifmin_action
ormax_action
are numpy arrays, the shape must match the shape of the environment’s action space.Example
>>> import gymnasium as gym >>> from gymnasium.wrappers import RescaleAction >>> import numpy as np >>> env = gym.make("Hopper-v4") >>> _ = env.reset(seed=42) >>> obs, _, _, _, _ = env.step(np.array([1,1,1])) >>> _ = env.reset(seed=42) >>> min_action = -0.5 >>> max_action = np.array([0.0, 0.5, 0.75]) >>> wrapped_env = RescaleAction(env, min_action=min_action, max_action=max_action) >>> wrapped_env_obs, _, _, _, _ = wrapped_env.step(max_action) >>> np.alltrue(obs == wrapped_env_obs) True
- Parameters:
env (Env) – The environment to apply the wrapper
min_action (float, int or np.ndarray) – The min values for each action. This may be a numpy array or a scalar.
max_action (float, int or np.ndarray) – The max values for each action. This may be a numpy array or a scalar.