L i.ddlmZddlZddlmZddlZddlmZddlm Z ddlm Z ddlm Z Gdd Z d d Z dd Z dd Zy)) annotationsN)Any)BaseDistribution)CategoricalDistribution)FloatDistribution)IntDistributioncxeZdZdZ d d dZed dZed dZed dZd dZ ddZ y)_SearchSpaceTransformai 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. czt|||\}}}||_||_||_||_||_||_yN)_transform_search_space _raw_bounds_column_to_encoded_columns_encoded_column_to_column _search_space_transform_log_transform_0_1)self search_space transform_logtransform_step transform_0_1boundscolumn_to_encoded_columnsencoded_column_to_columns W/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/optuna/_transform.py__init__z_SearchSpaceTransform.__init__=sSG^ -G C)+C"*C')A&)++c|jr2tjddgg|jjdzS|jS)Ng?r)rnparrayrshapers rrz_SearchSpaceTransform.boundsNsB   88c3ZL4+;+;+A+A!+DDE E## #rc|jSr )rr$s rrz/_SearchSpaceTransform.column_to_encoded_columnsUs...rc|jSr )rr$s rrz._SearchSpaceTransform.encoded_column_to_columnYs---rctj|jjdtj}d}|j j D]\}}||vsJd||}t|tr;t|j|}d|||z<|t|jz }at|||j||<|dz }|jro|jdddf|jdddfk(}d||<|||j|dfz |j|df|j|dfz z ||<|S)a5Transform 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. rdtypez7Parameter configuration must contain all distributions.N?)r!zerosrr#float64ritems isinstancerintto_internal_reprlenchoices_transform_numerical_paramrr) rparams trans_params bound_idxname distributionparam choice_idx single_masks r transformz_SearchSpaceTransform.transform]svxx 0 0 6 6q 9L  "&"4"4":":"<  D,6> \#\ \>4LE,(?@ !>!>u!EF 78 Y34S!5!566 *D<)<)<+ Y'Q     **1a40D4D4DQT4JJK(+L %k\*T-=-={lAo-NN!!;,/2T5E5E{lTUo5VV*XL+ &rc |j|jjdfk(sJ|jr>|jdddf||jdddf|jdddfz zz}i}t|jj |j D]g\\}}}||}t|tr |j|j}n%t|j||j}|||<i|S)aUntransform 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. rNr*)r#rrziprr.rr/rto_external_reprargmax_untransform_numerical_paramitemr)rr6r5r8r9encoded_columns trans_paramr:s r untransformz!_SearchSpaceTransform.untransforms!!d&6&6&<&z*_transform_search_space..s/ %Q(?@AIIaGs13r()rr*r+r *Should not reach. Unexpected distribution.r*)r2sumvaluesr!emptyr-int64r/rr3arangeappendrrstepr4lowhighlog atleast_1d)rrrn_boundsrrrr7r9rV n_choicesrD half_stepbdsencoded_columns rr r s | q S"SS $$&H XXxm2:: 6F24!xxAI$++-1G  a0 1AIII8>F9y94 5 ii 9y3HIO8;aff CI2155!]KiW21661mLyXC 3155!]K21661mLCA/,:C!&&L 55215593DaW2166I3Eq-XC 3155!]KiW21661mLyXC KJJu #F9 ]]95N7:;T7U $^ 4 % , ,^ < NI FF F5c1Gf   ,.F FFrcv|}t|trJdt|tr=|jr$|rt j|n t |}|St |}|St|t r=|jr$|rt j|n t |}|St |}|SJd)N,Should not reach. Should be one-hot encoded.rY)r/rrrcmathfloatr)r:r9rrVrEs rr4r4s A!,-DDDu A( ) 55-:$((5/e K  ,K  A ' 55-:$((5/e K   ,K  CBBurc |}t|trJdt|trG|jre|rt j |n|}|j r |St|tj|j|jdz }|S|j}ttjtj||jz |jz |jz|jz|j|j}|S|j r|}|St|tj|j|jdz }|St|t r|jrk|r\t#tjtjt j ||j|j}|St#|}|St#tjtj||jz |jz |jz|jz|j|j}|SJd)Nrkr*rY)r/rrrcrlexpsingleminr! nextafterrbr`rmcliproundrarr0)rEr9rrVr:s rrBrB s A!,-DDDu A( ) 55-:DHH[) Exxz2 L/E2<< #CD. L-VV +"5!?@166IAEEQSTSXSXZ[Z`Z`aE* L#xxz# LKaffaffqj)IJ L A ' 55BGGBHHTXXk-B$CQUUAFFST LK( L +"5!?@166IAEEQSTSXSXZ[Z`Z`aE L CBBur)rrGrrHrrHrIz/tuple[np.ndarray, list[np.ndarray], np.ndarray])r: int | floatr9rrrHrIrm)rErmr9rrrHrIru) __future__rrltypingrnumpyr!optuna.distributionsrrrrr r r4rBrRrrrzs" 1820ZZzDG-DG>BDGTXDG4DGN &6GK .$$&6$GK$$r