ó
    ,Êžj!/  ã                  óØ   • S SK Jr  S SKrS SKJr  S SKJr  S SKrS SKJ	r	  S SKJ
r
  S SKJr  \(       a  S SKJr   " S	 S
5      r        SS jr        SS jr        SS jrg)é    )ÚannotationsN)ÚAny)ÚTYPE_CHECKING)ÚCategoricalDistribution)ÚFloatDistribution)ÚIntDistribution)ÚBaseDistributionc                  óŽ   • \ rS rSrSr   S         SS jjr\SS j5       r\SS j5       r\SS j5       r	SS jr
SS jrS	rg
)Ú_SearchSpaceTransformé   aÑ  Transform a search space and parameter configurations to continuous space.

The search space bounds and parameter configurations are represented as ``numpy.ndarray``s and
transformed into continuous space. Bounds and parameters associated with categorical
distributions are one-hot encoded. Parameter configurations in this space can additionally be
untransformed, or mapped back to the original space. This type of
transformation/untransformation is useful for e.g. implementing samplers without having to
condition on distribution types before sampling parameter values.

Args:
    search_space:
        The search space. If any transformations are to be applied, parameter configurations
        are assumed to hold parameter values for all of the distributions defined in this
        search space. Otherwise, assertion failures will be raised.
    transform_log:
        If :obj:`True`, apply log/exp operations to the bounds and parameters with
        corresponding distributions in log space during transformation/untransformation.
        Should always be :obj:`True` if any parameters are going to be sampled from the
        transformed space.
    transform_step:
        If :obj:`True`, offset the lower and higher bounds by a half step each, increasing the
        space by one step. This allows fair sampling for values close to the bounds.
        Should always be :obj:`True` if any parameters are going to be sampled from the
        transformed space.
    transform_0_1:
        If :obj:`True`, apply a linear transformation to the bounds and parameters so that
        they are in the unit cube.

Attributes:
    bounds:
        Constructed bounds from the given search space.
    column_to_encoded_columns:
        Constructed mapping from original parameter column index to encoded column indices.
    encoded_column_to_column:
        Constructed mapping from encoded column index to original parameter column index.

Note:
    Parameter values are not scaled to the unit cube.

Note:
    ``transform_log`` and ``transform_step`` are useful for constructing bounds and parameters
    without any actual transformations by setting those arguments to :obj:`False`. This is
    needed for e.g. the hyperparameter importance assessments.

c                ój   • [        XU5      u  pVnXPl        X`l        Xpl        Xl        X l        X@l        g ©N)Ú_transform_search_spaceÚ_raw_boundsÚ_column_to_encoded_columnsÚ_encoded_column_to_columnÚ_search_spaceÚ_transform_logÚ_transform_0_1)ÚselfÚsearch_spaceÚtransform_logÚtransform_stepÚtransform_0_1ÚboundsÚcolumn_to_encoded_columnsÚencoded_column_to_columns           ÚŸC:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\optuna/_transform.pyÚ__init__Ú_SearchSpaceTransform.__init__A   sB   € ô G^Ø¨óG
ÑCˆÐ+Cð "ÔØ*CÔ'Ø)AÔ&Ø)ÔØ+ÔØ+Õó    c                ó¢   • U R                   (       a3  [        R                  " SS//U R                  R                  S   -  5      $ U R                  $ )Nç        g      ð?r   )r   ÚnpÚarrayr   Úshape©r   s    r   r   Ú_SearchSpaceTransform.boundsR   sB   € à××Ü—8’8˜c 3˜Z˜L¨4×+;Ñ+;×+AÑ+AÀ!Ñ+DÑDÓEÐEà×#Ñ#Ð#r!   c                ó   • U R                   $ r   )r   r'   s    r   r   Ú/_SearchSpaceTransform.column_to_encoded_columnsY   s   € à×.Ñ.Ð.r!   c                ó   • U R                   $ r   )r   r'   s    r   r   Ú._SearchSpaceTransform.encoded_column_to_column]   s   € à×-Ñ-Ð-r!   c                ó¸  • [         R                  " U R                  R                  S   [         R                  S9nSnU R
                  R                  5        H‚  u  pEXA;   d   S5       eX   n[        U[        5      (       a;  [        UR                  U5      5      nSX#U-   '   U[        UR                  5      -  nMe  [        XeU R                  5      X#'   US-  nM„     U R                  (       al  U R                  SS2S4   U R                  SS2S4   :H  nSX('   X()    U R                  U) S4   -
  U R                  U) S4   U R                  U) S4   -
  -  X() '   U$ )zýTransform a parameter configuration from actual values to continuous space.

Args:
    params:
        A parameter configuration to transform.

Returns:
    A 1-dimensional ``numpy.ndarray`` holding the transformed parameters in the
    configuration.

r   ©Údtypez7Parameter configuration must contain all distributions.é   Nç      à?)r$   Úzerosr   r&   Úfloat64r   ÚitemsÚ
isinstancer   ÚintÚto_internal_reprÚlenÚchoicesÚ_transform_numerical_paramr   r   )	r   ÚparamsÚtrans_paramsÚ	bound_idxÚnameÚdistributionÚparamÚ
choice_idxÚsingle_masks	            r   Ú	transformÚ_SearchSpaceTransform.transforma   sb  € ô —x’x × 0Ñ 0× 6Ñ 6°qÑ 9ÄÇÁÑLˆàˆ	Ø"&×"4Ñ"4×":Ñ":Ö"<ÑˆDØ“>Ð\Ð#\Ó\�>Ø‘LˆEä˜,Ô(?×@Ñ@Ü  ×!>Ñ!>¸uÓ!EÓF�
Ø78�¨Ñ3Ñ4ØœS ×!5Ñ!5Ó6Ñ6’	ä*DØ¨×)<Ñ)<ó+�Ñ'ð ˜Q‘’	ñ #=ð ××Ø×*Ñ*ª1¨a¨4Ñ0°D×4DÑ4DÂQÈÀTÑ4JÑJˆKØ(+ˆLÑ%à˜\Ñ*¨T×-=Ñ-=¸{¸lÈA¸oÑ-NÑNØ×!Ñ! ; ,° /Ñ2°T×5EÑ5EÀ{ÀlÐTUÀoÑ5VÑVñ*XˆL˜Ñ&ð Ðr!   c                ó2  • UR                   U R                  R                   S   4:X  d   eU R                  (       a>  U R                  SS2S4   UU R                  SS2S4   U R                  SS2S4   -
  -  -   n0 n[        U R                  R                  5       U R                  5       Hi  u  u  p4nX   n[        U[        5      (       a   UR                  UR                  5       5      nO$[        UR                  5       X@R                  5      nXrU'   Mk     U$ )ap  Untransform a parameter configuration from continuous space to actual values.

Args:
    trans_params:
        A 1-dimensional ``numpy.ndarray`` in the transformed space corresponding to a
        parameter configuration.

Returns:
    A dictionary of an untransformed parameter configuration. Keys are parameter names.
    Values are untransformed parameter values.

r   Nr0   )r&   r   r   Úzipr   r4   r   r5   r   Úto_external_reprÚargmaxÚ_untransform_numerical_paramÚitemr   )r   r<   r;   r>   r?   Úencoded_columnsÚtrans_paramr@   s           r   ÚuntransformÚ!_SearchSpaceTransform.untransform‡   s  € ð ×!Ñ! d×&6Ñ&6×&<Ñ&<¸QÑ&?Ð%AÓAÐAÐAà××Ø×+Ñ+ªA¨q¨DÑ1°LØ× Ñ ¢ A Ñ&¨×)9Ñ)9º!¸Q¸$Ñ)?Ñ?ñ5ñ ˆLð ˆä58Ø×Ñ×$Ñ$Ó&¨×(FÑ(Fö6
Ñ1Ñ ˆT /ð 'Ñ7ˆKä˜,Ô(?×@Ñ@à$×5Ñ5°k×6HÑ6HÓ6JÓK‘ä4Ø×$Ñ$Ó&¨×6IÑ6Ió�ð !�4‹Lñ6
ð ˆr!   )r   r   r   r   r   r   N)TTF)
r   údict[str, BaseDistribution]r   Úboolr   rP   r   rP   ÚreturnÚNone)rQ   ú
np.ndarray)rQ   zlist[np.ndarray])r;   údict[str, Any]rQ   rS   )r<   rS   rQ   rT   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   Úpropertyr   r   r   rC   rM   Ú__static_attributes__© r!   r   r   r      s�   † ñ,ðb #Ø#Ø#ð,à1ð,ð ð,ð ð	,ð
 ð,ð 
õ,ð" ó$ó ð$ð ó/ó ð/ð ó.ó ð.ô$÷L%r!   r   c                óv  • [        U 5      S:”  d   S5       e[        S U R                  5        5       5      n[        R                  " US4[        R
                  S9n/ n[        R                  " U[        R                  S9nSnU R                  5        GH  nUn	[        U	[        5      (       aX  [        U	R                  5      n
SXGXz-   & [        R                  " XwU
-   5      n[        U5      Xk'   UR                  U5        Xz-  nMs  [        U	[        [        45      (       Ga…  [        U	[        5      (       a…  U	R                  bK  U(       a  SU	R                  -  OSn[        U	R                   X‘5      U-
  [        U	R"                  X‘5      U-   4nOØ[        U	R                   X‘5      [        U	R"                  X‘5      4nO«[        U	[        5      (       a�  U(       a  SU	R                  -  OSnU	R$                  (       a3  [        U	R                   U-
  X‘5      [        U	R"                  U-   X‘5      4nO:[        U	R                   X‘5      U-
  [        U	R"                  X‘5      U-   4nO S	5       eXÔU'   [        R&                  " U5      n[        U5      Xn'   UR                  U5        US
-  nGM   S	5       e   Xs:X  d   eXEU4$ )Nr   z/Cannot transform if no distributions are given.c              3  óz   #   • U  H1  n[        U[        5      (       a  [        UR                  5      OS v •  M3     g7f)r0   N)r5   r   r8   r9   )Ú.0Úds     r   Ú	<genexpr>Ú*_transform_search_space.<locals>.<genexpr>´   s1   é € ð â&ˆAô % QÔ(?×@Ñ@ŒˆA�I‰IŒÀaÔGÚ&ùs   ‚9;é   r.   )r   r0   r1   r#   ú*Should not reach. Unexpected distribution.r0   )r8   ÚsumÚvaluesr$   Úemptyr3   Úint64r5   r   r9   ÚarangeÚappendr   r   Ústepr:   ÚlowÚhighÚlogÚ
atleast_1d)r   r   r   Ún_boundsr   r   r   r=   r?   r`   Ú	n_choicesrK   Ú	half_stepÚbdsÚencoded_columns                  r   r   r   ¯   s{  € ô ˆ|Ó˜qÓ ÐSÐ"SÓSÐ äñ à×$Ñ$Ô&óó €Hô
 �XŠX�x �m¬2¯:©:Ñ6€FØ24ÐÜ!Ÿxšx¨¼¿¹ÑAÐà€IØ$×+Ñ+×-ˆØˆÜ�aÔ0×1Ñ1Ü˜AŸI™I›ˆIØ8>ˆF˜yÑ4Ð5Ü Ÿiši¨	¸yÑ3HÓIˆOÜ8;Ð<UÓ8VÐ$Ñ5Ø%×,Ñ,¨_Ô=ØÑ"ŠIÜØä!Üð÷
ò 
ô ˜!Ô.×/Ñ/Ø—6‘6Ñ%Þ0>  a§f¡f¢ÀC�Iä2°1·5±5¸!ÓKÈiÑWÜ2°1·6±6¸1ÓLÈyÑXð‘Cô 3°1·5±5¸!ÓKÜ2°1·6±6¸1ÓLð‘Cô ˜Aœ×/Ñ/Þ,:˜C !§&¡&šLÀ�	Ø—5—5ä2°1·5±5¸9Ñ3DÀaÓWÜ2°1·6±6¸IÑ3EÀqÓXð‘Cô 3°1·5±5¸!ÓKÈiÑWÜ2°1·6±6¸1ÓLÈyÑXð‘Cð
 KÐJÓJ�uà #�9ÑÜŸ]š]¨9Ó5ˆNÜ7:Ð;TÓ7UÐ$Ñ4Ø%×,Ñ,¨^Ô<Ø˜‰N‹IàFÐFÓF�5ñc .ðf Ó Ð Ð àÐ.FÐFÐFr!   c                óÄ  • Un[        U[        5      (       a   S5       e[        U[        5      (       aI  UR                  (       a*  U(       a  [        R                  " U 5      O
[        U 5      nU$ [        U 5      n U$ [        U[        5      (       aI  UR                  (       a*  U(       a  [        R                  " U 5      O
[        U 5      nU$ [        U 5      n U$  S5       e)Nú,Should not reach. Should be one-hot encoded.rd   )r5   r   r   rn   ÚmathÚfloatr   )r@   r?   r   r`   rL   s        r   r:   r:   ö   s»   € ð 	€Aä�!Ô,×-Ñ-ØDÐDÓDˆuÜ	�AÔ(×	)Ñ	)Ø�5�5Þ-:œ$Ÿ(š( 5œ/ÄÀeÃˆKð Ðô   ›,‰Kð Ðô 
�A”×	'Ñ	'Ø�5�5Þ-:œ$Ÿ(š( 5œ/ÄÀeÃˆKð Ðô	   ›,‰Kð Ðð 	CÐBÓBˆur!   c                ó†  • Un[        U[        5      (       a   S5       e[        U[        5      (       Ga€  UR                  (       a�  U(       a  [        R
                  " U 5      OU nUR                  5       (       a   U$ [        [        R                  " XCR                  [        R                  " UR                  UR                  S-
  5      5      5      n U$ UR                  b~  [        [        R                  " [        R                  " XR                  -
  UR                  -  5      UR                  -  UR                  -   UR                  UR                  5      5      nU$ UR                  5       (       a  U nU$ [        U [        R                  " UR                  UR                  S-
  5      5      n U$ [        U[         5      (       aò  UR                  (       ah  U(       aS  [#        [        [        R
                  " U 5      5      5      n[        [%        XCR                  5      UR                  5      nU$ [#        U 5      n U$ [        R&                  " XR                  -
  UR                  -  S-   5      nXSR                  -  UR                  -   n[        [%        XCR                  5      UR                  5      n U$  S5       e)Nrv   r0   r1   rd   )r5   r   r   rn   rw   ÚexpÚsinglerx   r$   Úcliprl   Ú	nextafterrm   rk   ÚroundÚminr   r6   ÚmaxÚfloor)rL   r?   r   r`   r@   Ú
step_indexs         r   rI   rI     s  € ð 	€Aä�!Ô,×-Ñ-ØDÐDÓDˆuÜ	�AÔ(×	)Ò	)Ø�5�5Þ-:”D—H’H˜[Ô)ÀˆEØ�x‰x�z‰zØð4 €Lô1 œbŸgšg e¯U©U´B·L²LÀÇÁÈÏÉÐRSÉÓ4TÓUÓV‘ð0 €Lð/ �V‰VÑÜÜ—’œŸš +·±Ñ"5¸¿¹Ñ!?Ó@À1Ç6Á6ÑIÈAÏEÉEÑQÐST×SXÑSXÐZ[×Z`ÑZ`ÓaóˆEð, €Lð% �x‰x�z‰zØ#�ð" €Lô ˜K¬¯ª°a·f±f¸a¿f¹fÀq¹jÓ)IÓJ‘ð €Lô 
�A”×	'Ñ	'Ø�5�5ÞÜœE¤$§(¢(¨;Ó"7Ó8Ó9�ÜœC §u¡uÓ-¨q¯v©vÓ6�ð €Lô ˜KÓ(‘ð €Lô Ÿš [·5±5Ñ%8¸A¿F¹FÑ$BÀSÑ$HÓIˆJØ§¡Ñ'¨!¯%©%Ñ/ˆEÜœ˜E§5¡5Ó)¨1¯6©6Ó2‰Eð €Lð 	CÐBÓBˆur!   )r   rO   r   rP   r   rP   rQ   z/tuple[np.ndarray, list[np.ndarray], np.ndarray])r@   úint | floatr?   r	   r   rP   rQ   rx   )rL   rx   r?   r	   r   rP   rQ   rƒ   )Ú
__future__r   rw   Útypingr   r   Únumpyr$   Úoptuna.distributionsr   r   r   r	   r   r   r:   rI   r\   r!   r   Ú<module>rˆ      s¬   ðÝ "ã Ý Ý  ã å 8Ý 2Ý 0ö Ý5÷Zñ ZðzDGØ-ðDGØ>BðDGØTXðDGà4ôDGðNØðØ&6ðØGKðà
ôð.%Øð%Ø&6ð%ØGKð%àõ%r!   