
    !#hD~                       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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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& er+d dl'Z'ee'jP                  e'jR                  e'jT                  f   Z+n ed      Z' ejX                  e-      Z.dZ/dZ0 G d de      Z1 G d de      Z2	 	 	 	 	 	 ddZ3y)     )annotations)Callable)SequenceN)Any)cast)
NamedTuple)TYPE_CHECKING)Union)logging)warn_experimental_argument)_LazyImport)_SearchSpaceTransform)BaseDistribution)FloatDistribution)IntDistribution)BaseSampler)LazyRandomState)IntersectionSearchSpace)StudyDirection)FrozenTrial)
TrialStatecmaesg|=i  c                  ^    e Zd ZU ded<   ded<   ded<   ded<   ded<   ded	<   ded
<   ded<   y)_CmaEsAttrKeyszCallable[[int], str]	optimizer
generationzCallable[[], str]popsize
n_restartsstrn_restarts_with_largepoptypesmall_n_evallarge_n_evalN)__name__
__module____qualname____annotations__     S/var/www/html/sandstorm/venv/lib/python3.12/site-packages/optuna/samplers/_cmaes.pyr   r   .   s1    ##$$!!Lr)   r   c            
      l   e Zd ZdZ	 	 	 	 	 	 dddddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 dd	Zedd
       Z	dddZ
dddZ	 d	 	 	 	 	 ddZ	 	 d	 	 	 	 	 	 	 	 	 d dZ	 	 	 	 	 	 	 	 	 	 d!dZd"dZd#dZ	 	 	 	 	 	 	 	 d$dZd%dZ	 	 	 	 	 	 	 	 	 	 d&dZy)'CmaEsSampleru%  A sampler using `cmaes <https://github.com/CyberAgentAILab/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.
      <https://arxiv.org/abs/1604.00772>`__
    - `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. <https://doi.org/10.1109/CEC.2005.1554902>`__
    - `N. Hansen. Benchmarking a BI-Population CMA-ES on the BBOB-2009 Function Testbed.
      GECCO Workshop, 2009. <https://doi.org/10.1145/1570256.1570333>`__
    - `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. <https://doi.org/10.1007/978-3-540-87700-4_30>`__
    - `Masahiro Nomura, Shuhei Watanabe, Youhei Akimoto, Yoshihiko Ozaki, Masaki Onishi.
      Warm Starting CMA-ES for Hyperparameter Optimization, AAAI. 2021.
      <https://doi.org/10.1609/aaai.v35i10.17109>`__
    - `R. Hamano, S. Saito, M. Nomura, S. Shirakawa. CMA-ES with Margin: Lower-Bounding Marginal
      Probability for Mixed-Integer Black-Box Optimization, GECCO. 2022.
      <https://doi.org/10.1145/3512290.3528827>`__
    - `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.
      <https://doi.org/10.1145/3583131.3590358>`__

    .. seealso::
        You can also use `optuna_integration.PyCmaSampler <https://optuna-integration.readthedocs.io/en/stable/reference/generated/optuna_integration.PyCmaSampler.html#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.

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

        popsize:
            A population size of CMA-ES. When ``restart_strategy = 'ipop'``
            or ``restart_strategy = 'bipop'`` is specified,
            this is used as the initial population size.

        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.

        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
                <https://github.com/optuna/optuna/pull/1229>`__ 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_trialsrestart_strategyr   inc_popsizeuse_separable_cmawith_marginlr_adaptsource_trialsc               6   || _         || _        |xs  t        j                  j	                  |      | _        || _        || _        t        |      | _	        t               | _        || _        || _        |	| _        |
| _        || _        || _        || _        || _        | j                  rt)        d       | j                  rt)        d       | j                   rt)        d       | j&                  t)        d       | j"                  rt)        d       | j$                  rt)        d       |||t+        d      ||rt+        d	      |r|s|rt+        d
      |dvrt+        dj-                  |            | j                   r| j"                  rt+        d      y y )N)seedr/   r.   r1   r4   r2   r3   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`.)ipopbipopNzMrestart_strategy={} is unsupported. Please specify: 'ipop', 'bipop', or None.zMCurrently, we do not support `use_separable_cma=True` and `with_margin=True`.)_x0_sigma0optunasamplersRandomSampler_independent_sampler_n_startup_trials_warn_independent_samplingr   _cma_rngr   _search_space_consider_pruned_trials_restart_strategy_initial_popsize_inc_popsize_use_separable_cma_with_margin	_lr_adapt_source_trialsr   
ValueErrorformat)selfx0sigma0n_startup_trialsindependent_samplerwarn_independent_samplingr6   r.   r/   r   r0   r1   r2   r3   r4   s                  r*   __init__zCmaEsSampler.__init__   s   $ $7$c6??;X;X^b;X;c!!1*C''-46'=$!1 ''"3'!+!!&'9:''&'?@""&':;*&7&}5>>&z2$".F<N-  $):`  *k$ 
  $
 

 <<BFCS<T  ""t'8'8_  (9"r)   c                8    | j                   j                          y N)r>   
reseed_rng)rM   s    r*   rV   zCmaEsSampler.reseed_rngO  s    !!,,.r)   c                    i }| j                   j                  |      j                         D ]2  \  }}|j                         rt	        |t
        t        f      s.|||<   4 |S rU   )rB   	calculateitemssingle
isinstancer   r   )rM   studytrialsearch_spacenamedistributions         r*   infer_relative_search_spacez(CmaEsSampler.infer_relative_search_spaceS  sm     57"&"4"4">">u"E"K"K"M 
	.D,""$ l->,PQ!-L
	. r)   c           	        | j                  |       t        |      dk(  ri S | j                  |      }t        |      | j                  k  ri S t        |      dk(  rW| j                  rIt
        j                  dj                  | j                  j                  j                               d| _        i S t        || j                   d      }| j                  Fdt        j                  dt        j                   t        |j"                              z        z   | _        | j                  }d}d}d	}	d}
d}t        |      dk7  r?|d
   }| j$                  j'                         }||j(                  v r|j(                  |   }n| j                  }| j$                  j+                         }|j(                  j-                  |d      }|j(                  j-                  | j$                  j.                  d      }|j(                  j-                  | j$                  j0                  d	      }	|j(                  j-                  | j$                  j2                  d      }
|j(                  j-                  | j$                  j4                  d      }| j7                  ||      }|(| j9                  ||j:                  | j                        }|j<                  t        |j"                        k7  rW| j                  rIt
        j                  dj                  | j                  j                  j                               d| _        i S | j?                  ||j@                  |      }t        |      |k\  rog }|d | D ]  }|jB                  J d       tE        |tF        jH                        r#tK        jL                  |j(                  d         }n|jO                  |jP                        }|j:                  tR        jT                  k(  r|jB                  n|jB                   }|jW                  ||f        |jY                  |       | jZ                  dk(  rC|j]                         r3|dz  }|| j^                  z  }| j9                  ||j:                  |d      }| jZ                  dk(  r|j]                         r|dz  }||j@                  z  }|	d	k(  r|
|z  }
n||z  }|
|k  r\d	}	| j^                  |z  }t        j                  | j                  || j`                  jb                  je                         dz  z  z        }n#d}	|dz  }| j                  | j^                  |z  z  }| j9                  ||j:                  |d      }tg        jh                  |      jk                         }| jm                  ||      }|D ],  }|jn                  jq                  |jr                  |||          . | j`                  jb                  ju                  dd      |jv                  z   }|jx                  j{                  |       tE        |tF        jH                        rI|j}                         \  }}|jn                  jq                  |jr                  d|j                                n|j}                         }| j$                  jA                  |      }|jn                  jq                  |jr                  ||j@                         | j$                  j'                         }|jn                  jq                  |jr                  ||       | j$                  j+                         }|jn                  jq                  |jr                  ||       |jn                  jq                  |jr                  | j$                  j.                  |       |jn                  jq                  |jr                  | j$                  j0                  |	       |jn                  jq                  |jr                  | j$                  j2                  |
       |jn                  jq                  |jr                  | j$                  j4                  |       |j                  |      }|S )Nr      zu`CmaEsSampler` only supports two or more dimensional continuous search space. `{}` is used instead of `CmaEsSampler`.FT)transform_steptransform_0_1      small)population_sizez]`CmaEsSampler` does not support dynamic search space. `{}` is used instead of `CmaEsSampler`.z"completed trials must have a value
x_for_tellr7   )rj   randomize_start_pointr8   r-   largei   )A_raise_error_if_multi_objectivelen_get_trialsr?   r@   _loggerwarningrL   r>   	__class__r$   r   rH   rE   mathfloorlogbounds
_attr_keysr   system_attrsr   getr    r!   r"   r#   _restore_optimizer_init_optimizer	directiondim_get_solution_trialsr   valuer[   r   CMAwMnparray	transformparamsr   MINIMIZEappendtellrD   should_stoprF   rA   rnguniformpickledumpshex_split_optimizer_str_storageset_trial_system_attr	_trial_idrandintnumber_rngr6   asktolistuntransform)rM   r\   r]   r^   completed_trialstransr   r   r    r!   r"   r#   latest_trialpopsize_attr_keyn_restarts_attr_keyr   solution_trials	solutionstxyn_evalpopsize_multiplieroptimizer_stroptimizer_attrskeyr6   r   rk   generation_attr_keyexternal_valuess                                  r*   sample_relativezCmaEsSampler.sample_relativee  s4    	,,U3|!I++E2 4#9#99I|!..LLRF11;;DDM 38/I &T->->)>d
   ($%

1txxELL@Q7R3R(S$SD!,,
%& A%+B/L#668<#<#<<&334DE//"&//"<"<">%22667JANJ$0$=$=$A$A55q%! #//33DOO4K4KWUG'44889U9UWXYL'44889U9UWXYL++,<jI	,,u8M8M - I ==C--..>>Df11;;DD? 38/I 33i22J
 7*8:I$Xg. )ww*P,PP*i5!=>A1A$.2I2IIAGGPQPWPWx  !Q() NN9%%%/I4I4I4Ka
!D$5$55 005??G[_ 1 	 %%0Y5J5J5La
 9#7#77g% F*L F*L,.%G)-):):<Q)Q&"jj--,1B1B1J1J1LPQ1QRSG
 &G)Q.)"33t7H7HJ_7_`G 005??G[_ 1 	
 #LL3779M"77zRO& a44U__c?[^K_`a }}  ((E2U\\AD!i-!*FJNN00z/@/@/B ]]_F"oo88D,,OO0)2F2F	
  ??224,,U__>NPWX"oo88:,,U__>QS]^,,OOT__BBDY	
 	,,U__doo>U>UW^_,,OOT__99<	
 	,,OOT__99<	
  ++F3r)   c                      j                   rdn j                  rdndd fd}d fd}d fd}d fd}t        ||||dz   d	z   d
z   dz         S )Nzsepcma:zcmawm:zcma:c                d    j                   dz   S dj                  j                   |       z   S )Nr   z{}:restart_{}:optimizerrD   rL   restartattr_prefixrM   s    r*   optimizer_key_templatez7CmaEsSampler._attr_keys.<locals>.optimizer_key_template  s@    %%-"[00"%>%E%E**G&  r)   c                d    j                   dz   S dj                  j                   |       z   S )Nr   z{}:restart_{}:generationr   r   s    r*   generation_attr_key_templatez=CmaEsSampler._attr_keys.<locals>.generation_attr_key_template  s@    %%-"\11"%?%F%F**G&  r)   c                 b    j                    dz   S  dj                  j                         z   S )Nr   z
{}:popsizer   r   rM   s   r*   popsize_attr_key_templatez:CmaEsSampler._attr_keys.<locals>.popsize_attr_key_template$  s6    %%-"Y.."\%8%89O9O%PPPr)   c                 b    j                    dz   S  dj                  j                         z   S )Nr   z{}:n_restartsr   r   s   r*   n_restarts_attr_key_templatez=CmaEsSampler._attr_keys.<locals>.n_restarts_attr_key_template*  s6    %%-"\11"_%;%;D<R<R%SSSr)   r    r!   r"   r#   )r   intreturnr   )r   r   )rG   rH   r   )rM   r   r   r   r   r   s   `    @r*   rx   zCmaEsSampler._attr_keys  sv    ""#K"K K			Q	T "(%(11)#.(.(	
 		
r)   c                d     dj                   fdt        t                    D              S )N c              3  z   K   | ]2  }d j                  j                  j                        |          4 yw){}:{}N)rL   rx   r   ).0ir   r   rM   s     r*   	<genexpr>z7CmaEsSampler._concat_optimizer_attrs.<locals>.<genexpr><  s9      
 GNN4??+D+DZ+PRSTU
s   8;)joinrangero   )rM   r   r   s   ```r*   _concat_optimizer_attrsz$CmaEsSampler._concat_optimizer_attrs;  s+    ww 
3/0
 
 	
r)   c                   t        |      }i }t        t        j                  |t        z              D ]R  }|t        z  }t        |dz   t        z  |      }||| |dj                  | j                  j                  |      |      <   T |S )Nrc   r   )	ro   r   rt   ceil_SYSTEM_ATTR_MAX_LENGTHminrL   rx   r   )rM   r   r   optimizer_lenattrsr   startends           r*   r   z!CmaEsSampler._split_optimizer_strA  s    M*tyy1H!HIJ 	A//Eq1u 77GCN[cOE'..!:!::!FJK	 r)   c           
     r   t        |      D ]  }|j                  j                         D ci c]2  \  }}|j                  | j                  j                  |            r||4 }}}t        |      dk(  ri| j                  ||      }t        j                  t        j                  |            c S  y c c}}w )Nr   )reversedry   rY   
startswithrx   r   ro   r   r   loadsbytesfromhex)rM   r   r   r]   r   r   r   r   s           r*   r{   zCmaEsSampler._restore_optimizerL  s     ./ 
	>E #("4"4":":"<C>>$//";";J"GH U
O 
 ?#q( 88*UM<<m <==
	> s   7B3c                   |j                   d d df   }|j                   d d df   }t        |j                         }| j                  |r/|||z
  | j                  j                  j                  |      z  z   }n3| j                  |||z
  dz  z   }n|j                  | j                        }| j                  t        j                  ||z
  dz        }	n| j                  }	d }
nt        j                  g}| j                  r|j                  t        j                         |t         j"                  k(  rdnd}| j                  D cg c]^  }|j$                  |v rNt'        ||j(                        r8|j                  |j*                        |t-        t.        |j0                        z  f` }}t        |      dk(  rt3        d      t5        j6                  |      \  }}	}
t9        |	t:              }	| j<                  rLt5        j>                  ||	|j                   | j                  j                  jA                  dd      d|z  |	      S | jB                  r%t        jD                  t        |jF                        t.        
      }tI        |jF                  jK                               D ]  \  }}tM        |tN        tP        f      sJ |jR                  |jT                  rd||<   <|jV                  |jX                  k(  rd||<   [|jR                  |jX                  |jV                  z
  z  ||<    t5        jZ                  ||	|j                   ||
| j                  j                  jA                  dd      d|z  |      S t5        j\                  ||	|
|j                   | j                  j                  jA                  dd      d|z  || j^                        S c c}w )Nr   rc   r-      ri   zNo compatible source_trialsi
   )meansigmarw   r6   n_max_resamplingrj   )dtypeg        g      ?)r   r   rw   stepscovr6   r   rj   )r   r   r   rw   r6   r   rj   r3   )0rw   ro   rJ   rA   r   randr9   r   r:   r   r   r   COMPLETErC   r   PRUNEDr   r   state_is_compatible_search_spacedistributionsr   r   floatr   rK   r   get_warm_start_mgdmax_EPSrG   SepCMAr   rH   emptyrB   	enumeratevaluesr[   r   r   steprv   lowhighr   CMArI   )rM   r   r}   rj   rl   lower_boundsupper_boundsn_dimensionr   rO   r   expected_statessignr   source_solutionsr   r   dists                     r*   r|   zCmaEsSampler._init_optimizer_  sc    ||AqD)||AqD)%,,'&$#|l'BdmmFWFWF\F\G '  !#|l'Ba&GG txx0||#!< ABC)223O++&&z'8'89 "^%<%<<1"D ,, 77o-/qG *D4qww3G,GH    #$) !>?? !& 8 89I JD&# VT"""<<||]]&&..q)<!#k!1 /  HHS!4!45UCE$U%8%8%?%?%AB B4!$:K(LMMM99$"E!HXX*"E!H#yyDII,@AE!HB ;;||]]&&..q)<!#k!1 /	 	 yy<<""**1i8+-+^^	
 		
a s   A#Oc                    | j                  |       | j                  r;| j                  |      }t        |      | j                  k\  r| j                  ||       | j                  j                  ||||      S rU   )rn   r@   rp   ro   r?   _log_independent_samplingr>   sample_independent)rM   r\   r]   
param_nameparam_distributioncomplete_trialss         r*   r   zCmaEsSampler.sample_independent  sq     	,,U3**"..u5O?#t'='==..ujA((;;5*&8
 	
r)   c                    t         j                  dj                  ||j                  | j                  j
                  j                               y )Na  The parameter '{}' in trial#{} is sampled independently by using `{}` instead of `CmaEsSampler` (optimization performance may be degraded). `CmaEsSampler` does not support dynamic search space or `CategoricalDistribution`. You can suppress this warning by setting `warn_independent_sampling` to `False` in the constructor of `CmaEsSampler`, if this independent sampling is intended behavior.)rq   rr   rL   r   r>   rs   r$   )rM   r]   r   s      r*   r   z&CmaEsSampler._log_independent_sampling  s@    A BHELL$*C*C*M*M*V*VB
	
r)   c                   g }|j                  dd      D ]  }|j                  t        j                  k(  r|j	                  |       2|j                  t        j
                  k(  sPt        |j                        dkD  si| j                  svt        |j                  j                               \  }}|t        j                  |      }||_        |j	                  |        |S )NFT)deepcopy	use_cacher   )rp   r   r   r   r   r   ro   intermediate_valuesrC   r   rY   copyr   r   )rM   r\   r   r   _r   copied_ts          r*   rp   zCmaEsSampler._get_trials  s    ""ET"B 	1Aww*---&&q):,,,--.200q44::<=5===+!&&&x0	1 r)   c                    | j                   j                  |      }|D cg c]$  }||j                  j                  |d      k(  s#|& c}S c c}w )Nri   )rx   r   ry   rz   )rM   trialsr   r   r   r   s         r*   r   z!CmaEsSampler._get_solution_trials  sG     #oo88D!_aZ1>>3E3EFY[]3^%^___s   $AAc                <    | j                   j                  ||       y rU   )r>   before_trial)rM   r\   r]   s      r*   r  zCmaEsSampler.before_trial  s    !!..ue<r)   c                @    | j                   j                  ||||       y rU   )r>   after_trial)rM   r\   r]   r   r   s        r*   r  zCmaEsSampler.after_trial  s     	!!--eUE6Jr)   )NNrc   NTN)rN   zdict[str, Any] | NonerO   zfloat | NonerP   r   rQ   zBaseSampler | NonerR   boolr6   
int | Noner.   r	  r/   z
str | Noner   r
  r0   r   r1   r	  r2   r	  r3   r	  r4   zlist[FrozenTrial] | Noner   None)r   r  )r\   'optuna.Study'r]   'optuna.trial.FrozenTrial'r   dict[str, BaseDistribution])r\   r  r]   r  r^   r  r   zdict[str, Any])r   r   )r   )r   dict[str, str]r   r   r   r   )r   r   r   r   r   r  )r   z 'list[optuna.trial.FrozenTrial]'r   r   r   z'CmaClass' | None)NF)
r   r   r}   r   rj   r
  rl   r	  r   z
'CmaClass')
r\   r  r]   r  r   r   r   r   r   r   )r]   r   r   r   r   r  )r\   r  r   list[FrozenTrial])r  r  r   r   r   r   r   r  )r\   zoptuna.Studyr]   r   r   r  )
r\   r  r]   r  r   r   r   zSequence[float] | Noner   r  )r$   r%   r&   __doc__rS   rV   ra   r   propertyrx   r   r   r{   r|   r   r   rp   r   r  r  r(   r)   r*   r,   r,   9   sl   }B %)# !26*.T (-'+""'!26!T!T T 	T
 0T $(T T !%T %T T T  T T T  0!T" 
#Tl/#,F	$$dd *d 2	d
 
dL -
 -
^
	 :  
	. '+&+\
$\
 "\
 $	\

  $\
 
\
|

 *
 	

 -
 

$
&`'`58`FI`	`=KK *K 	K
 'K 
Kr)   r,   c                    t        t        | j                  j                               j	                  |j                                     }|t        | j                        cxk(  xr t        |      k(  S c S rU   )ro   setrB   keysintersection)r   r^   intersection_sizes      r*   r   r     s]     C 3 3 8 8 :;HHIZIZI\]^E$7$7 8MC<MMMMMr)   )r   r   r^   r  r   r	  )4
__future__r   collections.abcr   r   r   rt   r   typingr   r   r   r	   r
   numpyr   r;   r   optuna._experimentalr   optuna._importsr   optuna._transformr   optuna.distributionsr   r   r   optuna.samplersr   "optuna.samplers._lazy_random_stater   optuna.search_spacer   optuna.study._study_directionr   optuna.trialr   r   r   r   r   r   CmaClass
get_loggerr$   rq   r   r   r   r,   r   r(   r)   r*   <module>r'     s    " $ $             ; ' 3 1 2 0 ' > 7 8 $ # UYYekk9:H E
'

X
& Z FK; FKRN N0KN	Nr)   