o
    }oi                     @  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lmZ er:d dlmZ d dlmZ d d	lmZ ed
G dd de
ZdS )    )annotationsN)TYPE_CHECKING)experimental_class)BaseErrorEvaluator)
TrialState)StudyDirection)BaseImprovementEvaluator)FrozenTrialz4.0.0c                   @  s,   e Zd ZdZ			ddddZdddZdS )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.
    
      {Gz?paired_improvement_evaluatorr   warm_up_trialsintn_initial_trialsthreshold_ratiofloatreturnNonec                 C  s\   |dk rt d|dkrt d|dkst|st d|| _|| _|| _|| _d | _d S )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)selfr   r   r   r    r   V/home/ubuntu/.local/lib/python3.10/site-packages/optuna/terminator/median_erroreval.py__init__,   s   
zMedianErrorEvaluator.__init__trialslist[FrozenTrial]study_directionr   c              	   C  s   | j d ur| j S dd |D }t|| j| j k rtjj S |jdd d g }td| jd D ]}|	| j
|| j| j|  | q0|  |t|d  | _ | j d usYJ ttjj| j | j | _ | j S )Nc                 S  s   g | ]
}|j tjkr|qS r   )stater   COMPLETE).0trialr   r   r    
<listcomp>H   s    z1MedianErrorEvaluator.evaluate.<locals>.<listcomp>c                 S  s   | j S )N)number)r(   r   r   r    <lambda>M   s    z/MedianErrorEvaluator.evaluate.<locals>.<lambda>)key      )r   lenr   r   sys
float_infominsortrangeappendr   evaluatemaxr   )r   r"   r$   criteriair   r   r    r6   @   s&   
zMedianErrorEvaluator.evaluateN)r   r   r   )
r   r   r   r   r   r   r   r   r   r   )r"   r#   r$   r   r   r   )__name__
__module____qualname____doc__r!   r6   r   r   r   r    r
      s    r
   )
__future__r   r0   typingr   numpyr   optuna._experimentalr   optuna.terminator.errorevalr   optuna.trial._stater   optuna.studyr   'optuna.terminator.improvement.evaluatorr   optuna.trialr	   r
   r   r   r   r    <module>   s    