
    &`iO                     4   d dl Z d dlmZmZmZmZmZmZ d dlZ	d dl
Zd dlmZmZ d dlmZmZmZ d dlmZmZmZmZmZmZmZ d dlmZ  e j        e          Ze G d d                      Z e G d	 d
e                       Z!e G d de                      Z"dS )    N)CallableListOptionalSetTupleUnion)_DUMMY_AGENT_IDBaseEnv)
DeprecatedOldAPIStackoverride)AgentIDEnvActionTypeEnvIDEnvInfoDict
EnvObsTypeEnvTypeMultiEnvDict)log_oncec                   V   e Zd ZdZdej        dej        defdZe	 	 	 	 	 	 	 	 d'd	e	e
egef                  d
e	eej                          dede	ej                 de	ej                 deddfd            Zdddde	ee                  de	ee                  deee         ee         f         fdZ	 d(dddde	e         de	e         de	e         deeeef         ef         fdZd(de	e         ddfdZdee         deee         ee         ee         ee         ee         f         fdZdee         fdZd(de	e         de	ej                 fdZ	 	 	 	 	 d)d	e	e
egef                  dededededdfd Z  e!d!d"#          d*d$            Z" e!d%d"#          dee         fd&            Z#dS )+	VectorEnvzGAn environment that supports batch evaluation using clones of sub-envs.observation_spaceaction_spacenum_envsc                 0    || _         || _        || _        dS )a  Initializes a VectorEnv instance.

        Args:
            observation_space: The observation Space of a single
                sub-env.
            action_space: The action Space of a single sub-env.
            num_envs: The number of clones to make of the given sub-env.
        Nr   r   r   )selfr   r   r   s       l/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/rllib/env/vector_env.py__init__zVectorEnv.__init__   s     "3(     N   Fmake_envexisting_envsrestart_failed_sub_environmentsreturn_VectorizedGymEnvc                 0    t          | |pg ||||          S )a  Translates any given gym.Env(s) into a VectorizedEnv object.

        Args:
            make_env: Factory that produces a new gym.Env taking the sub-env's
                vector index as only arg. Must be defined if the
                number of `existing_envs` is less than `num_envs`.
            existing_envs: Optional list of already instantiated sub
                environments.
            num_envs: Total number of sub environments in this VectorEnv.
            action_space: The action space. If None, use existing_envs[0]'s
                action space.
            observation_space: The observation space. If None, use
                existing_envs[0]'s observation space.
            restart_failed_sub_environments: If True and any sub-environment (within
                a vectorized env) throws any error during env stepping, the
                Sampler will try to restart the faulty sub-environment. This is done
                without disturbing the other (still intact) sub-environment and without
                the RolloutWorker crashing.

        Returns:
            The resulting _VectorizedGymEnv object (subclass of VectorEnv).
        )r"   r#   r   r   r   r$   )r&   )r"   r#   r   r   r   r$   
env_configpolicy_configs           r   vectorize_gym_envszVectorEnv.vectorize_gym_envs*   s3    D !'-2/%,K
 
 
 	
r    seedsoptionsr,   r-   c                    t           )a?  Resets all sub-environments.

        Args:
            seed: The list of seeds to be passed to the sub-environments' when resetting
                them. If None, will not reset any existing PRNGs. If you pass
                integers, the PRNGs will be reset even if they already exists.
            options: The list of options dicts to be passed to the sub-environments'
                when resetting them.

        Returns:
            Tuple consitsing of a list of observations from each environment and
            a list of info dicts from each environment.
        NotImplementedError)r   r,   r-   s      r   vector_resetzVectorEnv.vector_resetU   s
      "!r    seedr-   indexr3   c                    t           )a  Resets a single sub-environment.

        Args:
            index: An optional sub-env index to reset.
            seed: The seed to be passed to the sub-environment at index `index` when
                resetting it. If None, will not reset any existing PRNG. If you pass an
                integer, the PRNG will be reset even if it already exists.
            options: An options dict to be passed to the sub-environment at index
                `index` when resetting it.

        Returns:
            Tuple consisting of observations from the reset sub environment and
            an info dict of the reset sub environment. Alternatively an Exception
            can be returned, indicating that the reset operation on the sub environment
            has failed (and why it failed).
        r/   )r   r4   r3   r-   s       r   reset_atzVectorEnv.reset_atg   s
    . "!r    c                     t           )zsRestarts a single sub-environment.

        Args:
            index: An optional sub-env index to restart.
        r/   r   r4   s     r   
restart_atzVectorEnv.restart_at   s
     "!r    actionsc                     t           )a  Performs a vectorized step on all sub environments using `actions`.

        Args:
            actions: List of actions (one for each sub-env).

        Returns:
            A tuple consisting of
            1) New observations for each sub-env.
            2) Reward values for each sub-env.
            3) Terminated values for each sub-env.
            4) Truncated values for each sub-env.
            5) Info values for each sub-env.
        r/   )r   r:   s     r   vector_stepzVectorEnv.vector_step   s
    $ "!r    c                     g S )zxReturns the underlying sub environments.

        Returns:
            List of all underlying sub environments.
         r   s    r   get_sub_environmentszVectorEnv.get_sub_environments   s	     	r    c                     dS )a  Renders a single environment.

        Args:
            index: An optional sub-env index to render.

        Returns:
            Either a numpy RGB image (shape=(w x h x 3) dtype=uint8) or
            None in case rendering is handled directly by this method.
        Nr>   r8   s     r   try_render_atzVectorEnv.try_render_at   s	     	r    r   remote_envsremote_env_batch_wait_msr
   c                 $    t          |           }|S )a  Converts an RLlib MultiAgentEnv into a BaseEnv object.

        The resulting BaseEnv is always vectorized (contains n
        sub-environments) to support batched forward passes, where n may
        also be 1. BaseEnv also supports async execution via the `poll` and
        `send_actions` methods and thus supports external simulators.

        Args:
            make_env: A callable taking an int as input (which indicates
                the number of individual sub-environments within the final
                vectorized BaseEnv) and returning one individual
                sub-environment.
            num_envs: The number of sub-environments to create in the
                resulting (vectorized) BaseEnv. The already existing `env`
                will be one of the `num_envs`.
            remote_envs: Whether each sub-env should be a @ray.remote
                actor. You can set this behavior in your config via the
                `remote_worker_envs=True` option.
            remote_env_batch_wait_ms: The wait time (in ms) to poll remote
                sub-environments for, if applicable. Only used if
                `remote_envs` is True.

        Returns:
            The resulting BaseEnv object.
        )VectorEnvWrapper)r   r"   r   rC   rD   r$   envs          r   to_base_envzVectorEnv.to_base_env   s    B t$$
r    r*   T)newerrorc                     d S Nr>   )r   argskwargss      r   wrapzVectorEnv.wrap       r    r@   c                     d S rL   r>   r?   s    r   get_unwrappedzVectorEnv.get_unwrapped   rP   r    )NNr!   NNFNNrL   )Nr!   Fr   F)r%   r&   )$__name__
__module____qualname____doc__gymSpaceintr   staticmethodr   r   r   r   Envboolr*   dictr   r   r   r1   r   	Exceptionr6   r9   r   floatr<   r@   npndarrayrB   rH   r   rO   rR   r>   r    r   r   r      s}       QQ!!$!:=)!OR! ! ! ! 7;15,01505(
 (
8SE7N34(
SW.(
 (
 sy)	(

 $CI.(
 *.(
 
(
 (
 (
 \(
V /3TX" " " c+"=Ed4j=Q"	tJk!22	3" " " "(  $" #"&" " "}" sm	"
 $" 
uZ,-y8	9" " " "2" " " " " " ""M*"	Z$u+tDz4:tK?PP
" " " "(d7m    
 
8C= 
HRZ<P 
 
 
 
 8<!()05" "8SE7N34" " 	"
 #&" *." 
" " " "H Z(555   65 Z*$777tG}    87  r    r   c                   4    e Zd ZdZ	 	 	 ddddddddeeegef                  deee	j
                          ded	ee	j                 d
ee	j                 def fdZ ee          ddddeee                  deee                  deee         ee         f         fd            Z ee          	 dddddee         dee         dee         deeeef         eeef         f         fd            Z ee          ddee         ddfd            Z ee          dee         deee         ee         ee         ee         ee         f         fd            Z ee          dee         fd            Z ee          ddee         fd            Z xZS )r&   zCInternal wrapper to translate any gym.Envs into a VectorEnv object.Nr!   F)r   r   r$   r(   r)   r"   r#   r   r   r   r$   c                   || _         || _        || _        t          | j                   |k     rM| j                              |t          | j                                        t          | j                   |k     Mt                                          |p| j         d         j        |p| j         d         j        |           dS )a  Initializes a _VectorizedGymEnv object.

        Args:
            make_env: Factory that produces a new gym.Env taking the sub-env's
                vector index as only arg. Must be defined if the
                number of `existing_envs` is less than `num_envs`.
            existing_envs: Optional list of already instantiated sub
                environments.
            num_envs: Total number of sub environments in this VectorEnv.
            action_space: The action space. If None, use existing_envs[0]'s
                action space.
            observation_space: The observation space. If None, use
                existing_envs[0]'s observation space.
            restart_failed_sub_environments: If True and any sub-environment (within
                a vectorized env) throws any error during env stepping, we will try to
                restart the faulty sub-environment. This is done
                without disturbing the other (still intact) sub-environments.
        r   r   N)	envsr"   r$   lenappendsuperr   r   r   )
r   r"   r#   r   r   r   r$   r(   r)   	__class__s
            r   r   z_VectorizedGymEnv.__init__   s    > "	 /N, $)nnx''IXXc$)nn55666 $)nnx'' 	/Q49Q<3Q%B1)B 	 	
 	
 	
 	
 	
r    r+   r,   r-   r%   c                `   |p
d g| j         z  }|p
d g| j         z  }g }g }t          t          | j                            D ]k}	 |                     |||         ||                   \  }}t          |t                    sn>|                    |           |                    |           l||fS )NTr2   )r   rangere   rd   r6   
isinstancer^   rf   )r   r,   r-   resetted_obsresetted_infosiobsinfoss           r   r1   z_VectorizedGymEnv.vector_reset  s     /$$-/3dVdm3 s49~~&& 	) 	)A!]]158WQZ]PP
U!#y11  $$$!!%((((^++r    r2   r4   r3   c                   |d}	 | j         |                             ||          }n_# t          $ rR}| j        r?t                              |j        d                    |                     |           |i f}n|Y d }~nd }~ww xY w|S )Nr   r2   )rd   resetr^   r$   logger	exceptionrM   r9   )r   r4   r3   r-   obs_and_infoses         r   r6   z_VectorizedGymEnv.reset_at#  s     =E		 Ie,22g2NNMM 	 	 	3   +++&&& !2 		 s   ") 
BAB  Bc                    |d}	 | j         |                                          nF# t          $ r9}t          d          r t                              d| d|            Y d }~nd }~ww xY w| j         |         }d | j         |<   ~t                              d| d           |                     |          | j         |<   t                              d| d           d S )	Nr   close_sub_envzBTrying to close old and replaced sub-environment (at vector index=z"), but closing resulted in error:
z+Trying to restart sub-environment at index .zSub-environment at index z restarted successfully.)rd   closer^   r   rs   warningr"   )r   r4   rv   
env_to_dels       r   r9   z_VectorizedGymEnv.restart_at:  s   =E	Ie""$$$$ 	 	 	(( K"K KGHK K  	 Yu%
	% 	MUMMMNNN==//	%R5RRRSSSSSs   & 
A)/A$$A)r:   c           	         g g g g g f\  }}}}}t          | j                  D ]C}	 | j        |                             ||                   }nb# t          $ rU}	| j        rBt                              |	j        d                    | 	                    |           |	dddi f}n|	Y d }	~	nd }	~	ww xY w|\  }
}}}}t          |t                    s0t          d                    |t          |                              |                    |
           |                    |           |                    |           |                    |           |                    |           E|||||fS )Nr           Tz"Info should be a dict, got {} ({}))rj   r   rd   stepr^   r$   rs   rt   rM   r9   rk   r]   
ValueErrorformattyperf   )r   r:   	obs_batchreward_batchterminated_batchtruncated_batch
info_batchrn   resultsrv   ro   reward
terminated	truncatedinfos                  r   r<   z_VectorizedGymEnv.vector_stepQ  s    R
N	<!1?J t}%% 	$ 	$A)A,++GAJ77   7 $$QVAY///OOA&&&dD"4GGG GGGG	 8?4CYdD))  8??d4jjQQ   S!!!'''##J///""9---d####,(8/:UUs   &A
B,AB''B,c                     | j         S rL   )rd   r?   s    r   r@   z&_VectorizedGymEnv.get_sub_environmentsv  s
    yr    c                 H    |d}| j         |                                         S )Nr   )rd   renderr8   s     r   rB   z_VectorizedGymEnv.try_render_atz  s%    =Ey&&(((r    )NNr!   rL   )rS   rT   rU   rV   r   r   rY   r   r   rW   r[   rX   r\   r   r   r   r]   r   r   r   r1   r   r^   r6   r9   r   r_   r<   r@   rB   __classcell__)rh   s   @r   r&   r&      s       MM 8<15	,
 26,005,
 ,
 ,
8SE7N34,
  SW.,
 	,
 $CI.,
 sy),
 *.,
 ,
 ,
 ,
 ,
 ,
\ Xi.2TX, , , c+,=Ed4j=Q,	tJk!22	3, , , ,$ Xi  $ #"&  } sm	
 $ 
uZ*+U;	3I-JJ	K   , XiT T T T T T T, Xi"VM*"V	Z$u+tDz4:tK?PP
"V "V "V "VH Xid7m     Xi) )8C= ) ) ) ) ) ) ) )r    r&   c                      e Zd ZdZdefdZ ee          dee	e	e	e	e	e	f         fd            Z
 ee          de	ddfd            Z ee          	 dddd	d
ee         dee         dee         dee	e	f         fd            Z ee          dd
ee         ddfd            Z ee          ddedeee         ef         fd            Z ee          dd
ee         ddfd            Ze ee          dej        fd                        Ze ee          dej        fd                        Z ee          dee         fd            Zddee         ddfdZ dS )rF   zInternal adapter of VectorEnv to BaseEnv.

    We assume the caller will always send the full vector of actions in each
    call to send_actions(), and that they call reset_at() on all completed
    environments before calling send_actions().
    
vector_envc                     || _         |j        | _        |j        | _        |j        | _        d | _        d | _        d | _        d | _	        d | _
        d| _        |                     d            d S )NF)idx)r   r   r   _observation_spacer   _action_spacenew_obscur_rewardscur_terminatedscur_truncateds	cur_infosfirst_reset_done_init_env_state)r   r   s     r   r   zVectorEnvWrapper.__init__  sv    $"+",">'4 #" %&&&&&r    r%   c                 t   ddl m} | j        s-d| _        | j                                        \  | _        | _        t          t          | j                            }t          t          | j	                            }t          t          | j
                            }t          t          | j                            }t          t          | j                            }g | _        g | _	        g | _
        g | _        g | _         ||           ||           ||d           ||d           ||          i fS )Nr   )with_dummy_agent_idT__all__)ray.rllib.env.base_envr   r   r   r1   r   r   r]   	enumerater   r   r   )r   r   r   rewardsterminateds
truncatedsrp   s          r   pollzVectorEnvWrapper.poll  s=    	?>>>>>$ 	J$(D!+/?+G+G+I+I(DL$.y..//y!122339T%9::;;)D$78899
Yt~..// !   ((((Y77
I66&&
 	
r    action_dictNc                     ddl m} d g| j        z  }t          | j                  D ]}||         |         ||<   | j                            |          \  | _        | _        | _        | _	        | _
        d S )Nr   r	   )r   r	   r   rj   r   r<   r   r   r   r   r   )r   r   r	   action_vectorrn   s        r   send_actionszVectorEnvWrapper.send_actions  s    ::::::.t}%% 	? 	?A*1~o>M! O''66	
L NNNr    r2   env_idr3   r-   c                    ddl m} |d}t          |t                    sJ | j                            |||          \  }}t          |t                    r||i||ifS |||ii|||iifS )Nr   r   r2   )r   r	   rk   rY   r   r6   r^   )r   r   r3   r-   r	   ro   rp   s          r   	try_resetzVectorEnvWrapper.try_reset  s     	;:::::>F&#&&&&&_--f4-QQ
U c9%% 	XC=65/11 _c23fPU>V5WWWr    c                     |t          |t                    sJ | j                            |           |                     |           d S rL   )rk   rY   r   r9   r   r   r   s     r   try_restartzVectorEnvWrapper.try_restart  sJ    ~FC!8!8~~8""6***V$$$$$r    Fas_dictc                     |s| j                                         S t          t          | j                                                             S rL   )r   r@   r]   r   )r   r   s     r   r@   z%VectorEnvWrapper.get_sub_environments  sC     	K?77999	$/"F"F"H"HIIJJJr    c                 h    |t          |t                    sJ | j                            |          S rL   )rk   rY   r   rB   r   s     r   
try_renderzVectorEnvWrapper.try_render  s0    ~FC!8!8~~8,,V444r    c                     | j         S rL   )r   r?   s    r   r   z"VectorEnvWrapper.observation_space  s     &&r    c                     | j         S rL   )r   r?   s    r   r   zVectorEnvWrapper.action_space  s     !!r    c                     t           hS rL   r   r?   s    r   get_agent_idszVectorEnvWrapper.get_agent_ids  s      r    r   c                    |d t          | j                  D             | _        d t          | j                  D             | _        d t          | j                  D             | _        d t          | j                  D             | _        d t          | j                  D             | _        dS | j                            |          \  | j        |<   | j        |<   d| j        |<   d| j        |<   d| j        |<   dS )	zResets all or one particular sub-environment's state (by index).

        Args:
            idx: The index to reset at. If None, reset all the sub-environments' states.
        Nc                     g | ]}d S rL   r>   .0_s     r   
<listcomp>z4VectorEnvWrapper._init_env_state.<locals>.<listcomp>  s    ???QD???r    c                     g | ]}d S )r~   r>   r   s     r   r   z4VectorEnvWrapper._init_env_state.<locals>.<listcomp>  s    BBBBBBr    c                     g | ]}d S Fr>   r   s     r   r   z4VectorEnvWrapper._init_env_state.<locals>.<listcomp>  s    #H#H#HaE#H#H#Hr    c                     g | ]}d S r   r>   r   s     r   r   z4VectorEnvWrapper._init_env_state.<locals>.<listcomp>  s    "G"G"GQ5"G"G"Gr    c                     g | ]}i S r>   r>   r   s     r   r   z4VectorEnvWrapper._init_env_state.<locals>.<listcomp>  s    ???Qb???r    r~   F)	rj   r   r   r   r   r   r   r   r6   )r   r   s     r   r   z VectorEnvWrapper._init_env_state  s    ;??%*>*>???DLBBU4=-A-ABBBD#H#H53G3G#H#H#HD "G"G%2F2F"G"G"GD??%*>*>???DNNN 6:_5M5Mc5R5R2DLt~c2$'DS!(-D %',D$$$r    rL   r   )!rS   rT   rU   rV   r   r   r   r
   r   r   r   r   r   r   rY   r]   r   r   r\   r   r   r   r@   r   propertyrW   rX   r   r   r   r   r   r   r>   r    r   rF   rF     s        '9 ' ' ' '" Xg$
		
$
 $
 $
 $
L Xg7 7 7 7 7 7 Xg #'X #"&X X XX sm	X
 $X 
|\)	*X X X X. Xg% %(5/ %T % % % % XgK KD KU4=RVCV=W K K K K Xg5 5% 5D 5 5 5 5 Xg'39 ' ' '  X' Xg"ci " " "  X" Xg!s7| ! ! ! !- -8C= -D - - - - - -r    rF   )#loggingtypingr   r   r   r   r   r   	gymnasiumrW   numpyr`   r   r	   r
   ray.rllib.utils.annotationsr   r   r   ray.rllib.utils.typingr   r   r   r   r   r   r   ray.utilr   	getLoggerrS   rs   r   r&   rF   r>   r    r   <module>r      s    > > > > > > > > > > > > > > > >         ; ; ; ; ; ; ; ; I I I I I I I I I I                       		8	$	$ C C C C C C C CL _) _) _) _) _)	 _) _) _)D \- \- \- \- \-w \- \- \- \- \-r    