
    Di                        d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 erd dl
mZ d dlmZ dd	Z G d
 de      Zy)    )annotationsN)Any)TYPE_CHECKING)
BasePruner)_is_first_in_interval_step)Study)FrozenTrialc                    	 t        |       } | S # t        t        f$ r& dt        |       j                   d}t        |      d w xY w)Nz!The `value` argument is of type 'z' but supposed to be a float.)float	TypeError
ValueErrortype__name__)valuemessages     _/home/ubuntu/crypto_trading_bot/.venv/lib/python3.12/site-packages/optuna/pruners/_threshold.py_check_valuer      sY    +e L z" +/U0D0D/EEbc 	  d*	+s	    5Ac                  <    e Zd ZdZ	 	 	 	 d	 	 	 	 	 	 	 	 	 ddZddZy)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.

    Nc                <   ||t        d      |t        |      }|t        |      }||nt        d       }||n
t        d      }||kD  rt        d      |dk  rt        d| d      |dk  rt        d| d      || _        || _        || _        || _        y )	Nz(Either lower or upper must be specified.infz#lower should be smaller than upper.r   z2Number of warmup steps cannot be negative but got .   z2Pruning interval steps must be at least 1 but got )r   r   r   r   _lower_upper_n_warmup_steps_interval_steps)selfloweruppern_warmup_stepsinterval_stepss        r   __init__zThresholdPruner.__init__V   s     =U]FGG 'E 'E*u*e5=BCCAD^DTTUV  AD^DTTUV  --    c                2   |j                   }|y| j                  }||k  ryt        ||j                  j	                         || j
                        sy|j                  |   }t        j                  |      ry|| j                  k  ry|| j                  kD  ryy)NFT)
	last_stepr   r   intermediate_valueskeysr   mathisnanr   r   )r   studytrialstepr!   latest_values         r   prunezThresholdPruner.prunew   s    <--. )%++002NDDXDX
 006::l#$++%$++%r$   )NNr   r   )
r   float | Noner    r0   r!   intr"   r1   returnNone)r+   r   r,   r	   r2   bool)r   
__module____qualname____doc__r#   r/    r$   r   r   r      sQ    6t #".. . 	.
 . 
.Br$   r   )r   r   r2   r   )
__future__r   r)   typingr   r   optuna.prunersr   optuna.pruners._percentiler   optuna.studyr   optuna.trialr	   r   r   r8   r$   r   <module>r?      s5    "     % A "(
rj rr$   