o
    $i                     @   sR   d dl Z d dlmZmZ d dlmZ eG dd de jZeG dd deZdS )    N)AnyDict)	PublicAPIc                   @   s<   e Zd ZdZdedeeef defddZdefddZ	d	S )
Stoppera  Base class for implementing a Tune experiment stopper.

    Allows users to implement experiment-level stopping via ``stop_all``. By
    default, this class does not stop any trials. Subclasses need to
    implement ``__call__`` and ``stop_all``.

    Examples:

        >>> import time
        >>> from ray import tune
        >>> from ray.tune import Stopper
        >>>
        >>> class TimeStopper(Stopper):
        ...     def __init__(self):
        ...         self._start = time.time()
        ...         self._deadline = 2  # Stop all trials after 2 seconds
        ...
        ...     def __call__(self, trial_id, result):
        ...         return False
        ...
        ...     def stop_all(self):
        ...         return time.time() - self._start > self._deadline
        ...
        >>> def train_fn(config):
        ...     for i in range(100):
        ...         time.sleep(1)
        ...         tune.report({"iter": i})
        ...
        >>> tuner = tune.Tuner(
        ...     train_fn,
        ...     tune_config=tune.TuneConfig(num_samples=2),
        ...     run_config=tune.RunConfig(stop=TimeStopper()),
        ... )
        >>> print("[ignore]"); result_grid = tuner.fit()  # doctest: +ELLIPSIS
        [ignore]...

    trial_idresultreturnc                 C      t )z@Returns true if the trial should be terminated given the result.NotImplementedErrorselfr   r    r   U/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/tune/stopper/stopper.py__call__/      zStopper.__call__c                 C   r	   )z4Returns true if the experiment should be terminated.r
   r   r   r   r   stop_all3   r   zStopper.stop_allN)
__name__
__module____qualname____doc__strr   r   boolr   r   r   r   r   r   r      s    &r   c                   @   sJ   e Zd ZdZdefddZdedeeef de	fdd	Z
de	fd
dZdS )CombinedStoppera  Combine several stoppers via 'OR'.

    Args:
        *stoppers: Stoppers to be combined.

    Examples:

        >>> import numpy as np
        >>> from ray import tune
        >>> from ray.tune.stopper import (
        ...     CombinedStopper,
        ...     MaximumIterationStopper,
        ...     TrialPlateauStopper,
        ... )
        >>>
        >>> stopper = CombinedStopper(
        ...     MaximumIterationStopper(max_iter=10),
        ...     TrialPlateauStopper(metric="my_metric"),
        ... )
        >>> def train_fn(config):
        ...     for i in range(15):
        ...         tune.report({"my_metric": np.random.normal(0, 1 - i / 15)})
        ...
        >>> tuner = tune.Tuner(
        ...     train_fn,
        ...     run_config=tune.RunConfig(stop=stopper),
        ... )
        >>> print("[ignore]"); result_grid = tuner.fit()  # doctest: +ELLIPSIS
        [ignore]...
        >>> all(result.metrics["training_iteration"] <= 20 for result in result_grid)
        True

    stoppersc                 G   s
   || _ d S N)	_stoppers)r   r   r   r   r   __init__\   s   
zCombinedStopper.__init__r   r   r   c                    s   t  fdd| jD S )Nc                 3   s    | ]}| V  qd S r   r   .0sr   r   r   r   	<genexpr>`   s    z+CombinedStopper.__call__.<locals>.<genexpr>anyr   r   r   r"   r   r   _   s   zCombinedStopper.__call__c                 C   s   t dd | jD S )Nc                 s   s    | ]}|  V  qd S r   )r   r   r   r   r   r#   c   s    z+CombinedStopper.stop_all.<locals>.<genexpr>r$   r   r   r   r   r   b   s   zCombinedStopper.stop_allN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   8   s
    "r   )	abctypingr   r   ray.util.annotationsr   ABCr   r   r   r   r   r   <module>   s    0