o
    }oin                     @  s   d dl mZ d dlZd dlm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
ZG dd dejdZe	dG dd deZe	ddddZe	dG dd deZdS )    )annotationsN)cast)TYPE_CHECKING)experimental_class)StudyDirection)
TrialState)FrozenTrial)Trialzterminator:cv_scoresc                   @  s    e Zd ZdZejddd	Zd
S )BaseErrorEvaluatorz Base class for error evaluators.trialslist[FrozenTrial]study_directionr   returnfloatc                 C  s   d S N selfr   r   r   r   O/home/ubuntu/.local/lib/python3.10/site-packages/optuna/terminator/erroreval.pyevaluate   s   zBaseErrorEvaluator.evaluateNr   r   r   r   r   r   )__name__
__module____qualname____doc__abcabstractmethodr   r   r   r   r   r
      s    r
   )	metaclassz3.2.0c                   @  s   e Zd ZdZddd	Zd
S )CrossValidationErrorEvaluatora  An error evaluator for objective functions based on cross-validation.

    This evaluator evaluates the objective function's statistical error, which comes from the
    randomness of dataset. This evaluator assumes that the objective function is the average of
    the cross-validation and uses the scaled variance of the cross-validation scores in the best
    trial at the moment as the statistical error.

    r   r   r   r   r   r   c           
      C  s   dd |D }t |dksJ |tjkrt|dd d}nt|dd d}|j}t|v r1|t }ntdt |}|d	ksAJ d
d	| d	|d	   }|t	| }t
|}	t|	S )a  Evaluate the statistical error of the objective function based on cross-validation.

        Args:
            trials:
                A list of trials to consider. The best trial in ``trials`` is used to compute the
                statistical error.

            study_direction:
                The direction of the study.

        Returns:
            A float representing the statistical error of the objective function.

        c                 S  s   g | ]
}|j tjkr|qS r   )stater   COMPLETE).0trialr   r   r   
<listcomp>@   s    z:CrossValidationErrorEvaluator.evaluate.<locals>.<listcomp>r   c                 S     t t| jS r   r   r   valuetr   r   r   <lambda>D       z8CrossValidationErrorEvaluator.evaluate.<locals>.<lambda>)keyc                 S  r$   r   r%   r'   r   r   r   r)   F   r*   zCross-validation scores have not been reported. Please call `report_cross_validation_scores(trial, scores)` during a trial and pass the list of scores as `scores`.   z9Should be guaranteed by `report_cross_validation_scores`.)lenr   MAXIMIZEmaxminsystem_attrs_CROSS_VALIDATION_SCORES_KEY
ValueErrornpvarsqrtr   )
r   r   r   
best_trialbest_trial_attrs	cv_scoreskscaler5   stdr   r   r   r   -   s"   


z&CrossValidationErrorEvaluator.evaluateNr   )r   r   r   r   r   r   r   r   r   r   "   s    	r   r"   r	   scoreslist[float]r   Nonec                 C  s*   t |dkr
td| j| jt| dS )a  A function to report cross-validation scores of a trial.

    This function should be called within the objective function to report the cross-validation
    scores. The reported scores are used to evaluate the statistical error for termination
    judgement.

    Args:
        trial:
            A :class:`~optuna.trial.Trial` object to report the cross-validation scores.
        scores:
            The cross-validation scores of the trial.

    r,   z:The length of `scores` is expected to be greater than one.N)r-   r3   storageset_trial_system_attr	_trial_idr2   )r"   r=   r   r   r   report_cross_validation_scores\   s   rC   c                   @  s$   e Zd ZdZdddZdddZdS )StaticErrorEvaluatora7  An error evaluator that always returns a constant value.

    This evaluator can be used to terminate the optimization when the evaluated improvement
    potential is below the fixed threshold.

    Args:
        constant:
            A user-specified constant value to always return as an error estimate.

    constantr   r   r?   c                 C  s
   || _ d S r   	_constant)r   rE   r   r   r   __init__}   s   
zStaticErrorEvaluator.__init__r   r   r   r   c                 C  s   | j S r   rF   r   r   r   r   r      s   zStaticErrorEvaluator.evaluateN)rE   r   r   r?   r   )r   r   r   r   rH   r   r   r   r   r   rD   p   s    
rD   )r"   r	   r=   r>   r   r?   )
__future__r   r   typingr   r   numpyr4   optuna._experimentalr   optuna.studyr   optuna.trial._stater   optuna.trialr   r	   r2   ABCMetar
   r   rC   rD   r   r   r   r   <module>   s&    9