o
    $i                     @   s8   d dl Zd dlmZ d dlmZ eG dd deZdS )    N)Stopper)	PublicAPIc                   @   sP   e Zd ZdZ				ddededed	ed
ef
ddZdd Zdd Z	d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                 C   s   |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    r   `/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/tune/stopper/experiment_plateau.py__init__#   s&   
z!ExperimentPlateauStopper.__init__c                 C   sx   | j || j  | jdkrt| j d| j | _ nt| j | j d | _ |  r5|  jd7  _|  S 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resultr   r   r   __call__@   s   
z!ExperimentPlateauStopper.__call__c                 C   s"   t | j| jkot| j| jkS )N)lenr   r   npr	   r   r   r   r   r   r    T   s    z&ExperimentPlateauStopper.has_plateauedc                 C   s   |   o	| j| jkS )z8Return whether to stop and prevent trials from starting.)r    r   r   r'   r   r   r   r!   Y   s   z!ExperimentPlateauStopper.stop_allN)r   r   r   r   )__name__
__module____qualname____doc__strr   r   r   r$   r    r!   r   r   r   r   r      s(    
r   )numpyr&   ray.tune.stopper.stopperr   ray.util.annotationsr   r   r   r   r   r   <module>   s
    