o
    i3                     @   sN   d Z ddlmZmZ ddlZddlZddlmZm	Z	 ddl
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   sF   t  | _ddg | _td| _d| _tddgddg| _	d| _
d S )N   )   r           g      g      @  )	threadinglocal	fun_callsbounds_spherenpzerosoptimum_sphere_posoptimum_spherer   bounds_stylinski_tangmaxiter)self r   Z/home/kim/smarthome/.venv/lib/python3.10/site-packages/scipy/optimize/tests/test_direct.pysetup_method   s   

zTestDIRECT.setup_methodc                 C   s2   t | jds
d| j_| j jd7  _t| S )Ncr      )hasattrr   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jksRJ |j| jksZJ d S )	Nr3   MbP?rtolatolh㈵>r8   r7   r   r   r   )r   r"   r   r   r!   r   funr   r   Zasarrayr   nfevlenr   r   nitr   )r   r3   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 )	Nr4   c                 S   s   d|  } t | }td |S )Nr.   z+DIRECT minimization algorithm callback test)r   r   print)r!   dummyr   r   r   callbackJ   s   
z1TestDIRECT.test_direct_callback.<locals>.callback)r3   rC   r5   r6   r9   r:   )r   r"   r   r   r!   r>   r<   statussuccessr;   messager   r   )r   r3   r?   rC   Zres_callbackr   r   r   test_direct_callbackD   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
   )boundsr3   )pytestraisesr$   r   r%   r   r3   rJ   r   r   r   test_exceptiond   s   
"zTestDIRECT.test_exceptionc                 C   s   ddg }t | j||d d S rH   )r   r)   rM   r   r   r   test_nank   s   

zTestDIRECT.test_nanlen_tolr5   -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)rJ   rP   vol_tolr3   r/   r
   zjThe side length measure of the hyperrectangle containing the lowest function value found is below len_tol=	r   r"   rD   rE   r   r!   r   r   rF   )r   rP   r3   rJ   r?   rF   r   r   r   test_len_tolq   s   


zTestDIRECT.test_len_tolrT   ư>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   rR   r   )rJ   rT   rP   r3   r
   z]The volume of the hyperrectangle containing the lowest function value found is below vol_tol=rU   )r   rT   r3   rJ   r?   rF   r   r   r   test_vol_tol   s   


zTestDIRECT.test_vol_tol
f_min_rtol)r5   r9   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   )       rS   )rJ   f_minrY   r3   r	   z9The best function value found is within a relative error=z$ of the (known) global optimum f_min)r   r"   rD   rE   r;   rF   )r   rY   r3   r\   rJ   r?   rF   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.   )r[          @)r   r   i  )argsmaxfunr3   rZ   r9   r7   )r   r`   r   r!   r   array)r   r3   rJ   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   )rc   r3   Fr   z:Number of function evaluations done is larger than maxfun=)r   r2   r   rE   rD   r<   rF   )r   r3   rc   resultrF   r   r   r   test_failure_maxfun   s   
zTestDIRECT.test_failure_maxfunc                 C   rg   )NrI   )r   r3   Fr.   z,Number of iterations is larger than maxiter=)r   r2   r   rE   rD   r>   rF   )r   r3   r   ri   rF   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      rZ         )      g      @      @)rm   rZ   r   r4   g{Gz?rd   )r   re   listzipr   r   r"   r<   rF   rE   r>   r   r!   )	r   r3   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)r9   rQ   r5   c                 C   s2   t | j| j|d|d}|jdksJ |jsJ d S )NrW   )rr   rT   r3   r   )r   r2   r   rD   rE   )r   rr   r3   ri   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)rl   g      4@rh   i i@B )rc   r   r3   )r   r"   r   r3   rJ   ri   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)rl   rn   r.   r4   )r   r,   rt   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)rP   rK   rL   
ValueErrorr   r2   r   )r   rP   	error_msgr   r   r   test_len_tol_validation      
"z"TestDIRECT.test_len_tol_validationc                 C   rx   )Nz vol_tol must be between 0 and 1.ry   )rT   r{   )r   rT   r}   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.ry   r   )rY   r\   r{   )r   rY   r}   r   r   r   test_fmin_rtol_validation  s   
"z$TestDIRECT.test_fmin_rtol_validationrc   )g      ?string)r   r.   c                 C   rx   )Nzmaxfun must be of type int.ry   rc   r{   )r   rc   r}   r   r   r   test_maxfun_wrong_type  r   z!TestDIRECT.test_maxfun_wrong_typer   c                 C   rx   )Nzmaxiter must be of type int.ry   r   r{   )r   r   r}   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.ry   rw   r   r{   r   r}   r   r   r   test_negative_maxiter     
"z TestDIRECT.test_negative_maxiterc                 C   r   )Nzmaxfun must be > 0.ry   rw   r   r{   r   r   r   r   test_negative_maxfun  r   zTestDIRECT.test_negative_maxfunrJ   )rJ   ra   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 classry   rK   rL   r|   r   r2   r   rJ   r}   r   r   r   test_invalid_bounds_type%  s   "z#TestDIRECT.test_invalid_bounds_typerm   r   r   c                 C   r   )Nz#Bounds are not consistent min < maxry   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.rw   r   ry   )r   r   r+   rK   rL   r|   r   r2   )r   r}   rJ   r   r   r   test_inf_bounds6  s
   "zTestDIRECT.test_inf_boundsZbiasr   ra   c                 C   rx   )Nz%locally_biased must be True or False.ry   r4   r{   )r   r3   r}   r   r   r   test_locally_biased_validation<  r   z)TestDIRECT.test_locally_biased_validationN)*__name__
__module____qualname__r   r"   r%   r)   r,   r2   rK   markZparametrizer@   rG   rN   rO   rV   rX   r]   r`   rf   rj   rk   rq   rs   Zxslowru   rv   r~   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   rK   numpyr   Zscipy.optimizer   r   r   r   r   r   r   r   <module>   s    