
    h%.                       S r SSKJr  SSKJr  SSKrSSKrSSK	J
r
JrJr  SSKJrJrJrJr  / SQrSSKJr   " S	 S
\R*                  \\\
4   \R,                  R.                  5      r " S S\\\\
4   \R,                  R.                  5      r " S S\\\\
4   \R,                  R.                  5      r " S S\\\\
4   \R,                  R.                  5      rg)ae  A collection of wrappers that all use the LambdaAction class.

* ``TransformAction`` - Transforms the actions based on a function
* ``ClipAction`` - Clips the action within a bounds
* ``DiscretizeAction`` - Discretizes a continuous Box action space into a single Discrete space
* ``RescaleAction`` - Rescales the action within a minimum and maximum actions
    )annotations)CallableN)ActTypeObsTypeWrapperActType)BoxDiscreteMultiDiscreteSpace)TransformAction
ClipActionRescaleAction)rescale_boxc                  8    \ rS rSrSr      SS jrSS jrSrg)	r      a  Applies a function to the ``action`` before passing the modified value to the environment ``step`` function.

A vector version of the wrapper exists :class:`gymnasium.wrappers.vector.TransformAction`.

Example:
    >>> import numpy as np
    >>> import gymnasium as gym
    >>> env = gym.make("MountainCarContinuous-v0")
    >>> _ = env.reset(seed=123)
    >>> obs, *_= env.step(np.array([0.0, 1.0]))
    >>> obs
    array([-4.6397772e-01, -4.4808415e-04], dtype=float32)
    >>> env = gym.make("MountainCarContinuous-v0")
    >>> env = TransformAction(env, lambda a: 0.5 * a + 0.1, env.action_space)
    >>> _ = env.reset(seed=123)
    >>> obs, *_= env.step(np.array([0.0, 1.0]))
    >>> obs
    array([-4.6382770e-01, -2.9808417e-04], dtype=float32)

Change logs:
 * v1.0.0 - Initially added
c                    [         R                  R                  R                  XUS9  [         R                  R                  X5        Ub  X0l        X l        g)zInitialize TransformAction.

Args:
    env: The environment to wrap
    func: Function to apply to the :meth:`step`'s ``action``
    action_space: The updated action space of the wrapper given the function.
)funcaction_spaceN)gymutilsRecordConstructorArgs__init__Wrapperr   r   )selfenvr   r   s       ]/home/james-whalen/.local/lib/python3.13/site-packages/gymnasium/wrappers/transform_action.pyr   TransformAction.__init__3   sK     			''00, 	1 	
 	T'# ,	    c                $    U R                  U5      $ )zApply function to action.)r   )r   actions     r   r    TransformAction.actionJ   s    yy  r   )r   r   N)r   gym.Env[ObsType, ActType]r   z#Callable[[WrapperActType], ActType]r   zSpace[WrapperActType] | None)r    r   returnr   )__name__
__module____qualname____firstlineno____doc__r   r    __static_attributes__ r   r   r   r      s-    .& 2 3	.!r   r   c                  "    \ rS rSrSrSS jrSrg)r   O   a  Clips the ``action`` pass to ``step`` to be within the environment's `action_space`.

A vector version of the wrapper exists :class:`gymnasium.wrappers.vector.ClipAction`.

Example:
    >>> import gymnasium as gym
    >>> from gymnasium.wrappers import ClipAction
    >>> import numpy as np
    >>> env = gym.make("Hopper-v4", disable_env_checker=True)
    >>> env = ClipAction(env)
    >>> env.action_space
    Box(-inf, inf, (3,), float32)
    >>> _ = env.reset(seed=42)
    >>> _ = env.step(np.array([5.0, -2.0, 0.0], dtype=np.float32))
    ... # Executes the action np.array([1.0, -1.0, 0]) in the base environment

Change logs:
 * v0.12.6 - Initially added
 * v1.0.0 - Action space is updated to infinite bounds as is technically correct
c                l  ^ [        TR                  [        5      (       d   e[        R                  R
                  R                  U 5        [        R                  U TU4S j[        [        R                  * [        R                  TR                  R                  TR                  R                  S9S9  g)zjA wrapper for clipping continuous actions within the valid bound.

Args:
    env: The environment to wrap
c                   > [         R                  " U TR                  R                  TR                  R                  5      $ N)npclipr   lowhigh)r    r   s    r   <lambda>%ClipAction.__init__.<locals>.<lambda>s   s-    ((,,c.>.>.C.C!r   )shapedtyper   r   r   N)
isinstancer   r   r   r   r   r   r   r0   infr6   r7   )r   r   s    `r   r   ClipAction.__init__g   s     #**C0000		''006   &&,,&&,,	 	! 	
r   r*   N)r   r"   r$   r%   r&   r'   r(   r   r)   r*   r   r   r   r   O   s    *
r   r   c                  .    \ rS rSrSr      SS jrSrg)r      aD  Affinely (linearly) rescales a ``Box`` action space of the environment to within the range of ``[min_action, max_action]``.

The base environment :attr:`env` must have an action space of type :class:`spaces.Box`. If :attr:`min_action`
or :attr:`max_action` are numpy arrays, the shape must match the shape of the environment's action space.

A vector version of the wrapper exists :class:`gymnasium.wrappers.vector.RescaleAction`.

Example:
    >>> import gymnasium as gym
    >>> from gymnasium.wrappers import RescaleAction
    >>> import numpy as np
    >>> env = gym.make("Hopper-v4", disable_env_checker=True)
    >>> _ = env.reset(seed=42)
    >>> obs, _, _, _, _ = env.step(np.array([1, 1, 1], dtype=np.float32))
    >>> _ = env.reset(seed=42)
    >>> min_action = -0.5
    >>> max_action = np.array([0.0, 0.5, 0.75], dtype=np.float32)
    >>> wrapped_env = RescaleAction(env, min_action=min_action, max_action=max_action)
    >>> wrapped_env_obs, _, _, _, _ = wrapped_env.step(max_action)
    >>> np.all(obs == wrapped_env_obs)
    np.True_

Change logs:
 * v0.15.4 - Initially added
c                    [        UR                  [        5      (       d   e[        R                  R
                  R                  XUS9  [        UR                  X#5      u  pEn[        R                  U UUUS9  g)a>  Constructor for the Rescale Action wrapper.

Args:
    env (Env): The environment to wrap
    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.
)
min_action
max_actionr8   N)	r9   r   r   r   r   r   r   r   r   )r   r   r@   rA   	act_space_r   s          r   r   RescaleAction.__init__   sw     #**C0000		''00J 	1 	
 ))9)9:R	d  "	 	! 	
r   r*   N)r   r"   r@   %np.floating | np.integer | np.ndarrayrA   rE   r<   r*   r   r   r   r      s(    4
&
 :
 :	
r   r   c                  D    \ rS rSrSr S	     S
S jjrS rS rS rSr	g)DiscretizeAction   a  Uniformly discretizes a continuous Box action space into a single Discrete space.

Example 1 - Discretize Pendulum action space:
    >>> env = gym.make("Pendulum-v1")
    >>> env.action_space
    Box(-2.0, 2.0, (1,), float32)
    >>> obs, _ = env.reset(seed=42)
    >>> obs, *_ = env.step([-0.6])
    >>> obs
    array([-0.17606162,  0.9843792 ,  0.5292768 ], dtype=float32)
    >>> env = DiscretizeAction(env, bins=10)
    >>> env.action_space
    Discrete(10)
    >>> obs, _ = env.reset(seed=42)
    >>> obs, *_ = env.step(3)
    >>> obs
    array([-0.17606162,  0.9843792 ,  0.5292768 ], dtype=float32)

Example 2 - Discretize Reacher action space:
    >>> env = gym.make("Reacher-v5")
    >>> env.action_space
    Box(-1.0, 1.0, (2,), float32)
    >>> obs, _ = env.reset(seed=42)
    >>> obs, *_ = env.step([-0.3, -0.5])
    >>> obs
    array([ 0.99908342,  0.99948506,  0.04280567, -0.03208766,  0.10445588,
            0.11442572, -1.18958125, -1.97979484,  0.1054461 , -0.10896341])
    >>> env = DiscretizeAction(env, bins=10)
    >>> env.action_space
    Discrete(100)
    >>> obs, _ = env.reset(seed=42)
    >>> obs, *_ = env.step(32)
    >>> obs
    array([ 0.99908342,  0.99948506,  0.04280567, -0.03208766,  0.10445588,
            0.11442572, -1.18958118, -1.97979484,  0.1054461 , -0.10896341])

Example 2 - Discretize Reacher action space with MultiDiscrete:
    >>> env = gym.make("Reacher-v5")
    >>> env.action_space
    Box(-1.0, 1.0, (2,), float32)
    >>> obs, _ = env.reset(seed=42)
    >>> obs, *_ = env.step([-0.3, -0.5])
    >>> obs
    array([ 0.99908342,  0.99948506,  0.04280567, -0.03208766,  0.10445588,
            0.11442572, -1.18958125, -1.97979484,  0.1054461 , -0.10896341])
    >>> env = DiscretizeAction(env, bins=10, multidiscrete=True)
    >>> env.action_space
    MultiDiscrete([10 10])
    >>> obs, _ = env.reset(seed=42)
    >>> obs, *_ = env.step([3, 2])
    >>> obs
    array([ 0.99908342,  0.99948506,  0.04280567, -0.03208766,  0.10445588,
            0.11442572, -1.18958118, -1.97979484,  0.1054461 , -0.10896341])
c                   [        UR                  [        5      (       d  [        S5      eUR                  R                  U l        UR                  R
                  U l        U R                  R                  S   U l        [        R                  " [        R                  " U R                  5      5      (       d9  [        R                  " [        R                  " U R
                  5      5      (       a%  [        SU R                   SU R
                   35      eX0l        [        R                  R                  R!                  XS9  [        R"                  R!                  X5        [        U[$        5      (       a*  [        R&                  " U/U R                  -  5      U l        OT[+        U5      U R                  :X  d    SU R                   S[+        U5       35       e[        R&                  " U5      U l        [-        U R                  5       Vs/ s H  nS[        R.                  " U R                  U   U R
                  U   U R(                  U   S	-   5      S
S [        R.                  " U R                  U   U R
                  U   U R(                  U   S	-   5      S	S
 -   -  PM     snU l        U R                  (       a  [3        U R(                  5      U l        g
[5        [        R6                  " U R(                  5      5      U l        g
s  snf )zConstructor for the discretize action wrapper.

Args:
    env: The environment to wrap.
    bins: int or tuple of ints (number of bins per dimension).
    multidiscrete: If True, use MultiDiscrete action space instead of flattening to Discrete.
z@DiscretizeAction is only compatible with Box continuous actions.r   z>Discretization requires action space to be finite. Found: low=z, high=)binsz,bins must match action dimensions: expected z, got g      ?   N)r9   r   r   	TypeErrorr2   r3   r6   n_dimsr0   anyisinf
ValueErrormultidiscreter   r   r   r   ActionWrapperintarrayrJ   lenrangelinspacebin_centersr
   r	   prod)r   r   rJ   rR   is        r   r   DiscretizeAction.__init__   sL    #**C00R  ##''$$))	hhnnQ'66"((488$%%0C)D)D"hhZwtyyk; 
 +		''000A""4-dC  $$++!56DI D	T[[(]=dkk]&QTUYQZP[\](DI 4;;'
 ( DHHQK1tyy|a7GH"M++dhhqk499Q<19IJ12NO
 (
  -dii 8D (); <D
s   BLc                   U R                   (       a  [        R                  " U[        S9nOU R	                  U5      n[        U5       VVs/ s H;  u  p4U R                  U   [        [        US5      U R                  U   S-
  5         PM=     nnn[        R                  " XPR                  R                  R                  S9$ s  snnf )zDiscretizes the action.r7   r   rK   )rR   r0   asarrayrT   _unflatten_index	enumeraterY   minmaxrJ   rU   r   r   r7   )r   actindicesr[   idxcenterss         r   r    DiscretizeAction.action)  s    jjC0G++C0G $G,
, QCQK11A BC, 	 
 xxxx'<'<'B'BCC	
s   ACc           
     ^   [        U R                  5       Vs/ s H?  n[        R                  " [        R                  " U R
                  U   X   -
  5      5      PMA     nnU R                  (       a  [        R                  " U[        S9$ [        R                  " X0R                  5      $ s  snf )z^Converts a discretized action to a possible continuous action (the center of the closest bin).r^   )rW   rN   r0   argminabsrY   rR   rU   rT   ravel_multi_indexrJ   )r   r    r[   re   s       r   revert_actionDiscretizeAction.revert_action5  s     4;;'
' IIbffT--a069<=>' 	 
 88G3//'';;
s   AB*c                    / n[        U R                  5       H  nUR                  X-  5        X-  nM     [        [        U5      5      $ r/   )reversedrJ   appendlist)r   
flat_indexre   bs       r   r`   !DiscretizeAction._unflatten_index@  sB    $))$ANN:>*J % HW%&&r   )r   rY   rJ   r3   r2   rR   rN   N)F)r   r"   rJ   zint | tuple[int, ...]rR   bool)
r$   r%   r&   r'   r(   r   r    rm   r`   r)   r*   r   r   rG   rG      s?    5v $	4=&4= $4= 	4=l
D	<'r   rG   )r(   
__future__r   collections.abcr   numpyr0   	gymnasiumr   gymnasium.corer   r   r   gymnasium.spacesr   r	   r
   r   __all__gymnasium.wrappers.utilsr   rS   r   r   r   r   r   rG   r*   r   r   <module>r      s    # $   ; ; @ @ = 03!g~w679X9X3!l-
G^W45syy7V7V-
`6
G^W45syy7V7V6
rM'G^W45II##M'r   