
    (^ir,                        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Z G d de      Z G d d      Zy)    )annotations)Callable)
NamedTupleN)BaseDistribution)CategoricalChoiceType)CategoricalDistribution)FloatDistribution)IntDistribution) _BatchedCategoricalDistributions)&_BatchedDiscreteTruncNormDistributions)_BatchedDistributions)_BatchedTruncNormDistributions)_MixtureOfProductDistributiong-q=c                  J    e Zd ZU ded<   ded<   ded<   ded<   ded<   d	ed
<   y)_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__     f/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/optuna/samplers/_tpe/parzen_estimator.pyr   r      s*    ((  r    r   c                      e Zd Z	 d	 	 	 	 	 	 	 	 	 ddZddZddZedd       Zedd       ZddZ	ddZ
	 	 	 	 	 	 	 	 	 	 dd	Z	 	 	 	 	 	 	 	 	 	 dd
Z	 	 	 	 	 	 	 	 	 	 	 	 ddZy)_ParzenEstimatorNc                ^   |j                   dk  rt        d|j                    d      || _        | j                  |      }|t	        |      t	        |      k(  sJ ||n$| j                  |j                  t	        |            }t	        |      dk(  rt        j                  dg      }n!t        j                  ||j                   g      }||j                         z  }t        |t        |      D cg c]#  \  }}| j                  |d d |f   |||   |      % c}}      | _        y c c}}w )Nr   zAA non-negative value must be specified for prior_weight, but got .      ?)r   distributions)r   
ValueError_search_space
_transformlen_call_weights_funcr   nparrayappendsumr   	enumerate_calculate_distributions_mixture_distribution)	selfobservationssearch_space
parameterspredetermined_weightstransformed_observationsr   iparams	            r!   __init__z_ParzenEstimator.__init__#   sJ    ""Q&&334A7 
 *#'??<#@ $,4L0MQT!R
 1
 	
 

 %0 "((););SAY=Z[ 	 '(A-hhuoGii**A*A)BCG7;;= %B
 !*, 7	 Au --,QT2E<;NPZ&
"s   2(D)c                \    | j                   j                  ||      }| j                  |      S N)r3   sample_untransform)r4   rngsizesampleds       r!   r?   z_ParzenEstimator.sampleL   s+    ,,33C>  ))r    c                Z    | j                  |      }| j                  j                  |      S r>   )r*   r3   log_pdf)r4   samples_dicttransformed_sampless      r!   rE   z_ParzenEstimator.log_pdfP   s)    "ool;))112EFFr    c                   t        j                   | |            d | }t        j                  |dk        rt        d| dd| dz         t	        |      dkD  r.t        j
                  |      dk  rt        d| dd| dz         t        j                  t        j                  |            st        d| d	| dz         |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.   anyr(   r+   r0   allisfinite)weights_funcnws      r!   r,   z#_ParzenEstimator._call_weights_funcT   s    HH\!_%bq)66!a%=RSTRUUWX>qcCD  q6A:"&&)q.QRSQTTUV?s!DE  vvbkk!n%XCCA3aHI  r    c                J    t        | t        t        f      xr | j                  S r>   )
isinstancer	   r
   log)dists    r!   _is_logz_ParzenEstimator._is_logk   s    $!2O DER$((Rr    c           	         t        j                  | j                  D cg c]=  }| j                  | j                  |         rt        j                  ||         n||   ? c}      j
                  S c c}w r>   )r-   r.   r)   rS   rQ   T)r4   rF   r;   s      r!   r*   z_ParzenEstimator._transformo   ss    xx "//  ||D$6$6u$=> FF<./%e,-	
 !		s   AA1c                X   t        | j                        D ci c]I  \  }}|| j                  | j                  |         rt        j                  |d d |f         n|d d |f   K }}}| j                  j                         D ci c]  \  }}|t        |t              rut        j                  |j                  t        j                  ||   |j                  z
  |j                  z        |j                  z  z   |j                  |j                        n||    c}}S c c}}w c c}}w r>   )r1   r)   rS   r-   expitemsrP   r
   cliplowroundstephigh)r4   samples_arrayr:   r;   resrR   s         r!   r@   z_ParzenEstimator._untransform{   s    &d&8&89
 5 << 2 25 9: }QT*+"1a4()
 
& "&!3!3!9!9!;
   dO4 HHrxxUdhh)>$))(KLtyyXXHHII Z 
 	


s   AD BD&c                6   t        |t              r| j                  ||||      S t        |t        t        f      sJ |j
                  r?t        j
                  |j                        }t        j
                  |j                        }n|j                  }|j                  }|j                  }|X|j
                  rLt        j
                  |j                  |dz  z
        }t        j
                  |j                  |dz  z         }d }| j                  |||||      S )N   )rP   r   $_calculate_categorical_distributionsr	   r
   rQ   r-   rZ   r]   r\   "_calculate_numerical_distributions)r4   r9   
param_namer6   r7   rZ   r]   r\   s           r!   r2   z)_ParzenEstimator._calculate_distributions   s    l$;<<<(*lJ  l->,PQQQff\--.vvl//0"&&#(($$D L$4$4ff\--q89vvl//$(:;::(#tT: r    c                H   |j                   }t        |      }t        |      dk(  r&t        t        j                  d|fd|z              S t        |      dz   }t        j                  ||f|j
                  |z        }|j                  t              }	||j                  v rt        j                  |	d      \  }
}|j                  |   }t        j                  |
D cg c]  }|D cg c]  } |||   |       c} c}}      }t        j                  ||j
                  z        t        j                  |      z  t        j                  d	      z  }t        j                  |t        j                  |d
      d d t        j                  f   z  dz   |z        }||   |d t        |	       n+|t        j                  t        |	            |	fxx   dz  cc<   |j!                  dd      }|t        j"                  |dk(  d|      z  }t        |      S c c}w c c}}w )Nr      r&   )
fill_value)r   shaperg   T)return_inverse   )axisra   )rl   keepdims)choicesr+   r   r-   fullr   astypeintr   uniquer.   rQ   rW   maxnewaxisaranger0   where)r4   r5   rd   r6   r7   rn   	n_choices	n_kernelsr   observed_indicesused_indicesrev_indices	dist_funcr:   cdistscoefcat_weightsrow_sumss                      r!   rb   z5_ParzenEstimator._calculate_categorical_distributions   s    &&L	|!3I3?K  %)	''i(!..:
 (..s3=== )+		2BSW(X%L+"<<ZHIHHS_`aIAyQ7I`aE66)j&=&==>	ARRUWU[U[\]U^^D&&EBFF5q,A!RZZ-,P$PUV#V!WZ^!^_K/:;/GG+c*+,BIIc"2346FFG1LG;;A;5288HM1h77/88 J`s   !	H
*H=H
H
c                    	 |xs d	d 	fd} |       }t        j                  dz   z  g      t        j                  |dz
  	z   z  g      }|t        |      S t        ||      S )Nr   c                    j                   rbd} | t        t              d      dt        j                        dz   z  z  z  z
  z   z  }t	        j
                  t              f|      }n dz   z  }t	        j                  |      }t	        j                  |      }||   }t	        j                  t        |      dz   t              }dz  z
  |d	<   ||dd
 dz  z   |d
<   t	        j                  |dd
 |d	d z
  |dd  |dd
 z
        }j                  s.|j                  d	   dk\  r|d   |d   z
  |d	<   |d   |d   z
  |d
<   |t	        j                  |         d t               }dz
  z   z  }	j                  r*t              dz   }
dz
  z   z  t        dd|
z         z  }nt        }t	        j                   t	        j"                  |||	            S )Ng?rf   g         rh         ?ra   )dtyper   r&   g      Y@)r   rs   r+   r)   r-   ro   r/   argsortemptyr   maximumr   ri   r   minEPSasarrayrY   )SIGMA0_MAGNITUDEsigmasigmasprior_mumus_with_priorsorted_indices
sorted_mussorted_mus_with_endpointssorted_sigmasmaxsigmarx   minsigmar]   rZ   musr5   r7   r4   	step_or_0s               r!   compute_sigmaszK_ParzenEstimator._calculate_numerical_distributions.<locals>.compute_sigmas   s)   &&#& $#l+Q/DC@R@R<SVW<W4XYZczI-/ 
 L(9';N #*-!#3!9!#N!;+N;
,.HHS5H15LTY,Z)/2Y]/B)!,2<)!B/04y1}0D)"- "

-a36OPQRT6UU-ab14MaPR4SS!
 "449R9X9XYZ9[_`9`'@'CF_`aFb'bM!$1"58QRT8UU ""% 'rzz.'ABCVSEVW dSj945H---1	$*y"89Ci<YY::bggfhABBr    r   r&   )return
np.ndarray)r-   r/   r   r   )
r4   r5   rZ   r]   r\   r7   r   r   r   r   s
   ```` `  @@r!   rc   z3_ParzenEstimator._calculate_numerical_distributions   s     IA	*	C *	CX  !iicS4Z0126C4#:	+A$B#CD<1#vsDII9#vsDRVWWr    r>   )
r5   dict[str, np.ndarray]r6   zdict[str, BaseDistribution]r7   r   r8   znp.ndarray | Noner   None)rA   znp.random.RandomStaterB   rq   r   r   )rF   r   r   r   )rL   r   rM   rq   r   r   )rR   r   r   r   )r^   r   r   r   )
r9   r   rd   strr6   r   r7   r   r   r   )
r5   r   rd   r   r6   r   r7   r   r   r   )r5   r   rZ   r   r]   r   r\   zfloat | Noner7   r   r   r   )r   r   r   r<   r?   rE   staticmethodr,   rS   r*   r@   r2   rb   rc   r   r    r!   r#   r#   "   s4    48'
+'
 2'
 /	'

  1'
 
'
R*G  , S S

.",  '	
 / 
>"9 "9 "9 .	"9
 /"9 
"9H@X @X @X 	@X
 @X /@X 
@Xr    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!   <module>r      sP    " $   1 6 8 2 0 [ a P Y X  sX sXr    