a
    ‡ù»jž4  ã                   @   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   úFE:\soccer\ml_env\Lib\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s’J ‚|j| jks¢J ‚|j| jks²J ‚d S )	N©r-   çü©ñÒMbP?©ÚrtolÚatolçñhãˆµøä>©r2   r1   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sLJ ‚|j|jks\J ‚|j|jkslJ ‚|j|jks|J ‚|j	|j	ksŒJ ‚t|j|jƒ |j
|j
ksªJ ‚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   r8   r6   ÚstatusÚsuccessr5   Úmessager   r   )r   r-   r9   r=   Zres_callbackr   r   r   Útest_direct_callbackA   s,    
ÿ
þ
ÿ
ÿzTestDIRECT.test_direct_callbackc                 C   sH   ddg }t  t¡�  t| j||d� W d   ƒ n1 s:0    Y  d S ©Nr   )iöÿÿÿé
   )Úboundsr-   )ÚpytestÚraisesr    r   r!   ©r   r-   rD   r   r   r   Útest_exceptiona   s
    
ÿzTestDIRECT.test_exceptionc                 C   s   ddg }t | j||d� d S rB   )r   r$   rG   r   r   r   Útest_nanh   s    
ÿzTestDIRECT.test_nanÚlen_tolr/   ç-Cëâ6?c                 C   sd   ddg }t | j||d|d�}|jdks,J ‚|js6J ‚t|jt d¡ƒ d|› �}|j|ks`J ‚d S )Nr   ©g      $Àç      $@g ÂëþKH´9)rD   rJ   Úvol_tolr-   r)   r
   zjThe side length measure of the hyperrectangle containing the lowest function value found is below len_tol=©	r   r   r>   r?   r   r   r   r   r@   )r   rJ   r-   rD   r9   r@   r   r   r   Útest_len_toln   s    

ÿ
þzTestDIRECT.test_len_tolrN   ç�íµ ÷Æ°>g:Œ0âŽyE>c                 C   sd   ddg }t | j||d|d�}|jdks,J ‚|js6J ‚t|jt d¡ƒ d|› �}|j|ks`J ‚d S )Nr   rL   r   )rD   rN   rJ   r-   r
   z]The volume of the hyperrectangle containing the lowest function value found is below vol_tol=rO   )r   rN   r-   rD   r9   r@   r   r   r   Útest_vol_tol|   s    

ÿ
ÿzTestDIRECT.test_vol_tolÚ
f_min_rtol)r/   r3   gH¯¼šò×z>c                 C   sn   d}ddg }t | j||||d�}|jdks0J ‚|js:J ‚|j|d|  k sPJ ‚d|› d�}|j|ksjJ ‚d S )Nç      ð?r   )ç       ÀrM   )rD   Ú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>   r?   r5   r@   )r   rS   r-   rV   rD   r9   r@   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-   rD   r9   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u s&J ‚|jdks4J ‚|j|ksBJ ‚d|› �}|j|ksZJ ‚d S )Néd   )r]   r-   Fr   z:Number of function evaluations done is larger than maxfun=)r   r,   r   r?   r>   r6   r@   )r   r-   r]   Úresultr@   r   r   r   Útest_failure_maxfun¥   s    
ÿÿzTestDIRECT.test_failure_maxfunc                 C   s^   d}t | j| j||d�}|jdu s&J ‚|jdks4J ‚|j|ksBJ ‚d|› �}|j|ksZJ ‚d S )NrC   )r   r-   Fr(   z,Number of iterations is larger than maxiter=)r   r,   r   r?   r>   r8   r@   )r   r-   r   rb   r@   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sfJ ‚|j|jksvJ ‚|j	|j	ks†J ‚|j
|j
ks–J ‚t|j|jƒ t|j|dd� d S )N)g      ÀrT   ç      À)ç      ð¿g      @ç      @)rf   rT   r   r.   g{®Gáz„?r^   )r   r_   ÚlistÚzipr   r   r   r6   r@   r?   r8   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)r3   rK   r/   c                 C   s2   t | j| j|d|d�}|jdks$J ‚|js.J ‚d S )NrQ   )rk   rN   r-   r   )r   r,   r   r>   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 us*J ‚d S )N)re   g      4@ra   i€–˜ i@B )r]   r   r-   )r   r   ©r   r-   rD   rb   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)re   rg   r(   r.   )r   r&   rm   r   r   r   Útest_inf_funë   s
    
ÿzTestDIRECT.test_inf_funéÿÿÿÿr(   c                 C   sH   d}t jt|d��" t| j| j|d� W d   ƒ n1 s:0    Y  d S )Nz len_tol must be between 0 and 1.©Úmatch)rJ   ©rE   rF   Ú
ValueErrorr   r,   r   )r   rJ   Ú	error_msgr   r   r   Útest_len_tol_validationó   s
    
ÿz"TestDIRECT.test_len_tol_validationc                 C   sH   d}t jt|d��" t| j| j|d� W d   ƒ n1 s:0    Y  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   sJ   d}t jt|d��$ t| j| j|dd� W d   ƒ n1 s<0    Y  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   sH   d}t jt|d��" t| j| j|d� W d   ƒ n1 s:0    Y  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   sH   d}t jt|d��" t| j| j|d� W d   ƒ n1 s:0    Y  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   sH   d}t jt|d��" t| j| jdd� W d   ƒ n1 s:0    Y  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   sH   d}t jt|d��" t| j| jdd� W d   ƒ n1 s:0    Y  d S )Nzmaxfun must be > 0.rq   rp   rz   rs   r~   r   r   r   Útest_negative_maxfun  s
    
ÿzTestDIRECT.test_negative_maxfunrD   )rD   r[   r   c                 C   sB   d}t jt|d�� t| j|ƒ W d   ƒ n1 s40    Y  d S )Nz5bounds must be a sequence or instance of Bounds classrq   ©rE   rF   rt   r   r,   ©r   rD   ru   r   r   r   Útest_invalid_bounds_type"  s    z#TestDIRECT.test_invalid_bounds_typerf   r   r   c                 C   sB   d}t jt|d�� t| j|ƒ W d   ƒ n1 s40    Y  d S )Nz#Bounds are not consistent min < maxrq   r�   r‚   r   r   r   Útest_incorrect_bounds)  s    z TestDIRECT.test_incorrect_boundsc                 C   sZ   d}t tj dgdtjgƒ}tjt|d�� t| j|ƒ W d   ƒ n1 sL0    Y  d S )NzBounds must not be inf.rp   r   rq   )r   r   r%   rE   rF   rt   r   r,   )r   ru   rD   r   r   r   Útest_inf_bounds3  s    zTestDIRECT.test_inf_boundsZbiasr   r[   c                 C   sH   d}t jt|d��" t| j| j|d� W d   ƒ n1 s:0    Y  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,   rE   ÚmarkZparametrizer:   rA   rH   rI   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   rE   Únumpyr   Zscipy.optimizer   r   r   r   r   r   r   Ú<module>   s
   