L ijddlmZddlmZddlZddlZddlZddlmZddlm Z ddlm Z ddlm Z ddl Z ddl ZddlZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddl m!Z!ddl"m#Z#ddl$m%Z%ddl&m'Z'ddl&m(Z(e r+ddl)Z)e e)jTe)jVe)jXfZ-nedZ)ej\e/Z0dZ1dZ2GddeZ3 ddZ4y)) annotations)SequenceN)Any)cast) TYPE_CHECKING)Union) _deprecated)logging)warn_experimental_argument) _LazyImport)_SearchSpaceTransform)BaseDistribution)FloatDistribution)IntDistribution) BaseSampler)&_INDEPENDENT_SAMPLING_WARNING_TEMPLATE)LazyRandomState)IntersectionSearchSpace)StudyDirection) FrozenTrial) TrialStatecmaesg|=ic deZdZdZ dddddddddd ddZddZ ddZ dd Zedd Z edd Z dd Z dd Z ddZ ddZ d dZd!dZd"dZ d#dZd$dZ d%dZy)& CmaEsSampleruh'A sampler using `cmaes `__ as the backend. Example: Optimize a simple quadratic function by using :class:`~optuna.samplers.CmaEsSampler`. .. code-block:: console $ pip install cmaes .. 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.CmaEsSampler() study = optuna.create_study(sampler=sampler) study.optimize(objective, n_trials=20) Please note that this sampler does not support CategoricalDistribution. However, :class:`~optuna.distributions.FloatDistribution` with ``step``, (:func:`~optuna.trial.Trial.suggest_float`) and :class:`~optuna.distributions.IntDistribution` (:func:`~optuna.trial.Trial.suggest_int`) are supported. If your search space contains categorical parameters, I recommend you to use :class:`~optuna.samplers.TPESampler` instead. Furthermore, there is room for performance improvements in parallel optimization settings. This sampler cannot use some trials for updating the parameters of multivariate normal distribution. For further information about CMA-ES algorithm, please refer to the following papers: - `N. Hansen, The CMA Evolution Strategy: A Tutorial. arXiv:1604.00772, 2016. `__ - `A. Auger and N. Hansen. A restart CMA evolution strategy with increasing population size. In Proceedings of the IEEE Congress on Evolutionary Computation (CEC 2005), pages 1769–1776. IEEE Press, 2005. `__ - `N. Hansen. Benchmarking a BI-Population CMA-ES on the BBOB-2009 Function Testbed. GECCO Workshop, 2009. `__ - `Raymond Ros, Nikolaus Hansen. A Simple Modification in CMA-ES Achieving Linear Time and Space Complexity. 10th International Conference on Parallel Problem Solving From Nature, Sep 2008, Dortmund, Germany. inria-00287367. `__ - `Masahiro Nomura, Shuhei Watanabe, Youhei Akimoto, Yoshihiko Ozaki, Masaki Onishi. Warm Starting CMA-ES for Hyperparameter Optimization, AAAI. 2021. `__ - `R. Hamano, S. Saito, M. Nomura, S. Shirakawa. CMA-ES with Margin: Lower-Bounding Marginal Probability for Mixed-Integer Black-Box Optimization, GECCO. 2022. `__ - `M. Nomura, Y. Akimoto, I. Ono. CMA-ES with Learning Rate Adaptation: Can CMA-ES with Default Population Size Solve Multimodal and Noisy Problems?, GECCO. 2023. `__ .. seealso:: You can also use `optuna_integration.PyCmaSampler `__ which is a sampler using cma library as the backend. Args: x0: A dictionary of an initial parameter values for CMA-ES. By default, the mean of ``low`` and ``high`` for each distribution is used. Note that ``x0`` is sampled uniformly within the search space domain for each restart if you specify ``restart_strategy`` argument. sigma0: Initial standard deviation of CMA-ES. By default, ``sigma0`` is set to ``min_range / 6``, where ``min_range`` denotes the minimum range of the distributions in the search space. seed: A random seed for CMA-ES. n_startup_trials: The independent sampling is used instead of the CMA-ES algorithm until the given number of trials finish in the same study. independent_sampler: A :class:`~optuna.samplers.BaseSampler` instance that is used for independent sampling. The parameters not contained in the relative search space are sampled by this sampler. The search space for :class:`~optuna.samplers.CmaEsSampler` is determined by :func:`~optuna.search_space.intersection_search_space()`. 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 always sampled via an independent sampler, so no warning messages are emitted in this case. restart_strategy: Strategy for restarting CMA-ES optimization when converges to a local minimum. If :obj:`None` is given, CMA-ES will not restart (default). If 'ipop' is given, CMA-ES will restart with increasing population size. if 'bipop' is given, CMA-ES will restart with the population size increased or decreased. Please see also ``inc_popsize`` parameter. .. warning:: Deprecated in v4.4.0. ``restart_strategy`` 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.4.0 onward, ``restart_strategy`` automatically falls back to ``None``, and ``restart_strategy`` will be supported in OptunaHub. See https://github.com/optuna/optuna/releases/tag/v4.4.0. popsize: A population size of CMA-ES. inc_popsize: Multiplier for increasing population size before each restart. This argument will be used when ``restart_strategy = 'ipop'`` or ``restart_strategy = 'bipop'`` is specified. .. warning:: Deprecated in v4.4.0. ``inc_popsize`` 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.4.0 onward, ``inc_popsize`` is no longer utilized within Optuna, and ``inc_popsize`` will be supported in OptunaHub. See https://github.com/optuna/optuna/releases/tag/v4.4.0. consider_pruned_trials: If this is :obj:`True`, the PRUNED trials are considered for sampling. .. note:: Added in v2.0.0 as an experimental feature. The interface may change in newer versions without prior notice. See https://github.com/optuna/optuna/releases/tag/v2.0.0. .. note:: It is suggested to set this flag :obj:`False` when the :class:`~optuna.pruners.MedianPruner` is used. On the other hand, it is suggested to set this flag :obj:`True` when the :class:`~optuna.pruners.HyperbandPruner` is used. Please see `the benchmark result `__ for the details. use_separable_cma: If this is :obj:`True`, the covariance matrix is constrained to be diagonal. Due to reduce the model complexity, the learning rate for the covariance matrix is increased. Consequently, this algorithm outperforms CMA-ES on separable functions. .. note:: Added in v2.6.0 as an experimental feature. The interface may change in newer versions without prior notice. See https://github.com/optuna/optuna/releases/tag/v2.6.0. with_margin: If this is :obj:`True`, CMA-ES with margin is used. This algorithm prevents samples in each discrete distribution (:class:`~optuna.distributions.FloatDistribution` with ``step`` and :class:`~optuna.distributions.IntDistribution`) from being fixed to a single point. Currently, this option cannot be used with ``use_separable_cma=True``. .. note:: Added in v3.1.0 as an experimental feature. The interface may change in newer versions without prior notice. See https://github.com/optuna/optuna/releases/tag/v3.1.0. lr_adapt: If this is :obj:`True`, CMA-ES with learning rate adaptation is used. This algorithm focuses on working well on multimodal and/or noisy problems with default settings. Currently, this option cannot be used with ``use_separable_cma=True`` or ``with_margin=True``. .. note:: Added in v3.3.0 or later, as an experimental feature. The interface may change in newer versions without prior notice. See https://github.com/optuna/optuna/releases/tag/v3.3.0. source_trials: This option is for Warm Starting CMA-ES, a method to transfer prior knowledge on similar HPO tasks through the initialization of CMA-ES. This method estimates a promising distribution from ``source_trials`` and generates the parameter of multivariate gaussian distribution. Please note that it is prohibited to use ``x0``, ``sigma0``, or ``use_separable_cma`` argument together. .. note:: Added in v2.6.0 as an experimental feature. The interface may change in newer versions without prior notice. See https://github.com/optuna/optuna/releases/tag/v2.6.0. NF)consider_pruned_trialsrestart_strategypopsize inc_popsizeuse_separable_cma with_marginlr_adapt source_trialsc|| dk7r?tjjddd}tj|dt ||_||_|xs tjj||_ ||_ ||_ t||_t!|_||_| |_| |_| |_| |_||_|j(rd|_n|j*rd |_nd |_|j$r t3d |j(r t3d |j. t3d |j*r t3d|j,r t3d||| t5d| | r t5d| r| s| r t5d|j(r|j*r t5dyy)Nrz`restart_strategy`z4.4.0z6.0.0)named_verr_verz~ From v4.4.0 onward, `restart_strategy` automatically falls back to `None`. `restart_strategy` will be supported in OptunaHub.)seedzsepcma:zcmawm:zcma:rr r#r!r"zQIt is prohibited to pass `source_trials` argument when x0 or sigma0 is specified.zNIt is prohibited to pass `source_trials` argument when using separable CMA-ES.z]It is prohibited to pass `use_separable_cma` or `with_margin` argument when using `lr_adapt`.zMCurrently, we do not support `use_separable_cma=True` and `with_margin=True`.)r _DEPRECATION_WARNING_TEMPLATEformatwarningswarn FutureWarning_x0_sigma0optunasamplers RandomSampler_independent_sampler_n_startup_trials_warn_independent_samplingr_cma_rngr _search_space_consider_pruned_trials_popsize_use_separable_cma _with_margin _lr_adapt_source_trials _attr_prefixr ValueError)selfx0sigma0n_startup_trialsindependent_samplerwarn_independent_samplingr(rrrrr r!r"r#msgs \/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/optuna/samplers/_cmaes.py__init__zCmaEsSampler.__init__s$  ';"+<;;BB)CC MM%MM   $7$c6??;X;X^b;X;c!!1*C''- 46'=$ "3'!+  " " )D     (D  &D   ' ' &'? @  " " &': ;    * & 7    &} 5 >> &z 2  $".F">u"E"K"K"M . D,""$l->,PQ!-L  .rIc(|j|t|dk(riS|j|}t||jkriSt ||j d}|j |}||j||j}|jt|jk7rW|jrItjdj|jj j"d|_ iS|j%||j&}t||j(k\r@g}|d|j(D]} | j*Jdt-|t.j0r#t3j4| j6d} n|j9| j:} |jt<j>k(r | j*n | j* } |jA| | f|jC|tEjF|jI} |jK| } | D],}|jLjO|jP|| |.|jRjTjWdd |jXz}|jZj]|t-|t.j0rI|j_\}}|jLjO|jPd|jan|j_}|jb}|jLjO|jP||j&|je|}|S) NrT)transform_step transform_0_1z]`CmaEsSampler` does not support dynamic search space. `{}` is used instead of `CmaEsSampler`.Fz"completed trials must have a value x_for_telli)3_raise_error_if_multi_objectivelen _get_trialsr4r r;_restore_optimizer_init_optimizer directiondimboundsr5_loggerwarningr*r3 __class____name___get_solution_trials generationpopulation_sizevaluerRrCMAwMnparray system_attrs transformparamsrMINIMIZEappendtellpickledumpshex_split_optimizer_str_storageset_trial_system_attr _trial_idr6rngrandintnumber_rngr(asktolist_attr_key_generation untransform)r@rSrTrUcompleted_trialstrans optimizersolution_trials solutionstxy optimizer_stroptimizer_attrskeyr(rrr[generation_attr_keyexternal_valuess rGsample_relativezCmaEsSampler.sample_relativefs ,,U3 |  !I++E2  4#9#9 9I& T->->)>d ++,<=  ,,UEOODI ==C - -..>>Df11;;DD? 38/I334DiFZFZ[  9#<#< <8:I$%@y'@'@A )ww*P,PP*i5 !=>A1A$.2I2IIAGGPQPWPWx  !Q( ) NN9 %#LL3779M"77 FO& a44U__c?[^K_` a}}  ((E2U\\AD! i -!* FJ NN 0 0z/@/@/B ]]_F"77 ,, OO0)2F2F  ++F3rIc |jdzS)Nrjr>rMs rGrz!CmaEsSampler._attr_key_generations  <//rIc |jdzS)NrrrMs rG_attr_key_optimizerz CmaEsSampler._attr_key_optimizers  ;..rIc`djfdttDS)Nc3\K|]#}djj|%yw){}:{}N)r*r).0irr@s rG z7CmaEsSampler._concat_optimizer_attrs..s/  GNN4+C+CQG H s),)joinranger^)r@rs``rG_concat_optimizer_attrsz$CmaEsSampler._concat_optimizer_attrss+ww 3/0   rIct|}i}ttj|tz D]C}|tz}t |dztz|}||||dj |j|<E|S)Nr\r)r^rmathceil_SYSTEM_ATTR_MAX_LENGTHminr*r)r@r optimizer_lenattrsrstartends rGryz!CmaEsSampler._split_optimizer_strsM* tyy1H!HIJ ZA//Eq1u 77GCANuUXAYE'..!9!91= > Z rIcRt|D]}|jjDcic]#\}}|j|jr||%}}}t |dk(rZ|j |}tjtj|cSycc}}w)Nr) reversedrprP startswithrr^rrvloadsbytesfromhex)r@rrTrrlrrs rGr`zCmaEsSampler._restore_optimizers ./ >E#("4"4":":"<C>>$":":;U O ?#q( 88IM<< m <= = >s(B#c |jdddf}|jdddf}t|j}|jj|j |||z dz z}n|j |j}|j t j||z dz }n |j }d}ntjg} |jr| jtj|tjk(rdnd} |jD cgc]^} | j| vrNt!|| j"r8|j | j$| t't(| j*zf`} } t| dk(r t-dt/j0| \}}}t3|t4}|j6rt|jdk(rt9j:dt<nVt/j>|||j|j@jBjEddd |z|jF S|jHr/t jJt|jLt( } tO|jLjQD]\}}tS|tTtVfsJ|jX |jZrd | |<<|j\|j^k(rd | |<[|jX|j^|j\z z | |<t/j`|||j| ||j@jBjEddd |z|jFSt/jb||||j|j@jBjEddd |z|jF|jdScc} w)Nrr\rzNo compatible source_trialszSeparable CMA-ES does not operate meaningfully on single-dimensional search spaces. The setting `use_separable_cma=True` will be ignored.i )meansigmardr(n_max_resamplingrk)dtypegg?)rrrdstepscovr(rrk)rrrrdr(rrkr")3rdr^r=r.rqr/rnrrCOMPLETEr8rtPRUNEDrrsstate_is_compatible_search_space distributionsrrrfloatrlr?rget_warm_start_mgdmax_EPSr:r+r, UserWarningSepCMAr6r}r~r9r;emptyr7 enumeratevaluesrRrrsteploglowhighrmCMAr<)r@rrb lower_bounds upper_bounds n_dimensionrrBrexpected_statessignrsource_solutionsrrdists rGrazCmaEsSampler._init_optimizersa ||AqD) ||AqD) %,,'    &xx#|l'Ba&GGtxx0||# !< ABC)223O++&&z'8'89"^%<%<<1"D,, 77o-/qG*D4qww3G,GH    #$) !>??!& 8 89I J D&#VT"  " "5<< A% [ ||  <<**221i@%'+%5$(MM    HHS!4!45UCE$U%8%8%?%?%AB B4!$:K(LMMM99$"E!HXX*"E!H#yyDII,@AE!H B;;||]]&&..q)#77!_aZ1>>3E3EFY[]3^%^___s$==c<|jj||yrK)r3 before_trial)r@rSrTs rGrzCmaEsSampler.before_trialus !!..uers"$   ;'3120'H>78$#UYY ekk9:H  E '  X & P K;P KfN N0KN NrI