
    &`i                     r    d dl mZmZ d dlmZmZ d dlZd dlm	Z	 d dl
mZ e G d de	                      ZdS )    )defaultdictdeque)DictOptionalN)Stopper)	PublicAPIc                   l    e Zd ZdZ	 	 	 	 	 ddedededed	ee         d
ee         fdZdede	fdZ
d ZdS )TrialPlateauStoppera&  Early stop single trials when they reached a plateau.

    When the standard deviation of the `metric` result of a trial is
    below a threshold `std`, the trial plateaued and will be stopped
    early.

    Args:
        metric: Metric to check for convergence.
        std: Maximum metric standard deviation to decide if a
            trial plateaued. Defaults to 0.01.
        num_results: Number of results to consider for stdev
            calculation.
        grace_period: Minimum number of timesteps before a trial
            can be early stopped
        metric_threshold (Optional[float]):
            Minimum or maximum value the result has to exceed before it can
            be stopped early.
        mode: If a `metric_threshold` argument has been
            passed, this must be one of [min, max]. Specifies if we optimize
            for a large metric (max) or a small metric (min). If max, the
            `metric_threshold` has to be exceeded, if min the value has to
            be lower than `metric_threshold` in order to early stop.
    {Gz?   Nmetricstdnum_resultsgrace_periodmetric_thresholdmodec                      | _         | _        | _        | _        | _        | _         j        r|dvrt          d|           t          d            _        t           fd           _	        d S )N)minmaxz\When specifying a `metric_threshold`, the `mode` argument has to be one of [min, max]. Got: c                      dS )Nr    r       r/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/stopper/trial_plateau.py<lambda>z.TrialPlateauStopper.__init__.<locals>.<lambda>=   s     r   c                  .    t           j                  S )N)maxlen)r   _num_resultsselfs   r   r   z.TrialPlateauStopper.__init__.<locals>.<lambda>>   s    %t?P2Q2Q2Q r   )
_metric_mode_stdr   _grace_period_metric_threshold
ValueErrorr   _iter_trial_results)r   r   r   r   r   r   r   s   `      r   __init__zTrialPlateauStopper.__init__$   s     
	')!1! 	>)) # # #   !++
)*Q*Q*Q*QRRr   trial_idresultc                    |                     | j                  }| j        |                             |           | j        |xx         dz  cc<   | j        |         | j        k     rdS t          | j        |                   | j        k     rdS | j        0| j	        dk    r|| j        k    rdS | j	        dk    r|| j        k     rdS 	 t          j        | j        |                   }n# t          $ r t          d          }Y nw xY w|| j        k     S )N   Fr   r   inf)getr    r'   appendr&   r#   lenr   r$   r!   npr   	Exceptionfloatr"   )r   r)   r*   metric_resultcurrent_stds        r   __call__zTrialPlateauStopper.__call__@   s0   

4<00H%,,];;;
8! :h$"4445 t"8,--0AAA5 !-zU""}t7M'M'Muu$$9O)O)Ou	'&!4X!>??KK 	' 	' 	',,KKK	' TY&&s   C% %D Dc                     dS )NFr   r   s    r   stop_allzTrialPlateauStopper.stop_all]   s    ur   )r   r   r   NN)__name__
__module____qualname____doc__strr3   intr   r(   r   r6   r8   r   r   r   r
   r
   
   s         6 ,0"S SS S 	S
 S #5/S smS S S S8' 'd ' ' ' ':    r   r
   )collectionsr   r   typingr   r   numpyr1   ray.tune.stopper.stopperr   ray.util.annotationsr   r
   r   r   r   <module>rD      s    * * * * * * * * ! ! ! ! ! ! ! !     , , , , , , * * * * * * S S S S S' S S S S Sr   