
    &`i~p                         d dl mZ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 d dlmZmZmZ d dlmZmZ d dlmZmZmZ d dlmZ e G d d	                      ZdS )
    )AnyDictListOptionalUnionN)data_equivalence)LARGE_INTEGERone_hotone_hot_multidiscrete)gym_space_from_dictgym_space_to_dict)batchget_base_struct_from_spaceget_dummy_batch_for_space)DeveloperAPIc                      e Zd Z	 	 	 d2deeeej        f                  dedee	j
                 fdZdd defd	Zed
             Zej        d             Zed             Zdeeef         fdZedeeef         dd fd            Zd3dZd3dZd4dZd5deddfdZd3dZ	 d6dddddddeeeeee         f                  dedee         dededee         defdZdeed eee f         dd fd Z!d! Z"d" Z#ddd#d$eeeeee         f                  deddfd%Z$d& Z%d' Z&d( Z'd7d)Z(d* Z)	 	 	 	 	 d8d+Z*	 d9d,Z+	 	 	 	 	 d8d-efd.Z,d/ Z-	 d6d0Z.d1 Z/dS ):InfiniteLookbackBufferNr   datalookbackspacec                     ||ng | _         t          |t          | j                             | _        t	          | j         t
                     | _        || _        d S N)r   minlenr   
isinstancelist	finalizedr   )selfr   r   r   s       /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/rllib/env/utils/infinite_lookback_buffer.py__init__zInfiniteLookbackBuffer.__init__   sN     !,DD"	Hc$)nn55'	4888


    otherreturnc                     t          |t                    oYt          | j        |j                  o?| j        |j        k    o/| j        |j        k    o| j        |j        k    o| j        |j        k    S )a\  Compares two `InfiniteLookbackBuffers.

        Args:
            other: Another object. If another `LookbackBuffer` instance all
                their attributes are compared.

        Returns:
            `True`, if `other` is an `InfiniteLookbackBuffer` instance and all
            attributes are identical. Otherwise, returns `False`.
        )r   r   r   r   r   r   space_structr   r   r"   s     r   __eq__zInfiniteLookbackBuffer.__eq__    s{     u455 * EJ77* /* %/1	*
 !U%77* 
ek)	
r!   c                     | j         S r   )_spacer   s    r   r   zInfiniteLookbackBuffer.space8   s
    {r!   c                 <    || _         t          |          | _        d S r   )r)   r   _space_struct)r   values     r   r   zInfiniteLookbackBuffer.space<   s    7>>r!   c                     | j         S r   )r,   r*   s    r   r%   z#InfiniteLookbackBuffer.space_structA   s    !!r!   c                 d    | j         | j        | j        | j        rt	          | j                  nddS )a?  Returns the pickable state of a buffer.

        The data in the buffer is stored into a dictionary. Note that
        buffers can also be generated from pickable states (see
        `InfiniteLookbackBuffer.from_state`)

        Returns:
            A dict containing all the data and metadata from the buffer.
        N)r   r   r   r   )r   r   r   r   r   r*   s    r   	get_statez InfiniteLookbackBuffer.get_stateE   s<     I6:jJ&tz222d	
 
 	
r!   statec                     t                      }| d         |_        | d         |_        | d         rt          | d                   nd|_        | d         |_        |S )a  Creates a new `InfiniteLookbackBuffer` from a state dict.

        Args:
            state: The state dict, as returned by `self.get_state`.

        Returns:
            A new `InfiniteLookbackBuffer` instance with the data and metadata
            from the state dict.
        r   r   r   Nr   )r   r   r   r   r   r   )r1   buffers     r   
from_statez!InfiniteLookbackBuffer.from_stateV   s[     ())
+ ->CGnV*5>:::RVFmr!   c                     | j         r#t          j        d | j        |          | _        dS | j                            |           dS )z1Appends the given item to the end of this buffer.c                 4    t          j        | |ggd          S Nr   )axisnpconcatenatedis     r   <lambda>z/InfiniteLookbackBuffer.append.<locals>.<lambda>n   s    R^QH1=== r!   N)r   treemap_structurer   appendr   items     r   rB   zInfiniteLookbackBuffer.appendj   sN    > 	#*==ty$ DIII IT"""""r!   c                     | j         rLt          j        d | j        t	          |t
                    rt          j        |          n|          | _        dS |D ]}|                     |           dS )z7Appends all items in `items` to the end of this buffer.c                 2    t          j        | |gd          S r7   r9   r<   s     r   r?   z/InfiniteLookbackBuffer.extend.<locals>.<lambda>z   s    R^QF;;; r!   N)	r   r@   rA   r   r   r   r:   arrayrB   )r   itemsrD   s      r   extendzInfiniteLookbackBuffer.extends   s    > 	" *;;	#-eT#:#:E	 DIII  " "D!!!!" "r!   c                 ^    | j                             |                                           dS )zConcatenates the data of `other` (w/o its lookback) to `self`.

        Args:
            other: The other InfiniteLookbackBuffer to be concatenated to self.
        N)r   rI   getr&   s     r   concatzInfiniteLookbackBuffer.concat   s(     		%%%%%r!   indexc                     | j         r$t          j        fd| j                  | _        dS | j                                       dS )zRemoves the item at `index` from this buffer, but does NOT return it.

        Args:
            index: The index to pop out of this buffer (w/o returning it from this
                method).
        c                 2    t          j        | d          S r7   )r:   delete)srN   s    r   r?   z,InfiniteLookbackBuffer.pop.<locals>.<lambda>   s    ")Au1555 r!   N)r   r@   rA   r   pop)r   rN   s    `r   rS   zInfiniteLookbackBuffer.pop   sS     > 	!*5555ty DIII IMM%     r!   c                 X    | j         s"t          | j                  | _        d| _         dS dS )zFinalizes this buffer by converting internal data lists into numpy arrays.

        Thereby, if the individual items in the list are nested structures, the
        resulting buffer content will be a nested struct of np.ndarrays (leafs).
        TN)r   r   r   r*   s    r   finalizezInfiniteLookbackBuffer.finalize   s4     ~ 	"di((DI!DNNN	" 	"r!   F)neg_index_as_lookbackfillone_hot_discrete_ignore_last_ts_add_last_ts_valueindicesrV   rW   rX   rY   rZ   c                    |                                }nt          |t                    r                     |          }nqt          |t                    r* fd|D             } j        rt          |          }n2t          |t                    sJ                      |          }|S )a
  Returns data, based on the given args, from this buffer.

        Args:
            indices: A single int is interpreted as an index, from which to return the
                individual data stored at this index.
                A list of ints is interpreted as a list of indices from which to gather
                individual data in a batch of size len(indices).
                A slice object is interpreted as a range of data to be returned.
                Thereby, negative indices by default are interpreted as "before the end"
                unless the `neg_index_as_lookback=True` option is used, in which case
                negative indices are interpreted as "before ts=0", meaning going back
                into the lookback buffer.
            neg_index_as_lookback: If True, negative values in `indices` are
                interpreted as "before ts=0", meaning going back into the lookback
                buffer. For example, a buffer with data [4, 5, 6,  7, 8, 9],
                where [4, 5, 6] is the lookback buffer range (ts=0 item is 7), will
                respond to `get(-1, neg_index_as_lookback=True)` with `6` and to
                `get(slice(-2, 1), neg_index_as_lookback=True)` with `[5, 6,  7]`.
            fill: An optional float value to use for filling up the returned results at
                the boundaries. This filling only happens if the requested index range's
                start/stop boundaries exceed the buffer's boundaries (including the
                lookback buffer on the left side). This comes in very handy, if users
                don't want to worry about reaching such boundaries and want to zero-pad.
                For example, a buffer with data [10, 11,  12, 13, 14] and lookback
                buffer size of 2 (meaning `10` and `11` are part of the lookback buffer)
                will respond to `get(slice(-7, -2), fill=0.0)`
                with `[0.0, 0.0, 10, 11, 12]`.
            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 if `fill=0` and the
                requested `indices` are out of the range of our data, the returned
                one-hot vectors will actually be zero-hot (all slots zero).
            _ignore_last_ts: Whether to ignore the last record in our internal
                `self.data` when getting the provided indices.
            _add_last_ts_value: Whether to add the value of this arg to the end of
                the internal `self.data` buffer (just for the duration of this get
                operation, not permanently).
        N)rX   rY   rW   rV   rX   rY   rZ   c           
      F    g | ]}                     |           S )r]   )_get_int_index).0idxrZ   rY   rW   rV   rX   r   s     r   
<listcomp>z.InfiniteLookbackBuffer.get.<locals>.<listcomp>   sR     
 
 
  ##*?%5$3'9 $  
 
 
r!   )	_get_all_datar   slice
_get_slicer   r   r   intr_   )r   r[   rV   rW   rX   rY   rZ   r   s   ` ````` r   rK   zInfiniteLookbackBuffer.get   s*   ` ?%%!1 / &  DD ''  	??&;!1 /#5 #  DD && 	
 
 
 
 
 
 
 
 
 #
 
 
D ~ #T{{gs+++++&&&;!1 /#5 '  D r!   c                 x   | j         r%t          dt          |           j         d          t	          t
          t          t          f          rfd| j        D             }n<t	          t                    r| j        j        j
        d         z   }n
| j        z   }t          || j
        | j                  S )a  Adds another InfiniteLookbackBuffer object or list to the end of this one.

        Args:
            other: Another `InfiniteLookbackBuffer` or a `list` or a number.
                If a `InfiniteLookbackBuffer` its data (w/o its lookback buffer) gets
                concatenated to self's data. If a `list`, we concat it to self's data.
                If a number, we add this number to each element of self (if possible).

        Returns:
            A new `InfiniteLookbackBuffer` instance `self.data` containing
            concatenated data from `self` and `other` (or adding `other` to each element
            in self's data).
        zCannot `add` to a finalized .c                 b    g | ]+}t          |t          t          t          f          r|z   n|,S  )r   rf   floatcomplex)r`   r=   r"   s     r   rb   z2InfiniteLookbackBuffer.__add__.<locals>.<listcomp>  sG        $.a#ug1F#G#GNQYYQ  r!   N)r   r   r   )r   RuntimeErrortype__name__r   rf   rk   rl   r   r   r   r   )r   r"   r   s    ` r   __add__zInfiniteLookbackBuffer.__add__   s    " > 	Td4jj>QTTTUUU %#ug!677 )   !Y   E#9:: )y5:en.>.>#?? y5()j   r!   c                 ,    |                      |          S )z0Support squared bracket syntax, e.g. buffer[:5].)rK   rC   s     r   __getitem__z"InfiniteLookbackBuffer.__getitem__'  s    xx~~r!   c                 4    |                      ||           d S )N)new_data
at_indices)set)r   keyr-   s      r   __setitem__z"InfiniteLookbackBuffer.__setitem__+  s    %C00000r!   )ru   rV   ru   c                   ||                      |           dS t          |t                    r|                     |||           dS t          |t                    r5t          |          D ]#\  }}|                     ||         ||           $dS t          |t                    sJ |                     |||           dS )a  Overwrites all or some of the data in this buffer with the provided data.

        Args:
            new_data: The new data to overwrite existing records with.
            at_indices: A single int is interpreted as an index, at which to overwrite
                the individual record stored at this index with `new_data`.
                A list of ints is interpreted as a list of indices, which to overwrite
                with `new_data`, which must be a batch of size `len(at_indices)`.
                A slice object is interpreted as a range, which to overwrite with
                `new_data`. Thereby, negative indices by default are interpreted as
                "before the end" unless the `neg_index_as_lookback=True` option is
                used, in which case negative indices are interpreted as
                "before ts=0", meaning going back into the lookback buffer.
            neg_index_as_lookback: If True, negative values in `at_indices` are
                interpreted as "before ts=0", meaning going back into the lookback
                buffer. For example, a buffer with data [4, 5, 6,  7, 8, 9],
                where [4, 5, 6] is the lookback buffer range (ts=0 item is 7), will
                handle a call `set(99, at_indices=-1, neg_index_as_lookback=True)`
                with `6` being replaced by 99 and to `set([98, 99, 100],
                at_indices=slice(-2, 1), neg_index_as_lookback=True)` with
                `[5, 6,  7]` being replaced by `[98, 99,  100]`.
        N)slice_rV   )ra   rV   )_set_all_datar   rd   
_set_slicer   	enumerate_set_int_indexrf   )r   rt   ru   rV   r>   ra   s         r   rv   zInfiniteLookbackBuffer.set.  s   < x(((((
E** 	OO!&;      
 
D)) 	#J//  3##QK*? $      j#.....&;       r!   c                 Z    |                                  }t          || j        z
  d          S )z=Return the length of our data, excluding the lookback buffer.r   )len_incl_lookbackmaxr   )r   len_s     r   __len__zInfiniteLookbackBuffer.__len__d  s*    %%''4$-'+++r!   c                     | j         r,t          t          j        | j                  d                   S t          | j                  S Nr   )r   r   r@   flattenr   r*   s    r   r   z(InfiniteLookbackBuffer.len_incl_lookbackj  s9    > 	"t|DI..q1222ty>>!r!   c           	          t          |           j         d| j        d | j                  d| j         d| j        | j        d           dS )N(z <- lookback(z) | ))rn   ro   r   r   r*   s    r   __repr__zInfiniteLookbackBuffer.__repr__p  sm    Dzz" H HTY~~%> H HH H+/9T]^^+DH H H	
r!   c                 ^    | d |sd nd         }|r|                      || j                  }|S )NrM   r%   )_one_hotr%   )r   rX   rY   r   s       r   rc   z$InfiniteLookbackBuffer._get_all_datav  sA    ;?:tt;< 	G==D4E=FFDr!   c                 N    |                      |t          dd                      d S r   )r|   rd   )r   rt   s     r   r{   z$InfiniteLookbackBuffer._set_all_data|  s$    %4..11111r!   c           	         | j         }|r2| j        rt          j        d | j                   }n| j         d d         }|X| j        r*t          j        d |                                |          }n't          j        |                                |          }|                     ||                                 t          |d u          z   t          |          z
            \  }}	}
d }|dk    rF| j        rt          j        fd|          }n|         }|r| 
                    || j                  }||
dk    s|	dk    r| j        r|	rT| j        t          j        |g|	z            }nt          | j        ||	|          }|t          j        d	 ||          }n|}|
rT| j        t          j        |g|
z            }nt          | j        ||
|          }|t          j        d
 ||          }n9|}n6| j        |g}nt          | j        |d|          g}||	z  ||ng z   ||
z  z   }|'| j        rt          j        fd|          S |         S |S )Nc                     | d d         S NrM   rj   rR   s    r   r?   z3InfiniteLookbackBuffer._get_slice.<locals>.<lambda>      1SbS6 r!   rM   c                 ,    t          j        | |          S r   r:   rB   )rR   ts     r   r?   z3InfiniteLookbackBuffer._get_slice.<locals>.<lambda>  s    1a r!   )len_self_plus_lookbackr   c                     |          S r   rj   rR   rz   s    r   r?   z3InfiniteLookbackBuffer._get_slice.<locals>.<lambda>  s    !F) r!   r   
fill_value
batch_sizerX   c                 .    t          j        | |g          S r   r9   s0rR   s     r   r?   z3InfiniteLookbackBuffer._get_slice.<locals>.<lambda>  s    "."a*A*A r!   c                 .    t          j        || g          S r   r9   r   s     r   r?   z3InfiniteLookbackBuffer._get_slice.<locals>.<lambda>  s    ".!R*A*A r!   c                     |          S r   rj   r   s    r   r?   z3InfiniteLookbackBuffer._get_slice.<locals>.<lambda>  s    AfI r!   )r   r   r@   rA   copyr:   rB   _interpret_slicer   rf   r   r%   r   rG   r   )r   rz   rW   rV   rX   rY   rZ   data_to_use	slice_lenfill_left_countfill_right_count
data_slice
fill_batchs    `           r   re   z!InfiniteLookbackBuffer._get_slice  s0    i 	-~ -"01A1A49MM"in)~ P"000$$&&&  !i(8(8(:(:<NOO?C?T?T!&&(((4556o&&'	 @U @
 @
<	?,< 
q==~ 1!/0C0C0C0C[QQ

(0
 W!]]:DDU]VV
 !1A!5!519L9L~ 6" 0z)%'Xtf.F%G%G

%> J'+'6-=	& & &
 "-%)%7AA&&& &

 &0
# 0z)%'Xtf7G.G%H%H

%> J'+'7-=	& & &
 "-%)%7AA&&& &

 &0
 :%"&JJ 2 J'+'(-=	  "J 0%/%;zzE #334  ~ +)*=*=*=*={KKK"6**r!   c                 L                          |          \  }}}	  j        r# fd}t          j        | j        |           d S t           j                           t          |          k    sJ | j        <   d S # t          $ r t          d d| d          w xY w)Nc                     j         r"j                             |d                   sJ t          |                    t          |          k    sJ || <   d S r   )r   containsr   )rR   nr   rz   s     r   __setz0InfiniteLookbackBuffer._set_slice.<locals>.__set  s[    z 9#z221Q488888qy>>SVV3333 !AfIIIr!   z$Cannot `set()` value via at_indices= (option neg_index_as_lookback=z;)! Slice of data does NOT have the same size as `new_data`.)r   r   r@   rA   r   r   AssertionError
IndexError)r   rt   rz   rV   __InfiniteLookbackBuffer__sets   ` `   r   r|   z!InfiniteLookbackBuffer._set_slice  s     //8MNN1a	~ -" " " " " " "5$)X>>>>>49V,--X>>>>$,	&!!! 	 	 	=v = =)>= = =  	s   (B 	7B !B#ra   c                    | j         }|r2| j        rt          j        d | j                   }n| j         d d         }|H| j        rt          j        d ||          }n)|                                }|                    |           dk    s|r
| j        z   |r"dk     rt          |           | j        z   |du z
  	 | j        rt          j        fd|          }n|         }nA# t          $ r4}	|+| j	        |cY d }	~	S t          | j	        |d|          cY d }	~	S |	d }	~	ww xY w|r|                     || j                  }|S )Nc                     | d d         S r   rj   r   s    r   r?   z7InfiniteLookbackBuffer._get_int_index.<locals>.<lambda>  r   r!   rM   c                 ,    t          j        | |          S r   r   )rR   lasts     r   r?   z7InfiniteLookbackBuffer._get_int_index.<locals>.<lambda>  s    BIa$6$6 r!   r   Tc                     |          S r   rj   )rR   ra   s    r   r?   z7InfiniteLookbackBuffer._get_int_index.<locals>.<lambda>1  s    AcF r!   r   )r   r   r@   rA   r   rB   r   r   r   r   r   r   r%   )
r   ra   rW   rV   rX   rY   rZ   r   r   es
    `        r   r_   z%InfiniteLookbackBuffer._get_int_index  s    i 	-~ -"01A1A49MM"in)~ 7"066EW  *..00""#5666 !88,8-#%C
 ! 	HS1WWd))dm+$/FGC	~ ()*:*:*:*:KHH"3' 	 	 	:%KKKKKK0J# %5	         	  	:==t'899Ds0   >(C' '
D%1
D ;D%D D%D  D%c                 "    |dk    s|r
 j         z   |rdk     rt                      j         z   	  j        r# fd}t          j        | j        |           d S | j        <   d S # t          $ r t          d| d| d          w xY w)Nr   c                 h    j         r$j                             |          s
J |            || <   d S r   )r   r   )rR   r   
actual_idxr   s     r   r   z4InfiniteLookbackBuffer._set_int_index.<locals>.__setW  s>    z 9#z2215588q885$%AjMMMr!   zCannot `set()` value at index r   z)! Out of range of buffer data.)r   r   r   r@   rA   r   r   )r   rt   ra   rV   r   r   s   `    @r   r~   z%InfiniteLookbackBuffer._set_int_indexG  s    
 ??3?3J
 ! 	3Z!^^TT]2J	~ 	1& & & & & &
 "5$)X>>>>>(0	*%%% 	 	 	# # #)># # #  	s   (A- !
A- -!Bc                    |t          |           | j        z   }|j        }|j        }|| j        }n#|dk     r|r| j        |z   }n||z   }n
| j        |z   }||}n#|dk     r|r| j        |z   }n||z   }n
| j        |z   }dx}}|dk     r|dk     rt	          ||z
            }d}dx}}nf||k    r||k    rt	          ||z
            }d}|x}}nA|dk     r| }d}n5||k    r||z
  }|}n'|dk     r!||k    r||z
  dz   }|dz
  }| dz
  }t
           }|dk    r|dk    s|t
           k    sJ ||f            |j        |j        nd}t          |||          }t          d||z
  ||dk    rdndz
  z   |z            }	||	||fS )Nr      rM   )	r   r   startstopabsr	   steprd   r   )
r   rz   rV   r   r   r   r   r   r   r   s
             r   r   z'InfiniteLookbackBuffer._interpret_slicef  sG    ")%(YY%>" { =MEEQYY % 7- /6 ME)E <)DDAXX % 5}t+ .4 =4'D-..*199!%$,//O EDD,,,9O1O1O"54<00O11EDDQYY$fOEE+++#&<<)DDAXX..."'*@"@1"D.2 $uqy!>DzztqyyD]N,B,B,BeT],B,BC$k5v{{1udD))D5LDAAr,JKPTTUU	y/3CCCr!   c                     %t          dt          |           j         d          d t          |t                    rfd|D             }nt          j        |          }|S )NzCannot `one_hot` data in `z6` if a gym.Space was NOT provided during construction!c                     t          |t          j        j                  rt	          | |j                  S t          |t          j        j                  rt          | |j                  S | S )N)depth)depths)	r   gymspacesDiscreter
   r   MultiDiscreter   nvec)dat_r   s     r   _convertz1InfiniteLookbackBuffer._one_hot.<locals>._convert  sa    %!455 Ft573333E3:#;<< F,T%*EEEEKr!   c                 <    g | ]}t          j        |          S rj   )r@   rA   )r`   dslicer   r%   s     r   rb   z3InfiniteLookbackBuffer._one_hot.<locals>.<listcomp>  s5       GM"8V\BB  r!   )
ValueErrorrn   ro   r   r   r@   rA   )r   r   r%   r   s     `@r   r   zInfiniteLookbackBuffer._one_hot  s    BT$ZZ-@ B B B  
	 	 	 dD!! 	D    QU  DD %hlCCDr!   )Nr   N)r#   N)r"   r   r#   N)rM   r   )FF)NFFFN)F)0ro   
__module____qualname__r   r   r   r:   ndarrayrf   r   Spacer    boolr'   propertyr   setterr%   r   strr   r0   staticmethodr4   rB   rI   rL   rS   rU   rd   rK   rk   rl   rp   rr   rx   rv   r   r   r   rc   r{   re   r|   r_   r~   r   r   rj   r!   r   r   r      s        37%)	
 
uT2:-./
 
 	"	
 
 
 

'
 

 
 
 
0   X \? ? \? " " X"
4S> 
 
 
 
" $sCx. -E    \&# # # #" " " " & & & &! ! !d ! ! ! !" " " " ;?W ',"!& %,0W W W%UDI 567W  $	W
 smW W W %SMW 
W W W Wr)4!93wNO)	!) ) ) )V  1 1 1 >B&+4 4 4 U3tCy#89:	4
  $4 
4 4 4 4l, , ," " "
 
 
   2 2 2 #m m m mf $	   B #9 99 9 9 9v  F  $	OD OD OD ODb    r!   r   )typingr   r   r   r   r   	gymnasiumr   numpyr:   r@   gymnasium.utils.env_checkerr   ray.rllib.utils.numpyr	   r
   r   ray.rllib.utils.serializationr   r   "ray.rllib.utils.spaces.space_utilsr   r   r   ray.util.annotationsr   r   rj   r!   r   <module>r      s&   3 3 3 3 3 3 3 3 3 3 3 3 3 3          8 8 8 8 8 8 O O O O O O O O O O P P P P P P P P         
 . - - - - - x
 x
 x
 x
 x
 x
 x
 x
 x
 x
r!   