
    (^i1                       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 d dlmZ d dlmZ d	Z G d
 dej"                        Z ed       G d de             Z ed      dd       Z ed       G d de             Zy)    )annotationsN)cast)experimental_class)StudyDirection)FrozenTrial)Trial)
TrialStatezterminator:cv_scoresc                  B    e Zd ZdZej
                  	 	 	 	 	 	 dd       Zy)BaseErrorEvaluatorz Base class for error evaluators.c                     y N selftrialsstudy_directions      \/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/optuna/terminator/erroreval.pyevaluatezBaseErrorEvaluator.evaluate   s     	    Nr   zlist[FrozenTrial]r   r   returnfloat)__name__
__module____qualname____doc__abcabstractmethodr   r   r   r   r   r      s9    *! ( 
	 r   r   )	metaclassz3.2.0c                  $    e Zd ZdZ	 	 	 	 	 	 ddZy)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.

    c                   |D cg c]"  }|j                   t        j                  k(  s!|$ }}t        |      dkD  sJ |t        j
                  k(  rt        |d       }nt        |d       }|j                  }t        |v r
|t           }nt        d      t        |      }|dkD  sJ d       d|z  d|dz
  z  z   }|t        j                  |      z  }	t        j                  |	      }
t        |
      S c c}w )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.

        r   c                6    t        t        | j                        S r   r   r   valuets    r   <lambda>z8CrossValidationErrorEvaluator.evaluate.<locals>.<lambda>@       4qww3G r   )keyc                6    t        t        | j                        S r   r$   r&   s    r   r(   z8CrossValidationErrorEvaluator.evaluate.<locals>.<lambda>B   r)   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scaler7   stds              r   r   z&CrossValidationErrorEvaluator.evaluate)   s    & &,REu{{j>Q>Q/Q%R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
   "C4C4Nr   )r   r   r   r   r   r   r   r   r!   r!      s(    ,!, (, 
	,r   r!   c                    t        |      dk  rt        d      | j                  j                  | j                  t
        |       y)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/   r5   storageset_trial_system_attr	_trial_idr4   )r9   scoress     r   report_cross_validation_scoresrE   X   s8     6{aUVV	MM''9UW]^r   c                  ,    e Zd ZdZddZ	 	 	 	 	 	 ddZy)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.

    c                    || _         y r   	_constant)r   constants     r   __init__zStaticErrorEvaluator.__init__y   s	    !r   c                    | j                   S r   rI   r   s      r   r   zStaticErrorEvaluator.evaluate|   s    
 ~~r   N)rK   r   r   Noner   )r   r   r   r   rL   r   r   r   r   rG   rG   l   s-    	"! ( 
	r   rG   )r9   r   rD   zlist[float]r   rN   )
__future__r   r   typingr   numpyr6   optuna._experimentalr   optuna.studyr   optuna.trialr   r   optuna.trial._stater	   r4   ABCMetar   r!   rE   rG   r   r   r   <module>rW      s    " 
   3 ' $  *  6 	3;; 	 G6$6 6 6r G_ _& G-  r   