
    (^i$'                       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 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 d dlmZ d dlmZ n.d dlmZ  ed      Z ed      Z ed      Z ed      Z ed      ZdZdZd#d$dZ 	 	 	 d%	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d&dZ! ed       G d dejD                               Z# ed       G d de#             Z$ ed        G d! d"e#             Z%y)'    )annotationsN)TYPE_CHECKING)experimental_class)BaseDistribution)LazyRandomState)intersection_search_space)StudyDirection)FrozenTrial)
TrialState)acqf)gp)optim_sample)prior)search_space)_LazyImportzoptuna._gp.gpzoptuna._gp.optim_samplezoptuna._gp.acqfzoptuna._gp.priorzoptuna._gp.search_spaceg      ?   c                    dt        j                  | |dz  z  t         j                  dz  z  dz  |z        z  }|dz  }|S )N         )nplogpi)n_paramsn_trialsdeltabetas       h/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/optuna/terminator/improvement/evaluator.py	_get_betar   %   sG     rvvh1,ruuax7!;eCDDD
 	AIDK    c                n   |j                   \  }}t        |||      }	t        j                  | ||	      }
t	        |
j                  |      j	                         t        j                  |
||      d         }t        j                  | ||	      }t        j                  |j                  |            }||z
  S )a  
    # In the original paper, f(x) was intended to be minimized, but here we would like to
    # maximize f(x). Hence, the following changes happen:
    #     1. min(ucb) over top trials becomes max(lcb) over top trials, and
    #     2. min(lcb) over the search space becomes max(ucb) over the search space, and
    #     3. Regret bound becomes max(ucb) over the search space minus max(lcb) over top trials.
    )	n_samplesrng   )gprr   r   )
shaper   acqf_moduleUCBmaxeval_acqf_no_gradr   optimize_acqf_sampleLCBr   )r%   r   normalized_top_n_paramsstandarized_top_n_valuesr   optimize_n_samplesr#   r   r   r   ucb_acqfstandardized_ucb_valuelcb_acqfstandardized_lcb_values                 r   "_compute_standardized_regret_boundr4   2   s    " 166Hh Xx/DsL$7H ""#:;??A))(>PVYZ[\] 3\MHVVH$>$>?V$WX "$:::r    z3.2.0c                  6    e Zd ZdZej
                  dd       Zy)BaseImprovementEvaluatorz&Base class for improvement evaluators.c                     y N )selftrialsstudy_directions      r   evaluatez!BaseImprovementEvaluator.evaluate[   s    r    Nr;   list[FrozenTrial]r<   r	   returnfloat)__name__
__module____qualname____doc__abcabstractmethodr=   r9   r    r   r6   r6   W   s    0 r    r6   )	metaclassc                  h    e Zd ZdZeedf	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZd	dZe		 	 	 	 	 	 d
d       Z
y)RegretBoundEvaluatora  An error evaluator for upper bound on the regret with high-probability confidence.

    This evaluator evaluates the regret of current best solution, which defined as the difference
    between the objective value of the best solution and of the global optimum. To be specific,
    this evaluator calculates the upper bound on the regret based on the fact that empirical
    estimator of the objective function is bounded by lower and upper confidence bounds with
    high probability under the Gaussian process model assumption.

    Args:
        top_trials_ratio:
            A ratio of top trials to be considered when estimating the regret. Default to 0.5.
        min_n_trials:
            A minimum number of complete trials to estimate the regret. Default to 20.
        seed:
            Seed for random number generator.

    For further information about this evaluator, please refer to the following paper:

    - `Automatic Termination for Hyperparameter Optimization <https://proceedings.mlr.press/v188/makarova22a.html>`__
    Nc                    || _         || _        t        j                  | _        t        j
                  | _        d| _        t        |      | _	        y )N   )
_top_trials_ratio_min_n_trialsr   default_log_prior
_log_priorDEFAULT_MINIMUM_NOISE_VAR_minimum_noise_optimize_n_samplesr   _rng)r:   top_trials_ratiomin_n_trialsseeds       r   __init__zRegretBoundEvaluator.__init__w   sC     "2)11#==#' #D)	r    c                   t        |      t        |      k(  sJ t        |      }t        j                  t        || j                  z        | j
                  |      }t        j                  |||z
        ||z
     }||k\  }||   ||   fS r8   )lenr   clipintrM   rN   	partition)r:   normalized_paramsvaluesr   top_n	top_n_val
top_n_masks          r   
_get_top_nzRegretBoundEvaluator._get_top_n   s     $%V444()Ht'='==>@R@RT\]LLE)9:8e;KL	y(
 ,fZ.@@@r    c           	        t        |      }| j                  ||       |D cg c]"  }|j                  t        j                  k(  s!|$ }}|t
        j                  k(  rdnd}t        j                  |D cg c]  }|j                   c}      |z  }t        j                  |      }|j                  |      }	| j                  |	|      \  }
}|j                         }t        d|j!                               }||z
  |z  }t#        j$                  |
||j&                  | j(                  | j*                  dd       }t-        |||
|| j.                  j0                        }||z  S c c}w c c}w )Nr$   g|=F)XYis_categorical	log_priorminimum_noisedeterministic_objective	gpr_cache)r#   )r   _validate_inputstater   COMPLETEr	   MINIMIZEr   arrayvaluegp_search_spaceSearchSpaceget_normalized_paramsrc   meanr)   stdr   fit_kernel_paramsrh   rP   rR   r4   rT   r#   )r:   r;   r<   optuna_search_spacetcomplete_trialssignr_   r   r^   r-   top_n_valuestop_n_values_meantop_n_values_stdr.   r%   standardized_regret_bounds                    r   r=   zRegretBoundEvaluator.evaluate   s^   7?V%89&,O:;N;N0N1OO %(?(??rQO<q177<=D&223FG(>>O04@QSY0Z-(--/ul&6&6&89$03D$DHX#X ""%&'66oo--$)

 %G#$		%
! )+;;;? P =s   "E"E"5E'c                    t        |D cg c]"  }|j                  t        j                  k(  s!|$ c}      dk(  rt	        d      t        |      dk(  rt	        d| j
                   d      y c c}w )Nr   zNBecause no trial has been completed yet, the regret bound cannot be evaluated.zKThe intersection search space is empty. This condition is not supported by .)rZ   rn   r   ro   
ValueErrorrB   )clsr;   r   rz   s       r   rm   z$RegretBoundEvaluator._validate_input   sw     6DaQWW
0C0C%CDEJ`  |!]<<.#  " Es
   "A.A.)rU   rA   rV   r\   rW   z
int | Noner@   None)r^   
np.ndarrayr_   r   r@   ztuple[np.ndarray, np.ndarray]r>   )r;   r?   r   zdict[str, BaseDistribution]r@   r   )rB   rC   rD   rE   DEFAULT_TOP_TRIALS_RATIODEFAULT_MIN_N_TRIALSrX   rc   r=   classmethodrm   r9   r    r   rJ   rJ   `   s    . #;0	** * 	*
 
*A!+A5?A	&A#<J &6Q	 r    rJ   z3.4.0c                  4    e Zd ZdZdddZddZed	d       Zy)
BestValueStagnationEvaluatora)  Evaluates the stagnation period of the best value in an optimization process.

    This class is initialized with a maximum stagnation period (``max_stagnation_trials``)
    and is designed to evaluate the remaining trials before reaching this maximum period
    of allowed stagnation. If this remaining trials reach zero, the trial terminates.
    Therefore, the default error evaluator is instantiated by ``StaticErrorEvaluator(const=0)``.

    Args:
        max_stagnation_trials:
            The maximum number of trials allowed for stagnation.
    c                2    |dk  rt        d      || _        y )Nr   z;The maximum number of stagnant trials must not be negative.)r   _max_stagnation_trials)r:   max_stagnation_trialss     r   rX   z%BestValueStagnationEvaluator.__init__   s     1$Z[[&;#r    c                   | j                  |       |t        j                  k(  rdnd}|D cg c]"  }|j                  t        j
                  k(  s!|$ }}t        |      dz
  }d}t        |      D ]=  \  }}||   j                  }	|j                  }
|	J |
J |r|	|
k  r|}3|r6|	|
kD  s<|}? | j                  ||z
  z
  S c c}w )NTFr$   r   )
rm   r	   MAXIMIZErn   r   ro   rZ   	enumeraterr   r   )r:   r;   r<   is_maximize_directionrz   current_step	best_stepitrial
best_valuecurrent_values              r   r=   z%BestValueStagnationEvaluator.evaluate   s    V$)8N<S<S)SZ_#Fqww*2E2E'E!FF6{Q	!&) 	HAu	*00J!KKM))) ,,,$*}*D	+*}2L		 **lY.FGG Gs   "CCc                    t        |D cg c]"  }|j                  t        j                  k(  s!|$ c}      dk(  rt	        d      y c c}w )Nr   zMBecause no trial has been completed yet, the improvement cannot be evaluated.)rZ   rn   r   ro   r   )r   r;   rz   s      r   rm   z,BestValueStagnationEvaluator._validate_input   sD    6DaQWW
0C0C%CDEJ_  KDs
   "AAN)   )r   r\   r@   r   r>   )r;   r?   r@   r   )rB   rC   rD   rE   rX   r=   r   rm   r9   r    r   r   r      s&    
<
H&  r    r   )皙?)r   r\   r   r\   r   rA   r@   rA   )r   rL   N)r%   zgp.GPRegressorr   zgp_search_space.SearchSpacer-   r   r.   r   r   rA   r/   r\   r#   znp.random.RandomState | Noner@   rA   )&
__future__r   rF   typingr   numpyr   optuna._experimentalr   optuna.distributionsr   "optuna.samplers._lazy_random_stater   optuna.search_spacer   optuna.studyr	   optuna.trialr
   r   
optuna._gpr   r'   r   r   r   r   rs   optuna._importsr   r   r   r   r4   ABCMetar6   rJ   r   r9   r    r   <module>r      sK   " 
    3 1 > 9 ' $ # .' :+	_	%B89L/0K*+E!";<O  
$ "(,";	";-"; ("; )	";
 "; "; 
&"; ";J G   G_3 _ _D G*#; * *r    