
    -jiI                    
   S SK Jr  S SKJr  S SKJr  S SKJr  S SKrS SKrS SK	J
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  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S SK J!r!  S SK J"r"  S SK J#r#  S SK J$r$  S SK%J&r&  S SK'J(r(  S SK)J*r*  S SK)J+r+  S SK,J-r-  S SK.J/r/  S SK.J0r0  S SK1J2r2  S SK1J3r3  S SK4J5r5  S S K6J7r7  S S!K6J8r8  \(       a  S S"K9J:r:  S#r;\" \<5      r=S$r>S%r?S8S& jr@S8S' jrAS9S( jrB " S) S*\$5      rCS:S+ jrD          S;S, jrE        S<S- jrF        S<S. jrG        S<S/ jrHS=S0 jrI        S<S1 jrJS>S2 jrK      S?S3 jrL        S@S4 jrM\" S5S69          SAS7 j5       rNg)B    )annotations)Callable)Sequence)	lru_cacheN)Any)cast)TYPE_CHECKING)_deprecated)convert_positional_args)warn_experimental_argument)compute_hypervolume)_solve_hssp)optuna_warn)BaseDistribution)CategoricalChoiceType)
get_logger)_CONSTRAINTS_KEY)&_INDEPENDENT_SAMPLING_WARNING_TEMPLATE) _process_constraints_after_trial)BaseSampler)LazyRandomState)RandomSampler)_ParzenEstimator)_ParzenEstimatorParameters)IntersectionSearchSpace)_GroupDecomposedSearchSpace)_SearchSpaceGroup)_fast_non_domination_rank)_is_pareto_front)StudyDirection)FrozenTrial)
TrialState)Studyg-q=ztpe:relative_paramsi  c                H    [        [        R                  " SU -  5      S5      $ )Ng?   minmathceilxs    V/home/james-whalen/.local/lib/python3.13/site-packages/optuna/samplers/_tpe/sampler.pydefault_gammar-   5   s    tyyq!2&&    c                N    [        [        R                  " SU S-  -  5      S5      $ )Ng      ?g      ?r%   r&   r*   s    r,   hyperopt_default_gammar0   9   s     tyy3',,r.   c                   U S:X  a  [         R                  " / 5      $ U S:  a  [         R                  " U 5      $ [         R                  " SU -  SU S-
  S9n[         R                  " S5      n[         R                  " X/SS9$ )Nr   r%         ?)numaxis)npasarrayoneslinspaceconcatenate)r+   rampflats      r,   default_weightsr=   =   sf    Avzz"~	
Rwwqz{{37CQV4wwr{~~tl33r.   c                     \ rS rSrSr\" / SQSSS9SSSS	S
S\\SS	S	SS	SSS.                               SS jj5       rSS jr	      S S jr
        S!S jr        S!S jr          S"S jrS#S jr      S$S jr          S%S jr          S&S jr        S'S jr\      S(S j5       r\S)S j5       rS*S jr          S+S jrSrg),
TPESamplerH   ah+  Sampler using TPE (Tree-structured Parzen Estimator) algorithm.

On each trial, for each parameter, TPE fits one Gaussian Mixture Model (GMM) ``l(x)`` to
the set of parameter values associated with the best objective values, and another GMM
``g(x)`` to the remaining parameter values. It chooses the parameter value ``x`` that
maximizes the ratio ``l(x)/g(x)``.

For further information about TPE algorithm, please refer to the following papers:

- `Algorithms for Hyper-Parameter Optimization
  <https://papers.nips.cc/paper/4443-algorithms-for-hyper-parameter-optimization.pdf>`__
- `Making a Science of Model Search: Hyperparameter Optimization in Hundreds of
  Dimensions for Vision Architectures <http://proceedings.mlr.press/v28/bergstra13.pdf>`__
- `Tree-Structured Parzen Estimator: Understanding Its Algorithm Components and Their Roles for
  Better Empirical Performance <https://arxiv.org/abs/2304.11127>`__

For multi-objective TPE (MOTPE), please refer to the following papers:

- `Multiobjective Tree-Structured Parzen Estimator for Computationally Expensive Optimization
  Problems <https://doi.org/10.1145/3377930.3389817>`__
- `Multiobjective Tree-Structured Parzen Estimator <https://doi.org/10.1613/jair.1.13188>`__

For the `categorical_distance_func`, please refer to the following paper:

- `Tree-Structured Parzen Estimator Can Solve Black-Box Combinatorial Optimization More
  Efficiently <https://arxiv.org/abs/2507.08053>`__

Please also check our articles:

- `Significant Speed Up of Multi-Objective TPESampler in Optuna v4.0.0
  <https://medium.com/optuna/significant-speed-up-of-multi-objective-tpesampler-in-optuna-v4-0-0-2bacdcd1d99b>`__
- `Multivariate TPE Makes Optuna Even More Powerful
  <https://medium.com/optuna/multivariate-tpe-makes-optuna-even-more-powerful-63c4bfbaebe2>`__

Example:
    An example of a single-objective optimization is as follows:

    .. testcode::

        import optuna
        from optuna.samplers import TPESampler


        def objective(trial):
            x = trial.suggest_float("x", -10, 10)
            return x**2


        study = optuna.create_study(sampler=TPESampler())
        study.optimize(objective, n_trials=10)

.. note::
    :class:`~optuna.samplers.TPESampler`, which became much faster in v4.0.0, c.f. `our article
    <https://medium.com/optuna/significant-speed-up-of-multi-objective-tpesampler-in-optuna-v4-0-0-2bacdcd1d99b>`__,
    can handle multi-objective optimization with many trials as well.
    Please note that :class:`~optuna.samplers.NSGAIISampler` will be used by default for
    multi-objective optimization, so if users would like to use
    :class:`~optuna.samplers.TPESampler` for multi-objective optimization, ``sampler`` must be
    explicitly specified when study is created.

Args:
    consider_prior:
        Enhance the stability of Parzen estimator by imposing a Gaussian prior when
        :obj:`True`. The prior is only effective if the sampling distribution is
        either :class:`~optuna.distributions.FloatDistribution`,
        or :class:`~optuna.distributions.IntDistribution`.

        .. warning::
            Deprecated in v4.3.0. ``consider_prior`` argument will be removed in the future.
            The removal of this feature is currently scheduled for v6.0.0,
            but this schedule is subject to change.
            From v4.3.0 onward, ``consider_prior`` automatically falls back to ``True``.
            See https://github.com/optuna/optuna/releases/tag/v4.3.0.
    prior_weight:
        The weight of the prior. This argument is used in
        :class:`~optuna.distributions.FloatDistribution`,
        :class:`~optuna.distributions.IntDistribution`, and
        :class:`~optuna.distributions.CategoricalDistribution`.
    consider_magic_clip:
        Enable a heuristic to limit the smallest variances of Gaussians used in
        the Parzen estimator.
    consider_endpoints:
        Take endpoints of domains into account when calculating variances of Gaussians
        in Parzen estimator. See the original paper for details on the heuristics
        to calculate the variances.
    n_startup_trials:
        The random sampling is used instead of the TPE algorithm until the given number
        of trials finish in the same study.
    n_ei_candidates:
        Number of candidate samples used to calculate the expected improvement.
    gamma:
        A function that takes the number of finished trials and returns the number
        of trials to form a density function for samples with low grains.
        See the original paper for more details.
    weights:
        A function that takes the number of finished trials and returns a weight for them.
        See `Making a Science of Model Search: Hyperparameter Optimization in Hundreds of
        Dimensions for Vision Architectures
        <http://proceedings.mlr.press/v28/bergstra13.pdf>`__ for more details.

        .. note::
            In the multi-objective case, this argument is only used to compute the weights of
            bad trials, i.e., trials to construct `g(x)` in the `paper
            <https://papers.nips.cc/paper/4443-algorithms-for-hyper-parameter-optimization.pdf>`__
            ). The weights of good trials, i.e., trials to construct `l(x)`, are computed by a
            rule based on the hypervolume contribution proposed in the `paper of MOTPE
            <https://doi.org/10.1613/jair.1.13188>`__.
    seed:
        Seed for random number generator.
    multivariate:
        If this is :obj:`True`, the multivariate TPE is used when suggesting parameters.
        The multivariate TPE is reported to outperform the independent TPE. See `BOHB: Robust
        and Efficient Hyperparameter Optimization at Scale
        <http://proceedings.mlr.press/v80/falkner18a.html>`__ and `our article
        <https://medium.com/optuna/multivariate-tpe-makes-optuna-even-more-powerful-63c4bfbaebe2>`__
        for more details.

        .. note::
            Added in v2.2.0 as an experimental feature. The interface may change in newer
            versions without prior notice. See
            https://github.com/optuna/optuna/releases/tag/v2.2.0.
    group:
        If this and ``multivariate`` are :obj:`True`, the multivariate TPE with the group
        decomposed search space is used when suggesting parameters.
        The sampling algorithm decomposes the search space based on past trials and samples
        from the joint distribution in each decomposed subspace.
        The decomposed subspaces are a partition of the whole search space. Each subspace
        is a maximal subset of the whole search space, which satisfies the following:
        for a trial in completed trials, the intersection of the subspace and the search space
        of the trial becomes subspace itself or an empty set.
        Sampling from the joint distribution on the subspace is realized by multivariate TPE.
        If ``group`` is :obj:`True`, ``multivariate`` must be :obj:`True` as well.

        .. note::
            Added in v2.8.0 as an experimental feature. The interface may change in newer
            versions without prior notice. See
            https://github.com/optuna/optuna/releases/tag/v2.8.0.

        Example:

        .. testcode::

            import optuna


            def objective(trial):
                x = trial.suggest_categorical("x", ["A", "B"])
                if x == "A":
                    return trial.suggest_float("y", -10, 10)
                else:
                    return trial.suggest_int("z", -10, 10)


            sampler = optuna.samplers.TPESampler(multivariate=True, group=True)
            study = optuna.create_study(sampler=sampler)
            study.optimize(objective, n_trials=10)
    warn_independent_sampling:
        If this is :obj:`True` and ``multivariate=True``, a warning message is emitted when
        the value of a parameter is sampled by using an independent sampler.
        If ``multivariate=False``, this flag has no effect.
    constant_liar:
        If :obj:`True`, penalize running trials to avoid suggesting parameter configurations
        nearby.

        .. note::
            Abnormally terminated trials often leave behind a record with a state of
            ``RUNNING`` in the storage.
            Such "zombie" trial parameters will be avoided by the constant liar algorithm
            during subsequent sampling.
            When using an :class:`~optuna.storages.RDBStorage`, it is possible to enable the
            ``heartbeat_interval`` to change the records for abnormally terminated trials to
            ``FAIL``.

        .. note::
            It is recommended to set this value to :obj:`True` during distributed
            optimization to avoid having multiple workers evaluating similar parameter
            configurations. In particular, if each objective function evaluation is costly
            and the durations of the running states are significant, and/or the number of
            workers is high.

        .. note::
            Added in v2.8.0 as an experimental feature. The interface may change in newer
            versions without prior notice. See
            https://github.com/optuna/optuna/releases/tag/v2.8.0.
    constraints_func:
        An optional function that computes the objective constraints. It must take a
        :class:`~optuna.trial.FrozenTrial` and return the constraints. The return value must
        be a sequence of :obj:`float` s. A value strictly larger than 0 means that a
        constraints is violated. A value equal to or smaller than 0 is considered feasible.
        If ``constraints_func`` returns more than one value for a trial, that trial is
        considered feasible if and only if all values are equal to 0 or smaller.

        The ``constraints_func`` will be evaluated after each successful trial.
        The function won't be called when trials fail or they are pruned, but this behavior is
        subject to change in the future releases.

        .. note::
            Added in v3.0.0 as an experimental feature. The interface may change in newer
            versions without prior notice.
            See https://github.com/optuna/optuna/releases/tag/v3.0.0.
    categorical_distance_func:
        A dictionary of distance functions for categorical parameters. The key is the name of
        the categorical parameter and the value is a distance function that takes two
        :class:`~optuna.distributions.CategoricalChoiceType` s and returns a :obj:`float`
        value. The distance function must return a non-negative value.

        While categorical choices are handled equally by default, this option allows users to
        specify prior knowledge on the structure of categorical parameters. When specified,
        categorical choices closer to current best choices are more likely to be sampled.

        .. note::
            Added in v3.4.0 as an experimental feature. The interface may change in newer
            versions without prior notice.
            See https://github.com/optuna/optuna/releases/tag/v3.4.0.
)
selfconsider_priorprior_weightconsider_magic_clipconsider_endpointsn_startup_trialsn_ei_candidatesgammaweightsseedz4.4.06.0.0)previous_positional_arg_namesdeprecated_versionremoved_versionTr2   F
      N)rB   rC   rD   rE   rF   rG   rH   rI   rJ   multivariategroupwarn_independent_samplingconstant_liarconstraints_funccategorical_distance_funcc          	        U(       d2  [         R                  R                  SSSS9n[        U S3[        5        [        UUUUU
U=(       d    0 S9U l        XPl        X`l        Xpl	        Xl
        [        U	5      U l        [        U	S9U l        Xl        Xl        S U l        S U l        ['        SS	9U l        Xl        Xl        [.        U l        U
(       a  [3        S
5        U(       a-  U
(       d  [5        S5      e[3        S5        [7        S5      U l        U(       a  [3        S5        Ub  [3        S5        Ub  [3        S5        g g )Nz`consider_prior`z4.3.0rK   )named_verr_verzI From v4.3.0 onward, `consider_prior` automatically falls back to `True`.)rC   rD   rE   rI   rQ   rV   )rJ   T)include_prunedrQ   zF``group`` option can only be enabled when ``multivariate`` is enabled.rR   rT   rU   rV   )r
   _DEPRECATION_WARNING_TEMPLATEformatr   FutureWarningr   _parzen_estimator_parameters_n_startup_trials_n_ei_candidates_gamma_warn_independent_samplingr   _rngr   _random_sampler_multivariate_group_group_decomposed_search_space_search_space_groupr   _search_space_constant_liar_constraints_funcr   _parzen_estimator_clsr   
ValueErrorr   )rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rQ   rR   rS   rT   rU   rV   msgs                    r,   __init__TPESampler.__init__!  s>   J ;;BB'wg C C %`a
 -G% 31%&?&E2-
) "2 /*C'#D)	,$7)RV+=A 4DI+!1%5"&~6 \  'w/2Md2SD/&7'&'9:$0&'BC 1r.   c                    U R                   R                  R                  5         U R                  R	                  5         g N)rd   rngrJ   re   
reseed_rng)rA   s    r,   ru   TPESampler.reseed_rng}  s(    		'')r.   c                J   U R                   (       d  0 $ 0 nU R                   =(       d    U R                  (       + nU R                  (       a  U R                  c   eU R                  R	                  X5      U l        U R
                  R                   H@  n[        UR                  5       5       H   u  pgUR                  5       (       a  M  XsU'   M"     MB     U$ U R                  R	                  X5      R                  5        H   u  pgUR                  5       (       a  M  XsU'   M"     U$ rs   )rf   rk   rg   rh   	calculateri   search_spacessorteditemssinglerj   )rA   studytrialsearch_spaceuse_trial_cache	sub_spacerX   distributions           r,   infer_relative_search_space&TPESampler.infer_relative_search_space  s    !!I46,,GD4G4G0G;;66BBB'+'J'J'T'T(D$ "55CC	*01B*C&D#**,, )5& +D D  "&"4"4">">u"V"\"\"^D""$$!- #_
 r.   c           	        U R                   (       a  U R                  c   e0 nU R                  R                   Hc  n0 n[        UR	                  5       5       H   u  pgUR                  5       (       a  M  XsU'   M"     UR                  U R                  XU5      5        Me     OU R                  XU5      nU0 :w  a  U R                  (       aw  [        R                  " U5      n[        S[        U5      [        5       HC  n	UR                  R                  UR                   ["         SU	[        -   3XU	[        -    5        ME     U$ )Nr   :)rg   ri   ry   rz   r{   r|   update_sample_relativerk   jsondumpsrangelen_SYSTEM_ATTR_MAX_LENGTH_storageset_trial_system_attr	_trial_id_RELATIVE_PARAMS_KEY)
rA   r}   r~   r   paramsr   rX   r   
params_stris
             r,   sample_relativeTPESampler.sample_relative  s    ;;++777F!55CC	!*01B*C&D'..00-9T* +D d33E,OP D **5FFR<D//F+J1c*o/FG44OO+,Aa3J.J-KL1'>#>? H r.   c                    U0 :X  a  0 $ [         R                  [         R                  4nUR                  SUSS9n[	        U5      U R
                  :  a  0 $ U R                  XUSS9$ )NFTdeepcopystates	use_cacher   )r"   COMPLETEPRUNED_get_trialsr   r`   _sample)rA   r}   r~   r   r   trialss         r,   r   TPESampler._sample_relative  sj     2I%%z'8'89""E&D"Qv;///I||E,|MMr.   c           
     d  ^ [         R                  [         R                  4nUR                  SUSS9n[	        U5      U R
                  :  a  U R                  R                  XTU5      $ U R                  (       a  U R                  (       a  [        U4S jU 5       5      (       ag  [        R                  [        R                  " TUR                  U R                  R                   R"                  U R                   R"                  SS95        TU0nU R%                  XXpR&                  (       + S9T   $ )NFTr   c              3  B   >#    U  H  nTUR                   ;   v   M     g 7frs   )r   ).0r~   
param_names     r,   	<genexpr>0TPESampler.sample_independent.<locals>.<genexpr>  s     B6%:-6s   z=dynamic search space is not supported for `multivariate=True`)r   trial_numberindependent_sampler_namesampler_namefallback_reasonr   )r"   r   r   r   r   r`   re   sample_independentrc   rf   any_loggerwarningr   r]   number	__class____name__r   rk   )rA   r}   r~   r   param_distributionr   r   r   s      `    r,   r   TPESampler.sample_independent  s    %%z'8'89""E&D"Q v;///''::j*<  **t/A/AB6BBB:AA#-%*\\151E1E1O1O1X1X%)^^%<%<[
 #$67||E,L_L_H_|`
 	
r.   c                   UR                   R                  5       (       d  U R                  (       d  UR                  $ / nSnUR                  R                  [         SU 35      =n(       aC  UR                  U5        US-  nUR                  R                  [         SU 35      =n(       a  MC  [        U5      S:X  a  UR                  $ [        R                  " SR                  U5      5      nUR                  UR                  5        U$ )Nr   r       )stateis_finishedrf   r   system_attrsgetr   appendr   r   loadsjoinr   )rA   r~   params_strsr   params_str_ir   s         r,   _get_paramsTPESampler._get_params  s    ;;""$$D,>,> <<#00448L7MQqc5RSSlS|,FA $00448L7MQqc5RSSlS {q <<BGGK01ell#r.   c                   U Vs0 s H  o3/ _M     nnU Hw  nU R                  U5      nUR                  5       UR                  5       ::  d  M8  UR                  5        H+  u  p7Xc   nXC   R                  UR	                  U5      5        M-     My     UR                  5        V	V
s0 s H  u  pU	[
        R                  " U
5      _M     sn
n	$ s  snf s  sn
n	f rs   )r   keysr{   r   to_internal_reprr6   r7   )rA   r   r   r   valuesr~   r   r   paramkvs              r,   _get_internal_reprTPESampler._get_internal_repr  s     LX)X<Zb.<)XE%%e,F  "fkkm30<0B0B0D,J".E&--l.K.KE.RS 1E  .4\\^<^TQ2::a= ^<< *Y =s   C
##Cc                <   U R                   (       a0  [        R                  [        R                  [        R                  /nO [        R                  [        R                  /nUR                  SXTS9nU R                   (       a-  U Vs/ s H   orR                  UR                  :w  d  M  UPM"     nn[        S U 5       5      n[        UUU R                  U5      U R                  S L5      u  pU R                  XU	SS9nU R                  XU
SS9nUR                  U R                  R                  U R                  5      nU R!                  XU5      n["        R%                  X5      nUR'                  5        H  u  nnUR)                  UU   5      UU'   M     U$ s  snf )NFr   c              3  Z   #    U  H!  oR                   [        R                  :g  v   M#     g 7frs   )r   r"   RUNNING)r   r~   s     r,   r   %TPESampler._sample.<locals>.<genexpr>  s     Fvez111vs   )+T)handle_below)rk   r"   r   r   r   r   r   sum_split_trialsrb   rl   _build_parzen_estimatorsamplerd   rt   ra   _compute_acquisition_funcr?   _comparer{   to_external_repr)rA   r}   r~   r   r   r   r   tnbelow_trialsabove_trials	mpe_below	mpe_abovesamples_belowacq_func_valsretr   dists                     r,   r   TPESampler._sample	  s|     )):+<+<j>P>PQF )):+<+<=F""E&"\!'DA<<188+CaFD FvFF%2KKN""$.	&
" 00D 1 
	 00E 1 
	 "((8M8MN66}QZ[!!-? , 2 2 4J"33C
ODC
O !5 
3 Es   F(Fc                $   U R                  X25      nU(       a  UR                  5       (       a  U Vs/ s H2  obR                  5       U R                  U5      R                  5       :*  PM4     nn[	        XU R
                  5      U   n[        R                  " U5      R                  5       (       d   eU R                  XRU R                  U5      n	OU R                  XRU R                  5      n	[        U	[        5      (       d  [        S5      eU	$ s  snf )Nz5_parzen_estimator_cls must override _ParzenEstimator.)r   _is_multi_objectiver   r   ,_calculate_weights_below_for_multi_objectiverl   r6   isfiniteallrm   r_   
isinstancer   RuntimeError)
rA   r}   r   r   r   observationsr~   param_mask_belowweights_belowmpes
             r,   r   "TPESampler._build_parzen_estimator3  s    ..vDE5577SY SY%!!#t'7'7'>'C'C'EESY    It55 M ;;}-113333,,D,M,M}C ,,D,M,MC #/00VWW
% s   9Dc                R    UR                  U5      nUR                  U5      nXE-
  nU$ rs   )log_pdf)rA   samplesr   r   log_likelihoods_belowlog_likelihoods_abover   s          r,   r   $TPESampler._compute_acquisition_funcP  s4     !* 1 1' : ) 1 1' :-Er.   c                ~   [        [        UR                  5       5      5      R                  nUS:X  a  [	        SU S35      eX2R                  :w  a  [	        SU SUR                   S35      e[
        R                  " U5      nUR                  5        VVs0 s H  u  pVXVU   R                  5       _M     snn$ s  snnf )Nr   z0The size of `samples` must be positive, but got .zwThe sizes of `samples` and `acquisition_func_vals` must be same, but got (samples.size, acquisition_func_vals.size) = (z, z).)	nextiterr   sizern   r6   argmaxr{   item)clsr   acquisition_func_valssample_sizebest_idxr   r   s          r,   r   TPESampler._compare[  s     4 01277!OP[}\]^__444=#8#=#=">bB  992329--/B/$!X;##%%/BBBs   B9c            	     (    SSSSSS[         [        S.$ )a  Return the the default parameters of hyperopt (v0.1.2).

:class:`~optuna.samplers.TPESampler` can be instantiated with the parameters returned
by this method.

Example:

    Create a :class:`~optuna.samplers.TPESampler` instance with the default
    parameters of `hyperopt <https://github.com/hyperopt/hyperopt/tree/0.1.2>`__.

    .. testcode::

        import optuna
        from optuna.samplers import TPESampler


        def objective(trial):
            x = trial.suggest_float("x", -10, 10)
            return x**2


        sampler = TPESampler(**TPESampler.hyperopt_parameters())
        study = optuna.create_study(sampler=sampler)
        study.optimize(objective, n_trials=10)

Returns:
    A dictionary containing the default parameters of hyperopt.

Tr2   F   rP   )rB   rC   rD   rE   rF   rG   rH   rI   )r0   r=    r.   r,   hyperopt_parametersTPESampler.hyperopt_parametersm  s'    B ##'"' "!+&	
 		
r.   c                :    U R                   R                  X5        g rs   )re   before_trial)rA   r}   r~   s      r,   r  TPESampler.before_trial  s    ))%7r.   c                    U[         R                  [         R                  [         R                  4;   d   eU R                  b  [        U R                  XU5        U R                  R                  XX45        g rs   )r"   r   FAILr   rl   r   re   after_trial)rA   r}   r~   r   r   s        r,   r  TPESampler.after_trial  s^     ,,jooz?P?PQQQQ!!-,T-C-CUSXY((uEr.   )rk   rl   rb   rg   rh   rf   ra   r`   rm   r_   re   rd   rj   ri   rc   ) rB   boolrC   floatrD   r  rE   r  rF   intrG   r  rH   zCallable[[int], int]rI   zCallable[[int], np.ndarray]rJ   z
int | NonerQ   r  rR   r  rS   r  rT   r  rU   /Callable[[FrozenTrial], Sequence[float]] | NonerV   zQdict[str, Callable[[CategoricalChoiceType, CategoricalChoiceType], float]] | NonereturnNone)r  r  )r}   r#   r~   r!   r  dict[str, BaseDistribution])r}   r#   r~   r!   r   r  r  dict[str, Any])
r}   r#   r~   r!   r   strr   r   r  r   )r~   r!   r  r  )r   list[FrozenTrial]r   r  r  dict[str, np.ndarray])
r}   r#   r~   r!   r   r  r   r  r  r  )
r}   r#   r   r  r   r  r   r  r  r   )r   r  r   r   r   r   r  
np.ndarray)r   r  r   r  r  zdict[str, int | float])r  r  )r}   r#   r~   r!   r  r  )
r}   r#   r~   r!   r   r"   r   zSequence[float] | Noner  r  )r   
__module____qualname____firstlineno____doc__r   r-   r=   rp   ru   r   r   r   r   r   r   r   r   r   classmethodr   staticmethodr  r  r  __static_attributes__r  r.   r,   r?   r?   H   s   Vp '
 #&  $!$(#( "!&3/>"*.#LP 'JD JD 	JD
 "JD !JD JD JD $JD -JD JD JD JD $(JD JD  J!JD$ ^%JD( 
)JD! JDX*#.	$:#.>Y	6NN#.N>YN	N"
"
 "
 	"

 -"
 
"
H$
='
=7R
=	
=(( ( 2	(
 ( 
(T 2 "	
  
:	&	 $	 $		
 
	 C+CDNC	C C" (
 (
T8
F
F 
F 	
F
 '
F 

Fr.   r?   c                    [         R                  " U SS9n[         R                  " SU-  SU-  5      n[        X"S:H  '   U$ )Nr   r4   g?g?)r6   maxmaximumEPS)	loss_valsworst_pointreference_points      r,   _get_reference_pointr(    s?    &&+Kjj{!2C+4EFO,/Oq()r.   c                   / n/ n/ n/ nU H  nUR                   [        R                  :X  a  UR                  U5        M4  U(       a"  [	        U5      S:  a  UR                  U5        M]  UR                   [        R
                  :X  a  UR                  U5        M  UR                   [        R                  :X  a  UR                  U5        M   e   [        X@U5      u  p[        SU[        U	5      -
  5      n[        XPU5      u  p[        SU[        U5      -
  5      n[        Xr5      u  pX-   U-   nX-   U-   U-   nUR                  S S9  UR                  S S9  UU4$ )Nr   c                    U R                   $ rs   r   r~   s    r,   <lambda>_split_trials.<locals>.<lambda>      r.   keyc                    U R                   $ rs   r+  r,  s    r,   r-  r.    r/  r.   )r   r"   r   r   _get_infeasible_trial_scorer   r   _split_complete_trialsr"  r   _split_pruned_trials_split_infeasible_trialssort)r}   r   n_belowconstraints_enabledcomplete_trialspruned_trialsrunning_trialsinfeasible_trialsr~   below_completeabove_completebelow_prunedabove_prunedbelow_infeasibleabove_infeasibler   r   s                    r,   r   r     sT    OMN;;*,,, !!%( %@%G!%K$$U+[[J///""5)[[J---  '5  &<OT[%\"N!Ws>223G!5mG!TL!Ws<001G)ABS)]&!03CCL!03CCnTL4545%%r.   c                    [        U[        U 5      5      n[        UR                  5      S::  a  [        XU5      $ [	        XU5      $ )Nr   )r'   r   
directions'_split_complete_trials_single_objective&_split_complete_trials_multi_objective)r   r}   r8  s      r,   r4  r4    sA     '3v;'G
5!6vgNN5fWMMr.   c                    UR                   [        R                  :X  a  [        U S S9nO[        U S SS9nUS U X2S  4$ )Nc                6    [        [        U R                  5      $ rs   r   r  valuer,  s    r,   r-  9_split_complete_trials_single_objective.<locals>.<lambda>      eU[[9Qr.   r0  c                6    [        [        U R                  5      $ rs   rJ  r,  s    r,   r-  rL    rM  r.   T)r1  reverse)	directionr    MINIMIZErz   r   r}   r8  sorted_trialss       r,   rF  rF    sJ     .111v+QRv+Q[_`'"M($;;;r.   c           
        US:X  a  / [        U 5      4$ U[        U 5      :X  a  [        U 5      / 4$ SUs=:  a  [        U 5      :  d   e   e[        R                  " U  Vs/ s H  o3R                  PM     sn5      nXAR
                   Vs/ s H  oU[        R                  :X  a  SOSPM     sn-  n[        XBS9n[        R                  " USS9u  px[        [        R                  " U[        R                  " U5      U:*     SS95      n	[        US U	S	-    [        R                  " U	S	-   5      :H  5      (       d   e[        R                  " [        U 5      5      n
XU	:*     nUR                  U:  a  XyS	-      U	S	-   :X  d   eXiS	-   :H  nXL   nX+R                  -
  n[!        [#        UR%                  5       5      [#        X   5      U[#        ['        U5      5      5      n[        R(                  " X5      n[+        [-        [         UR/                  5       5      5      n[1        [        U 5      5       Vs/ s H  nUU;   d  M  U U   PM     nn[1        [        U 5      5       Vs/ s H  nUU;  d  M  U U   PM     nnUU4$ s  snf s  snf s  snf s  snf )
Nr         r2   )r8  T)return_counts)initialr   )listr   r6   arrayr   rE  r    MAXIMIZEr   uniquer  r"  cumsumr   aranger   _solve_hssp_with_cachetupleravelr(  r   setr   tolistr   )r   r}   r8  r~   lvalsdnondomination_ranksranksrank_countslast_rank_before_tiebreakindicesindices_belowneed_tiebreakrank_i_lvalssubset_sizeselected_indicesbelow_indices_setr   r   r   s                       r,   rG  rG    sd    !|4<	CK	F|Rw$V$$$$$HH7ull78E	EUEUVEU>222d;EUVVE3EK#6dKE #BFF5;1G71R+S]_$` au40145C\_`C`9aabbbbiiF$G3LLMMG#237PST7TTTT+1/LL+ 2 221,$$&''()&|45	
 		-BD}';';'=>?',S['9T'9!QBS=SIF1I'9LT',S['9X'9!QFW=WIF1I'9LX%%3 8V, UXs$   I9	!I>-
J;	J
J*	Jc                $   [        U R                  5      S:  aw  [        U R                  R                  5       5      u  p#[        R
                  " U5      (       a  U* [        S5      4$ UR                  [        R                  :X  a  U* U4$ U* U* 4$ g)Nr   inf)r   g        )
r   intermediate_valuesr"  r{   r(   isnanr  rP  r    rQ  )r~   r}   stepintermediate_values       r,   _get_pruned_trial_scorerw    s    
5$$%)#&u'@'@'F'F'H#I ::())5%,&&__ 7 775,,,5----r.   c                \   ^ [        U[        U 5      5      n[        U U4S jS9nUS U X2S  4$ )Nc                   > [        U T5      $ rs   )rw  )r~   r}   s    r,   r-  &_split_pruned_trials.<locals>.<lambda>   s    5LUTY5Zr.   r0  )r'   r   rz   rR  s    `  r,   r5  r5    s9     '3v;'G6'Z[M'"M($;;;r.   c                    U R                   R                  [        5      nUc$  [        SU R                   S35        [        S5      $ [        S U 5       5      $ )NzTrial z[ does not have constraint values. It will be treated as a lower priority than other trials.rr  c              3  4   #    U  H  oS :  d  M
  Uv   M     g7fr   Nr  )r   r   s     r,   r   ._get_infeasible_trial_score.<locals>.<genexpr>.  s     2jE11js   		)r   r   r   r   r   r  r   )r~   
constraints     r,   r3  r3  $  s]    ##''(89JU\\N #I I	
 U| 2j222r.   c                Z    [        U[        U 5      5      n[        U [        S9nUS U X!S  4$ )Nr0  )r'   r   rz   r3  )r   r8  rS  s      r,   r6  r6  1  s8     '3v;'G6'BCM'"M($;;;r.   c                  ^ SU4S jjn[         R                  " U Vs/ s H
  oC" U5      PM     sn5      n[         R                  " US[        5      n[         R                  " U5      nUS::  a  U$ [         R                  " U Vs/ s H  oDR
                  PM     sn5      U   nXR                   V	s/ s H  o[        R                  :X  a  SOSPM     sn	-  n[        U5      n
[        USS9nX   n[        XSS9n[        R                  " U5      (       a  U$ [         R                  " UR                  S	   [         S
9) n[         R"                  " U[$        S
9n['        U R                  5      S::  a%  U Vs/ s H  nU[        UU   U
SS9-
  PM     snX'   O~[         R(                  " X-
  SS9X'   [         R*                  " XS S 2[         R,                  4   5      nX==   [/        U5       VVs/ s H  u  nn[        UUU4   U
5      PM     snn-  ss'   [         R*                  " U[1        [         R0                  " U5      [        5      -  [        5      Xe'   U$ s  snf s  snf s  sn	f s  snf s  snnf )Nc                J   > TS L =(       d    [        S T" U 5       5       5      $ )Nc              3  *   #    U  H	  oS :*  v   M     g7fr}  r  )r   cs     r,   r   R_calculate_weights_below_for_multi_objective.<locals>._feasible.<locals>.<genexpr>?  s     .W?V!Av?Vs   )r   )r~   rU   s    r,   	_feasible?_calculate_weights_below_for_multi_objective.<locals>._feasible>  s%    4'W3.W?OPU?V.W+WWr.   r2   r   rU  F)assume_unique_lexsortedT)assume_paretor   )dtype   rW  r4   )r~   r!   r  r  )r6   r7   wherer$  count_nonzeror   rE  r    r[  r(  r   r   r(   isinfeyeshaper  zerosr  r   prodr#  newaxis	enumerater"  )r}   r   rU   r  r   is_feasibler   n_below_feasiblerd  re  	ref_pointon_frontpareto_solshvloo_matcontribsloolimited_solsr   s     `                r,   r   r   9  s$   
X **LALqilLABKHH[#s3M''41JJ,7,Q,78EE	EUEUVEU>222d;EUVVE$U+IuEH/K	[4	HBzz"~~ vvk''*$77Gxx(6H
5! 
 $[%5yPTUU

  WWY%<2Fzz+1bjj=/IJOXY`Oa
OaVQQV 4i@Oa
 	
 "$Hs266(;KS7Q,QSV!WM? B 8V

s   II1!I!4I&,I+r   )maxsizec                    [        U5      [        U5      4n[        R                  " X5      n[        R                  " U5      n[        R                  " U5      n[	        XVX'5      $ rs   )r   r6   reshaperZ  r   )rank_i_lvals_tuplerank_i_indices_tuplern  ref_point_tuplelvals_shaperm  rank_i_indicesr  s           r,   r_  r_  c  sR     +,c/.BCK::0>LXX23N)I|[LLr.   )r+   r  r  r  )r+   r  r  r  )r%  r  r  r  )
r}   r#   r   r  r8  r  r9  r  r  +tuple[list[FrozenTrial], list[FrozenTrial]])r   Sequence[FrozenTrial]r}   r#   r8  r  r  r  )r~   r!   r}   r#   r  ztuple[float, float])r~   r!   r  r  )r   r  r8  r  r  r  )r}   r#   r   r  rU   r  r  r  )
r  tuple[float, ...]r  ztuple[int, ...]rn  r  r  r  r  r  )O
__future__r   collections.abcr   r   	functoolsr   r   r(   typingr   r   r	   numpyr6   optunar
   optuna._convert_positional_argsr   optuna._experimentalr   optuna._hypervolumer   optuna._hypervolume.hsspr   optuna._warningsr   optuna.distributionsr   r   optuna.loggingr   optuna.samplers._baser   r   r   r   "optuna.samplers._lazy_random_stater   optuna.samplers._randomr   %optuna.samplers._tpe.parzen_estimatorr   r   optuna.search_spacer   $optuna.search_space.group_decomposedr   r   optuna.study._multi_objectiver   r   optuna.study._study_directionr    optuna.trialr!   r"   optuna.studyr#   r$  r   r   r   r   r-   r0   r=   r?   r(  r   r4  rF  rG  rw  r5  r3  r6  r   r_  r  r.   r,   <module>r     s    " $ $          C ; 3 0 ( 1 6 % 2 H B - > 1 B L 7 L B C : 8 $ # " 
X
,  '-4]	F ]	F@$&$&+$&69$&PT$&0$&NN!N*/N:=N0N<!<*/<:=<0<"&!"&*/"&:="&0"&J
<!<*/<:=<0<
3<!<,/<0<''#' F' 	'T 1
M)
M)
M 
M '	
M
 
M 
Mr.   