
    &`i?F                     v   d dl mZmZmZmZ d dlZd dlZd dl	Z	d dl
mZ e G d dej                              Zedej        dej        fd            Zedej        defd	            Zedej        deej                 fd
            Zed             Ze	 d"ddddddej        dedeeeef         dee         dededej        fd            Zed             Zedddee         deeef         fd            Zed             Zed             Zed             Zed             Zeded edefd!            ZdS )#    )AnyListOptionalUnionN)DeveloperAPIc                       e Zd ZdZd ZdS )BatchedNdArraya  A ndarray-wrapper the usage of which indicates that there a batch dim exists.

    This is such that our `batch()` utility can distinguish between having to
    stack n individual batch items (each one w/o any batch dim) vs having to
    concatenate n already batched items (each one possibly with a different batch
    dim, but definitely with some batch dim).

    TODO (sven): Maybe replace this by a list-override instead.
    c                 T    t          j        |                              |           }|S N)npasarrayview)clsinput_arrayobjs      v/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/rllib/utils/spaces/space_utils.py__new__zBatchedNdArray.__new__   s$    j%%**3//
    N)__name__
__module____qualname____doc__r    r   r   r	   r	   
   s-             r   r	   spacereturnc                 N    t          | d          rt          | j                  S | S )aR  Returns the original space of a space, if any.

    This function recursively traverses the given space and returns the original space
    at the very end of the chain.

    Args:
        space: The space to get the original space for.

    Returns:
        The original space or the given space itself if no original space is found.
    original_space)hasattrget_original_spacer   r   s    r   r   r      s-     u&'' !%"6777r   c                     t          |           t          j        j        t          j        j        t          j        j        t          j        j        fv rdS dS )aa  Returns true, if the space is composite.

    Note, we follow here the glossary of `gymnasium` by which any spoace
    that holds other spaces is defined as being 'composite'.

    Args:
        space: The space to be checked for being composed of other spaces.

    Returns:
        True, if the space is composed of other spaces, otherwise False.
    TF)typegymspacesDictGraphSequenceTupler    s    r   is_composite_spacer)   /   sG     E{{



	   tur   c                 .    fdg } | |           |S )a  Flattens a gym.Space into its primitive components.

    Primitive components are any non Tuple/Dict spaces.

    Args:
        space: The gym.Space to flatten. This may be any
            supported type (including nested Tuples and Dicts).

    Returns:
        List[gym.Space]: The flattened list of primitive Spaces. This list
            does not contain Tuples or Dicts anymore.
    c                 :   ddl m} t          | t          j        j                  r| D ]} ||           d S t          | t          j        j        |f          r+t          | j                  D ]} | |         |           d S |                    |            d S )Nr   )FlexDict)	ray.rllib.utils.spaces.flexdictr,   
isinstancer#   r$   r(   r%   sortedappend)space_return_listr,   sk_helper_flattens        r   r5   z&flatten_space.<locals>._helper_flattenV   s    <<<<<<fcj.// 	' 0 0;////0 0( ;<< 	'FM** 8 8q	;77778 8 v&&&&&r   r   )r   retr5   s     @r   flatten_spacer7   G   s:    
' 
' 
' 
' 
' COE3Jr   c                 $    fd |           S )aa  Returns a Tuple/Dict Space as native (equally structured) py tuple/dict.

    Args:
        space: The Space to get the python struct for.

    Returns:
        Union[dict,tuple,gym.Space]: The struct equivalent to the given Space.
            Note that the returned struct still contains all original
            "primitive" Spaces (e.g. Box, Discrete).

    .. testcode::
        :skipif: True

        get_base_struct_from_space(Dict({
            "a": Box(),
            "b": Tuple([Discrete(2), Discrete(3)])
        }))

    .. testoutput::

        dict(a=Box(), b=tuple(Discrete(2), Discrete(3)))
    c                      t           t          j        j                  rt	          fd D                       S t           t          j        j                  r fd j        D             S  S )Nc              3   .   K   | ]} |          V  d S r   r   ).0r3   _helper_structs     r   	<genexpr>zEget_base_struct_from_space.<locals>._helper_struct.<locals>.<genexpr>   s-      ;;q**;;;;;;r   c                 4    i | ]}| |                   S r   r   )r;   r4   r<   r1   s     r   
<dictcomp>zFget_base_struct_from_space.<locals>._helper_struct.<locals>.<dictcomp>   s)    HHHQA~~fQi00HHHr   )r.   r#   r$   r(   tupler%   )r1   r<   s   `r   r<   z2get_base_struct_from_space.<locals>._helper_struct   sx    fcj.// 	;;;;F;;;;;;
00 	HHHHH&-HHHHMr   r   )r   r<   s    @r   get_base_struct_from_spacerA   g   s.    2     >%   r               F)
fill_value	time_size
time_majorone_hot_discrete
batch_sizerD   rE   rF   rG   c                    t           t          j        j        t          j        j        t
          t          f          r] }t           t          j        j        t          j        j        f          rt                     }t          j	        fd|          S rt           t          j        j
                  r3t          j                            dd j        ft          j                   nct           t          j        j                  rDt          j                            ddt          j         j                  ft          j                   dk    rʉzdk    rdk    sJ r5t          j         fdt'                    D              j                  S t          j         fd	t'                    D              j                  S t          j        dk    r fd
t'                    D             n                                  j                  S dk    rdk    sJ rg}ng}ndk    rgng }t          j        |t/           j                  z    j                  S )a  Returns batched dummy data (using `batch_size`) for the given `space`.

    Note: The returned batch will not pass a `space.contains(batch)` test
    as an additional batch dimension has to be added at axis 0, unless `batch_size` is
    set to 0.

    Args:
        space: The space to get a dummy batch for.
        batch_size: The required batch size (B). Note that this can also
            be 0 (only if `time_size` is None!), which will result in a
            non-batched sample for the given space (no batch dim).
        fill_value: The value to fill the batch with
            or "random" for random values.
        time_size: If not None, add an optional time axis
            of `time_size` size to the returned batch. This time axis might either
            be inserted at axis=1 (default) or axis=0, if `time_major` is True.
        time_major: If True AND `time_size` is not None, return batch
            as shape [T x B x ...], otherwise as [B x T x ...]. If `time_size`
            if None, ignore this setting and return [B x ...].
        one_hot_discrete: If True, will return one-hot vectors (instead of
            int-values) for those sub-components of a (possibly complex) `space`
            that are Discrete or MultiDiscrete. Note that in case `fill_value` is 0.0,
            this will result in zero-hot vectors (where all slots have a value of 0.0).

    Returns:
        The dummy batch of size `bqtch_size` matching the given space.
    c                 .    t          |           S )N)r   rH   rD   rE   rF   rG   )get_dummy_batch_for_space)r3   rH   rD   rG   rF   rE   s    r   <lambda>z+get_dummy_batch_for_space.<locals>.<lambda>   s)    /%%#%!1   r   rC         ?randomNr   c                 F    g | ]}fd t                    D             S )c                 8    g | ]}                                 S r   sampler;   _r   s     r   
<listcomp>z8get_dummy_batch_for_space.<locals>.<listcomp>.<listcomp>   s!    CCCACCCr   range)r;   trH   r   s     r   rU   z-get_dummy_batch_for_space.<locals>.<listcomp>   sF        DCCCz1B1BCCC  r   dtypec                 F    g | ]}fd t                    D             S )c                 8    g | ]}                                 S r   rQ   )r;   rX   r   s     r   rU   z8get_dummy_batch_for_space.<locals>.<listcomp>.<listcomp>   s!    BBBABBBr   rV   )r;   rT   r   rE   s     r   rU   z-get_dummy_batch_for_space.<locals>.<listcomp>   sF        CBBBy1A1ABBB  r   c                 8    g | ]}                                 S r   rQ   rS   s     r   rU   z-get_dummy_batch_for_space.<locals>.<listcomp>   s!    ;;;A;;;r   )rD   rZ   )r.   r#   r$   r%   r(   dictr@   rA   treemap_structureDiscreteBoxnr   float32MultiDiscretesumnvecarrayrW   rZ   rR   fulllistshape)r   rH   rD   rE   rF   rG   base_structrk   s   ``````  r   rK   rK      s   L %#*/3:+;T5IJJ 
ecjosz/?@AA 	<4U;;K!        

 

 
	
  PeSZ011 	PJNN3egZDDEEsz788 	PJNN3bfUZ.@.@-BBJOOE X >>i!mmm3 x    !&y!1!1    +    x    !&z!2!2    +    8>> <;;;z):):;;;;\\^^k	     >>i!mmm3 0"J/#Y/$.NNZLLEwD%%%*EK
 
 
 	
r   c                    t          | t          t          t          f          rjg }t	          j        |           D ]+}|                    t          j        |dg                     ,t          j	        |d                                          } | S )a:  Returns a single np.ndarray given a list/tuple of np.ndarrays.

    Args:
        input_ (Union[List[np.ndarray], np.ndarray]): The list of ndarrays or
            a single ndarray.

    Returns:
        np.ndarray: The result after concatenating all single arrays in input_.

    .. testcode::
        :skipif: True

        flatten_to_single_ndarray([
            np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]),
            np.array([7, 8, 9]),
        ])

    .. testoutput::

        np.array([
            1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0
        ])
    r   axis)
r.   rj   r@   r^   r_   flattenr0   r   reshapeconcatenate)input_expandedin_s      r   flatten_to_single_ndarrayrw      s    4 &4-.. <<'' 	3 	3COOBJsRD112222q11199;;Mr   )'individual_items_already_have_batch_dimlist_of_structsrx   c                    | st          d          |dk    r5t          j        | d                   }t          |d         t                    }|rt
          j        nt
          j        t          j        fdg| R  }|S )a  Converts input from a list of (nested) structs to a (nested) struct of batches.

    Input: List of structs (each of these structs representing a single batch item).
        [
            {"a": 1, "b": (4, 7.0)},  <- batch item 1
            {"a": 2, "b": (5, 8.0)},  <- batch item 2
            {"a": 3, "b": (6, 9.0)},  <- batch item 3
        ]

    Output: Struct of different batches (each batch has size=3 b/c there were 3 items
        in the original list):
        {
            "a": np.array([1, 2, 3]),
            "b": (np.array([4, 5, 6]), np.array([7.0, 8.0, 9.0]))
        }

    Args:
        list_of_structs: The list of (possibly nested) structs. Each item
            in this list represents a single batch item.
        individual_items_already_have_batch_dim: True, if the individual items in
            `list_of_structs` already have a batch dim. In this case, we will
            concatenate (instead of stack) at the end. In the example above, this would
            look like this: Input: [{"a": [1], "b": ([4], [7.0])}, ...] -> Output: same
            as in above example.
            If the special value "auto" is used,

    Returns:
        The struct of component batches. Each leaf item in this struct represents the
        batch for a single component (in case struct is tuple/dict). If the input is a
        simple list of primitive items, e.g. a list of floats, a np.array of floats
        will be returned.
    z3Input `list_of_structs` does not contain any items.autor   c                  B    t          j         | d                    S )Nr   ro   )r   ascontiguousarray)r3   np_funcs    r   rL   zbatch.<locals>.<lambda>E  s!    2'(:(:(:;; r   )	
ValueErrorr_   rq   r.   r	   r   rs   stackr`   )ry   rx   flatr6   r~   s       @r   batchr     s    L  PNOOO /&88|OA.//2<T!Wn2U2U/ GUbnnRXG

;;;;>M  C Jr   c                    t          j        |           g }t          t          d                             D ]Q|                    t          j        | fdt          t                              D                                  R|S )a  Converts input from (nested) struct of batches to batch of structs.

    Input: Struct of different batches (each batch has size=3):
        {
            "a": np.array([1, 2, 3]),
            "b": (np.array([4, 5, 6]), np.array([7.0, 8.0, 9.0]))
        }
    Output: Batch (list) of structs (each of these structs representing a
        single action):
        [
            {"a": 1, "b": (4, 7.0)},  <- action 1
            {"a": 2, "b": (5, 8.0)},  <- action 2
            {"a": 3, "b": (6, 9.0)},  <- action 3
        ]

    Args:
        batches_struct: The struct of component batches. Each leaf item
            in this struct represents the batch for a single component
            (in case struct is tuple/dict).
            Alternatively, `batches_struct` may also simply be a batch of
            primitives (non tuple/dict).

    Returns:
        The list of individual structs. Each item in the returned list represents a
        single (maybe complex) batch item.
    r   c                 ,    g | ]}|                  S r   r   )r;   i	batch_posflat_batchess     r   rU   zunbatch.<locals>.<listcomp>m  s"    NNNa+NNNr   )r_   rq   rW   lenr0   unflatten_as)batches_structoutr   r   s     @@r   unbatchr   J  s    8 <//L
C3|A//00 
 
	

NNNNNU3|;L;L5M5MNNN 	
 	
 	
 	
 Jr   c                 4    d }t          j        || |          S )a  Clips all components in `action` according to the given Space.

    Only applies to Box components within the action space.

    Args:
        action: The action to be clipped. This could be any complex
            action, e.g. a dict or tuple.
        action_space: The action space struct,
            e.g. `{"a": Distrete(2)}` for a space: Dict({"a": Discrete(2)}).

    Returns:
        Any: The input action, but clipped by value according to the space's
            bounds.
    c                     t          |t          j        j                  r t	          j        | |j        |j                  } | S r   )r.   r#   r$   rb   r   cliplowhighar3   s     r   map_zclip_action.<locals>.map_  s4    a(( 	*15!&))Ar   r_   r`   )actionaction_spacer   s      r   clip_actionr   s  s(    "  
 dFL999r   c                 4    d }t          j        || |          S )a  Unsquashes all components in `action` according to the given Space.

    Inverse of `normalize_action()`. Useful for mapping policy action
    outputs (normalized between -1.0 and 1.0) to an env's action space.
    Unsquashing results in cont. action component values between the
    given Space's bounds (`low` and `high`). This only applies to Box
    components within the action space, whose dtype is float32 or float64.

    Args:
        action: The action to be unsquashed. This could be any complex
            action, e.g. a dict or tuple.
        action_space_struct: The action space struct,
            e.g. `{"a": Box()}` for a space: Dict({"a": Box()}).

    Returns:
        Any: The input action, but unsquashed, according to the space's
            bounds. An unsquashed action is ready to be sent to the
            environment (`BaseEnv.send_actions([unsquashed actions])`).
    c                    t          |t          j        j                  rt	          j        |j                  rt	          j        |j                  r|j        t          j	        k    s|j        t          j
        k    rA|j        | dz   |j        |j        z
  z  dz  z   } t	          j        | |j        |j                  } n.t	          j        |j        t          j                  r
|j        | z   } | S )NrM          @)r.   r#   r$   rb   r   allbounded_belowbounded_aboverZ   rd   float64r   r   r   
issubdtypeintegerr   s     r   r   zunsquash_action.<locals>.map_  s    q#*.))	q''	 q''	
 w"*$$2:(=(= EQW!%83>> GAquaf--qw
33  EAIr   r   r   action_space_structr   s      r   unsquash_actionr     s)    ,  & dF,?@@@r   c                 4    d }t          j        || |          S )a  Normalizes all (Box) components in `action` to be in [-1.0, 1.0].

    Inverse of `unsquash_action()`. Useful for mapping an env's action
    (arbitrary bounded values) to a [-1.0, 1.0] interval.
    This only applies to Box components within the action space, whose
    dtype is float32 or float64.

    Args:
        action: The action to be normalized. This could be any complex
            action, e.g. a dict or tuple.
        action_space_struct: The action space struct,
            e.g. `{"a": Box()}` for a space: Dict({"a": Box()}).

    Returns:
        Any: The input action, but normalized, according to the space's
            bounds.
    c                     t          |t          j        j                  rJ|j        t
          j        k    s|j        t
          j        k    r | |j        z
  dz  |j	        |j        z
  z  dz
  } | S )Nr   rM   )
r.   r#   r$   rb   rZ   r   rd   r   r   r   r   s     r   r   znormalize_action.<locals>.map_  s^    a(( 	=Grz!!QW
%:%: ae)s"qv~6<Ar   r   r   s      r   normalize_actionr     s)    (   dF,?@@@r   elementsampled_elementc                 8    d }t          j        || |d          S )aK  Convert all the components of the element to match the space dtypes.

    Args:
        element: The element to be converted.
        sampled_element: An element sampled from a space to be matched
            to.

    Returns:
        The input element, but with all its components converted to match
        the space dtypes.
    c                    t          |t          j                  rt          | t          j                  st          | t          t          f          sJ d|  d            |j        dk    rt          j        | |j                  } nt          d	                    t          |                               |j        | j        k    r|                     |j                  } nt          |t                    st          |t          j                  rft          | t                    r#|                                 rt	          |           } t          | t          j                  rt          j        |           } | S )NzERROR: `elem` (z!) must be np.array, float or int!r   rY   zZElement should be of type np.ndarray but is instead of                             type {})r.   r   ndarrayfloatintrk   rh   rZ   r   formatr"   astypeint_
is_integerfloat_int64)elemr3   s     r   r   z+convert_element_to_space_type.<locals>.map_  s_   a$$ 	&dBJ// ,!5#,  M MLTLLLM M  7b==8D888DD$%%+V JJ& &   DJ&&{{17++ 3 	&:a#9#9 	&$&& !4??+<+< !4yy$	** &x~~r   F)check_typesr   )r   r   r   s      r   convert_element_to_space_typer     s-      8 dG_%PPPPr   )rB   )typingr   r   r   r   	gymnasiumr#   numpyr   r_   ray.rllib.utils.annotationsr   r   r	   Spacer   boolr)   r7   rA   r   r   strrK   rw   r   r   r   r   r   r   r   r   r   <module>r      s   - - - - - - - - - - - -          4 4 4 4 4 4     RZ   " ci CI    $ ci D    .  tCI    >  !  !  !F  c
 *-#"c
 c
 c
9c
c
 eS#o&	c

 }c
 c
 c
 Zc
 c
 c
 c
L   B  AF2 2 2#Y2 .349-=2 2 2 2j % % %P : : :0 (A (A (AV A A A< )Q3 )Q )Q )Q )Q )Q )Q )Q )Qr   