
    (^i4                       d dl mZ d dlmZ d dlmZ d dlmZ d dlZd dlZd dl	m
Z
 d dl	mZ d dl	mZ d dl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 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' 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l0m1Z1 d dl0m2Z2 d dl3m4Z4 d dl5m6Z6 d d l5m7Z7 erd d!l8m9Z9 d"Z: ee;      Z<d#Z=d$Z>d7d%Z?d7d&Z@d8d'ZA G d( d)e#      ZBd9d*ZC	 	 	 	 	 	 	 	 	 	 d:d+ZD	 	 	 	 	 	 	 	 d;d,ZE	 	 	 	 	 	 	 	 d;d-ZF	 	 	 	 	 	 	 	 d;d.ZGd<d/ZH	 	 	 	 	 	 	 	 d;d0ZId=d1ZJ	 	 	 	 	 	 d>d2ZK	 	 	 	 	 	 	 	 d?d3ZL ed45      	 	 	 	 	 	 	 	 	 	 d@d6       ZMy)A    )annotations)Callable)Sequence)	lru_cacheN)Any)cast)TYPE_CHECKING)_deprecated)convert_positional_args)warn_experimental_argument)compute_hypervolume)_solve_hssp)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                F    t        t        j                  d| z        d      S )Ng?   minmathceilxs    ]/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/optuna/samplers/_tpe/sampler.pydefault_gammar,   5   s    tyyq!2&&    c                L    t        t        j                  d| dz  z        d      S )Ng      ?g      ?r$   r%   r)   s    r+   hyperopt_default_gammar/   9   s     tyy3',,r-   c                   | dk(  rt        j                  g       S | dk  rt        j                  |       S t        j                  d| z  d| dz
        }t        j                  d      }t        j                  ||gd      S )Nr   r$         ?)numaxis)npasarrayoneslinspaceconcatenate)r*   rampflats      r+   default_weightsr<   =   sh    Avzz"~	
Rwwqz{{37CQV4wwr{~~tTl33r-   c                     e Zd ZdZ eg ddd      ddddd	d
eedddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd       ZddZ	 	 	 	 	 	 ddZ		 	 	 	 	 	 	 	 ddZ
	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 d dZd!dZ	 	 	 	 	 	 d"dZ	 	 	 	 	 	 	 	 	 	 d#dZ	 	 	 	 	 	 	 	 	 	 d$dZ	 	 	 	 	 	 	 	 d%dZe	 	 	 	 	 	 d&d       Zed'd       Zd(dZ	 	 	 	 	 	 	 	 	 	 d)dZy)*
TPESamplera0.  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_versionTr1   F
      N)r@   rA   rB   rC   rD   rE   rF   rG   rH   multivariategroupwarn_independent_samplingconstant_liarconstraints_funccategorical_distance_funcc          	     ~   |s?t         j                  j                  ddd      }t        j                  | dt
               t        |||||
|xs i       | _        || _        || _	        || _
        || _        t        |	      | _        t        |	      | _        |
| _        || _        d | _        d | _        t)        d	      | _        || _        || _        t0        | _        |
rt5        d
       |r(|
st7        d      t5        d       t9        d      | _        |rt5        d       |t5        d       |t5        d       y y )Nz`consider_prior`z4.3.0rI   )named_verr_verzI From v4.3.0 onward, `consider_prior` automatically falls back to `True`.)rA   rB   rC   rG   rO   rT   )rH   T)include_prunedrO   zF``group`` option can only be enabled when ``multivariate`` is enabled.rP   rR   rS   rT   )r
   _DEPRECATION_WARNING_TEMPLATEformatwarningswarn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   )r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rO   rP   rQ   rR   rS   rT   msgs                    r+   __init__zTPESampler.__init__!  sR   J ;;BB'wg C C MM%`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                    | j                   j                  j                          | j                  j	                          y N)rd   rngrH   re   
reseed_rng)r?   s    r+   rt   zTPESampler.reseed_rng}  s(    		'')r-   c                
   | j                   si S i }| j                   xs | j                   }| j                  r| j                  J | j                  j	                  ||      | _        | j
                  j                  D ]9  }t        |j                               D ]  \  }}|j                         r|||<    ; |S | j                  j	                  ||      j                         D ]  \  }}|j                         r|||<    |S rr   )rf   rk   rg   rh   	calculateri   search_spacessorteditemssinglerj   )r?   studytrialsearch_spaceuse_trial_cache	sub_spacerV   distributions           r+   infer_relative_search_spacez&TPESampler.infer_relative_search_space  s    !!I46,,GD4G4G0G;;66BBB'+'J'J'T'T(D$ "55CC 6	*01B*C 6&D,#**, )5L&66  "&"4"4">">uo"V"\"\"^ 	.D,""$!-L	.
 r-   c           	     ^   | j                   r| j                  J i }| j                  j                  D ]]  }i }t        |j	                               D ]  \  }}|j                         r|||<    |j                  | j                  |||             _ n| j                  |||      }|i k7  r| j                  rut        j                  |      }t        dt        |      t              D ]C  }	|j                  j                  |j                   t"         d|	t        z   ||	|	t        z           E |S )Nr   :)rg   ri   rw   rx   ry   rz   update_sample_relativerk   jsondumpsrangelen_SYSTEM_ATTR_MAX_LENGTH_storageset_trial_system_attr	_trial_id_RELATIVE_PARAMS_KEY)
r?   r{   r|   r}   paramsr   rV   r   
params_stris
             r+   sample_relativezTPESampler.sample_relative  s-    ;;++777F!55CC Q	!*01B*C :&D,'..0-9T*: d33E5,OPQ **5%FFR<D//F+J1c*o/FG 44OO+,Aa3J.J-KLq1'>#>? r-   c                    |i k(  ri S t         j                  t         j                  f}|j                  d|d      }t	        |      | j
                  k  ri S | j                  |||d      S )NFTdeepcopystates	use_cacher~   )r!   COMPLETEPRUNED_get_trialsr   r`   _sample)r?   r{   r|   r}   r   trialss         r+   r   zTPESampler._sample_relative  sl     2I%%z'8'89""E&D"Qv;///I||E5,|MMr-   c           	     P   t         j                  t         j                  f}|j                  d|d      }t	        |      | j
                  k  r| j                  j                  |||      S | j                  r| j                  r}t        fd|D              rit        j                  t        j                  |j                  | j                  j                   j"                  | j                   j"                  d             |i}| j%                  |||| j&                            S )NFTr   c              3  :   K   | ]  }|j                   v   y wrr   )r   ).0r|   
param_names     r+   	<genexpr>z0TPESampler.sample_independent.<locals>.<genexpr>  s     B%:-Bs   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   )r?   r{   r|   r   param_distributionr   r   r}   s      `    r+   r   zTPESampler.sample_independent  s
    %%z'8'89""E&D"Q v;///''::uj*<  **t/A/AB6BB:AA#-%*\\151E1E1O1O1X1X%)^^%<%<[
 #$67||E5,DL_L_H_|`
 	
r-   c                   |j                   j                         s| j                  s|j                  S g }d}|j                  j                  t         d|       x}r=|j                  |       |dz  }|j                  j                  t         d|       x}r=t        |      dk(  r|j                  S t        j                  dj                  |            }|j                  |j                         |S )Nr   r       )stateis_finishedrf   r   system_attrsgetr   appendr   r   loadsjoinr   )r?   r|   params_strsr   params_str_ir   s         r+   _get_paramsz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                   |D ci c]  }|g  }}|D ]u  }| j                  |      }|j                         |j                         k  s6|j                         D ]-  \  }}||   }||   j                  |j	                  |             / w |j                         D 	
ci c]  \  }	}
|	t        j                  |
       c}
}	S c c}w c c}
}	w rr   )r   keysry   r   to_internal_reprr5   r6   )r?   r   r}   r   valuesr|   r   r   paramkvs              r+   _get_internal_reprzTPESampler._get_internal_repr  s     LX)XZ*b.)X)X 	TE%%e,F  "fkkm30<0B0B0D T,J":.E:&--l.K.KE.RST	T .4\\^<TQ2::a= << *Y =s   
C Cc                :   | j                   r0t        j                  t        j                  t        j                  g}n t        j                  t        j                  g}|j                  d||      }| j                   r)|D cg c]  }|j                  |j                  k7  s|  }}t        d |D              }t        ||| j                  |      | j                  d u      \  }	}
| j                  |||	d      }| j                  |||
d      }|j                  | j                  j                  | j                        }| j!                  |||      }t"        j%                  ||      }|j'                         D ]  \  }}|j)                  ||         ||<    |S c c}w )NFr   c              3  V   K   | ]!  }|j                   t        j                  k7   # y wrr   )r   r!   RUNNING)r   r|   s     r+   r   z%TPESampler._sample.<locals>.<genexpr>  s     Fez111Fs   ')T)handle_below)rk   r!   r   r   r   r   r   sum_split_trialsrb   rl   _build_parzen_estimatorsamplerd   rs   ra   _compute_acquisition_funcr>   _comparery   to_external_repr)r?   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   zTPESampler._sample	  s     )):+<+<j>P>PQF )):+<+<=F""E&O"\!'DA5<<188+CaDFD FvFF%2KKN""$.	&
"l 00<D 1 
	 00<E 1 
	 "((8M8MN66}iQZ[!!-? , 2 2 4 	EJ"33C
ODC
O	E 
3 Es   F Fc                   | j                  ||      }|r|j                         r|D cg c]2  }|j                         | j                  |      j                         k  4 }}t	        ||| j
                        |   }t        j                  |      j                         sJ | j                  ||| j                  |      }	n| j                  ||| j                        }	t        |	t              st        d      |	S c c}w )Nz5_parzen_estimator_cls must override _ParzenEstimator.)r   _is_multi_objectiver   r   ,_calculate_weights_below_for_multi_objectiverl   r5   isfiniteallrm   r_   
isinstancer   RuntimeError)
r?   r{   r}   r   r   observationsr|   param_mask_belowweights_belowmpes
             r+   r   z"TPESampler._build_parzen_estimator3  s    ..v|DE557SY JO!!#t'7'7'>'C'C'EE    Ivt55 M ;;}-11333,,lD,M,M}C ,,lD,M,MC #/0VWW
% s   7C:c                T    |j                  |      }|j                  |      }||z
  }|S rr   )log_pdf)r?   samplesr   r   log_likelihoods_belowlog_likelihoods_abover   s          r+   r   z$TPESampler._compute_acquisition_funcP  s7     !* 1 1' : ) 1 1' :-0EEr-   c                x   t        t        |j                                     j                  }|dk(  rt	        d| d      ||j                  k7  rt	        d| d|j                   d      t        j                  |      }|j                         D ci c]  \  }}|||   j                          c}}S c c}}w )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   r5   argmaxry   item)clsr   acquisition_func_valssample_sizebest_idxr   r   s          r+   r   zTPESampler._compare[  s     4 01277!OP[}\]^__/444=#8#=#=">bB  992329--/B$!Q1X;##%%BBBs   B6c            	     (    ddddddt         t        d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.

        Tr1   F   rN   )r@   rA   rB   rC   rD   rE   rF   rG   )r/   r<    r-   r+   hyperopt_parameterszTPESampler.hyperopt_parametersm  s'    B ##'"' "!+&	
 		
r-   c                <    | j                   j                  ||       y rr   )re   before_trial)r?   r{   r|   s      r+   r   zTPESampler.before_trial  s    ))%7r-   c                    |t         j                  t         j                  t         j                  fv sJ | j                  t        | j                  |||       | j                  j                  ||||       y rr   )r!   r   FAILr   rl   r   re   after_trial)r?   r{   r|   r   r   s        r+   r   zTPESampler.after_trial  sd     ,,jooz?P?PQQQQ!!-,T-C-CUESXY((ufEr-   ) r@   boolrA   floatrB   r   rC   r   rD   intrE   r   rF   zCallable[[int], int]rG   zCallable[[int], np.ndarray]rH   z
int | NonerO   r   rP   r   rQ   r   rR   r   rS   /Callable[[FrozenTrial], Sequence[float]] | NonerT   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____doc__r   r,   r<   rp   rt   r   r   r   r   r   r   r   r   r   classmethodr   staticmethodr   r   r   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                    t        j                  | d      }t        j                  d|z  d|z        }t        ||dk(  <   |S )Nr   r3   g?g?)r5   maxmaximumEPS)	loss_valsworst_pointreference_points      r+   _get_reference_pointr    sA    &&+Kjj{!2C+4EFO,/OOq()r-   c                   g }g }g }g }|D ]  }|j                   t        j                  k(  r|j                  |       2|r t	        |      dkD  r|j                  |       T|j                   t        j
                  k(  r|j                  |       |j                   t        j                  k(  r|j                  |       J  t        || |      \  }	}
t        d|t        |	      z
        }t        || |      \  }}t        d|t        |      z
        }t        ||      \  }}|	|z   |z   }|
|z   |z   |z   }|j                  d        |j                  d        ||fS )Nr   c                    | j                   S rr   r   r|   s    r+   <lambda>z_split_trials.<locals>.<lambda>  
     r-   keyc                    | j                   S rr   r  r  s    r+   r  z_split_trials.<locals>.<lambda>  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     sk    OMN ;;*,,, !!%( %@%G!%K$$U+[[J///""5)[[J---  '5 &<OUT[%\"NN!Ws>223G!5mUG!TL,!Ws<001G)ABSU\)]&&!L03CCL!L03CCnTL4545%%r-   c                    t        |t        |             }t        |j                        dk  rt        | ||      S t	        | ||      S )Nr   )r&   r   
directions'_split_complete_trials_single_objective&_split_complete_trials_multi_objective)r   r{   r$  s      r+   r   r     sE     '3v;'G
5!6vugNN5feWMMr-   c                    |j                   t        j                  k(  rt        | d       }nt        | d d      }|d | ||d  fS )Nc                6    t        t        | j                        S rr   r   r   valuer  s    r+   r  z9_split_complete_trials_single_objective.<locals>.<lambda>      eU[[9Q r-   r  c                6    t        t        | j                        S rr   r6  r  s    r+   r  z9_split_complete_trials_single_objective.<locals>.<lambda>  r8  r-   T)r  reverse)	directionr   MINIMIZErx   r   r{   r$  sorted_trialss       r+   r2  r2    sL     .111v+QRv+Q[_`'"M'($;;;r-   c           
        |dk(  rg t        |       fS |t        |       k(  rt        |       g fS d|cxk  rt        |       k  sJ  J t        j                  | D cg c]  }|j                   c}      }||j
                  D cg c]  }|t        j                  k(  rdnd c}z  }t        ||      }t        j                  |d      \  }}t        t        j                  |t        j                  |      |k     d            }	t        |d |	d	z    t        j                  |	d	z         k(        sJ t        j                  t        |             }
|
||	k     }|j                  |k  r||	d	z      |	d	z   k(  sJ ||	d	z   k(  }||   }||j                  z
  }t!        t#        |j%                               t#        |
|         |t#        t'        |                  }t        j(                  ||      }t+        t-        t         |j/                                     }t1        t        |             D cg c]  }||v s| |    }}t1        t        |             D cg c]  }||vs| |    }}||fS c c}w c c}w c c}w c c}w )
Nr         r1   )r$  T)return_counts)initialr   )listr   r5   arrayr   r1  r   MAXIMIZEr   uniquer   r  cumsumr   aranger   _solve_hssp_with_cachetupleravelr  r   setr   tolistr   )r   r{   r$  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+   r3  r3    si    !|4<	CK	F|Rw$V$$$$$HH7uell78E	UEUEUVa>222d;VVE3E7K#6dKE; #BFF5;1G71R+S]_$` au40145C\_`C`9aabbbiiF$G/3LLMMG#.237PST7TTTT+/H1/LL]+ 2 221,$$&''-()&|45	
 		-1ABD}';';'=>?',S['9T!QBS=SF1ITLT',S['9X!QFW=WF1IXLX%%3 8V, UXs$   I)I.)	I33I3	I8I8c                   t        | j                        dkD  rqt        | j                  j                               \  }}t	        j
                  |      r| t        d      fS |j                  t        j                  k(  r| |fS | | fS y)Nr   inf)r   g        )
r   intermediate_valuesr  ry   r'   isnanr   r;  r   r<  )r|   r{   stepintermediate_values       r+   _get_pruned_trial_scorerb    s    
5$$%)#&u'@'@'F'F'H#I  ::()5%,&&__ 7 775,,,5----r-   c                b    t        |t        |             }t        | fd      }|d | ||d  fS )Nc                    t        |       S rr   )rb  )r|   r{   s    r+   r  z&_split_pruned_trials.<locals>.<lambda>   s    5LUTY5Z r-   r  )r&   r   rx   r=  s    `  r+   r!  r!    s;     '3v;'G6'Z[M'"M'($;;;r-   c                    | j                   j                  t              }|.t        j                  d| j
                   d       t        d      S t        d |D              S )NzTrial z[ does not have constraint values. It will be treated as a lower priority than other trials.r]  c              3  ,   K   | ]  }|d kD  s	|  ywr   Nr   )r   r   s     r+   r   z._get_infeasible_trial_score.<locals>.<genexpr>.  s     2AE12s   
)r   r   r   r\   r]   r   r   r   )r|   
constraints     r+   r  r  $  sa    ##''(89JU\\N #I I	
 U| 2j222r-   c                b    t        |t        |             }t        | t              }|d | ||d  fS )Nr  )r&   r   rx   r  )r   r$  r>  s      r+   r"  r"  1  s:     '3v;'G6'BCM'"M'($;;;r-   c                   dfd}t        j                  |D cg c]
  } ||       c}      }t        j                  |dt              }t        j                  |      }|dk  r|S t        j                  |D cg c]  }|j
                   c}      |   }|| j                  D 	cg c]  }	|	t        j                  k(  rdnd c}	z  }t        |      }
t        |d      }||   }t        ||
d      }t        j                  |      r|S t        j                  |j                  d	   t         
       }t        j"                  |t$        
      }t'        | j                        dk  r&|D cg c]  }|t        ||   |
d      z
   c}||<   nt        j(                  |
|z
  d      ||<   t        j*                  ||d d t         j,                  f         }||xx   t/        |      D cg c]  \  }}t        |||f   |
       c}}z  cc<   t        j*                  |t1        t        j0                  |      t              z  t              ||<   |S c c}w c c}w c c}	w c c}w c c}}w )Nc                @    d u xs t        d  |       D              S )Nc              3  &   K   | ]	  }|d k    ywrg  r   )r   cs     r+   r   zR_calculate_weights_below_for_multi_objective.<locals>._feasible.<locals>.<genexpr>?  s     .W!qAv.Ws   )r   )r|   rS   s    r+   	_feasiblez?_calculate_weights_below_for_multi_objective.<locals>._feasible>  s%    4'W3.W?OPU?V.W+WWr-   r1   r   r@  F)assume_unique_lexsortedT)assume_paretor   )dtype   rB  r3   )r|   r    r  r   )r5   r6   wherer  count_nonzeror   r1  r   rF  r  r   r   r'   isinfeyeshaper   zerosr   r   prodr  newaxis	enumerater  )r{   r   rS   rn  r   is_feasibler   n_below_feasiblerO  rP  	ref_pointon_frontpareto_solshvloo_matcontribsloolimited_solsr   s     `                r+   r   r   9  s    
X **LAqilABKHH[#s3M''41JJ,7Q78EE	UEUEUVa>222d;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
EKQQV 4i@
 	
 "$Hs266(;KS7Q,QSV!WM+? B 8V

s   I;I'I,I *I%r   )maxsizec                    t        |      t        |      f}t        j                  | |      }t        j                  |      }t        j                  |      }t	        ||||      S rr   )r   r5   reshaperE  r   )rank_i_lvals_tuplerank_i_indices_tuplerY  ref_point_tuplelvals_shaperX  rank_i_indicesr~  s           r+   rJ  rJ  c  sX     +,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  r$  r   r%  r   r  +tuple[list[FrozenTrial], list[FrozenTrial]])r   Sequence[FrozenTrial]r{   r"   r$  r   r  r  )r|   r    r{   r"   r  ztuple[float, float])r|   r    r  r   )r   r  r$  r   r  r  )r{   r"   r   r  rS   r   r  r  )
r  tuple[float, ...]r  ztuple[int, ...]rY  r   r  r  r  r  )N
__future__r   collections.abcr   r   	functoolsr   r   r'   typingr   r   r	   r\   numpyr5   optunar
   optuna._convert_positional_argsr   optuna._experimentalr   optuna._hypervolumer   optuna._hypervolume.hsspr   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,   r/   r<   r>   r  r   r   r2  r3  rb  r!  r  r"  r   rJ  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-   