o
    /wÖi˜  ã                   @  s^   d dl mZ d dlZd dlmZ d dlZd dlmZ d dlm	Z	 dd
d„Z
G dd„ deƒZdS )é    )ÚannotationsN)ÚAny)Ú
BasePruner)Ú_is_first_in_interval_stepÚvaluer   ÚreturnÚfloatc              	   C  s<   zt | ƒ} W | S  ttfy   d t| ƒj¡}t|ƒd ‚w )Nz@The `value` argument is of type '{}' but supposed to be a float.)r   Ú	TypeErrorÚ
ValueErrorÚformatÚtypeÚ__name__)r   Úmessage© r   úV/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/optuna/pruners/_threshold.pyÚ_check_value   s   
úÿ
ür   c                   @  s.   e Zd ZdZ				dddd„Zddd„ZdS )ÚThresholdPruneraÎ  Pruner to detect outlying metrics of the trials.

    Prune if a metric exceeds upper threshold,
    falls behind lower threshold or reaches ``nan``.

    Example:
        .. testcode::

            from optuna import create_study
            from optuna.pruners import ThresholdPruner
            from optuna import TrialPruned


            def objective_for_upper(trial):
                for step, y in enumerate(ys_for_upper):
                    trial.report(y, step)

                    if trial.should_prune():
                        raise TrialPruned()
                return ys_for_upper[-1]


            def objective_for_lower(trial):
                for step, y in enumerate(ys_for_lower):
                    trial.report(y, step)

                    if trial.should_prune():
                        raise TrialPruned()
                return ys_for_lower[-1]


            ys_for_upper = [0.0, 0.1, 0.2, 0.5, 1.2]
            ys_for_lower = [100.0, 90.0, 0.1, 0.0, -1]

            study = create_study(pruner=ThresholdPruner(upper=1.0))
            study.optimize(objective_for_upper, n_trials=10)

            study = create_study(pruner=ThresholdPruner(lower=0.0))
            study.optimize(objective_for_lower, n_trials=10)

    Args:
        lower:
            A minimum value which determines whether pruner prunes or not.
            If an intermediate value is smaller than lower, it prunes.
        upper:
            A maximum value which determines whether pruner prunes or not.
            If an intermediate value is larger than upper, it prunes.
        n_warmup_steps:
            Pruning is disabled if the step is less than the given number of warmup steps.
        interval_steps:
            Interval in number of steps between the pruning checks, offset by the warmup steps.
            If no value has been reported at the time of a pruning check, that particular check
            will be postponed until a value is reported. Value must be at least 1.

    Nr   é   Úlowerúfloat | NoneÚupperÚn_warmup_stepsÚintÚinterval_stepsr   ÚNonec                 C  sº   |d u r|d u rt dƒ‚|d urt|ƒ}|d urt|ƒ}|d ur"|ntdƒ }|d ur-|ntdƒ}||kr9tdƒ‚|dk rDtd |¡ƒ‚|dk rOtd |¡ƒ‚|| _|| _|| _|| _d S )Nz(Either lower or upper must be specified.Úinfz#lower should be smaller than upper.r   z5Number of warmup steps cannot be negative but got {}.r   z5Pruning interval steps must be at least 1 but got {}.)	r	   r   r   r
   r   Ú_lowerÚ_upperÚ_n_warmup_stepsÚ_interval_steps)Úselfr   r   r   r   r   r   r   Ú__init__Q   s,   ÿÿ
zThresholdPruner.__init__Ústudyú'optuna.study.Study'Útrialú'optuna.trial.FrozenTrial'Úboolc                 C  sv   |j }|d u r	dS | j}||k rdS t||j ¡ || jƒsdS |j| }t |¡r+dS || jk r2dS || j	kr9dS dS )NFT)
Ú	last_stepr   r   Úintermediate_valuesÚkeysr   ÚmathÚisnanr   r   )r    r"   r$   Ústepr   Úlatest_valuer   r   r   Úpruner   s$   ÿ



zThresholdPruner.prune)NNr   r   )
r   r   r   r   r   r   r   r   r   r   )r"   r#   r$   r%   r   r&   )r   Ú
__module__Ú__qualname__Ú__doc__r!   r.   r   r   r   r   r      s    :û!r   )r   r   r   r   )Ú
__future__r   r*   Útypingr   ÚoptunaÚoptuna.prunersr   Úoptuna.pruners._percentiler   r   r   r   r   r   r   Ú<module>   s    
