o
    $i^                  
   @   s  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m	Z	m
Z
mZmZmZmZ d dlZd dlmZmZmZ zejjZdZW n eyT   G dd	 d	Zd
ZY nw e eZe ZdKddZG dd dZ ede eejj!e"f Z!eG dd dZ#eG dd dZ$eG dd de$Z%eG dd de$Z&eG dd de$Z'eG dd de$Z(eG dd de$Z)eG dd de#Z*eG d d! d!e#Z+eG d"d# d#e#Z,eG d$d% d%e#Z-eG d&d' d'e$Z.ed(e	e
gef fd)d*Z/ed+e0d,e0fd-d.Z1ed+e0d,e0d/e0fd0d1Z2eefd+e0d,e0d2efd3d4Z3eefd+e0d,e0d/e0d2efd5d6Z4ed7efd8d9Z5ed+e"d,e"fd:d;Z6eefd+e"d,e"d2efd<d=Z7edLd+e"d,e"d/e"fd?d@Z8eefd+e"d,e"d/e"d2efdAdBZ9edMdEe0dFe0fdGdHZ:edEe0dFe0d/e0fdIdJZ;dS )N    N)copy)	signature)isclose)AnyCallableDictListOptionalSequenceUnion)DeveloperAPI	PublicAPIRayDeprecationWarningFc                   @   s   e Zd ZdS )np_random_generatorN)__name__
__module____qualname__ r   r   S/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/ray/tune/search/sample.pyr      s    r   Treturnc                   C   s   t jdtdd d S )Nz`The `base` argument is deprecated. Please remove it as it is not actually needed in this method.   )
stacklevel)warningswarnr   r   r   r   r   _warn_for_base   s
   
r   c                   @   sh   e Zd ZdZdZ	ddeedejj	e
f  fddZedefdd	Zed
d ZdedefddZdS )_BackwardsCompatibleNumpyRngzhThin wrapper to ensure backwards compatibility between
    new and old numpy randomness generators.
    Ngenerator_or_seedr   c                 C   sL   |d u st |tjjtfr|| _d S trtj|| _d S tj|| _d S N)
isinstancenprandomRandomStater   _rng
LEGACY_RNGdefault_rng)selfr   r   r   r   __init__0   s   

z%_BackwardsCompatibleNumpyRng.__init__r   c                 C   s   t | jt S r   )r   r"   r   r%   r   r   r   
legacy_rng?   s   z'_BackwardsCompatibleNumpyRng.legacy_rngc                 C   s   | j d ur| j S tjS r   )r"   r   r    r'   r   r   r   rngC   s   z _BackwardsCompatibleNumpyRng.rngnamec                 C   s,   | j r|dkr
d}n|dkrd}t| j|S )Nintegersrandintr    rand)r(   getattrr)   )r%   r*   r   r   r   __getattr__H   s   z(_BackwardsCompatibleNumpyRng.__getattr__r   )r   r   r   __doc__r"   r	   r   r   r    r!   intr&   propertyboolr(   r)   strr   r/   r   r   r   r   r   )   s    

r   c                   @   s   e Zd ZdZdZdZdd ZdddZdd	 Z		
	dde	e
ee ef  deddfddZdd Zdd ZdefddZedd ZdS )Domaina  Base class to specify a type and valid range to sample parameters from.

    This base class is implemented by parameter spaces, like float ranges
    (``Float``), integer ranges (``Integer``), or categorical variables
    (``Categorical``). The ``Domain`` object contains information about
    valid values (e.g. minimum and maximum values), and exposes methods that
    allow specification of specific samplers (e.g. ``uniform()`` or
    ``loguniform()``).

    Nc                 C   s   |S )zCast value to domain typer   r%   valuer   r   r   castg      zDomain.castFc                 C   s,   | j r|std| jj| j ||| _ d S )NzmYou can only choose one sampler for parameter domains. Existing sampler for parameter {}: {}. Tried to add {})sampler
ValueErrorformat	__class__r   )r%   r:   allow_overrider   r   r   set_samplerk   s   

zDomain.set_samplerc                 C   s   | j }|s	|  }|S r   )r:   default_sampler_cls)r%   r:   r   r   r   get_samplerv   s   zDomain.get_sampler   configsizerandom_stater!   c                 C   s,   t |ts	t|}|  }|j| |||dS )N)rC   rD   rE   )r   r   rA   sample)r%   rC   rD   rE   r:   r   r   r   rF   |   s   
zDomain.samplec                 C   s   t | jtS r   )r   r:   Gridr'   r   r   r   is_grid   s   zDomain.is_gridc                 C      dS )NFr   r'   r   r   r   is_function      zDomain.is_functionr7   c                 C      t )z8Returns True if `value` is a valid value in this domain.NotImplementedErrorr6   r   r   r   is_valid   r9   zDomain.is_validc                 C   rI   )Nz	(unknown)r   r'   r   r   r   
domain_str   r9   zDomain.domain_str)FNrB   N)r   r   r   r0   r:   r@   r8   r?   rA   r	   r   r   r   r1   rF   rH   rJ   r   rO   r2   rP   r   r   r   r   r5   W   s,    

r5   c                	   @   s>   e Zd Z			d
dedeeee ef  deddfdd	Z	dS )SamplerNrB   domainrC   rD   rE   r!   c                 C   rL   r   rM   r%   rS   rC   rD   rE   r   r   r   rF      s   zSampler.samplerQ   )
r   r   r   r5   r	   r   r   r   r1   rF   r   r   r   r   rR      s    rR   c                   @      e Zd Zdd ZdS )BaseSamplerc                 C   rI   )NBaser   r'   r   r   r   __str__   rK   zBaseSampler.__str__Nr   r   r   rX   r   r   r   r   rV          rV   c                   @   rU   )Uniformc                 C   rI   )Nr[   r   r'   r   r   r   rX      rK   zUniform.__str__NrY   r   r   r   r   r[      rZ   r[   c                   @   s&   e Zd ZefdefddZdd ZdS )
LogUniformbasec                 C   s   |t ur	t  d S d S r   )_MISSINGr   )r%   r]   r   r   r   r&      s   
zLogUniform.__init__c                 C   rI   )Nr\   r   r'   r   r   r   rX      rK   zLogUniform.__str__N)r   r   r   r^   objectr&   rX   r   r   r   r   r\      s    r\   c                   @   s(   e Zd Zd	dedefddZdd ZdS )
Normal        meansdc                 C   s"   || _ || _| jdksJ dd S )Nr   z$SD has to be strictly greater than 0rb   rc   )r%   rb   rc   r   r   r   r&      s   zNormal.__init__c                 C   rI   )Nr`   r   r'   r   r   r   rX      rK   zNormal.__str__N)ra   ra   )r   r   r   floatr&   rX   r   r   r   r   r`      s    r`   c                	   @   sB   e Zd ZdZ			ddedeeee ef  de	ddfd	d
Z
dS )rG   z"Dummy sampler used for grid searchNrB   rS   rC   rD   rE   r!   c                 C   s   t dS )NzDo not call `sample()` on grid.)RuntimeErrorrT   r   r   r   rF         zGrid.samplerQ   )r   r   r   r0   r5   r	   r   r   r   r1   rF   r   r   r   r   rG      s    rG   c                   @   s   e Zd ZG dd deZG dd deZG dd deZeZ	de
e de
e fd	d
Zdd Zdd ZefdefddZdddZdefddZdefddZedd ZdS ) Floatc                	   @   >   e Zd Z			ddddeeee ef  deddfd	d
ZdS )zFloat._UniformNrB   rS   rh   rC   rD   rE   r!   c                 C   sp   t |ts	t|}|jtdksJ d|jtdk sJ d|j|j|j|d}t|dkr1|S ||d S )N-infzUniform needs a lower boundinfzUniform needs a upper boundrD   rB   r   )r   r   lowerre   upperuniformlenr8   r%   rS   rC   rD   rE   itemsr   r   r   rF      s   
zFloat._Uniform.samplerQ   	r   r   r   r	   r   r   r   r1   rF   r   r   r   r   _Uniform       rt   c                	   @   ri   )zFloat._LogUniformNrB   rS   rh   rC   rD   rE   r!   c                 C   s   t |ts	t|}|jdksJ dd|j  k r"tdk s'J d J dt|j}t|j}t|j|||d}t	|dkrF|S |
|d S Nr   z-LogUniform needs a lower bound greater than 0rk   z-LogUniform needs a upper bound greater than 0rl   rB   )r   r   rm   rn   re   r   logexpro   rp   r8   r%   rS   rC   rD   rE   logminlogmaxrr   r   r   r   rF      s   
zFloat._LogUniform.samplerQ   rs   r   r   r   r   _LogUniform   ru   r|   c                	   @   ri   )zFloat._NormalNrB   rS   rh   rC   rD   rE   r!   c                 C   s|   t |ts	t|}|jr|jtdksJ d|jr%|jtdks%J d|j| j| j|d}t|dkr7|S |	|d S )Nrj   z3Normal sampling does not allow a lower value bound.rk   z3Normal sampling does not allow a upper value bound.rl   rB   r   )
r   r   rm   re   rn   normalrb   rc   rp   r8   rq   r   r   r   rF      s   
zFloat._Normal.samplerQ   rs   r   r   r   r   _Normal   ru   r~   rm   rn   c                 C   s6   |d ur|nt d| _|d ur|| _d S t d| _d S )Nrj   rk   )re   rm   rn   r%   rm   rn   r   r   r   r&     s    zFloat.__init__c                 C      t |S r   )re   r6   r   r   r   r8        z
Float.castc                 C   sF   | j tdkstd| jtdk stdt| }||   |S )Nrj   zTUniform requires a lower bound. Make sure to set the `lower` parameter of `Float()`.rk   zTUniform requires a upper bound. Make sure to set the `upper` parameter of `Float()`.)rm   re   r;   rn   r   r?   rt   r%   newr   r   r   ro     s   zFloat.uniformr]   c                 C   r   |t urt  | jdkstd| j dd| j  k r"tdk s,n td| j dt| }||   |S Nr   z6LogUniform requires a lower bound greater than 0.Got: zg. Did you pass a variable that has been log-transformed? If so, pass the non-transformed value instead.rk   z7LogUniform requires a upper bound greater than 0. Got: 	r^   r   rm   r;   rn   re   r   r?   r|   r%   r]   r   r   r   r   
loguniform#  "   
zFloat.loguniformra         ?c                 C   s   t | }|| || |S r   )r   r?   r~   )r%   rb   rc   r   r   r   r   r}   8  s   zFloat.normalqc                 C   s   | j tdkr t| j | t| j | s td| j  d| d| jtdk r@t| j| t| j| s@td| j d| dt| }|jt|	 |dd |S )	Nrj   zYour lower variable bound z) is not divisible by quantization factor .rk   zYour upper variable bound Tr>   )
rm   re   r   roundr;   rn   r   r?   	QuantizedrA   r%   r   r   r   r   r   	quantized=  s&   

zFloat.quantizedr7   c                 C      | j |  ko| jkS   S r   rm   rn   r6   r   r   r   rO   Q     zFloat.is_validc                 C      d| j  d| j dS N(z, )r   r'   r   r   r   rP   T     zFloat.domain_strNra   r   )r   r   r   r[   rt   r\   r|   r`   r~   r@   r	   re   r&   r8   ro   r^   r_   r   r}   r   rO   r2   rP   r   r   r   r   rh      s    
rh   c                   @   s   e Zd ZG dd deZG dd deZeZdd Zdd Z	d	e
fd
dZdd ZefdefddZde
fddZedd ZdS )Integerc                	   @   ri   )zInteger._UniformNrB   rS   r   rC   rD   rE   r!   c                 C   sD   t |ts	t|}|j|j|j|d}t|dkr|S ||d S )Nrl   rB   r   )r   r   r+   rm   rn   rp   r8   rq   r   r   r   rF   \  s   
zInteger._Uniform.samplerQ   rs   r   r   r   r   rt   [  ru   rt   c                	   @   ri   )zInteger._LogUniformNrB   rS   r   rC   rD   rE   r!   c                 C   s   t |ts	t|}|jdksJ dd|j  k r"tdk s'J d J dt|j}t|j}t|j|||d}t	|
t}t|dkrN|S ||d S rv   )r   r   rm   rn   re   r   rw   rx   ro   floorastyper1   rp   r8   ry   r   r   r   rF   i  s   
zInteger._LogUniform.samplerQ   rs   r   r   r   r   r|   h  ru   r|   c                 C   s   || _ || _d S r   r   r   r   r   r   r&     s   
zInteger.__init__c                 C   r   r   )r1   r6   r   r   r   r8     r   zInteger.castr   c                 C   s$   t | }|jt| |dd |S )NTr   )r   r?   r   rA   r   r   r   r   r     s   zInteger.quantizedc                 C      t | }||   |S r   r   r?   rt   r   r   r   r   ro        zInteger.uniformr]   c                 C   r   r   r   r   r   r   r   r     r   zInteger.loguniformr7   c                 C   r   r   r   r6   r   r   r   rO     r   zInteger.is_validc                 C   r   r   r   r'   r   r   r   rP     r   zInteger.domain_strN)r   r   r   r[   rt   r\   r|   r@   r&   r8   r1   r   ro   r^   r_   r   rO   r2   rP   r   r   r   r   r   Y  s    r   c                   @   sh   e Zd ZG dd deZeZdefddZdd Zdd	 Z	d
d Z
dd ZdefddZedd ZdS )Categoricalc                	   @   ri   )zCategorical._UniformNrB   rS   r   rC   rD   rE   r!   c                    s^   t |ts	t|}|jtdt j|d} fdd|D }t|dkr(|S  |d S )Nr   rl   c                    s   g | ]} j | qS r   
categories).0indexrS   r   r   
<listcomp>  s    z/Categorical._Uniform.sample.<locals>.<listcomp>rB   )r   r   choicer   arangerp   r   r8   )r%   rS   rC   rD   rE   indicesrr   r   r   r   rF     s   
zCategorical._Uniform.samplerQ   rs   r   r   r   r   rt     ru   rt   r   c                 C   s   t || _d S r   )listr   )r%   r   r   r   r   r&     s   zCategorical.__init__c                 C   r   r   r   r   r   r   r   ro     r   zCategorical.uniformc                 C   s   t | }|t  |S r   )r   r?   rG   r   r   r   r   grid  s   zCategorical.gridc                 C   s
   t | jS r   )rp   r   r'   r   r   r   __len__     
zCategorical.__len__c                 C   s
   | j | S r   r   )r%   itemr   r   r   __getitem__  r   zCategorical.__getitem__r7   c                 C   s
   || j v S r   r   r6   r   r   r   rO     r   zCategorical.is_validc                 C   s   | j  S r   r   r'   r   r   r   rP     s   zCategorical.domain_strN)r   r   r   r[   rt   r@   r
   r&   ro   r   r   r   r   rO   r2   rP   r   r   r   r   r     s    r   c                   @   sP   e Zd ZG dd deZeZdefddZdd Zde	fd	d
Z
edd ZdS )Functionc                	   @   sX   e Zd Zdddeeef fddZ			ddddeee	e ef  de
d	d
fddZdS )zFunction._CallSamplerrS   r   rC   c              	   C   sP   z| |W S  ttfy'   ddlm} | |d|i}td | Y S w )Nr   )_UnresolvedAccessGuardrC   z~sample_from functions that take a spec dict are deprecated. Please update your function to work with the config dict directly.)funcAttributeErrorKeyError!ray.tune.search.variant_generatorr   loggerwarning)r%   rS   rC   r   rr   r   r   __try_fn  s   zFunction._CallSampler.__try_fnNrB   rD   rE   r!   c                    sh   t |ts	t|}jr fddt|D }nfddt|D }t|dkr-|S |d S )Nc                    s2   g | ]}t  tr | n qS r   )r   r   _CallSampler__try_fnr   irC   rS   r%   r   r   r     s    
z0Function._CallSampler.sample.<locals>.<listcomp>c                    s   g | ]}   qS r   r   r   r   r   r   r     s    rB   r   )r   r   pass_configrangerp   r8   rq   r   r   r   rF     s   
	zFunction._CallSampler.samplerQ   )r   r   r   r   r4   r   r   r	   r   r   r1   rF   r   r   r   r   _CallSampler  s    r   r   c              
   C   sv   t |}d}z|i  W n ty   d}Y nw |s3z|  W n ty2 } ztd|d }~ww || _|| _d S )NTFz]The function passed to a `Function` parameter must be callable with either 0 or 1 parameters.)r   bind	TypeErrorr;   r   r   )r%   r   sigr   excr   r   r   r&     s(   
zFunction.__init__c                 C   rI   NTr   r'   r   r   r   rJ     rK   zFunction.is_functionr7   c                 C   rI   r   r   r6   r   r   r   rO      rK   zFunction.is_validc                 C   s   | j  dS )Nz()r   r'   r   r   r   rP   #  s   zFunction.domain_strN)r   r   r   rV   r   r@   r   r&   rJ   r   rO   r2   rP   r   r   r   r   r     s    &r   c                	   @   s`   e Zd Zdedeeef fddZdd Z			dd	e	d
e
eee ef  deddfddZdS )r   r:   r   c                 C   s   || _ || _| j sJ dd S )Nz&Quantized() expects a sampler instance)r:   r   )r%   r:   r   r   r   r   r&   *  s   zQuantized.__init__c                 C   s   | j S r   )r:   r'   r   r   r   rA   0  s   zQuantized.get_samplerNrB   rS   rC   rD   rE   r!   c                 C   s   t |ts	t|}| jdkr| jj||||dS t|}t|j| j | j |_t	|j
| j | j |_
| jj||||d}tt|| j| j }t |tjsX||S t|S )NrB   )rE   )r   r   r   r:   rF   r   r   ceilrm   r   rn   r   dividendarrayr8   r   )r%   rS   rC   rD   rE   quantized_domainvaluesr   r   r   r   rF   3  s   


zQuantized.samplerQ   )r   r   r   rR   r   re   r1   r&   rA   r5   r	   r   r   rF   r   r   r   r   r   (  s    r   r   c                 C   s   t | S )zSpecify that tune should sample configuration values from this function.

    Arguments:
        func: An callable function to draw a sample from.
    )r   r   r   r   r   sample_fromM  rg   r   rm   rn   c                 C      t | | S )zSample a float value uniformly between ``lower`` and ``upper``.

    Sampling from ``tune.uniform(1, 10)`` is equivalent to sampling from
    ``np.random.uniform(1, 10))``

    )rh   ro   r   r   r   r   ro   W  s   ro   r   c                 C      t | | |S )a>  Sample a quantized float value uniformly between ``lower`` and ``upper``.

    Sampling from ``tune.uniform(1, 10)`` is equivalent to sampling from
    ``np.random.uniform(1, 10))``

    The value will be quantized, i.e. rounded to an integer increment of ``q``.
    Quantization makes the upper bound inclusive.

    )rh   ro   r   rm   rn   r   r   r   r   quniformb  s   r   r]   c                 C      |t urt  t| | S )zSugar for sampling in different orders of magnitude.

    Args:
        lower: Lower boundary of the output interval (e.g. 1e-4)
        upper: Upper boundary of the output interval (e.g. 1e-2)

    )r^   r   rh   r   rm   rn   r]   r   r   r   r   p  s   	r   c                 C   "   |t urt  t| | |S )a  Sugar for sampling in different orders of magnitude.

    The value will be quantized, i.e. rounded to an integer increment of ``q``.

    Quantization makes the upper bound inclusive.

    Args:
        lower: Lower boundary of the output interval (e.g. 1e-4)
        upper: Upper boundary of the output interval (e.g. 1e-2)
        q: Quantization number. The result will be rounded to an
            integer increment of this value.

    )r^   r   rh   r   r   rm   rn   r   r]   r   r   r   qloguniform~     r   r   c                 C   s   t |  S )zSample a categorical value.

    Sampling from ``tune.choice([1, 2])`` is equivalent to sampling from
    ``np.random.choice([1, 2])``

    )r   ro   r   r   r   r   r     s   r   c                 C   r   )a  Sample an integer value uniformly between ``lower`` and ``upper``.

    ``lower`` is inclusive, ``upper`` is exclusive.

    Sampling from ``tune.randint(10)`` is equivalent to sampling from
    ``np.random.randint(10)``

    .. versionchanged:: 1.5.0
        When converting Ray Tune configs to searcher-specific search spaces,
        the lower and upper limits are adjusted to keep compatibility with
        the bounds stated in the docstring above.

    )r   ro   r   r   r   r   r,     s   r,   c                 C   r   )aj  Sample an integer value log-uniformly between ``lower`` and ``upper``.

    ``lower`` is inclusive, ``upper`` is exclusive.

    .. versionchanged:: 1.5.0
        When converting Ray Tune configs to searcher-specific search spaces,
        the lower and upper limits are adjusted to keep compatibility with
        the bounds stated in the docstring above.

    )r^   r   r   r   r   r   r   r   
lograndint  s   r   rB   c                 C   r   )a  Sample an integer value uniformly between ``lower`` and ``upper``.

    ``lower`` is inclusive, ``upper`` is also inclusive (!).

    The value will be quantized, i.e. rounded to an integer increment of ``q``.
    Quantization makes the upper bound inclusive.

    .. versionchanged:: 1.5.0
        When converting Ray Tune configs to searcher-specific search spaces,
        the lower and upper limits are adjusted to keep compatibility with
        the bounds stated in the docstring above.

    )r   ro   r   r   r   r   r   qrandint  s   r   c                 C   r   )a  Sample an integer value log-uniformly between ``lower`` and ``upper``.

    ``lower`` is inclusive, ``upper`` is also inclusive (!).

    The value will be quantized, i.e. rounded to an integer increment of ``q``.
    Quantization makes the upper bound inclusive.

    .. versionchanged:: 1.5.0
        When converting Ray Tune configs to searcher-specific search spaces,
        the lower and upper limits are adjusted to keep compatibility with
        the bounds stated in the docstring above.

    )r^   r   r   r   r   r   r   r   r   qlograndint  r   r   ra   r   rb   rc   c                 C   s   t dd| |S )zSample a float value normally with ``mean`` and ``sd``.

    Args:
        mean: Mean of the normal distribution. Defaults to 0.
        sd: SD of the normal distribution. Defaults to 1.

    N)rh   r}   rd   r   r   r   randn  s   	r   c                 C   s   t dd| ||S )aa  Sample a float value normally with ``mean`` and ``sd``.

    The value will be quantized, i.e. rounded to an integer increment of ``q``.

    Args:
        mean: Mean of the normal distribution.
        sd: SD of the normal distribution.
        q: Quantization number. The result will be rounded to an
            integer increment of this value.

    N)rh   r}   r   )rb   rc   r   r   r   r   qrandn  s   r   )r   N)rB   r   )<loggingr   r   inspectr   mathr   typingr   r   r   r   r	   r
   r   numpyr   ray.util.annotationsr   r   r   r    	Generatorr   r#   r   	getLoggerr   r   r_   r^   r   r   r!   r1   r5   rR   rV   r[   r\   r`   rG   rh   r   r   r   r   r   re   ro   r   r   r   r   r,   r   r   r   r   r   r   r   r   r   <module>   s    $

)>	 S0I$	
 
 