U
    e`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"dd#d$g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/d0 Ze	j
dddge	j
d1d#ddg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@dAdBdCgdDdE Ze	j
dFdAdBdCgdGdH Z dIdJ Z!dKdL Z"e	j
dMdMdNdOgdPdQ Z#e	j
dMe$dRd8gdSdTge$e%j& d8gdSe%j&ggdUdV Z'dWdX Z(e	j
ddYdOdOgdNgdZd[ 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   a/var/www/html/assets/scripts/venv/lib/python3.8/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   s   t jS N)r   nanr   r   r   r   nan_fun   s    zTestDIRECT.nan_func                 C   s   t jS r"   )r   infr   r   r   r   inf_fun"   s    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st|j| jkst|j| jkstd S )	Nr-   MbP?rtolatolh㈵>r2   r1   r   r   r   )r   r   r   r   r   r   funr   r   Zasarrayr   nfevlenAssertionErrorr   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sLt|j|jks\t|j|jkslt|j	|j	ks|t|j
|j
kstt|j|j |j|jkstt|j| jddd t|j
| jddd d S )	Nr.   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>   r/   r0   r3   r4   )r   r   r   r   r   r9   r8   r6   statussuccessr5   messager   r   )r   r-   r:   r>   Zres_callbackr   r   r   test_direct_callbackA   s0    


 
 zTestDIRECT.test_direct_callbackc              	   C   s4   ddg }t t t| j||d W 5 Q R X d S Nr   )i
   )boundsr-   )pytestraisesr    r   r!   r   r-   rE   r   r   r   test_exceptiona   s
    
zTestDIRECT.test_exceptionc                 C   s   ddg }t | j||d d S rC   )r   r$   rH   r   r   r   test_nanh   s    
zTestDIRECT.test_nanlen_tolr/   g-C6?c                 C   sd   ddg }t | j||d|d}|jdks,t|js6tt|jtd d| }|j	|ks`td S )Nr   g      $      $@gKH9)rE   rK   vol_tolr-   r)   r
   zjThe side length measure of the hyperrectangle containing the lowest function value found is below len_tol=
r   r   r?   r8   r@   r   r   r   r   rA   )r   rK   r-   rE   r:   rA   r   r   r   test_len_toln   s    

 

zTestDIRECT.test_len_tolrN   ư>g:0yE>c                 C   sd   ddg }t | j||d|d}|jdks,t|js6tt|jtd d| }|j	|ks`td S )Nr   rL   r   )rE   rN   rK   r-   r
   z]The volume of the hyperrectangle containing the lowest function value found is below vol_tol=rO   )r   rN   r-   rE   r:   rA   r   r   r   test_vol_tol|   s    

 

zTestDIRECT.test_vol_tol
f_min_rtolr3   gHz>c                 C   sn   d}ddg }t | j||||d}|jdks0t|js:t|j|d|  k sPtd| d}|j|ksjtd S )N      ?r   )       rM   )rE   f_minrS   r-   r	   z9The best function value found is within a relative error=z$ of the (known) global optimum f_min)r   r   r?   r8   r@   r5   rA   )r   rS   r-   rV   rE   r:   rA   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(   )rU          @)r   r   i  )argsmaxfunr-   rT   r3   r1   )r   rZ   r   r   r   array)r   r-   rE   r:   r   r   r   test_f_circle_with_args   s
    
z"TestDIRECT.test_f_circle_with_argsc                 C   s^   d}t | j| j||d}|jdks&t|jdks4t|j|ksBtd| }|j|ksZtd S )Nd   )r]   r-   Fr   z:Number of function evaluations done is larger than maxfun=)r   r,   r   r@   r8   r?   r6   rA   )r   r-   r]   resultrA   r   r   r   test_failure_maxfun   s    
 
zTestDIRECT.test_failure_maxfunc                 C   s^   d}t | j| j||d}|jdks&t|jdks4t|j|ksBtd| }|j|ksZtd S )NrD   )r   r-   Fr(   z,Number of iterations is larger than maxiter=)r   r,   r   r@   r8   r?   r9   rA   )r   r-   r   rb   rA   r   r   r   test_failure_maxiter   s    
 
zTestDIRECT.test_failure_maxiterc           	      C   s   dddg}dddg}t dddg}tt||}t||}t| j||d}t| j||d}|j|jkslt|j	|j	ks|t|j
|j
kst|j|jkstt|j|j t|j|d	d
 d S )Ng      rT               g      @      @r   r.   g{Gz?r^   )r   r_   listzipr   r   r   r6   r8   rA   r@   r9   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epsc                 C   s2   t | j| j|d|d}|jdks$t|js.td S )NrQ   )rk   rN   r-   r   )r   r,   r   r?   r8   r@   )r   rk   r-   rb   r   r   r   test_epsilon   s    
 zTestDIRECT.test_epsilonc                 C   s.   dgd }t | j|dd|d}|d k	s*td S )N)re   g      4@ra   i i@B )r]   r   r-   )r   r   r8   r   r-   rE   rb   r   r   r   test_no_segmentation_fault   s    

 z%TestDIRECT.test_no_segmentation_faultc                 C   s*   dgd }t | j||d}|d k	s&td S )N)re   rg   r(   r.   )r   r&   r8   rm   r   r   r   test_inf_fun   s
    
zTestDIRECT.test_inf_funr(   c              	   C   s4   d}t jt|d t| j| j|d W 5 Q R X d S )Nz len_tol must be between 0 and 1.match)rK   rF   rG   
ValueErrorr   r,   r   )r   rK   	error_msgr   r   r   test_len_tol_validation   s
    
z"TestDIRECT.test_len_tol_validationc              	   C   s4   d}t jt|d t| j| j|d W 5 Q R X d S )Nz vol_tol must be between 0 and 1.rq   )rN   rs   )r   rN   ru   r   r   r   test_vol_tol_validation   s
    
z"TestDIRECT.test_vol_tol_validationc              	   C   s6   d}t jt|d t| j| j|dd W 5 Q R X d S )Nz#f_min_rtol must be between 0 and 1.rq   r   )rS   rV   rs   )r   rS   ru   r   r   r   test_fmin_rtol_validation  s    
 z$TestDIRECT.test_fmin_rtol_validationr]   g      ?string)r   r(   c              	   C   s4   d}t jt|d t| j| j|d W 5 Q R X d S )Nzmaxfun must be of type int.rq   r]   rs   )r   r]   ru   r   r   r   test_maxfun_wrong_type  s
    
z!TestDIRECT.test_maxfun_wrong_typer   c              	   C   s4   d}t jt|d t| j| j|d W 5 Q R X d S )Nzmaxiter must be of type int.rq   r   rs   )r   r   ru   r   r   r   test_maxiter_wrong_type  s
    
z"TestDIRECT.test_maxiter_wrong_typec              	   C   s4   d}t jt|d t| j| jdd W 5 Q R X d S )Nzmaxiter must be > 0.rq   rp   r|   rs   r   ru   r   r   r   test_negative_maxiter  s
    
z TestDIRECT.test_negative_maxiterc              	   C   s4   d}t jt|d t| j| jdd W 5 Q R X d S )Nzmaxfun must be > 0.rq   rp   rz   rs   r~   r   r   r   test_negative_maxfun  s
    
zTestDIRECT.test_negative_maxfunrE   r[   r   c              	   C   s.   d}t jt|d t| j| W 5 Q R X d S )Nz5bounds must be a sequence or instance of Bounds classrq   rF   rG   rt   r   r,   r   rE   ru   r   r   r   test_invalid_bounds_type"  s    z#TestDIRECT.test_invalid_bounds_typerf   r   r   c              	   C   s.   d}t jt|d t| j| W 5 Q R X d S )Nz#Bounds are not consistent min < maxrq   r   r   r   r   r   test_incorrect_bounds)  s    z TestDIRECT.test_incorrect_boundsc              	   C   sF   d}t tj dgdtjg}tjt|d t| j| W 5 Q R X d S )NzBounds must not be inf.rp   r   rq   )r   r   r%   rF   rG   rt   r   r,   )r   ru   rE   r   r   r   test_inf_bounds3  s    zTestDIRECT.test_inf_boundsZbiasc              	   C   s4   d}t jt|d t| j| j|d W 5 Q R X d S )Nz%locally_biased must be True or False.rq   r.   rs   )r   r-   ru   r   r   r   test_locally_biased_validation9  s
    
z)TestDIRECT.test_locally_biased_validationN)*__name__
__module____qualname__r   r   r!   r$   r&   r,   rF   markZparametrizer;   rB   rI   rJ   rP   rR   rW   rZ   r`   rc   rd   rj   rl   Zxslowrn   ro   rv   rw   rx   r{   r}   r   r   r   r   r   r#   r   r   r   r   r   r   r   r      s~   	















r   )__doc__Znumpy.testingr   r   rF   numpyr   Zscipy.optimizer   r   r   r   r   r   r   <module>   s
   