
    -jin                    $   S SK Jr  S SKrS SKJ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
r " S S\R$                  S9r\	" S5       " S S\5      5       r\	" S5      SS j5       r\	" S5       " S S\5      5       rg)    )annotationsN)cast)TYPE_CHECKING)experimental_class)StudyDirection)
TrialState)FrozenTrial)Trialzterminator:cv_scoresc                  L    \ rS rSrSr\R                        SS j5       rSrg)BaseErrorEvaluator   z Base class for error evaluators.c                    g N selftrialsstudy_directions      U/home/james-whalen/.local/lib/python3.13/site-packages/optuna/terminator/erroreval.pyevaluateBaseErrorEvaluator.evaluate   s     	    r   Nr   zlist[FrozenTrial]r   r   returnfloat)	__name__
__module____qualname____firstlineno____doc__abcabstractmethodr   __static_attributes__r   r   r   r   r      s9    *! ( 
	 r   r   )	metaclassz3.2.0c                  .    \ rS rSrSr      SS jrSrg)CrossValidationErrorEvaluator"   a  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.

c                   U Vs/ s H$  o3R                   [        R                  :X  d  M"  UPM&     nn[        U5      S:  d   eU[        R
                  :X  a  [        US S9nO[        US S9nUR                  n[        U;   a
  U[           nO[        S5      e[        U5      nUS:  d   S5       eSU-  SUS-
  -  -   nU[        R                  " U5      -  n	[        R                  " U	5      n
[        U
5      $ s  snf )ak  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.

r   c                6    [        [        U R                  5      $ r   r   r   valuets    r   <lambda>8CrossValidationErrorEvaluator.evaluate.<locals>.<lambda>D       4qww3Gr   )keyc                6    [        [        U R                  5      $ r   r*   r,   s    r   r.   r/   F   r0   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`.)stater   COMPLETElenr   MAXIMIZEmaxminsystem_attrs_CROSS_VALIDATION_SCORES_KEY
ValueErrornpvarsqrtr   )r   r   r   trial
best_trialbest_trial_attrs	cv_scoreskscaler>   stds              r   r   &CrossValidationErrorEvaluator.evaluate-   s    & &,RVE{{j>Q>Q/Q%VR6{Qn555V)GHJV)GHJ%22'+;;()EFI.  	N1uQQQuAQU#bffY''ggclSz3 Ss
   !C9C9r   Nr   )r   r   r   r   r    r   r#   r   r   r   r&   r&   "   s(    ,!, (, 
	,r   r&   c                    [        U5      S::  a  [        S5      eU R                  R                  U R                  [
        U5        g)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.

r3   z:The length of `scores` is expected to be greater than one.N)r6   r<   storageset_trial_system_attr	_trial_idr;   )r@   scoress     r   report_cross_validation_scoresrM   \   s8     6{aUVV	MM''9UW]^r   c                  8    \ rS rSrSrSS jr      SS jrSrg)	StaticErrorEvaluatorp   a  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.

c                    Xl         g r   	_constant)r   constants     r   __init__StaticErrorEvaluator.__init__}   s    !r   c                    U R                   $ r   rR   r   s      r   r   StaticErrorEvaluator.evaluate   s    
 ~~r   rR   N)rT   r   r   Noner   )r   r   r   r   r    rU   r   r#   r   r   r   rO   rO   p   s-    	"! ( 
	r   rO   )r@   r
   rL   zlist[float]r   rY   )
__future__r   r!   typingr   r   numpyr=   optuna._experimentalr   optuna.studyr   optuna.trial._stater   optuna.trialr	   r
   r;   ABCMetar   r&   rM   rO   r   r   r   <module>rb      s    " 
     3 ' * ("  6 	3;; 	 G6$6 6 6r G_ _& G-  r   