L ikddlmZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z ddl m Z ddl m Z ddlZddlmZe rdd lmZe deeeefZd Zd ZGd d ej4ZGddeZeddeGddeZeddeGddeZeddeGddeZGddeZ eddeGdde Z!eddeGdde Z" d*dZ#Gd d!eZ$e e"e!eeeee$fZ%d+d"Z&d,d#Z' d-d$Z(d.d%Z)d/d&Z*d0d'Z+ d1 d2d(Z,d3d)Z-y)4) annotationsN)Real)Any)cast) TYPE_CHECKING)Union)deprecated_class)Sequencez=Use :class:`~optuna.distributions.FloatDistribution` instead.z;Use :class:`~optuna.distributions.IntDistribution` instead.ceZdZdZd dZej d dZej d dZej ddZ ddZ ddZ ddZ dd Z y )BaseDistributionzBase class for distributions. Note that distribution classes are not supposed to be called by library users. They are used by :class:`~optuna.trial.Trial` and :class:`~optuna.samplers` internally. c|S)a0Convert internal representation of a parameter value into external representation. Args: param_value_in_internal_repr: Optuna's internal representation of a parameter value. Returns: Optuna's external representation of a parameter value. selfparam_value_in_internal_reprs Z/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/optuna/distributions.pyto_external_reprz!BaseDistribution.to_external_repr&s ,+ct)a0Convert external representation of a parameter value into internal representation. Args: param_value_in_external_repr: Optuna's external representation of a parameter value. Returns: Optuna's internal representation of a parameter value. NotImplementedError)rparam_value_in_external_reprs rto_internal_reprz!BaseDistribution.to_internal_repr3s "!rct)zTest whether the range of this distribution contains just a single value. Returns: :obj:`True` if the range of this distribution contains just a single value, otherwise :obj:`False`. rrs rsinglezBaseDistribution.singleAs "!rct)agTest if a parameter value is contained in the range of this distribution. Args: param_value_in_internal_repr: Optuna's internal representation of a parameter value. Returns: :obj:`True` if the parameter value is contained in the range of this distribution, otherwise :obj:`False`. rrs r _containszBaseDistribution._containsLs "!rc|jSN)__dict__rs r_asdictzBaseDistribution._asdict[s }}rct|tstSt|t|ury|j|jk(SNF) isinstancer NotImplementedtyper!)rothers r__eq__zBaseDistribution.__eq__^s9%!12! ! :T%[ (}}..rc t|jftt|jj zSr )hash __class__tuplesortedr!itemsrs r__hash__zBaseDistribution.__hash__es0T^^%fT]]5H5H5J.K(LLMMrcdjdt|jjD}|jj d|dS)N, c30K|]\}}|d|yw)=Nr).0kvs r z,BaseDistribution.__repr__..isQ$!QaS!:Qs())joinr.r"r/r,__name__)rkwargss r__repr__zBaseDistribution.__repr__hsHQ&9M9M9O2PQQ..))*!F8155rN)rfloatreturnr)rrr@r?r@boolrr?r@rBr@dictr(rr@rB)r@int)r@str)r< __module__ __qualname____doc__rabcabstractmethodrrrr"r)r0r>rrrr r so ,  " " ""  " "/N6rr cFeZdZdZ d ddZd dZd dZd dZy) FloatDistributionaaA distribution on floats. This object is instantiated by :func:`~optuna.trial.Trial.suggest_float`, and passed to :mod:`~optuna.samplers` in general. .. note:: When ``step`` is not :obj:`None`, if the range :math:`[\mathsf{low}, \mathsf{high}]` is not divisible by :math:`\mathsf{step}`, :math:`\mathsf{high}` will be replaced with the maximum of :math:`k \times \mathsf{step} + \mathsf{low} < \mathsf{high}`, where :math:`k` is an integer. Attributes: low: Lower endpoint of the range of the distribution. ``low`` is included in the range. ``low`` must be less than or equal to ``high``. If ``log`` is :obj:`True`, ``low`` must be larger than 0. high: Upper endpoint of the range of the distribution. ``high`` is included in the range. ``high`` must be greater than or equal to ``low``. log: If ``log`` is :obj:`True`, this distribution is in log-scaled domain. In this case, all parameters enqueued to the distribution must be positive values. This parameter must be :obj:`False` when the parameter ``step`` is not :obj:`None`. step: A discretization step. ``step`` must be larger than 0. This parameter must be :obj:`None` when the parameter ``log`` is :obj:`True`. NcH|r | td||kDrtd|d|d|r|dkrtd|d|d||dkrtd|d d|_|t|||}t||_t||_t||_||_y) Nz9The parameter `step` is not supported when `log` is true.&`low <= high` must hold, but got (low=, high=).z1`low > 0` must hold for `log=True`, but got (low=r#`step > 0` must hold, but got step=.) ValueErrorstep_adjust_discrete_uniform_highr?lowhighlogrrZr[r\rXs r__init__zFloatDistribution.__init__s 4#XY Y :F#x$PRST T 3#:QSFRZUYT[[]^_ _   CdWAFG G  0dDADd DI:$K rc|j|j|jk(S|j|jk(rytjt |j}tjt |j}tjt |j}||z |kSNT)rXrZr[decimalDecimalrH)rr[rZrXs rrzFloatDistribution.singles 99 88tyy( (xx499$??3tyy>2D//#dhh-0C??3tyy>2D3J$& &rc|}|j$|j|cxkxr|jkScS||jz |jz }|j|cxkxr|jkncxrt|t |z dkS)Ng:0yE>)rXrZr[absround)rrvaluer6s rrzFloatDistribution._containssu, 99 88u1 1 11 1!TYY.A88u1 1Pc!eAh,6G&6P Prc t|}tj|rtd|d|j r|dkrtd|d|S#ttf$r}td|d|d}~wwxYwN'z8' is not a valid type. float-castable value is expected.`z` is invalid value.rTz)` is invalid value for the case log=True.r?rW TypeErrormathisnanr\rr internal_repres rrz"FloatDistribution.to_internal_repr !">?M ::m $q!= >>QRS S 88 ,011Z[ I& 01244   AA7"A22A7)FN) rZr?r[r?r\rBrXz None | floatr@NonerArC)rr?r@r?)r<rIrJrKr^rrrrrrrOrOmsK<PT %,0@L 0 'QrrOz3.0.0z6.0.0)textc,eZdZdZdfd ZddZxZS)UniformDistributionaA uniform distribution in the linear domain. This object is instantiated by :func:`~optuna.trial.Trial.suggest_float`, and passed to :mod:`~optuna.samplers` in general. Attributes: low: Lower endpoint of the range of the distribution. ``low`` is included in the range. ``low`` must be less than or equal to ``high``. high: Upper endpoint of the range of the distribution. ``high`` is included in the range. ``high`` must be greater than or equal to ``low``. c,t|||ddyNFrZr[r\rXsuperr^rrZr[r,s rr^zUniformDistribution.__init__ StTBrctj|j}|jd|jd|SNr\rXcopydeepcopyr!poprds rr"zUniformDistribution._asdict/ MM$-- ( e  f rrZr?r[r?r@rtrDr<rIrJrKr^r" __classcell__r,s@rrwrws Crrwc,eZdZdZdfd ZddZxZS)LogUniformDistributionaKA uniform distribution in the log domain. This object is instantiated by :func:`~optuna.trial.Trial.suggest_float` with ``log=True``, and passed to :mod:`~optuna.samplers` in general. Attributes: low: Lower endpoint of the range of the distribution. ``low`` is included in the range. ``low`` must be larger than 0. ``low`` must be less than or equal to ``high``. high: Upper endpoint of the range of the distribution. ``high`` is included in the range. ``high`` must be greater than or equal to ``low``. c,t|||ddyNTrzr{r}s rr^zLogUniformDistribution.__init__ StDArctj|j}|jd|jd|Srrrs rr"zLogUniformDistribution._asdictrrrrDrrs@rrrs BrrcdeZdZdZdfd ZddZeddZejd dZxZ S) DiscreteUniformDistributionaoA discretized uniform distribution in the linear domain. This object is instantiated by :func:`~optuna.trial.Trial.suggest_float` with ``step`` argument, and passed to :mod:`~optuna.samplers` in general. .. note:: If the range :math:`[\mathsf{low}, \mathsf{high}]` is not divisible by :math:`q`, :math:`\mathsf{high}` will be replaced with the maximum of :math:`k q + \mathsf{low} < \mathsf{high}`, where :math:`k` is an integer. Args: low: Lower endpoint of the range of the distribution. ``low`` is included in the range. ``low`` must be less than or equal to ``high``. high: Upper endpoint of the range of the distribution. ``high`` is included in the range. ``high`` must be greater than or equal to ``low``. q: A discretization step. ``q`` must be larger than 0. Attributes: low: Lower endpoint of the range of the distribution. ``low`` is included in the range. high: Upper endpoint of the range of the distribution. ``high`` is included in the range. c*t||||y)NrZr[rXr{)rrZr[qr,s rr^z$DiscreteUniformDistribution.__init__s St!4rctj|j}|jd|jd}||d<|S)Nr\rXrr)rrrXs rr"z#DiscreteUniformDistribution._asdicts: MM$-- ( e uuV}#rc.td|jS)zDiscretization step. :class:`~optuna.distributions.DiscreteUniformDistribution` is a subtype of :class:`~optuna.distributions.FloatDistribution`. This property is a proxy for its ``step`` attribute. r?)rrXrs rrzDiscreteUniformDistribution.q'sGTYY''rc||_yr )rX)rr7s rrzDiscreteUniformDistribution.q1s  r)rZr?r[r?rr?r@rtrD)r@r?)r7r?r@rt) r<rIrJrKr^r"propertyrsetterrrs@rrrs>85((XXrrc:eZdZdZdd dZd dZd dZd dZd dZy)IntDistributiona^A distribution on integers. This object is instantiated by :func:`~optuna.trial.Trial.suggest_int`, and passed to :mod:`~optuna.samplers` in general. .. note:: When ``step`` is not :obj:`None`, if the range :math:`[\mathsf{low}, \mathsf{high}]` is not divisible by :math:`\mathsf{step}`, :math:`\mathsf{high}` will be replaced with the maximum of :math:`k \times \mathsf{step} + \mathsf{low} < \mathsf{high}`, where :math:`k` is an integer. Attributes: low: Lower endpoint of the range of the distribution. ``low`` is included in the range. ``low`` must be less than or equal to ``high``. If ``log`` is :obj:`True`, ``low`` must be larger than or equal to 1. high: Upper endpoint of the range of the distribution. ``high`` is included in the range. ``high`` must be greater than or equal to ``low``. log: If ``log`` is :obj:`True`, this distribution is in log-scaled domain. In this case, all parameters enqueued to the distribution must be positive values. This parameter must be :obj:`False` when the parameter ``step`` is not 1. step: A discretization step. ``step`` must be a positive integer. This parameter must be 1 when the parameter ``log`` is :obj:`True`. c`|r|dk7r td||kDrtd|d|d|r|dkrtd|d|d|dkrtd|d ||_t||_t||_t|}t |j||j|_y) NzZSamplers and other components in Optuna only accept step is 1 when `log` argument is True.rQrRrSz2`low >= 1` must hold for `log=True`, but got (low=rrUrV)rWr\rGrXrZ_adjust_int_uniform_highr[r]s rr^zIntDistribution.__init__Ts 419/  :F#x$PRST T 37RcVS[VZU\\^_` ` 19CdWAFG GI s84y,TXXtTYYG rct|Sr )rGrs rrz IntDistribution.to_external_reprjs/00rc t|}tj|rtd|d|j r|dkrtd|d|S#ttf$r}td|d|d}~wwxYwrhrkros rrz IntDistribution.to_internal_reprmrrrsc|jr|j|jk(S|j|jk(ry|j|jz |jkSr`)r\rZr[rXrs rrzIntDistribution.single~sK 8888tyy( ( 88tyy  DHH$ 11rc|}|j|cxkxr|jkncxr||jz |jzdk(SNrr)rrrfs rrzIntDistribution._containss<,xx5-DII-U54883Ctyy2PTU2UUrN)Fr) rZrGr[rGr\rBrXrGr@rt)rr?r@rG)rrGr@r?rArC) r<rIrJrKr^rrrrrrrrr6s":H,1"2Vrrc.eZdZdZddfd ZddZxZS)IntUniformDistributionaA uniform distribution on integers. This object is instantiated by :func:`~optuna.trial.Trial.suggest_int`, and passed to :mod:`~optuna.samplers` in general. .. note:: If the range :math:`[\mathsf{low}, \mathsf{high}]` is not divisible by :math:`\mathsf{step}`, :math:`\mathsf{high}` will be replaced with the maximum of :math:`k \times \mathsf{step} + \mathsf{low} < \mathsf{high}`, where :math:`k` is an integer. Attributes: low: Lower endpoint of the range of the distribution. ``low`` is included in the range. ``low`` must be less than or equal to ``high``. high: Upper endpoint of the range of the distribution. ``high`` is included in the range. ``high`` must be greater than or equal to ``low``. step: A discretization step. ``step`` must be a positive integer. c,t|||d|yryr{rrZr[rXr,s rr^zIntUniformDistribution.__init__r~rcftj|j}|jd|SNr\rrs rr"zIntUniformDistribution._asdict$ MM$-- ( e rrrZrGr[rGrXrGr@rtrDrrs@rrrs.Crrc.eZdZdZddfd ZddZxZS)IntLogUniformDistributionaA uniform distribution on integers in the log domain. This object is instantiated by :func:`~optuna.trial.Trial.suggest_int`, and passed to :mod:`~optuna.samplers` in general. Attributes: low: Lower endpoint of the range of the distribution. ``low`` is included in the range and must be larger than or equal to 1. ``low`` must be less than or equal to ``high``. high: Upper endpoint of the range of the distribution. ``high`` is included in the range. ``high`` must be greater than or equal to ``low``. step: A discretization step. ``step`` must be a positive integer. c,t|||d|yrr{rs rr^z"IntLogUniformDistribution.__init__rrcftj|j}|jd|Srrrs rr"z!IntLogUniformDistribution._asdictrrrrrDrrs@rrrs"Brrct|txrtjt |}t|txrtjt |}||k(xs|xr|S)zA function to check two choices equal considering NaN. This function can handle NaNs like np.float32("nan") other than float. )r%rrmrnr?)value1value2 value1_is_nan value2_is_nans r_categorical_choice_equalrsUvt,JE&M1JMvt,JE&M1JM f  B-"AMBrcXeZdZdZd dZd dZd dZd dZd dZddZ e jZ y)CategoricalDistributionaTA categorical distribution. This object is instantiated by :func:`~optuna.trial.Trial.suggest_categorical`, and passed to :mod:`~optuna.samplers` in general. Args: choices: Parameter value candidates. ``choices`` must have one element at least. .. note:: Not all types are guaranteed to be compatible with all storages. It is recommended to restrict the types of the choices to :obj:`None`, :class:`bool`, :class:`int`, :class:`float` and :class:`str`. Attributes: choices: Parameter value candidates. ct|dk(r td|D]W}|t|ttt t fr'd|dt|jd}tj|Yt||_ y)Nrz0The `choices` must contain one or more elements.zChoices for a categorical distribution should be a tuple of None, bool, int, float and str for persistent storage but contains z which is of type rV) lenrWr%rBrGr?rHr'r<warningswarnr-choices)rrchoicemessages rr^z CategoricalDistribution.__init__s w<1 OP P 'F!*VdCPS=T*UIh0f1F1F0GqJ  g& 'W~ rc2|jt|Sr )rrGrs rrz(CategoricalDistribution.to_external_reprs||C <=>>rc |jj|S#t$r2t|jD]\}}t ||s|ccYSYnwxYwtd|d|jd)Nriz ' not in rV)rindexrW enumerater)rrrrs rrz(CategoricalDistribution.to_internal_reprs} ! <<%%&BC C !!*4<QRSTTs0AAAAc2t|jdk(S)Nr)rrrs rrzCategoricalDistribution.single s4<< A%%rc^t|}d|cxkxrt|jkScSr)rGrr)rrrs rrz!CategoricalDistribution._containss,01E-C -----rct|tstSt||jsy|jj |jj k7ry|jj D]l\}}|dk(rQt|tt||k7ryt|t||D]\}}t||ry\|t||k7slyy)NFrT) r%r r&r,r!keysr/rgetattrzipr)rr(keyrfr other_choices rr)zCategoricalDistribution.__eq__s%!12! !%0 ==   5>>#6#6#8 8----/ !JCiu:WUC%8!99 ,/wuc7J,K%(FL4V\J$%GE3//  !rN)rzSequence[CategoricalChoiceType]r@rt)rr?r@CategoricalChoiceType)rrr@r?rArCrF) r<rIrJrKr^rrrrr)r r0rrrrrs3* &? U&.& ((Hrrctj|}d|vrh|dtjk(rt |dd|dd<t D]!}|d|jk(s|di|dcSt d|d|ddk(rt|dS|ddvrW|d}|d }|jd }|jd d }|dd k(rt||||S|d}t||||St d|d)zDeserialize a distribution in JSON format. Args: json_str: A JSON-serialized distribution. Returns: A deserialized distribution. name attributesrzUnknown distribution class: r' categorical)r?rGrZr[rXr\Fr?)r\rXrrzzUnknown distribution type: r) jsonloadsrr<r-DISTRIBUTION_CLASSESrWgetrOr)json_str json_dictclsrZr[rXr\s rjson_to_distributionr5sM 8$I  V  7 @ @ @16y7Ny7Y1ZIl #I .' 6C CLL05Y|455 67 &8I7JKLL V  -*9Y+?@ @ v "2 2E"CV$D==(D--u-C G+(d$GG<D&3TsNN6y7H6IJKKrcvtj|jj|j dS)zSerialize a distribution to JSON format. Args: dist: A distribution to be serialized. Returns: A JSON string of a given distribution. )rr)rdumpsr,r<r")dists rdistribution_to_jsonras( ::t~~66dllnU VVrcv|j|jk7r tdt|ttfr,  ""   L"8 9*  """"   L"; <*  """"  (<'0@n !!T V   g}- rcHt|ttfr |jSyr$)r%rOrr\rs r_is_distribution_logrs!,!2O DE r)rrrrr@rB)rrHr@r )rr r@rH)rr rr r@rt)rZr?r[r?rXr?r@r?)rZrGr[rGrXrGr@rG)rr r@z#int | float | CategoricalChoiceType)F)rr rrBr@r )rr r@rB). __future__rrLrrarrmnumbersrtypingrrrrroptuna._deprecatedr collections.abcr rBrGr?rHr"_float_distribution_deprecated_msg _int_distribution_deprecated_msgABCr rOrwrrrrrrrrrrrrYrrrrrrrrs"   /(dD#uc9:D#$a K6swwK6\X(Xv'7)KL+M4'7)KL.M4'7)KL4"34M4nRV&RVj'7)IJ_KB'7)IJK6 C ! C+@ C  CP).P)h )LX W# # *:#  # L$  (#8"888vr