o
    ci@                     @   sr  d dl mZ d dlZd dlZd dlZd dlmZm	Z	 d dl
mZmZ d dlmZ d dlmZ d dlmZ d dlmZ d	Zd
ZdZeeZeG dd dZeG dd deZeG dd deZeG dd deZeG dd deZeG dd deZ eG dd deZ!eG dd deZ"eG dd deZ#ed%dej$d e%fd!d"Z&dej$d e	e' fd#d$Z(dS )&    )OrderedDictN)AnyList)OldAPIStackoverride)Repeated)
TensorType)resize)convert_element_to_space_type)            d   c                   @   s   e Zd ZdZddejdefddZdejdedee	 fdd	Z
d
edejfddZd
edejde	ddfddZd
eddfddZede	fddZedejfddZdS )PreprocessorzDefines an abstract observation preprocessor function.

    Attributes:
        shape (List[int]): Shape of the preprocessed output.
    N	obs_spaceoptionsc                 C   sh   t | || _|sddlm} | | _n|| _| || j| _tt	
| j| _d| _| j | _d S )Nr   )MODEL_DEFAULTS)_legacy_patch_shapes
_obs_spaceray.rllib.models.catalogr   copy_options_init_shapeshapeintnpprod_size_isample_obs_for_type_matching)selfr   r   r    r$   R/home/ubuntu/.local/lib/python3.10/site-packages/ray/rllib/models/preprocessors.py__init__   s   zPreprocessor.__init__returnc                 C      t )z&Returns the shape after preprocessing.NotImplementedErrorr#   r   r   r$   r$   r%   r   -      zPreprocessor._init_shapeobservationc                 C   r(   )z%Returns the preprocessed observation.r)   r#   r-   r$   r$   r%   	transform1   r,   zPreprocessor.transformarrayoffsetc                 C   s   |  ||||| j < dS )z7Alternative to transform for more efficient flattening.N)r/   r   r#   r-   r0   r1   r$   r$   r%   write5      zPreprocessor.writec              
   C   s   | j t dkrZt|tu rt| jtjjrt	
|t	j}| j|s*t|| j}z| j|sFtd|t| jtjjr@|jnd| jW n tyY } ztd||d}~ww |  j d7  _ dS )z*Checks the shape of the given observation.r   z3Observation ({} dtype={}) outside given space ({})!Nz_Observation for a Box/MultiBinary/MultiDiscrete space should be an np.array, not a Python list.   )r    OBS_VALIDATION_INTERVALtypelist
isinstancer   gymspacesBoxr   r0   astypefloat32containsr
   r"   
ValueErrorformatdtypeAttributeError)r#   r-   er$   r$   r%   check_shape9   s>   

zPreprocessor.check_shapec                 C      | j S N)r   r#   r$   r$   r%   sizeX   s   zPreprocessor.sizec                 C   s>   t jjdd| jtjd}tttt	t
tf}t| |r| j|_|S )Ng            ?rB   )r:   r;   r<   r   r   r>   DictFlatteningPreprocessorOneHotPreprocessorRepeatedValuesPreprocessorTupleFlatteningPreprocessorAtariRamPreprocessorGenericPixelPreprocessorr9   r   original_space)r#   r   classesr$   r$   r%   observation_space\   s   
zPreprocessor.observation_spacerG   )__name__
__module____qualname____doc__r:   Spacedictr&   r   r   r   r   r   ndarrayr/   r3   r   rE   propertyrI   rT   r$   r$   r$   r%   r      s    r   c                   @   sP   e Zd ZdZeedejdede	e
 fddZeededejfdd	Zd
S )rQ   zGeneric image preprocessor.

    Note: for Atari games, use config {"preprocessor_pref": "deepmind"}
    instead for deepmind-style Atari preprocessing.
    r   r   r'   c                 C   sN   | d| _| d| _| d| _| jr| j| jdf}|S | j| jdf}|S )N	grayscale	zero_meandimr5   r   )get
_grayscale
_zero_mean_dim)r#   r   r   r   r$   r$   r%   r   v   s   z$GenericPixelPreprocessor._init_shaper-   c                 C   s   |  | |ddddddf }| jdk rt|ddd}t|| j| jd}| jr@|d}|tj}t|| j| jdg}| j	rK|d d }|S |d	9 }|S )
z?Downsamples images from (210, 160, 3) by the configured factor.   iNT   )heightwidth   r5   r   gp?)
rE   rc   r	   ra   meanr=   r   r>   reshaperb   )r#   r-   scaledr$   r$   r%   r/      s   


z"GenericPixelPreprocessor.transformN)rU   rV   rW   rX   r   r   r:   rY   rZ   r   r   r   r   r   r[   r/   r$   r$   r$   r%   rQ   n   s    rQ   c                   @   sL   e Zd Zeedejdedee	 fddZ
eededejfddZd	S )
rP   r   r   r'   c                 C   s   dS )Nr   r$   r+   r$   r$   r%   r      r,   z AtariRamPreprocessor._init_shaper-   c                 C   s   |  | |dd d S )Nr>   r   )rE   r=   r.   r$   r$   r%   r/      s   
zAtariRamPreprocessor.transformN)rU   rV   rW   r   r   r:   rY   rZ   r   r   r   r   r   r[   r/   r$   r$   r$   r%   rP      s
    rP   c                	   @   t   e Zd ZdZeedejdede	e
 fddZeededejfdd	Zeeded
ejde
ddfddZdS )rM   ar  One-hot preprocessor for Discrete and MultiDiscrete spaces.

    .. testcode::
        :skipif: True

        self.transform(Discrete(3).sample())

    .. testoutput::

        np.array([0.0, 1.0, 0.0])

    .. testcode::
        :skipif: True

        self.transform(MultiDiscrete([2, 3]).sample())

    .. testoutput::

        np.array([0.0, 1.0, 0.0, 0.0, 1.0])
    r   r   r'   c                 C   s(   t |tjjr| jjfS t| jjfS rG   )	r9   r:   r;   Discreter   nr   sumnvecr+   r$   r$   r%   r      s   
zOneHotPreprocessor._init_shaper-   c                 C   s$   |  | tjj| j|tjS rG   )	rE   r:   r;   utilsflattenr   r=   r   r>   r.   r$   r$   r%   r/      s   
zOneHotPreprocessor.transformr0   r1   Nc                 C   s   |  ||||| j < d S rG   )r/   rI   r2   r$   r$   r%   r3      r4   zOneHotPreprocessor.writerU   rV   rW   rX   r   r   r:   rY   rZ   r   r   r   r   r   r[   r/   r3   r$   r$   r$   r%   rM      s    "rM   c                	   @   s   e Zd Zeedejdedee	 fddZ
eededejfddZeeded	ejd
e	ddfddZeeedejfddZdS )NoPreprocessorr   r   r'   c                 C      | j jS rG   r   r   r+   r$   r$   r%   r         zNoPreprocessor._init_shaper-   c                 C   s   |  | |S rG   )rE   r.   r$   r$   r%   r/      s   
zNoPreprocessor.transformr0   r1   Nc                 C   $   t j|dd |||| j < d S NF)r   r   r0   ravelr   r2   r$   r$   r%   r3         $zNoPreprocessor.writec                 C   rF   rG   )r   rH   r$   r$   r%   rT      s   z NoPreprocessor.observation_space)rU   rV   rW   r   r   r:   rY   rZ   r   r   r   r   r   r[   r/   r3   r\   rT   r$   r$   r$   r%   rt      s    rt   c                	   @   s   e Zd ZdZeedejdede	e
 fddZeededejfdd	Zeeded
ejde
ddfddZeeedejfddZdS )MultiBinaryPreprocessora  Preprocessor that turns a MultiBinary space into a Box.

    Note: Before RLModules were introduced, RLlib's ModelCatalogV2 would produce
    ComplexInputNetworks that treat MultiBinary spaces as Boxes. This preprocessor is
    needed to get rid of the ComplexInputNetworks and use RLModules instead because
    RLModules lack the logic to handle MultiBinary or other non-Box spaces.
    r   r   r'   c                 C   ru   rG   rv   r+   r$   r$   r%   r      rw   z#MultiBinaryPreprocessor._init_shaper-   c                 C   s   |  | |tjS rG   )rE   r=   r   r>   r.   r$   r$   r%   r/      s   
z!MultiBinaryPreprocessor.transformr0   r1   Nc                 C   rx   ry   rz   r2   r$   r$   r%   r3      r|   zMultiBinaryPreprocessor.writec                 C   s$   t jjdd| jtjd}| j|_|S )Ng        rJ   rK   )r:   r;   r<   r   r   r>   r   rR   )r#   r   r$   r$   r%   rT      s   z)MultiBinaryPreprocessor.observation_space)rU   rV   rW   rX   r   r   r:   rY   rZ   r   r   r   r   r   r[   r/   r3   r\   rT   r$   r$   r$   r%   r}      s    r}   c                	   @   rl   )rO   zPreprocesses each tuple element, then flattens it all into a vector.

    RLlib models will unpack the flattened output before _build_layers_v2().
    r   r   r'   c                 C   s   t | jtjjs
J d}g | _tt| jjD ]6}| jj| }t	d
| t|}|d ur;||| j}||j7 }nd }|tt|j7 }| j| q|fS Nr   z Creating sub-preprocessor for {})r9   r   r:   r;   TuplepreprocessorsrangelenloggerdebugrA   get_preprocessorr   rI   r   r   r   r   append)r#   r   r   rI   ispacepreprocessor_classpreprocessorr$   r$   r%   r   	  s   z'TupleFlatteningPreprocessor._init_shaper-   c                 C   .   |  | tj| jtjd}| ||d |S NrK   r   rE   r   zerosr   r>   r3   r#   r-   r0   r$   r$   r%   r/        
z%TupleFlatteningPreprocessor.transformr0   r1   Nc                 C   sL   t |t | jksJ |t|| jD ]\}}|||| ||j7 }qd S rG   )r   r   zipr3   rI   r#   r-   r0   r1   opr$   r$   r%   r3   "  s
   z!TupleFlatteningPreprocessor.writers   r$   r$   r$   r%   rO     s    "rO   c                	   @   rl   )rL   zPreprocesses each dict value, then flattens it all into a vector.

    RLlib models will unpack the flattened output before _build_layers_v2().
    r   r   r'   c                 C   s   t | jtjjs
J d}g | _| jj D ]0}td	| t
|}|d ur3||| j}||j7 }nd }|tt|j7 }| j| q|fS r~   )r9   r   r:   r;   Dictr   valuesr   r   rA   r   r   rI   r   r   r   r   r   )r#   r   r   rI   r   r   r   r$   r$   r%   r   1  s   z&DictFlatteningPreprocessor._init_shaper-   c                 C   r   r   r   r   r$   r$   r%   r/   B  r   z$DictFlatteningPreprocessor.transformr0   r1   Nc                 C   sx   t |tstt| }t|t| jks!J t|t| jft| | jD ]\}}|||| ||j	7 }q)d S rG   )
r9   r   sorteditemsr   r   r   r   r3   rI   r   r$   r$   r%   r3   I  s   
z DictFlatteningPreprocessor.writers   r$   r$   r$   r%   rL   *  s    "rL   c                	   @   rl   )rN   z0Pads and batches the variable-length list value.r   r   r'   c                 C   s@   t | jtsJ |j}t||| j| _d| jj|j  }|fS )Nr5   )	r9   r   r   child_spacer   r   child_preprocessorrI   max_len)r#   r   r   r   rI   r$   r$   r%   r   Z  s   z&RepeatedValuesPreprocessor._init_shaper-   c                 C   sB   t | j}t|tr|D ]}| j| qn	 | ||d |S )Nr   )r   r   r   r9   r8   r   rE   r3   )r#   r-   r0   elemr$   r$   r%   r/   e  s   
z$RepeatedValuesPreprocessor.transformr0   r1   Nc                 C   s   t |ttjfstd| |t|| jjkr"td|| jjt|||< t	|D ]\}}|d || j
j  }| j
||| q,d S )Nz&Input for {} must be list type, got {}z$Input {} exceeds max len of space {}r5   )r9   r8   r   r[   r@   rA   r   r   r   	enumerater   rI   r3   )r#   r-   r0   r1   r   r   offset_ir$   r$   r%   r3   p  s   
z RepeatedValuesPreprocessor.writers   r$   r$   r$   r%   rN   V  s    

"rN   Fr   r'   c                 C   s   t |  | j}t| tjjtjjfrt}|S |tkr&t	
dt t}|S |tkr6t	
dt t}|S t| tjjrAt}|S t| tjjrLt}|S t| trUt}|S t| tjjrb|rbt}|S t}|S )z>Returns an appropriate preprocessor class for the given space.a  Defaulting to RLlib's GenericPixelPreprocessor because input space has the atari-typical shape {}. Turn this behaviour off by setting `preprocessor_pref=None` or `preprocessor_pref='deepmind'` or disabling the preprocessing API altogether with `_disable_preprocessor_api=True`.a  Defaulting to RLlib's AtariRamPreprocessor because input space has the atari-typical shape {}. Turn this behaviour off by setting `preprocessor_pref=None` or `preprocessor_pref='deepmind' or disabling the preprocessing API altogether with `_disable_preprocessor_api=True`.`.)r   r   r9   r:   r;   rm   MultiDiscreterM   ATARI_OBS_SHAPEr   r   rA   rQ   ATARI_RAM_OBS_SHAPErP   r   rO   r   rL   r   rN   MultiBinaryr}   rt   )r   include_multi_binary	obs_shaper   r$   r$   r%   r     sB    	
r   c                 C   sd   t | ds/t| tjjrd| _| jS t| tjjr/g }| jD ]}t|}|| qt	|| _| jS )zAssigns shapes to spaces that don't have shapes.

    This is only needed for older gym versions that don't set shapes properly
    for Tuple and Discrete spaces.
    r   r$   )
hasattrr9   r:   r;   rm   r   r   r   r   tuple)r   shapessr   r$   r$   r%   r     s   


r   )F))collectionsr   loggingnumpyr   	gymnasiumr:   typingr   r   ray.rllib.utils.annotationsr   r   ray.rllib.utils.spaces.repeatedr   ray.rllib.utils.typingr   ray.rllib.utils.imagesr	   "ray.rllib.utils.spaces.space_utilsr
   r   r   r6   	getLoggerrU   r   r   rQ   rP   rM   rt   r}   rO   rL   rN   rY   r7   r   r   r   r$   r$   r$   r%   <module>   sF    
V*''+,*