o
    $i                     @   sX   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 de	ZdS )    )defaultdictdeque)DictOptionalN)Stopper)	PublicAPIc                   @   s`   e Zd ZdZ					ddedededed	ee d
ee fddZdede	fddZ
d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                    sd   | _ | _| _| _| _| _ jr |dvr td| tdd  _t fdd _	d S )N)minmaxz\When specifying a `metric_threshold`, the `mode` argument has to be one of [min, max]. Got: c                   S      dS )Nr    r   r   r   [/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/tune/stopper/trial_plateau.py<lambda>=   s    z.TrialPlateauStopper.__init__.<locals>.<lambda>c                      s   t  jdS )N)maxlen)r   _num_resultsr   selfr   r   r   >   s    )
_metric_mode_stdr   _grace_period_metric_threshold
ValueErrorr   _iter_trial_results)r   r   r   r   r   r   r   r   r   r   __init__$   s   	zTrialPlateauStopper.__init__trial_idresultc                 C   s   | | j}| j| | | j|  d7  < | j| | jk r!dS t| j| | jk r-dS | jd urJ| j	dkr>|| jkr>dS | j	dkrJ|| jk rJdS z
t
| j| }W n tya   td}Y nw || jk S )N   Fr   r   inf)getr   r"   appendr!   r   lenr   r   r   npr   	Exceptionfloatr   )r   r$   r%   metric_resultcurrent_stdr   r   r   __call__@   s$   

zTrialPlateauStopper.__call__c                 C   r   )NFr   r   r   r   r   stop_all]   s   zTrialPlateauStopper.stop_all)r	   r
   r
   NN)__name__
__module____qualname____doc__strr-   intr   r#   r   r0   r1   r   r   r   r   r   
   s,    
r   )collectionsr   r   typingr   r   numpyr+   ray.tune.stopper.stopperr   ray.util.annotationsr   r   r   r   r   r   <module>   s    