o
    i`3                     @   sF   d Z ddlmZmZ ddlZddlZddlmZm	Z	 G dd dZ
dS )z.
Unit test for DIRECT optimization algorithm.
    )assert_allcloseassert_array_lessN)directBoundsc                   @   s  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Ze	j
dddgdd Ze	j
dddgdd Ze	j
dddgdd Ze	j
dddgdd Ze	j
dddge	j
dddgdd Ze	j
dddge	j
dddgd d! Ze	j
d"g d#e	j
dddgd$d% Zd&d' Ze	j
dddgd(d) Ze	j
dddgd*d+ Ze	j
dddgd,d- Ze	j
dddgd.d/ Ze	j
dddge	j
d0g d1d2d3 Ze	j
je	j
dddgd4d5 Ze	j
dddgd6d7 Ze	j
dd8d9gd:d; Ze	j
dd8d9gd<d= Ze	j
d"d8d9gd>d? Ze	j
d@g dAdBdC Ze	j
dDg dAdEdF Z dGdH Z!dIdJ Z"e	j
dKg dLdMdN Z#e	j
dKe$dOd8gdPdQge$e%j& d8gdPe%j&ggdRdS Z'dTdU Z(e	j
ddVdWdWgdXgdYdZ Z)d[S )\
TestDIRECTc                 C   sB   d| _ ddg | _td| _d| _tddgddg| _d| _d S )	Nr      )   r           g      g      @  )		fun_callsbounds_spherenpzerosoptimum_sphere_posoptimum_spherer   bounds_stylinski_tangmaxiter)self r   o/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/scipy/optimize/tests/test_direct.pysetup_method   s   
zTestDIRECT.setup_methodc                 C   s   |  j d7  _ t| S )N   )r   r   squaresumr   xr   r   r   sphere   s   zTestDIRECT.spherec                 C   s"   t |dkr
t dt | S Nr   r   )r   r   ZeroDivisionErrorr   r   r   r   inv   s   zTestDIRECT.invc                 C      t jS N)r   nanr   r   r   r   nan_fun      zTestDIRECT.nan_func                 C   r"   r#   )r   infr   r   r   r   inf_fun"   r&   zTestDIRECT.inf_func                 C   sD   |\}}d|d d|d   d|  |d  d|d   d|   S )Ng      ?r            r   )r   posr   yr   r   r   styblinski_tang%   s   <zTestDIRECT.styblinski_tanglocally_biasedTFc                 C   s   t | j| j|d}t|j| jddd t|j| jddd t	| j}t
|d d df |j t
|j|d d df  |jdt| jd  ksIJ |j| jksQJ |j| jksYJ d S )	Nr/   MbP?rtolatolh㈵>r4   r3   r   r   r   )r   r   r   r   r   r   funr   r   Zasarrayr   nfevlenr   nitr   )r   r/   resZ_boundsr   r   r   test_direct)   s   

zTestDIRECT.test_directc                 C   s   t | j| j|d}dd }t | j| j||d}t|j|j |j|jks&J |j|jks.J |j|jks6J |j|jks>J |j	|j	ksFJ t|j|j |j
|j
ksUJ t|j| jddd t|j	| jddd d S )	Nr0   c                 S   s   d|  } t | }td |S )Nr*   z+DIRECT minimization algorithm callback test)r   r   print)r   dummyr   r   r   callbackG   s   
z1TestDIRECT.test_direct_callback.<locals>.callback)r/   r?   r1   r2   r5   r6   )r   r   r   r   r   r:   r8   statussuccessr7   messager   r   )r   r/   r;   r?   Zres_callbackr   r   r   test_direct_callbackA   s,   




zTestDIRECT.test_direct_callbackc                 C   sJ   ddg }t t t| j||d W d    d S 1 sw   Y  d S Nr   )i
   )boundsr/   )pytestraisesr    r   r!   r   r/   rF   r   r   r   test_exceptiona   s   
"zTestDIRECT.test_exceptionc                 C   s   ddg }t | j||d d S rD   )r   r%   rI   r   r   r   test_nanh   s   

zTestDIRECT.test_nanlen_tolr1   -C6?c                 C   sd   ddg }t | j||d|d}|jdksJ |jsJ t|jtd d| }|j|ks0J d S )Nr   g      $      $@gKH9)rF   rL   vol_tolr/   r+   r
   zjThe side length measure of the hyperrectangle containing the lowest function value found is below len_tol=	r   r   r@   rA   r   r   r   r   rB   )r   rL   r/   rF   r;   rB   r   r   r   test_len_toln   s   


zTestDIRECT.test_len_tolrP   ư>g:0yE>c                 C   sd   ddg }t | j||d|d}|jdksJ |jsJ t|jtd d| }|j|ks0J d S )Nr   rN   r   )rF   rP   rL   r/   r
   z]The volume of the hyperrectangle containing the lowest function value found is below vol_tol=rQ   )r   rP   r/   rF   r;   rB   r   r   r   test_vol_tol|   s   


zTestDIRECT.test_vol_tol
f_min_rtol)r1   r5   gHz>c                 C   sn   d}ddg }t | j||||d}|jdksJ |jsJ |j|d|  k s(J d| d}|j|ks5J d S )N      ?r   )       rO   )rF   f_minrU   r/   r	   z9The best function value found is within a relative error=z$ of the (known) global optimum f_min)r   r   r@   rA   r7   rB   )r   rU   r/   rX   rF   r;   rB   r   r   r   
test_f_min   s   


zTestDIRECT.test_f_minc                 C   s(   t |d | t |d |   S r   )r   r   r   )r   r   abr   r   r   circle_with_args   s   (zTestDIRECT.circle_with_argsc                 C   s<   ddg }t | j|dd|d}t|jtddgdd d S )	Nr*   )rW          @)r   r   i  )argsmaxfunr/   rV   r5   r3   )r   r\   r   r   r   array)r   r/   rF   r;   r   r   r   test_f_circle_with_args   s
   
z"TestDIRECT.test_f_circle_with_argsc                 C   ^   d}t | j| j||d}|jdu sJ |jdksJ |j|ks!J d| }|j|ks-J d S )Nd   )r_   r/   Fr   z:Number of function evaluations done is larger than maxfun=)r   r.   r   rA   r@   r8   rB   )r   r/   r_   resultrB   r   r   r   test_failure_maxfun   s   
zTestDIRECT.test_failure_maxfunc                 C   rc   )NrE   )r   r/   Fr*   z,Number of iterations is larger than maxiter=)r   r.   r   rA   r@   r:   rB   )r   r/   r   re   rB   r   r   r   test_failure_maxiter   s   

zTestDIRECT.test_failure_maxiterc           	      C   s   g d}g d}t g d}tt||}t||}t| j||d}t| j||d}|j|jks3J |j|jks;J |j	|j	ksCJ |j
|j
ksKJ t|j|j t|j|dd d S )N)g      rV         )      g      @      @)ri   rV   r   r0   g{Gz?r`   )r   ra   listzipr   r   r   r8   rB   rA   r:   r   r   )	r   r/   ZlbZubZx_optZ
bounds_oldZ
bounds_newZres_old_boundsZres_new_boundsr   r   r   test_bounds_variants   s"   
zTestDIRECT.test_bounds_variantseps)r5   rM   r1   c                 C   s2   t | j| j|d|d}|jdksJ |jsJ d S )NrS   )rn   rP   r/   r   )r   r.   r   r@   rA   )r   rn   r/   re   r   r   r   test_epsilon   s   
zTestDIRECT.test_epsilonc                 C   s.   dgd }t | j|dd|d}|d usJ d S )N)rh   g      4@rd   i i@B )r_   r   r/   )r   r   r   r/   rF   re   r   r   r   test_no_segmentation_fault   s
   

z%TestDIRECT.test_no_segmentation_faultc                 C   s*   dgd }t | j||d}|d usJ d S )N)rh   rj   r*   r0   )r   r(   rp   r   r   r   test_inf_fun   s
   
zTestDIRECT.test_inf_funr*   c                 C   J   d}t jt|d t| j| j|d W d    d S 1 sw   Y  d S )Nz len_tol must be between 0 and 1.match)rL   rG   rH   
ValueErrorr   r.   r   )r   rL   	error_msgr   r   r   test_len_tol_validation      
"z"TestDIRECT.test_len_tol_validationc                 C   rt   )Nz vol_tol must be between 0 and 1.ru   )rP   rw   )r   rP   ry   r   r   r   test_vol_tol_validation   r{   z"TestDIRECT.test_vol_tol_validationc                 C   sL   d}t jt|d t| j| j|dd W d    d S 1 sw   Y  d S )Nz#f_min_rtol must be between 0 and 1.ru   r   )rU   rX   rw   )r   rU   ry   r   r   r   test_fmin_rtol_validation  s   
"z$TestDIRECT.test_fmin_rtol_validationr_   )g      ?string)r   r*   c                 C   rt   )Nzmaxfun must be of type int.ru   r_   rw   )r   r_   ry   r   r   r   test_maxfun_wrong_type  r{   z!TestDIRECT.test_maxfun_wrong_typer   c                 C   rt   )Nzmaxiter must be of type int.ru   r   rw   )r   r   ry   r   r   r   test_maxiter_wrong_type  r{   z"TestDIRECT.test_maxiter_wrong_typec                 C   J   d}t jt|d t| j| jdd W d    d S 1 sw   Y  d S )Nzmaxiter must be > 0.ru   rs   r   rw   r   ry   r   r   r   test_negative_maxiter     
"z TestDIRECT.test_negative_maxiterc                 C   r   )Nzmaxfun must be > 0.ru   rs   r   rw   r   r   r   r   test_negative_maxfun  r   zTestDIRECT.test_negative_maxfunrF   )rF   r]   r   c                 C   D   d}t jt|d t| j| W d    d S 1 sw   Y  d S )Nz5bounds must be a sequence or instance of Bounds classru   rG   rH   rx   r   r.   r   rF   ry   r   r   r   test_invalid_bounds_type"  s   "z#TestDIRECT.test_invalid_bounds_typeri   r   r   c                 C   r   )Nz#Bounds are not consistent min < maxru   r   r   r   r   r   test_incorrect_bounds)  s   "z TestDIRECT.test_incorrect_boundsc                 C   s\   d}t tj dgdtjg}tjt|d t| j| W d    d S 1 s'w   Y  d S )NzBounds must not be inf.rs   r   ru   )r   r   r'   rG   rH   rx   r   r.   )r   ry   rF   r   r   r   test_inf_bounds3  s
   "zTestDIRECT.test_inf_boundsZbiasr   r]   c                 C   rt   )Nz%locally_biased must be True or False.ru   r0   rw   )r   r/   ry   r   r   r   test_locally_biased_validation9  r{   z)TestDIRECT.test_locally_biased_validationN)*__name__
__module____qualname__r   r   r!   r%   r(   r.   rG   markZparametrizer<   rC   rJ   rK   rR   rT   rY   r\   rb   rf   rg   rm   ro   Zxslowrq   rr   rz   r|   r}   r   r   r   r   r   r   r   r$   r   r   r   r   r   r   r   r      s    	















r   )__doc__Znumpy.testingr   r   rG   numpyr   Zscipy.optimizer   r   r   r   r   r   r   <module>   s    