L i{4vddlmZddlmZddlZddlmZddlmZddlZ ddl Z ddl m Z ddl m Z ddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZerddlmZe j<eZ ejBejDfZ#ejHZ%e dGddeZ&y)) annotations)SequenceN)Any) TYPE_CHECKING)logging)experimental_class) _LazyImport)_SearchSpaceTransform)BaseDistribution)CategoricalDistribution) BaseSampler)&_INDEPENDENT_SAMPLING_WARNING_TEMPLATE) FrozenTrial) TrialState)Studyz3.0.0ceZdZdZddddddd ddZddZ dd Zdd Zedd Z dd Z dd Z ddZ ddZ ddZddZddZy) QMCSamplerapA Quasi Monte Carlo Sampler that generates low-discrepancy sequences. Quasi Monte Carlo (QMC) sequences are designed to have lower discrepancies than standard random sequences. They are known to perform better than the standard random sequences in hyperparameter optimization. For further information about the use of QMC sequences for hyperparameter optimization, please refer to the following paper: - `Bergstra, James, and Yoshua Bengio. Random search for hyper-parameter optimization. Journal of machine learning research 13.2, 2012. `__ We use the QMC implementations in Scipy. For the details of the QMC algorithm, see the Scipy API references on `scipy.stats.qmc `__. .. note: If your search space contains categorical parameters, it samples the categorical parameters by its `independent_sampler` without using QMC algorithm. .. note:: The search space of the sampler is determined by either previous trials in the study or the first trial that this sampler samples. If there are previous trials in the study, :class:`~optuna.samplers.QMCSampler` infers its search space using the trial which was created first in the study. Otherwise (if the study has no previous trials), :class:`~optuna.samplers.QMCSampler` samples the first trial using its `independent_sampler` and then infers the search space in the second trial. As mentioned above, the search space of the :class:`~optuna.samplers.QMCSampler` is determined by the first trial of the study. Once the search space is determined, it cannot be changed afterwards. Args: qmc_type: The type of QMC sequence to be sampled. This must be one of `"halton"` and `"sobol"`. Default is `"sobol"`. .. note:: Sobol' sequence is designed to have low-discrepancy property when the number of samples is :math:`n=2^m` for each positive integer :math:`m`. When it is possible to pre-specify the number of trials suggested by `QMCSampler`, it is recommended that the number of trials should be set as power of two. scramble: If this option is :obj:`True`, scrambling (randomization) is applied to the QMC sequences. seed: A seed for ``QMCSampler``. This argument is used only when ``scramble`` is :obj:`True`. If this is :obj:`None`, the seed is initialized randomly. Default is :obj:`None`. .. note:: When using multiple :class:`~optuna.samplers.QMCSampler`'s in parallel and/or distributed optimization, all the samplers must share the same seed when the `scrambling` is enabled. Otherwise, the low-discrepancy property of the samples will be degraded. independent_sampler: A :class:`~optuna.samplers.BaseSampler` instance that is used for independent sampling. The first trial of the study and the parameters not contained in the relative search space are sampled by this sampler. If :obj:`None` is specified, :class:`~optuna.samplers.RandomSampler` is used as the default. .. seealso:: :class:`~optuna.samplers` module provides built-in independent samplers such as :class:`~optuna.samplers.RandomSampler` and :class:`~optuna.samplers.TPESampler`. warn_independent_sampling: If this is :obj:`True`, a warning message is emitted when the value of a parameter is sampled by using an independent sampler. Note that the parameters of the first trial in a study are sampled via an independent sampler in most cases, so no warning messages are emitted in such cases. warn_asynchronous_seeding: If this is :obj:`True`, a warning message is emitted when the scrambling (randomization) is applied to the QMC sequence and the random seed of the sampler is not set manually. .. note:: When using parallel and/or distributed optimization without manually setting the seed, the seed is set randomly for each instances of :class:`~optuna.samplers.QMCSampler` for different workers, which ends up asynchronous seeding for multiple samplers used in the optimization. .. seealso:: See parameter ``seed`` in :class:`~optuna.samplers.QMCSampler`. Example: Optimize a simple quadratic function by using :class:`~optuna.samplers.QMCSampler`. .. testcode:: import optuna def objective(trial): x = trial.suggest_float("x", -1, 1) y = trial.suggest_int("y", -1, 1) return x**2 + y sampler = optuna.samplers.QMCSampler() study = optuna.create_study(sampler=sampler) study.optimize(objective, n_trials=8) sobolFNT)qmc_typescrambleseedindependent_samplerwarn_asynchronous_seedingwarn_independent_samplingcV||_|,tjjj n||_|xs t jj||_ d|_ ||_ |dvr||_ nd|d}t|||r|r|jyyyy)N)r)haltonrzThe `qmc_type`, "z:", is not a valid. It must be one of "halton" and "sobol".) _scramblenprandomPCG64 random_raw_seedoptunasamplers RandomSampler_independent_sampler_initial_search_space_warn_independent_sampling _qmc_type ValueError_log_asynchronous_seeding)selfrrrrrrmessages Z/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/optuna/samplers/_qmc.py__init__zQMCSampler.__init__s"7;|RYY__&113 $7$c6??;X;X^b;X;c!IM"*C' * *%DN$H:.:: W% % z8QMCSampler.infer_relative_search_space..s QXXr0)key)r' _get_trials_SUGGESTED_STATESlenmin_infer_initial_search_space)r,studytrial past_trials first_trials r.infer_relative_search_spacez&QMCSampler.infer_relative_search_spacesu  % % 1-- -''?P\`'a { q I++=> %)%E%Ek%R")))r0czi}|jjD]\}}t|tr|||<|Sr2) distributionsitems isinstancer )r,rC search_space param_name distributions r.rAz&QMCSampler._infer_initial_search_spacesJ46 (-(;(;(A(A(C 4 $J ,(?@'3L $ 4 r0c.tjdy)Na No seed is provided for `QMCSampler` and the seed is set randomly. If you are running multiple `QMCSampler`s in parallel and/or distributed environment, the same seed must be used in all samplers to ensure that resulting samples are taken from the same QMC sequence. )_loggerwarningr0r.r+z$QMCSampler._log_asynchronous_seedings = r0c tjtj||j|j j j|j jdy)NzTdynamic search space and `CategoricalDistribution` are not supported by `QMCSampler`)rL trial_numberindependent_sampler_name sampler_namefallback_reason)rOrPrformatr9r& __class____name__)r,rCrLs r._log_independent_samplingz$QMCSampler._log_independent_samplingsM 2 9 9%"\\)-)B)B)L)L)U)U!^^44&  r0c|j|jr|j|||jj ||||Sr2)r'r(rZr&sample_independent)r,rBrCrLparam_distributions r.r\zQMCSampler.sample_independentsL  % % 1....ujA((;; 5*&8  r0c|ik(riS|j||}t|}|jdddf||jdddf|jdddfz zz}|j|dddfS)Nr) _sample_qmcr bounds untransform)r,rBrCrKsampletranss r.sample_relativezQMCSampler.sample_relatives 2 I!!%6%l3ad#f QT0BU\\RSUVRVEW0W&XX  1..r0c<|jj||yr2)r& before_trial)r,rBrCs r.rgzQMCSampler.before_trials !!..ue>X %#**14::*WJ ^^w &! ['--adjj4>>-Z  [ [12 2  !   # #L 1""1%  [ [s 5)CC&cRd}||jz }|jr|d|jdz }n|dz }|dz}|jj |j }||j vr ||}|dz }nd}|jj|j |||S)Nz (scramble=True, seed=)z (scramble=False)z's last sample idr_r)r)rr"_storageget_study_system_attrs _study_idkeysset_study_system_attr)r,rBqmc_id key_qmc_id system_attrsrss r.rmzQMCSampler._find_sample_id-s$..  >> .tzzl!< Y /  /=KK*K K ' K  K6r0r)' __future__rcollections.abcr threadingtypingrrnumpyrr#roptuna._experimentalroptuna._importsr optuna._transformr optuna.distributionsr r optuna.samplersr optuna.samplers._baser optuna.trialrr optuna.studyr get_loggerrYrOCOMPLETEPRUNEDr>LockrorrQr0r.rs"$  3'318'H$#" '  X &((**;*;< ).."G```r0