o
    }oik                     @  s   d dl mZ d dlZd dl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mZ d d	lmZ erLd d
lmZ edeeeef ZdZdZG dd dejZG dd deZeddedG dd deZeddedG dd deZeddedG dd deZ G dd deZ!eddedG dd de!Z"eddedG dd de!Z#dHd%d&Z$G d'd( d(eZ%e!e#e"eeee e%fZ&dId+d,Z'dJd.d/Z(dKd3d4Z)dLd9d:Z*dMd<d=Z+dNd@dAZ,	BdOdPdDdEZ-dQdFdGZ.dS )R    )annotationsN)Real)Any)cast)TYPE_CHECKING)Union)deprecated_classoptuna_warn)Sequencez=Use :class:`~optuna.distributions.FloatDistribution` instead.z;Use :class:`~optuna.distributions.IntDistribution` instead.c                   @  sr   e Zd ZdZdddZejdd	d
ZejdddZejd ddZ	d!ddZ
d"ddZd#ddZd$ddZdS )%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.
    param_value_in_internal_reprfloatreturnr   c                 C  s   |S )a0  Convert 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.
         selfr   r   r   H/home/ubuntu/.local/lib/python3.10/site-packages/optuna/distributions.pyto_external_repr&   s   z!BaseDistribution.to_external_reprparam_value_in_external_reprc                 C     t )a0  Convert 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)r   r   r   r   r   to_internal_repr3   s   z!BaseDistribution.to_internal_reprboolc                 C  r   )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`.
        r   r   r   r   r   singleA   s   	zBaseDistribution.singlec                 C  r   )ag  Test 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`.
        r   r   r   r   r   	_containsL   s   zBaseDistribution._containsdictc                 C  s   | j S N)__dict__r   r   r   r   _asdict[   s   zBaseDistribution._asdictotherc                 C  s.   t |tstS t| t|urdS | j|jkS NF)
isinstancer   NotImplementedtyper    )r   r"   r   r   r   __eq__^   s
   
zBaseDistribution.__eq__intc                 C  s   t | jftt| j  S r   )hash	__class__tuplesortedr    itemsr   r   r   r   __hash__e   s   zBaseDistribution.__hash__strc                 C  s4   d dd t|   D }| jj d| dS )N, c                 s  s"    | ]\}}| d | V  qdS )=Nr   ).0kvr   r   r   	<genexpr>i   s     z,BaseDistribution.__repr__.<locals>.<genexpr>())joinr,   r!   r-   r*   __name__)r   kwargsr   r   r   __repr__h   s    zBaseDistribution.__repr__N)r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r"   r   r   r   )r   r(   )r   r/   )r9   
__module____qualname____doc__r   abcabstractmethodr   r   r   r!   r'   r.   r;   r   r   r   r   r      s    




r   c                   @  s<   e Zd ZdZ	ddddZdddZdddZdddZdS )FloatDistributionaa  A 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`.

    FNlowr   highlogr   stepNone | floatr   Nonec                 C  s   |r
|d ur
t d||krt d|d|d|r*|dkr*t d|d|d|d ur:|dkr:t d|d	d | _|d urLt|||}t|| _t|| _t|| _|| _d S )
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=.)
ValueErrorrI   _adjust_discrete_uniform_highr   rF   rG   rH   r   rF   rG   rH   rI   r   r   r   __init__   s   



zFloatDistribution.__init__c                 C  sb   | j d u r| j| jkS | j| jkrdS tt| j}tt| j}tt| j }|| |k S NT)rI   rF   rG   decimalDecimalr/   )r   rG   rF   rI   r   r   r   r      s   
zFloatDistribution.singler   c                 C  sj   |}| j d u r| j|  ko| jkS   S || j | j  }| j|  ko(| jkn  o4t|t| dk S )Ng:0yE>)rI   rF   rG   absround)r   r   valuer3   r   r   r   r      s
   
0zFloatDistribution._containsr   c              
   C  x   zt |}W n ttfy } z	td| d|d }~ww t|r+td| d| jr:|dkr:td| d|S N'z8' is not a valid type. float-castable value is expected.`z` is invalid value.rO   z)` is invalid value for the case log=True.r   rR   	TypeErrormathisnanrH   r   r   internal_reprer   r   r   r      "   


z"FloatDistribution.to_internal_repr)FN)
rF   r   rG   r   rH   r   rI   rJ   r   rK   r<   r=   )r   r   r   r   )r9   r@   rA   rB   rU   r   r   r   r   r   r   r   rE   m   s    

rE   z3.0.0z6.0.0)textc                      ,   e Zd ZdZd fddZdd
dZ  ZS )UniformDistributiona  A 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``.

    rF   r   rG   r   rK   c                      t  j||dd d d S NFrF   rG   rH   rI   superrU   r   rF   rG   r*   r   r   rU         zUniformDistribution.__init__r   c                 C  $   t | j}|d |d |S NrH   rI   copydeepcopyr    popr   dr   r   r   r!         

zUniformDistribution._asdictrF   r   rG   r   r   rK   r>   r9   r@   rA   rB   rU   r!   __classcell__r   r   rq   r   rj          rj   c                      ri   )LogUniformDistributionaK  A 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``.

    rF   r   rG   r   rK   c                   rk   NTrm   rn   rp   rq   r   r   rU      rr   zLogUniformDistribution.__init__r   c                 C  rs   rt   ru   ry   r   r   r   r!      r{   zLogUniformDistribution._asdictr|   r>   r}   r   r   rq   r   r      r   r   c                      sJ   e Zd ZdZd fdd	ZdddZedddZejdddZ  Z	S )DiscreteUniformDistributionao  A 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.

    rF   r   rG   qr   rK   c                   s   t  j|||d d S )NrF   rG   rI   rn   )r   rF   rG   r   rq   r   r   rU     s   z$DiscreteUniformDistribution.__init__r   c                 C  s,   t | j}|d |d}||d< |S )NrH   rI   r   ru   )r   rz   rI   r   r   r   r!     s
   

z#DiscreteUniformDistribution._asdictc                 C  s   t d| jS )zDiscretization step.

        :class:`~optuna.distributions.DiscreteUniformDistribution` is a subtype of
        :class:`~optuna.distributions.FloatDistribution`.
        This property is a proxy for its ``step`` attribute.
        r   )r   rI   r   r   r   r   r   '  s   zDiscreteUniformDistribution.qr4   c                 C  s
   || _ d S r   )rI   )r   r4   r   r   r   r   1  s   
)rF   r   rG   r   r   r   r   rK   r>   )r   r   )r4   r   r   rK   )
r9   r@   rA   rB   rU   r!   propertyr   setterr~   r   r   rq   r   r      s    
	r   c                   @  sD   e Zd ZdZddddZdddZdddZdddZdddZdS ) 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`.

    F   rF   r(   rG   rH   r   rI   r   rK   c                 C  s   |r
|dkr
t d||krt d|d|d|r*|dk r*t d|d|d|dkr6t d|d	|| _t|| _t|| _t|}t| j|| j| _d S )
Nr   zZSamplers and other components in Optuna only accept step is 1 when `log` argument is True.rL   rM   rN   z2`low >= 1` must hold for `log=True`, but got (low=r   rP   rQ   )rR   rH   r(   rI   rF   _adjust_int_uniform_highrG   rT   r   r   r   rU   T  s   

zIntDistribution.__init__r   r   c                 C  s   t |S r   )r(   r   r   r   r   r   j  s   z IntDistribution.to_external_reprr   c              
   C  r\   r]   r`   rd   r   r   r   r   m  rg   z IntDistribution.to_internal_reprc                 C  s4   | j r	| j| jkS | j| jkrdS | j| j | jk S rV   )rH   rF   rG   rI   r   r   r   r   r   ~  s
   zIntDistribution.singlec                 C  s4   |}| j |  ko| jkn  o|| j  | j dkS Nr   r   )r   r   r[   r   r   r   r     s   0zIntDistribution._containsN)Fr   )
rF   r(   rG   r(   rH   r   rI   r(   r   rK   )r   r   r   r(   )r   r(   r   r   r<   r=   )	r9   r@   rA   rB   rU   r   r   r   r   r   r   r   r   r   6  s    


r   c                      .   e Zd ZdZdd fd	d
ZdddZ  ZS )IntUniformDistributiona  A 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.

    r   rF   r(   rG   rI   r   rK   c                      t  j||d|d d S rl   rn   r   rF   rG   rI   rq   r   r   rU     rr   zIntUniformDistribution.__init__r   c                 C     t | j}|d |S NrH   ru   ry   r   r   r   r!        
zIntUniformDistribution._asdictr   rF   r(   rG   r(   rI   r(   r   rK   r>   r}   r   r   rq   r   r     s    r   c                      r   )IntLogUniformDistributiona  A 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.

    r   rF   r(   rG   rI   r   rK   c                   r   r   rn   r   rq   r   r   rU     rr   z"IntLogUniformDistribution.__init__r   c                 C  r   r   ru   ry   r   r   r   r!     r   z!IntLogUniformDistribution._asdictr   r   r>   r}   r   r   rq   r   r     s    r   value1CategoricalChoiceTypevalue2r   r   c                 C  s@   t | tott| }t |tott|}| |kp|o|S )zA function to check two choices equal considering NaN.

    This function can handle NaNs like np.float32("nan") other than float.
    )r$   r   rb   rc   r   )r   r   value1_is_nanvalue2_is_nanr   r   r   _categorical_choice_equal  s   r   c                   @  sR   e Zd ZdZdddZdddZdddZdddZdddZdddZ	e
jZdS ) CategoricalDistributionaT  A 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.

    choicesSequence[CategoricalChoiceType]r   rK   c                 C  sf   t |dkr
td|D ]}|d ur+t|ttttfs+d| dt|j d}t	| qt
|| _d S )Nr   z0The `choices` must contain one or more elements.zChoices 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 rQ   )lenrR   r$   r   r(   r   r/   r&   r9   r
   r+   r   )r   r   choicemessager   r   r   rU     s   z CategoricalDistribution.__init__r   r   r   c                 C  s   | j t| S r   )r   r(   r   r   r   r   r        z(CategoricalDistribution.to_external_reprr   c                 C  sd   z| j |W S  ty%   t| j D ]\}}t||r"|   Y S qY nw td| d| j  d)Nr^   z	' not in rQ   )r   indexrR   	enumerater   )r   r   r   r   r   r   r   r     s   
z(CategoricalDistribution.to_internal_reprr   c                 C  s   t | jdkS )Nr   )r   r   r   r   r   r   r     r   zCategoricalDistribution.singlec                 C  s&   t |}d|  kot| jk S   S r   )r(   r   r   )r   r   r   r   r   r   r     s   z!CategoricalDistribution._containsr"   r   c                 C  s   t |tstS t || jsdS | j |j krdS | j D ]7\}}|dkrMt|tt||kr6 dS t	|t||D ]\}}t
||sK  dS q>q |t||krW dS q dS )NFr   T)r$   r   r%   r*   r    keysr-   r   getattrzipr   )r   r"   keyr[   r   other_choicer   r   r   r'     s&   

zCategoricalDistribution.__eq__N)r   r   r   rK   )r   r   r   r   )r   r   r   r   r<   r=   r?   )r9   r@   rA   rB   rU   r   r   r   r   r'   r   r.   r   r   r   r   r     s    






r   json_strr/   c                 C  s  t | }d|v r<|d tjkrt|d d |d d< tD ]}|d |jkr2|di |d   S qtd|d  |d dkrHt|d S |d dv r}|d }|d	 }|d
}|dd}|d dkrot||||dS |du rud}t	||||dS td|d  )zDeserialize a distribution in JSON format.

    Args:
        json_str: A JSON-serialized distribution.

    Returns:
        A deserialized distribution.

    name
attributesr   zUnknown distribution class: r&   categorical)r   r(   rF   rG   rI   rH   Fr   )rH   rI   Nr   rm   zUnknown distribution type: r   )
jsonloadsr   r9   r+   DISTRIBUTION_CLASSESrR   getrE   r   )r   	json_dictclsrF   rG   rI   rH   r   r   r   json_to_distribution5  s,   

r   distc                 C  s   t | jj|  dS )zSerialize a distribution to JSON format.

    Args:
        dist: A distribution to be serialized.

    Returns:
        A JSON string of a given distribution.

    )r   r   )r   dumpsr*   r9   r!   )r   r   r   r   distribution_to_jsona  s   r   dist_olddist_newrK   c                 C  s~   | j |j kr
tdt| ttfr$t|ttfsJ | j|jkr$tdt| ts+dS t|ts2dS | |kr=ttjd dS )a!  A function to check compatibility of two distributions.

    It checks whether ``dist_old`` and ``dist_new`` are the same kind of distributions.
    If ``dist_old`` is :class:`~optuna.distributions.CategoricalDistribution`,
    it further checks ``choices`` are the same between ``dist_old`` and ``dist_new``.
    Note that this method is not supposed to be called by library users.

    Args:
        dist_old:
            A distribution previously recorded in storage.
        dist_new:
            A distribution newly added to storage.

    zBCannot set different distribution kind to the same parameter name.zBCannot set different log configuration to the same parameter name.Nz& does not support dynamic value space.)r*   rR   r$   rE   r   rH   r   r9   )r   r   r   r   r    check_distribution_compatibilityo  s   

r   rF   r   rG   rI   c                 C  s   t t|}t t| }t t|}|| }|| t dkrB|}t|| | | }td|  d| d|d|  d| d |S )N0"The distribution is specified by [r0   ] and step=F, but the range is not divisible by `step`. It will be replaced with [].)rW   rX   r/   r   r
   )rF   rG   rI   d_highd_lowd_stepd_rold_highr   r   r   rS     s   rS   r(   c                 C  sT   ||  }|| dkr(|}|| | |  }t d|  d| d|d|  d| d |S )Nr   r   r0   r   r   r   r	   )rF   rG   rI   rr   r   r   r   r     s   r   distribution#int | float | CategoricalChoiceTypec                 C  s8   |   sJ t| ttfr| jS t| tr| jd S J r   )r   r$   rE   r   rF   r   r   r   r   r   r   _get_single_value  s   

r   Fsuppress_warningc                 C  s   t | trt| j| jdd d}nEt | tr t| j| jdd d}n5t | tr1t| j| jd| jd}n$t | trBt	| j| jd| j
d}nt | trSt	| j| jd| j
d}n| }|| krh|sh|  d| d}t|t |S )NFrm   Tz+ is deprecated and internally converted to z3. See https://github.com/optuna/optuna/issues/2941.)r$   rj   rE   rF   rG   r   r   r   r   r   rI   r   r
   FutureWarning)r   r   new_distributionr   r   r   r   -_convert_old_distribution_to_new_distribution  sV   




	
r   c                 C  s   t | ttfr
| jS dS r#   )r$   rE   r   rH   r   r   r   r   _is_distribution_log   s   r   )r   r   r   r   r   r   )r   r/   r   r   )r   r   r   r/   )r   r   r   r   r   rK   )rF   r   rG   r   rI   r   r   r   )rF   r(   rG   r(   rI   r(   r   r(   )r   r   r   r   )F)r   r   r   r   r   r   )r   r   r   r   )/
__future__r   rC   rv   rW   r   rb   numbersr   typingr   r   r   r   optuna._deprecatedr   optuna._warningsr
   collections.abcr   r   r(   r   r/   r   "_float_distribution_deprecated_msg _int_distribution_deprecated_msgABCr   rE   rj   r   r   r   r   r   r   r   r   r   r   r   rS   r   r   r   r   r   r   r   r   <module>   sh    N[7U!
T

,

&

;