o
    ci@                     @   sr   d dl Z d dlmZ d dlZd dlZd dlmZ d dlm	Z	 d dl
mZ e eZG dd deZedd	 ZdS )
    N)CartPoleEnv)make_multi_agent)override)EnvErrorc                       sb   e Zd ZdZd fdd	Zeeddd fdd
Zee fdd	Zd
d Z	dd Z
  ZS )CartPoleCrashinga@  A CartPole env that crashes (or stalls) from time to time.

    Useful for testing faulty sub-env (within a vectorized env) handling by
    EnvRunners.

    After crashing, the env expects a `reset()` call next (calling `step()` will
    result in yet another error), which may or may not take a very long time to
    complete. This simulates the env having to reinitialize some sub-processes, e.g.
    an external connection.

    The env can also be configured to stall (and do nothing during a call to `step()`)
    from time to time for a configurable amount of time.
    Nc                    s  t    |d ur|ni | _|dd| _|dd| _|d| _d | _| jd u s?t| jt	s?t| jt
r=t| jdks?J |dd }|rU|j|vrUd| _d| _d | _|dd| _|d	d| _|d
| _d | _|d| _| jd u st| jt	tfst| jt
rt| jdksJ |dd }|r|j|vrd| _d| _d | _d| _d}d|v rt|d t
s|d ntj|d d |d d }td| d t| tj | _d S )Np_crashg{Gzt?p_crash_resetg        crash_after_n_steps   crash_on_worker_indicesp_stallp_stall_resetstall_after_n_stepsstall_time_secstall_on_worker_indicesr   init_time_s   z.Initializing crashing env (with init-delay of zsec) ...)super__init__configgetr   r   r	   _crash_after_n_steps
isinstanceinttuplelenworker_indexr   r   r   _stall_after_n_stepsr   float	timestepsnprandomuniformprinttimesleepRandomState_rng)selfr   faulty_indicessample	__class__ e/home/ubuntu/.local/lib/python3.10/site-packages/ray/rllib/examples/envs/classes/cartpole_crashing.pyr      sZ   







zCartPoleCrashing.__init__)seedoptionsc                   sP   d| _ d | _| j| jdrtd| jj d| jj d| j| j	d t
  S )Nr   pSimulated env crash on worker=	 env-idx=zJ during `reset()`! Feel free to use any other exception type here instead.)r   r   _should_crashr   r   r   r   vector_index_stall_if_necessaryr   r   reset)r(   r/   r0   r+   r-   r.   r8   e   s   
zCartPoleCrashing.resetc                    sT   |  j d7  _ | j| jdrtd| jj d| jj d| j| jd t	 
|S )Nr   r1   r3   r4   zI during `step()`! Feel free to use any other exception type here instead.)r   r5   r   r   r   r   r6   r7   r   r   step)r(   actionr+   r-   r.   r9   w   s   zCartPoleCrashing.stepc                 C   s|   | j  }||k rtd dS | jd ur<| jd u r0t| jts"| jntj	| jd | jd | _| j| j
kr<td dS dS )NzCrashing due to p(crash)!Tr   r   z$Crashing due to n timesteps reached!F)r'   randr#   r	   r   r   r   r    r!   randintr   )r(   r2   rndr-   r-   r.   r5      s    



zCartPoleCrashing._should_crashc                 C   s   d}| j  |k rd}n)| jd ur5| jd u r-t| jts| jntj| jd | jd | _| j| j	kr5d}|r\t| j
ts@| j
ntj| j
d | j
d }td| d t| d S d S )NFTr   r   z -> will stall for zsec ...)r'   r;   r   r   r   r   r    r!   r<   r   r   r"   r#   r$   r%   )r(   r2   stallsecr-   r-   r.   r7      s*   



z$CartPoleCrashing._stall_if_necessaryN)__name__
__module____qualname____doc__r   r   r   r8   r9   r5   r7   __classcell__r-   r-   r+   r.   r      s    Ir   c                 C   s   t | S r@   )r   )r   r-   r-   r.   <lambda>   s    rF   )logginggymnasium.envs.classic_controlr   numpyr    r$   +ray.rllib.examples.envs.classes.multi_agentr   ray.rllib.utils.annotationsr   ray.rllib.utils.errorr   	getLoggerrA   loggerr   MultiAgentCartPoleCrashingr-   r-   r-   r.   <module>   s    
 *