
    &`iY                     6   d Z ddlZ	 ddlmZ n# e$ r dZY nw xY w	 ddlZej        	                    d           ddl
mZ n# eef$ r dZY nw xY wddlZddlmZmZ d Zd Ze G d	 d
ej                              Ze G d dej                              ZdS )a  
DeepMind Control Suite Wrapper directly sourced from:
https://github.com/denisyarats/dmc2gym

MIT License

Copyright (c) 2020 Denis Yarats

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
    N)specserror)suite)DeveloperAPI	PublicAPIc                 ^   d }g g }}| D ]:} ||          \  }}|                     |           |                     |           ;t          j        |d          }t          j        |d          }|j        |j        k    sJ t          j                            ||t          j                  S )Nc                    | j         t          j        k    s| j         t          j        k    sJ t          j        t          j        | j                            }t          |           t          j	        u r2t          j
        t          j        |t          j                  z  }| |fS t          |           t          j        u r4t          j        |t          j                  }| j        |z   | j        |z   fS d S )Ndtype)r   npfloat64float32int_prodshapetyper   ArrayinfonesBoundedArrayzerosminimummaximum)sdimboundr   s       }/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/rllib/env/wrappers/dm_control_wrapper.pyextract_min_maxz%_spec_to_box.<locals>.extract_min_max/   s    w"*$$2:(=(=(==gbgag&&''77ek!!FRWS
;;;;E65= !WW***HS
333E9u$ai%&777 +*    r   axisr
   )appendr   concatenater   gymspacesBoxr   )	specr   minsmaxsr   mnmxlowhighs	            r   _spec_to_boxr.   .   s    8 8 8 R$D   ##BBB
.A
&
&
&C>$Q'''D9
"""":>>#t2:>666r   c                    g }|                                  D ]T}t          j        |          rt          j        |g          n|                                }|                    |           Ut          j        |d          S )Nr   r    )valuesr   isscalararrayravelr"   r#   )obs
obs_piecesvflats       r   _flatten_obsr8   D   sr    JZZ\\     "A=rx}}}AGGII$>*1----r   c                       e Zd Z	 	 	 	 	 	 	 	 	 	 ddZd Zd	 Zd
 Zed             Zed             Z	ed             Z
d ZddddZddZdS )DMCEnvNF@   r      Tc                    || _         || _        || _        || _        |	| _        || _        || _        t          t          d          t          t          d          t          j
        |||||
          | _        t          | j                                        g          | _        t          j                            dd| j        j        t&          j                  | _        |rs|rd||gn||dg}t          j                            dd	|t&          j                  | _        |r2t          j                            d
d|t&          j                  | _        n=t          | j                                                                                  | _        t          | j                                                                                  | _        d | _        d S )NzThe `specs` module from `dm_env` was not imported. Make sure `dm_env` is installed and visible in the current python environment.zThe `suite` module from `dm_control` was not imported. Make sure `dm_control` is installed and visible in the current python enviornment.)domain_name	task_nametask_kwargsvisualize_rewardenvironment_kwargs            ?)r,   r-   r   r      r      g            ?)_from_pixels_height_width
_camera_id_frame_skip_channels_first
preprocessr   RuntimeErrorr   load_envr.   action_spec_true_action_spacer$   r%   r&   r   r   r   _norm_action_spaceuint8_observation_spaceobservation_specr0   _state_spacecurrent_state)selfr>   r?   r@   rA   from_pixelsheightwidth	camera_id
frame_skiprB   channels_firstrN   r   s                 r   __init__zDMCEnv.__init__N   s    (#%-$=#   =*   J##-1
 
 
	 #/	0E0E0G0G/H"I"I"%*..3d&=&C2: #1 #
 #

  	*8PQ&&vua>PE&)jnnCuBH '5 ' 'D#  *-*..3e2: +9 + +' '3	**,,3355' 'D# ))C)C)E)E)L)L)N)NOO!r   c                 ,    t          | j        |          S N)getattrrQ   )rZ   names     r   __getattr__zDMCEnv.__getattr__   s    ty$'''r   c                 D   | j         rg|                     | j        | j        | j                  }| j        r)|                    ddd                                          }| j        r|dz  dz
  }nt          |j
                  }|                    t          j                  S )Nr\   r]   r^   r<   r      g     o@rG   )rH   renderrI   rJ   rK   rM   	transposecopyrN   r8   observationastyper   r   )rZ   	time_stepr4   s      r   _get_obszDMCEnv._get_obs   s     		6++|4;$/   C # 4mmAq!,,1133 (EkC'y455Czz"*%%%r   c                 .   |                     t          j                  }| j        j        | j        j        z
  }| j        j        | j        j        z
  }|| j        j        z
  |z  }||z  | j        j        z   }|                     t          j                  }|S rc   )rn   r   r   rS   r-   r,   rT   r   )rZ   action
true_delta
norm_deltas       r   _convert_actionzDMCEnv._convert_action   s    rz**,1D4K4OO
,1D4K4OO
4266*D*$t'>'BBrz**r   c                     | j         S rc   )rV   rZ   s    r   observation_spacezDMCEnv.observation_space       &&r   c                     | j         S rc   )rX   rw   s    r   state_spacezDMCEnv.state_space   s      r   c                     | j         S rc   )rT   rw   s    r   action_spacezDMCEnv.action_space   ry   r   c                 <   | j                             |          sJ |                     |          }| j                            |          sJ d}d| j        j                                                                        i}dx}}t          | j	                  D ]D}| j        
                    |          }||j        pdz  }d}|                                }|s|r nE|                     |          }t          |j                  | _        |j        |d<   |||||fS )Ng        internal_stateFdiscount)rT   containsru   rS   rQ   physics	get_staterl   rangerL   steprewardlastrp   r8   rm   rY   r   )	rZ   rr   r   extra
terminated	truncated_ro   r4   s	            r   r   zDMCEnv.step   s3   &//77777%%f--&//77777!49#4#>#>#@#@#E#E#G#GH!&&
Yt'(( 	 	A	v..Ii&-#-FJ!((I Y mmI&&))*?@@%.jFJ	588r   )seedoptionsc                    | j                                         }t          |j                  | _        |                     |          }|i fS rc   )rQ   resetr8   rm   rY   rp   )rZ   r   r   ro   r4   s        r   r   zDMCEnv.reset   s@    IOO%%	))*?@@mmI&&Bwr   	rgb_arrayc                     |dk    s
J d            |p| j         }|p| j        }|p| j        }| j        j                            |||          S )Nr   zonly support for rgb_array moderh   )rI   rJ   rK   rQ   r   rj   )rZ   moder\   r]   r^   s        r   rj   zDMCEnv.render   s`    {"""$E"""'4<$0	y ''vUi'XXXr   )
NFFr;   r;   r   r<   NTT)r   NNr   )__name__
__module____qualname__ra   rf   rp   ru   propertyrx   r{   r}   r   r   rj    r   r   r:   r:   L   s        H" H" H" H"T( ( (& & &   ' ' X' ! ! X! ' ' X'9 9 9( !$     Y Y Y Y Y Yr   r:   c                   $     e Zd Z fdZd Z xZS )
ActionClipc                      t                      j        |i | d| _        d| _        t          j                            | j        | j        | j        j        | j        j	                  | _        d S )NrC   rD   )
superra   _low_highr$   r%   r&   r}   r   r   )rZ   argskwargs	__class__s      r   ra   zActionClip.__init__   sg    $)&)))	
JNNIJ##	
 
r   c                 B    t          j        || j        | j                  S rc   )r   clipr   r   )rZ   rr   s     r   rr   zActionClip.action   s    wvty$*555r   )r   r   r   ra   rr   __classcell__)r   s   @r   r   r      sG        	
 	
 	
 	
 	
6 6 6 6 6 6 6r   r   )__doc__	gymnasiumr$   dm_envr   ImportErrorabsl.loggingabslloggingset_verbosity
dm_controlr   OSErrornumpyr   ray.rllib.utils.annotationsr   r   r.   r8   Envr:   ActionWrapperr   r   r   r   <module>r      s   4       EEELw'''       W   EEE     ? ? ? ? ? ? ? ?7 7 7,. . . OY OY OY OY OYSW OY OY OYd 6 6 6 6 6" 6 6 6 6 6s    $A 	AA