
    ,YHh                        d dl mZ d dl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 d dlmZ d dlmZ  ed	       G d
 de
             Zy)    )annotationsN)experimental_class)StudyDirection)BaseErrorEvaluator)BaseImprovementEvaluator)FrozenTrial)
TrialStatez4.0.0c                  F    e Zd ZdZ	 	 	 d	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZy)MedianErrorEvaluatora:  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                    |dk  rt        d      |dk  rt        d      |dk  st        j                  |      st        d      || _        || _        || _        || _        d | _        y )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        [/var/www/html/planif/env/lib/python3.12/site-packages/optuna/terminator/median_erroreval.py__init__zMedianErrorEvaluator.__init__(   sr     AYZZq WXXc!_)E`aa-I*-!1 /(,    c           	        | j                   | j                   S |D cg c]"  }|j                  t        j                  k(  s!|$ }}t	        |      | j
                  | j                  z   k  rt        j                  j                   S |j                  d        g }t        d| j                  dz         D ]G  }|j                  | j                  j                  || j
                  | j
                  |z    |             I |j                          |t	        |      dz     | _         | j                   J t        t        j                  j                  | j                   | j                   z        | _         | j                   S c c}w )Nc                    | j                   S )N)number)trials    r   <lambda>z/MedianErrorEvaluator.evaluate.<locals>.<lambda>J   s
    ell r   )key      )r   stater	   COMPLETElenr   r   sys
float_infominsortrangeappendr   evaluatemaxr   )r   trialsstudy_directionr    criteriais         r   r.   zMedianErrorEvaluator.evaluate<   sI    ??&??"%+REu{{j>Q>Q/Q%RRv;$..1G1GGH### 	23q$00145 	AOO22;;4//$2F2F2JK_	 	"3x=A#56***cnn00$//DDYDY2YZ# Ss   "E> E>N)
      g{Gz?)
r   r   r   intr   r6   r   floatreturnNone)r0   zlist[FrozenTrial]r1   r   r8   r7   )__name__
__module____qualname____doc__r   r.    r   r   r   r      sh    4 ! "!%-&>- - 	-
 - 
-(! ( 
	r   r   )
__future__r   r(   numpyr   optuna._experimentalr   optuna.studyr   optuna.terminator.errorevalr   'optuna.terminator.improvement.evaluatorr   optuna.trialr   optuna.trial._stater	   r   r>   r   r   <module>rG      sD    " 
  3 ' : L $ * GF- F Fr   