o
    g`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   ^/home/ubuntu/cloudmapper/venv/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   asarrayr   nfevlenr   nitr   )r   r/   res_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/   rA   r1   r2   r5   r6   )r   r   r   r   r   r;   r9   statussuccessr7   messager   r   )r   r/   r<   rA   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/   rI   r   r   r   test_exceptiona   s   
"zTestDIRECT.test_exceptionc                 C   s   ddg }t | j||d d S rG   )r   r%   rL   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)rI   rO   vol_tolr/   r+   r
   zjThe side length measure of the hyperrectangle containing the lowest function value found is below len_tol=	r   r   rB   rC   r   r   r   r   rD   )r   rO   r/   rI   r<   rD   r   r   r   test_len_toln   s   


zTestDIRECT.test_len_tolrS   ư>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   rQ   r   )rI   rS   rO   r/   r
   z]The volume of the hyperrectangle containing the lowest function value found is below vol_tol=rT   )r   rS   r/   rI   r<   rD   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   )       rR   )rI   f_minrX   r/   r	   z9The best function value found is within a relative error=z$ of the (known) global optimum f_min)r   r   rB   rC   r7   rD   )r   rX   r/   r[   rI   r<   rD   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*   )rZ          @)r   r   i  )argsmaxfunr/   rY   r5   r3   )r   r_   r   r   r   array)r   r/   rI   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   )rb   r/   Fr   z:Number of function evaluations done is larger than maxfun=)r   r.   r   rC   rB   r9   rD   )r   r/   rb   resultrD   r   r   r   test_failure_maxfun   s   
zTestDIRECT.test_failure_maxfunc                 C   rf   )NrH   )r   r/   Fr*   z,Number of iterations is larger than maxiter=)r   r.   r   rC   rB   r;   rD   )r   r/   r   rh   rD   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      rY         )      g      @      @)rl   rY   r   r0   g{Gz?rc   )r   rd   listzipr   r   r   r9   rD   rC   r;   r   r   )	r   r/   lbubx_opt
bounds_old
bounds_newres_old_boundsres_new_boundsr   r   r   test_bounds_variants   s"   
zTestDIRECT.test_bounds_variantseps)r5   rP   r1   c                 C   s2   t | j| j|d|d}|jdksJ |jsJ d S )NrV   )rx   rS   r/   r   )r   r.   r   rB   rC   )r   rx   r/   rh   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)rk   g      4@rg   i i@B )rb   r   r/   )r   r   r   r/   rI   rh   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)rk   rm   r*   r0   )r   r(   rz   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)rO   rJ   rK   
ValueErrorr   r.   r   )r   rO   	error_msgr   r   r   test_len_tol_validation      
"z"TestDIRECT.test_len_tol_validationc                 C   r~   )Nz vol_tol must be between 0 and 1.r   )rS   r   )r   rS   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.r   r   )rX   r[   r   )r   rX   r   r   r   r   test_fmin_rtol_validation  s   
"z$TestDIRECT.test_fmin_rtol_validationrb   )g      ?string)r   r*   c                 C   r~   )Nzmaxfun must be of type int.r   rb   r   )r   rb   r   r   r   r   test_maxfun_wrong_type  r   z!TestDIRECT.test_maxfun_wrong_typer   c                 C   r~   )Nzmaxiter must be of type int.r   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.r   r}   r   r   r   r   r   r   r   test_negative_maxiter     
"z TestDIRECT.test_negative_maxiterc                 C   r   )Nzmaxfun must be > 0.r   r}   r   r   r   r   r   r   test_negative_maxfun  r   zTestDIRECT.test_negative_maxfunrI   )rI   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 classr   rJ   rK   r   r   r.   r   rI   r   r   r   r   test_invalid_bounds_type"  s   "z#TestDIRECT.test_invalid_bounds_typerl   r   r   c                 C   r   )Nz#Bounds are not consistent min < maxr   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.r}   r   r   )r   r   r'   rJ   rK   r   r   r.   )r   r   rI   r   r   r   test_inf_bounds3  s
   "zTestDIRECT.test_inf_boundsbiasr   r`   c                 C   r~   )Nz%locally_biased must be True or False.r   r0   r   )r   r/   r   r   r   r   test_locally_biased_validation9  r   z)TestDIRECT.test_locally_biased_validationN)*__name__
__module____qualname__r   r   r!   r%   r(   r.   rJ   markparametrizer>   rF   rM   rN   rU   rW   r\   r_   re   ri   rj   rw   ry   xslowr{   r|   r   r   r   r   r   r   r   r   r   r   r$   r   r   r   r   r   r   r   r      s    	















r   )__doc__numpy.testingr   r   rJ   numpyr   scipy.optimizer   r   r   r   r   r   r   <module>   s    