
    -ji                        S SK Jr  S SKrS SKJr  S SKrS SKJr  S SK	J
r
  S SKJr  \(       a  S SKJr  S SKJr  S S	KJr  \" S
5       " S S\
5      5       rg)    )annotationsN)TYPE_CHECKING)experimental_class)BaseErrorEvaluator)
TrialState)StudyDirection)BaseImprovementEvaluator)FrozenTrialz4.0.0c                  T    \ rS rSrSr   S         SS jjr      S	S jrSrg)
MedianErrorEvaluator   a  An error evaluator that returns the ratio to initial median.

This error evaluator is introduced as a heuristics in the following paper:

- `A stopping criterion for Bayesian optimization by the gap of expected minimum simple
  regrets <https://proceedings.mlr.press/v206/ishibashi23a.html>`__

Args:
    paired_improvement_evaluator:
        The ``improvement_evaluator`` instance which is set with this ``error_evaluator``.
    warm_up_trials:
        A parameter specifies the number of initial trials to be discarded before
        the calculation of median. Default to 10.
        In optuna, the first 10 trials are often random sampling.
        The ``warm_up_trials`` can exclude them from the calculation.
    n_initial_trials:
        A parameter specifies the number of initial trials considered in the calculation of
        median after ``warm_up_trials``. Default to 20.
    threshold_ratio:
        A parameter specifies the ratio between the threshold and initial median.
        Default to 0.01.
c                    US:  a  [        S5      eUS::  a  [        S5      eUS::  d  [        R                  " U5      (       d  [        S5      eXl        X l        X0l        X@l        S U l        g )Nr   z:`warm_up_trials` is expected to be a non-negative integer.z8`n_initial_trials` is expected to be a positive integer.g        zA`threshold_ratio_to_initial_median` is expected to be a positive.)
ValueErrornpisfinite_paired_improvement_evaluator_warm_up_trials_n_initial_trials_threshold_ratio
_threshold)selfpaired_improvement_evaluatorwarm_up_trialsn_initial_trialsthreshold_ratios        \/home/james-whalen/.local/lib/python3.13/site-packages/optuna/terminator/median_erroreval.py__init__MedianErrorEvaluator.__init__,   sm     AYZZq WXXc!_)E)E`aa-I*-!1 /(,    c           	        U R                   b  U R                   $ U Vs/ s H$  o3R                  [        R                  :X  d  M"  UPM&     nn[	        U5      U R
                  U R                  -   :  a  [        R                  R                  * $ UR                  S S9  / n[        SU R                  S-   5       HG  nUR                  U R                  R                  XR
                  U R
                  U-    U5      5        MI     UR                  5         U[	        U5      S-     U l         U R                   c   e[        [        R                  R                  U R                   U R                   -  5      U l         U R                   $ s  snf )Nc                    U R                   $ )N)number)trials    r   <lambda>/MedianErrorEvaluator.evaluate.<locals>.<lambda>M   s    ellr   )key      )r   stater   COMPLETElenr   r   sys
float_infominsortrangeappendr   evaluatemaxr   )r   trialsstudy_directionr#   criteriais         r   r2   MedianErrorEvaluator.evaluate@   sA   
 ??&??"%+RVE{{j>Q>Q/Q%VRv;$..1G1GGH### 	23q$00145AOO22;;//$2F2F2JK_ 6 	"3x=A#56***cnn00$//DDYDY2YZ# Ss   !FF)r   r   r   r   r   N)
      g{Gz?)
r   r	   r   intr   r;   r   floatreturnNone)r4   zlist[FrozenTrial]r5   r   r=   r<   )__name__
__module____qualname____firstlineno____doc__r   r2   __static_attributes__ r   r   r   r      sh    4 ! "!%-&>- - 	-
 - 
-(! ( 
	r   r   )
__future__r   r,   typingr   numpyr   optuna._experimentalr   optuna.terminator.errorevalr   optuna.trial._stater   optuna.studyr   'optuna.terminator.improvement.evaluatorr	   optuna.trialr
   r   rE   r   r   <module>rO      sL    " 
    3 : * +P( GE- E Er   