o
    `^h3                     @   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   ^/home/air/shanriGPT/back/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   asarrayr   nfevlenr   r   nitr   )r   r3   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 )	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   rE   r5   r6   r9   r:   )r   r"   r   r   r!   r?   r=   statussuccessr;   messager   r   )r   r3   r@   rE   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   rM   r   r   r   test_exceptiond   s   
"zTestDIRECT.test_exceptionc                 C   s   ddg }t | j||d d S rK   )r   r)   rP   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)rM   rS   vol_tolr3   r/   r
   zjThe side length measure of the hyperrectangle containing the lowest function value found is below len_tol=	r   r"   rF   rG   r   r!   r   r   rH   )r   rS   r3   rM   r@   rH   r   r   r   test_len_tolq   s   


zTestDIRECT.test_len_tolrW   ư>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   rU   r   )rM   rW   rS   r3   r
   z]The volume of the hyperrectangle containing the lowest function value found is below vol_tol=rX   )r   rW   r3   rM   r@   rH   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   )       rV   )rM   f_minr\   r3   r	   z9The best function value found is within a relative error=z$ of the (known) global optimum f_min)r   r"   rF   rG   r;   rH   )r   r\   r3   r_   rM   r@   rH   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   r]   r9   r7   )r   rc   r   r!   r   array)r   r3   rM   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   )rf   r3   Fr   z:Number of function evaluations done is larger than maxfun=)r   r2   r   rG   rF   r=   rH   )r   r3   rf   resultrH   r   r   r   test_failure_maxfun   s   
zTestDIRECT.test_failure_maxfunc                 C   rj   )NrL   )r   r3   Fr.   z,Number of iterations is larger than maxiter=)r   r2   r   rG   rF   r?   rH   )r   r3   r   rl   rH   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      r]         )      g      @      @)rp   r]   r   r4   g{Gz?rg   )r   rh   listzipr   r   r"   r=   rH   rG   r?   r   r!   )	r   r3   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)r9   rT   r5   c                 C   s2   t | j| j|d|d}|jdksJ |jsJ d S )NrZ   )r|   rW   r3   r   )r   r2   r   rF   rG   )r   r|   r3   rl   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)ro   g      4@rk   i i@B )rf   r   r3   )r   r"   r   r3   rM   rl   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)ro   rq   r.   r4   )r   r,   r~   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)rS   rN   rO   
ValueErrorr   r2   r   )r   rS   	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   )rW   r   )r   rW   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   )r\   r_   r   )r   r\   r   r   r   r   test_fmin_rtol_validation  s   
"z$TestDIRECT.test_fmin_rtol_validationrf   )g      ?string)r   r.   c                 C   r   )Nzmaxfun must be of type int.r   rf   r   )r   rf   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_maxfunrM   )rM   rd   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   rN   rO   r   r   r2   r   rM   r   r   r   r   test_invalid_bounds_type%  s   "z#TestDIRECT.test_invalid_bounds_typerp   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+   rN   rO   r   r   r2   )r   r   rM   r   r   r   test_inf_bounds6  s
   "zTestDIRECT.test_inf_boundsbiasr   rd   c                 C   r   )Nz%locally_biased must be True or False.r   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   rN   markparametrizerB   rJ   rQ   rR   rY   r[   r`   rc   ri   rm   rn   r{   r}   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   rN   numpyr   scipy.optimizer   r   r   r   r   r   r   r   <module>   s    