o
    /win                     @  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Zd dlmZ erJd 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"dGd$d%Z#G d&d' d'eZ$e e"e!eeeee$fZ%dHd*d+Z&dId-d.Z'dJd2d3Z(dKd8d9Z)dLd;d<Z*dMd?d@Z+	AdNdOdCdDZ,dPdEdFZ-dS )Q    )annotationsN)Real)Any)cast)TYPE_CHECKING)Union)deprecated_class)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   Q/home/ubuntu/sommelier/.venv/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  s0   d dd t|   D }d| jj|S )Nz, c                 s  s     | ]\}}d  ||V  qdS )z{}={}N)format).0kvr   r   r   	<genexpr>i   s    z,BaseDistribution.__repr__.<locals>.<genexpr>z{}({}))joinr*   r   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-   )r4   
__module____qualname____doc__r   abcabstractmethodr   r   r   r   r%   r,   r6   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|||r$|dkr$t d|||d ur3|dkr3t d|d | _|d urEt|||}t|| _t|| _t|| _|| _d S )Nz9The parameter `step` is not supported when `log` is true.TThe `low` value must be smaller than or equal to the `high` value (low={}, high={}).        zOThe `low` value must be larger than 0 for a log distribution (low={}, high={}).r   >The `step` value must be non-zero positive value, but step={}.)
ValueErrorr.   rD   _adjust_discrete_uniform_highr   rA   rB   rC   r   rA   rB   rC   rD   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)rD   rA   rB   decimalDecimalr-   )r   rB   rA   rD   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>)rD   rA   rB   absround)r   r   valuer0   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.rH   z)` is invalid value for the case log=True.r   rJ   	TypeErrormathisnanrC   r   r   internal_reprer   r   r   r      "   


z"FloatDistribution.to_internal_repr)FN)
rA   r   rB   r   rC   r   rD   rE   r   rF   r7   r8   )r   r   r   r   )r4   r;   r<   r=   rM   r   r   r   r   r   r   r   r@   m   s    
 
r@   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``.

    rA   r   rB   r   rF   c                      t  j||dd d d S NFrA   rB   rC   rD   superrM   r   rA   rB   r(   r   r   rM         zUniformDistribution.__init__r   c                 C  $   t | j}|d |d |S NrC   rD   copydeepcopyr   popr   dr   r   r   r         

zUniformDistribution._asdictrA   r   rB   r   r   rF   r9   r4   r;   r<   r=   rM   r   __classcell__r   r   ri   r   rb          rb   c                      ra   )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``.

    rA   r   rB   r   rF   c                   rc   NTre   rf   rh   ri   r   r   rM      rj   zLogUniformDistribution.__init__r   c                 C  rk   rl   rm   rq   r   r   r   r      rs   zLogUniformDistribution._asdictrt   r9   ru   r   r   ri   r   rx      rw   rx   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.

    rA   r   rB   qr   rF   c                   s   t  j|||d d S )NrA   rB   rD   rf   )r   rA   rB   r{   ri   r   r   rM   $  s   z$DiscreteUniformDistribution.__init__r   c                 C  s,   t | j}|d |d}||d< |S )NrC   rD   r{   rm   )r   rr   rD   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   rD   r   r   r   r   r{   /  s   zDiscreteUniformDistribution.qr1   c                 C  s
   || _ d S r   )rD   )r   r1   r   r   r   r{   9  s   
)rA   r   rB   r   r{   r   r   rF   r9   )r   r   )r1   r   r   rF   )
r4   r;   r<   r=   rM   r   propertyr{   setterrv   r   r   ri   r   rz     s    
	rz   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   rA   r&   rB   rC   r   rD   r   rF   c                 C  s   |r
|dkr
t d||krt d|||r$|dk r$t d|||dkr/t 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.rG   z\The `low` value must be equal to or greater than 1 for a log distribution (low={}, high={}).r   rI   )rJ   r.   rC   r&   rD   rA   _adjust_int_uniform_highrB   rL   r   r   r   rM   \  s.   

zIntDistribution.__init__r   r   c                 C  s   t |S r   )r&   r   r   r   r   r   z  s   z IntDistribution.to_external_reprr   c              
   C  rT   rU   rX   r\   r   r   r   r   }  r_   z IntDistribution.to_internal_reprc                 C  s4   | j r	| j| jkS | j| jkrdS | j| j | jk S rN   )rC   rA   rB   rD   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   rS   r   r   r   r     s   0zIntDistribution._containsN)Fr   )
rA   r&   rB   r&   rC   r   rD   r&   r   rF   )r   r   r   r&   )r   r&   r   r   r7   r8   )	r4   r;   r<   r=   rM   r   r   r   r   r   r   r   r   r   >  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   rA   r&   rB   rD   r   rF   c                      t  j||d|d d S rd   rf   r   rA   rB   rD   ri   r   r   rM     rj   zIntUniformDistribution.__init__r   c                 C     t | j}|d |S NrC   rm   rq   r   r   r   r        
zIntUniformDistribution._asdictr   rA   r&   rB   r&   rD   r&   r   rF   r9   ru   r   r   ri   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   rA   r&   rB   rD   r   rF   c                   r   ry   rf   r   ri   r   r   rM     rj   z"IntLogUniformDistribution.__init__r   c                 C  r   r   rm   rq   r   r   r   r     r   z!IntLogUniformDistribution._asdictr   r   r9   ru   r   r   ri   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   rZ   r[   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   rF   c                 C  sb   t |dkr
td|D ]}|d ur)t|ttttfs)d|t|j	}t
| qt|| _d S )Nr   z0The `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 {} which is of type {}.)lenrJ   r"   r   r&   r   r-   r.   r$   r4   warningswarnr)   r   )r   r   choicemessager   r   r   rM     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)NrV   z	' not in .)r   indexrJ   	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    keyrS   r   other_choicer   r   r   r%   #  s&   

zCategoricalDistribution.__eq__N)r   r   r   rF   )r   r   r   r   )r   r   r   r   r7   r8   r:   )r4   r;   r<   r=   rM   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&   rA   rB   rD   rC   Fr   )rC   rD   Nr   re   zUnknown distribution type: {}r   )jsonloadsr   r4   r)   DISTRIBUTION_CLASSESrJ   r.   getr@   r   )r   	json_dictclsrA   rB   rD   rC   r   r   r   json_to_distributionE  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(   r4   r   )r   r   r   r   distribution_to_jsonq  s   r   dist_olddist_newrF   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(   rJ   r"   r@   r   rC   r   r4   )r   r   r   r   r    check_distribution_compatibility  s   

r   rA   r   rB   rD   c                 C  sx   t t|}t t| }t t|}|| }|| t dkr:|}t|| | | }tdj| |||d |S )N0The distribution is specified by [{low}, {old_high}] and step={step}, but the range is not divisible by `step`. It will be replaced by [{low}, {high}].rA   old_highrB   rD   )rO   rP   r-   r   r   r   r.   )rA   rB   rD   d_highd_lowd_stepd_rr   r   r   r   rK     s   rK   r&   c                 C  sD   ||  }|| dkr |}|| | |  }t dj| |||d |S )Nr   r   r   )r   r   r.   )rA   rB   rD   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"   r@   r   rA   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ri|si|  d| d}t|t |S )NFre   Tz+ is deprecated and internally converted to z3. See https://github.com/optuna/optuna/issues/2941.)r"   rb   r@   rA   rB   rx   rz   r{   r   r   rD   r   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"   r@   r   rC   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   rF   )rA   r   rB   r   rD   r   r   r   )rA   r&   rB   r&   rD   r&   r   r&   )r   r
   r   r   )F)r   r
   r   r   r   r
   )r   r
   r   r   ).
__future__r   r>   rn   rO   r   rZ   numbersr   typingr   r   r   r   r   optuna._deprecatedr   collections.abcr	   r   r&   r   r-   r   "_float_distribution_deprecated_msg _int_distribution_deprecated_msgABCr
   r@   rb   rx   rz   r   r   r   r   r   r   r   r   r   rK   r   r   r   r   r   r   r   r   <module>   sh    Nc7]!
T

,

&

;