
    &`i                     R    d dl Zd dlmZ d dlmZ e G d de                      ZdS )    N)Stopper)	PublicAPIc                   J    e Zd ZdZ	 	 	 	 ddededed	ed
ef
dZd Zd Z	d Z
dS )ExperimentPlateauStoppera  Early stop the experiment when a metric plateaued across trials.

    Stops the entire experiment when the metric has plateaued
    for more than the given amount of iterations specified in
    the patience parameter.

    Args:
        metric: The metric to be monitored.
        std: The minimal standard deviation after which
            the tuning process has to stop.
        top: The number of best models to consider.
        mode: The mode to select the top results.
            Can either be "min" or "max".
        patience: Number of epochs to wait for
            a change in the top models.

    Raises:
        ValueError: If the mode parameter is not "min" nor "max".
        ValueError: If the top parameter is not an integer
            greater than 1.
        ValueError: If the standard deviation parameter is not
            a strictly positive float.
        ValueError: If the patience parameter is not
            a strictly positive integer.
    MbP?
   minr   metricstdtopmodepatiencec                    |dvrt          d          t          |t                    r|dk    rt          d          t          |t                    r|dk     rt          d          t          |t                    r|dk    rt          d          || _        || _        || _        d| _        || _        || _	        g | _
        d S )N)r	   maxz1The mode parameter can only be either min or max.   zDTop results to consider must be a positive integer greater than one.r   z-Patience must be a strictly positive integer.z@The standard deviation must be a strictly positive float number.)
ValueError
isinstanceintfloat_mode_metric	_patience_iterations_std_top_top_values)selfr
   r   r   r   r   s         w/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/tune/stopper/experiment_plateau.py__init__z!ExperimentPlateauStopper.__init__#   s     ~%%PQQQ#s## 	saxx8   (C(( 	NHqLLLMMM#u%% 	R   
!		    c                 ~   | j                             || j                            | j        dk    r't	          | j                   d| j                 | _         n't	          | j                   | j         d         | _         |                                 r| xj        dz  c_        nd| _        |                                 S )z8Return a boolean representing if the tuning has to stop.r	   Nr   r   )	r   appendr   r   sortedr   has_plateauedr   stop_all)r   trial_idresults      r   __call__z!ExperimentPlateauStopper.__call__@   s    t| 4555:%d&677$)DD%d&677
ED  	!!  !D }}r    c                     t          | j                  | j        k    o!t          j        | j                  | j        k    S )N)lenr   r   npr   r   r   s    r   r$   z&ExperimentPlateauStopper.has_plateauedT   s5     !!TY.X26$:J3K3Kty3X	
r    c                 J    |                                  o| j        | j        k    S )z8Return whether to stop and prevent trials from starting.)r$   r   r   r,   s    r   r%   z!ExperimentPlateauStopper.stop_allY   s#    !!##J(8DN(JJr    N)r   r   r	   r   )__name__
__module____qualname____doc__strr   r   r   r(   r$   r%    r    r   r   r      s         :    	
     :  (
 
 

K K K K Kr    r   )numpyr+   ray.tune.stopper.stopperr   ray.util.annotationsr   r   r3   r    r   <module>r7      s        , , , , , , * * * * * * SK SK SK SK SKw SK SK SK SK SKr    