o
    }oi)                     @  s   d dl mZ d dlmZ d dlm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 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ZG dd deZG dd dZdS )    )annotations)Callable)
NamedTupleN)BaseDistribution)CategoricalChoiceType)CategoricalDistribution)FloatDistribution)IntDistribution) _BatchedCategoricalDistributions))_BatchedDiscreteTruncLogNormDistributions)&_BatchedDiscreteTruncNormDistributions)_BatchedDistributions)!_BatchedTruncLogNormDistributions)_BatchedTruncNormDistributions)_MixtureOfProductDistributiong-q=c                   @  s>   e Zd ZU ded< ded< ded< ded< ded< d	ed
< dS )_ParzenEstimatorParametersfloatprior_weightboolconsider_magic_clipconsider_endpointsCallable[[int], np.ndarray]weightsmultivariatezJdict[str, Callable[[CategoricalChoiceType, CategoricalChoiceType], float]]categorical_distance_funcN)__name__
__module____qualname____annotations__ r   r   Y/home/ubuntu/.local/lib/python3.10/site-packages/optuna/samplers/_tpe/parzen_estimator.pyr      s   
 r   c                   @  sn   e Zd Z	d.d/ddZd0ddZd1ddZed2ddZd1ddZd3d d!Z	d4d&d'Z
d5d)d*Zd6d,d-ZdS )7_ParzenEstimatorNobservationsdict[str, np.ndarray]search_spacedict[str, BaseDistribution]
parametersr   predetermined_weightsnp.ndarray | NonereturnNonec                   s    j dk rtd j  d_||d u s$tt|ks$J |d ur*|n jt}tdkr@tdg}nt	| j g}||
  }t| fddtD d_d S )Nr   zAA non-negative value must be specified for prior_weight, but got .      ?c                   s0   g | ]\}} d d |f ||  qS N)_calculate_distributions.0iparamr&   r$   selftransformed_observationsr   r    
<listcomp>H   s    z-_ParzenEstimator.__init__.<locals>.<listcomp>)r   distributions)r   
ValueError_search_space
_transformlen_call_weights_funcr   nparrayappendsumr   	enumerate_mixture_distribution)r4   r"   r$   r&   r'   r   r   r3   r    __init__'   s2   


z_ParzenEstimator.__init__rngnp.random.RandomStatesizeintc                 C  s   | j ||}| |S r-   )rB   sample_untransform)r4   rD   rF   sampledr   r   r    rH   P   s   
z_ParzenEstimator.samplesamples_dict
np.ndarrayc                 C  s   |  |}| j|S r-   )r:   rB   log_pdf)r4   rK   transformed_samplesr   r   r    rM   T   s   
z_ParzenEstimator.log_pdfweights_funcr   nc                 C  s   t | |d | }t |dk r td| dd| d t|dkr;t |dkr;td| dd| d t t |sOtd| d	| d |S )
Nr   z@The `weights` function is not allowed to return negative values z. z*The argument of the `weights` function is r+   z?The `weight` function is not allowed to return all-zero values z+ The argument of the `weights` function is zFThe `weights`function is not allowed to return infinite or NaN values z,. The argument of the `weights` function is )r=   r>   anyr8   r;   r@   allisfinite)rO   rP   wr   r   r    r<   X   s(   



z#_ParzenEstimator._call_weights_funcc                   s   t  fdd| jD jS )Nc                   s   g | ]} | qS r   r   )r0   r2   rK   r   r    r6   p   s    z/_ParzenEstimator._transform.<locals>.<listcomp>)r=   r>   r9   T)r4   rK   r   rU   r    r:   o   s   z_ParzenEstimator._transformsamples_arrayc                   s    fddt | jD S )Nc                   s"   i | ]\}}| d d |f qS r-   r   r/   rW   r   r    
<dictcomp>s      " z1_ParzenEstimator._untransform.<locals>.<dictcomp>)rA   r9   )r4   rW   r   rX   r    rI   r   s   z_ParzenEstimator._untransform
param_namestrr   r   c                 C  s:   t |tr| ||||S t |ttfsJ | |||S r-   )
isinstancer   $_calculate_categorical_distributionsr   r	   "_calculate_numerical_distributions)r4   r"   r[   r$   r&   r   r   r    r.   u   s   
z)_ParzenEstimator._calculate_distributionsr   c                   sX  |j  t }t|dkrttjd|fd| ddS t|d }tj||f|j| d}|t}||jv rtj	|dd\}	}
|j| t
 fd	d
|	D }t||j t| td }t|tj|ddd d tjf  d  | }||
 |d t|< n|tt||f  d7  < |jddd}|t|dkd| }t|S )Nr      r,   )
fill_value)r   shapera   T)return_inversec                   s"   g | ]  fd dD qS )c                   s   g | ]	}  |qS r   r   )r0   c)choices	dist_funcr1   r   r    r6      s    zT_ParzenEstimator._calculate_categorical_distributions.<locals>.<listcomp>.<listcomp>r   )r0   rf   rg   )r1   r    r6      rZ   zI_ParzenEstimator._calculate_categorical_distributions.<locals>.<listcomp>   )axis   )rj   keepdims)rf   r;   r
   r=   fullr   astyperG   r   uniquer>   logexpmaxnewaxisaranger@   where)r4   r"   r[   r$   r&   	n_choices	n_kernelsr   observed_indicesused_indicesrev_indicesdistscoefcat_weightsrow_sumsr   rh   r    r^      s.   


$0z5_ParzenEstimator._calculate_categorical_distributions#FloatDistribution | IntDistributionc                   s  |j |j |jd ur|jd 8  |jd 7  |jr+ttt  d fdd}| }td   gt|  g}|jd u rj|jsat||j |jS t||j |jS |jsxt||j |j|jS t	||j |j|jS )Nrk   r)   rL   c                    s|  j r&d} | ttddtjd       }tjtf|d}nsd   }t|}t|}|| }tjt|d t	d}|d	< ||dd
<  |d
< t
|dd
 |d	d  |dd  |dd
  }js|jd	 dkr|d |d  |d	< |d |d  |d
< |t| d t }  }	jrtd }
  tdd|
  }nt}tt|||	S )Ng?r`   g         rb         ?rk   )dtyper   g      Y@r,   )r   rr   r;   r9   r=   rm   r?   argsortemptyr   maximumr   rc   r   minEPSasarrayclip)SIGMA0_MAGNITUDEsigmasigmasprior_mumus_with_priorsorted_indices
sorted_mussorted_mus_with_endpointssorted_sigmasmaxsigmarw   minsigmahighlowmusr"   r&   r4   r   r    compute_sigmas   s@   
zK_ParzenEstimator._calculate_numerical_distributions.<locals>.compute_sigmasr   )r)   rL   )
r   r   steprp   r=   r?   r   r   r   r   )r4   r"   r$   r&   r   r   r   r   r    r_      s:   



,
z3_ParzenEstimator._calculate_numerical_distributionsr-   )
r"   r#   r$   r%   r&   r   r'   r(   r)   r*   )rD   rE   rF   rG   r)   r#   )rK   r#   r)   rL   )rO   r   rP   rG   r)   rL   )rW   rL   r)   r#   )
r"   rL   r[   r\   r$   r   r&   r   r)   r   )
r"   rL   r[   r\   r$   r   r&   r   r)   r   )r"   rL   r$   r   r&   r   r)   r   )r   r   r   rC   rH   rM   staticmethodr<   r:   rI   r.   r^   r_   r   r   r   r    r!   &   s    
)




$r!   )
__future__r   collections.abcr   typingr   numpyr=   optuna.distributionsr   r   r   r   r	   .optuna.samplers._tpe.probability_distributionsr
   r   r   r   r   r   r   r   r   r!   r   r   r   r    <module>   s&    