
    tKg`3                     L    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      Z
y)z.
Unit test for DIRECT optimization algorithm.
    )assert_allcloseassert_array_lessN)directBoundsc            	          e Zd Zd Zd Zd Zd Zd Zd Ze	j                  j                  ddd	g      d
        Ze	j                  j                  ddd	g      d        Ze	j                  j                  ddd	g      d        Ze	j                  j                  ddd	g      d        Ze	j                  j                  dddg      e	j                  j                  ddd	g      d               Ze	j                  j                  dddg      e	j                  j                  ddd	g      d               Ze	j                  j                  dg d      e	j                  j                  ddd	g      d               Zd Ze	j                  j                  ddd	g      d        Ze	j                  j                  ddd	g      d        Ze	j                  j                  ddd	g      d        Ze	j                  j                  ddd	g      d        Ze	j                  j                  ddd	g      e	j                  j                  dg d      d                Ze	j                  j2                  e	j                  j                  ddd	g      d!               Ze	j                  j                  ddd	g      d"        Ze	j                  j                  dd#d$g      d%        Ze	j                  j                  dd#d$g      d&        Ze	j                  j                  dd#d$g      d'        Ze	j                  j                  d(g d)      d*        Ze	j                  j                  d+g d)      d,        Z d- Z!d. Z"e	j                  j                  d/g d0      d1        Z#e	j                  j                  d/ e$d2d#gd3d4g       e$e%jL                   d#gd3e%jL                  g      g      d5        Z'd6 Z(e	j                  j                  dd7d8d8gd9g      d:        Z)y;)<
TestDIRECTc                     d| _         ddgz  | _        t        j                  d      | _        d| _        t        ddgddg      | _        d| _        y )	Nr      )   r
           g      g      @  )		fun_callsbounds_spherenpzerosoptimum_sphere_posoptimum_spherer   bounds_stylinski_tangmaxiter)selfs    d/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/scipy/optimize/tests/test_direct.pysetup_methodzTestDIRECT.setup_method   sM    y["$((5/!%+S#JR%A"    c                 r    | xj                   dz  c_         t        j                  |      j                         S )N   )r   r   squaresumr   xs     r   spherezTestDIRECT.sphere   s'    !yy|!!r   c                 v    t        j                  |      dk(  r
t               dt        j                  |      z  S Nr   r   )r   r   ZeroDivisionErrorr    s     r   invzTestDIRECT.inv   s,    66!9>#%%{r   c                 "    t         j                  S N)r   nanr    s     r   nan_funzTestDIRECT.nan_fun       vvr   c                 "    t         j                  S r(   )r   infr    s     r   inf_funzTestDIRECT.inf_fun"   r+   r   c                 d    |\  }}d|dz  d|dz  z  z
  d|z  z   |dz  z   d|dz  z  z
  d|z  z   z  S )Ng      ?r
             )r   posr!   ys       r   styblinski_tangzTestDIRECT.styblinski_tang%   sO    1adR!Q$Y&Q.A5QT	AAEIJJr   locally_biasedTFc                 F   t        | j                  | j                  |      }t        |j                  | j
                  dd       t        |j                  | j                  dd       t        j                  | j                        }t        |d d df   |j                         t        |j                  |d d df          |j                  dt        | j                        dz   z  k  sJ |j                  | j                  k(  sJ |j                  | j                  k  sJ y )	Nr7   MbP?rtolatolh㈵>r=   r<   r   r   r   )r   r"   r   r   r!   r   funr   r   asarrayr   nfevlenr   nitr   )r   r7   res_boundss       r   test_directzTestDIRECT.test_direct)   s    T[[$"4"4$24 	t66!	.!4!44dK **T//0'!Q$-/#%%A/ xx43t'9'9#:Q#>????xx4>>))) ww$,,&&&r   c                    t        | j                  | j                  |      }d }t        | j                  | j                  ||      }t        |j                  |j                         |j
                  |j
                  k(  sJ |j                  |j                  k(  sJ |j                  |j                  k(  sJ |j                  |j                  k(  sJ |j                  |j                  k(  sJ t        |j                  |j                         |j                  |j                  k(  sJ t        |j                  | j                  dd       t        |j                  | j                  dd       y )Nr9   c                 P    d| z  } t        j                  |       }t        d       |S )Nr1   z+DIRECT minimization algorithm callback test)r   r   print)r!   dummys     r   callbackz1TestDIRECT.test_direct_callback.<locals>.callbackG   s&    !AIIaLE?@Lr   )r7   rL   r:   r;   r>   r?   )r   r"   r   r   r!   rD   rB   statussuccessr@   messager   r   )r   r7   rE   rL   res_callbacks        r   test_direct_callbackzTestDIRECT.test_direct_callbackA   s4    T[[$"4"4$24	 dkk4+=+=-;'/1 	|~~.ww,*****xx<,,,,,zz\00000{{l22222ww,*****|~~.{{l22222 	(?(?!	.(($*=*=!	.r   c                     ddgz  }t        j                  t              5  t        | j                  ||       d d d        y # 1 sw Y   y xY wNr
   )i
   )boundsr7   )pytestraisesr%   r   r&   r   r7   rU   s      r   test_exceptionzTestDIRECT.test_exceptiona   s9    I;]],-488F"02 .--s   AAc                 @    ddgz  }t        | j                  ||       y rS   )r   r*   rX   s      r   test_nanzTestDIRECT.test_nanh   s     I;t||F,	.r   len_tolr:   -C6?c                     ddgz  }t        | j                  ||d|      }|j                  dk(  sJ |j                  sJ t	        |j
                  t        j                  d             d| }|j                  |k(  sJ y )Nr
   g      $      $@gKH9)rU   r\   vol_tolr7   r2   r   zjThe side length measure of the hyperrectangle containing the lowest function value found is below len_tol=	r   r"   rM   rN   r   r!   r   r   rO   )r   r\   r7   rU   rE   rO   s         r   test_len_tolzTestDIRECT.test_len_toln   s     K=T[[">CzzQ{{{rxx/%Y( {{g%%%r   ra   ư>g:0yE>c                     ddgz  }t        | j                  ||d|      }|j                  dk(  sJ |j                  sJ t	        |j
                  t        j                  d             d| }|j                  |k(  sJ y )Nr
   r_   r   )rU   ra   r\   r7   r   z]The volume of the hyperrectangle containing the lowest function value found is below vol_tol=rb   )r   ra   r7   rU   rE   rO   s         r   test_vol_tolzTestDIRECT.test_vol_tol|   s     K=T[[@zzQ{{{rxx/<<C9F{{g%%%r   
f_min_rtol)r:   r>   gHz>c                     d}ddgz  }t        | j                  ||||      }|j                  dk(  sJ |j                  sJ |j                  |d|z   z  k  sJ d| d}|j
                  |k(  sJ y )N      ?r
   )       r`   )rU   f_minrg   r7   r   z9The best function value found is within a relative error=z$ of the (known) global optimum f_min)r   r"   rM   rN   r@   rO   )r   rg   r7   rk   rU   rE   rO   s          r   
test_f_minzTestDIRECT.test_f_min   s    
 J<T[[u *$24 zzQ{{{ww"z/2222&<'KM{{g%%%r   c                     t        j                  |d   |z
        t        j                  |d   |z
        j                         z   S r$   )r   r   r   )r   r!   abs       r   circle_with_argszTestDIRECT.circle_with_args   s8    yy1"RYYqtax%8%<%<%>>>r   c                     ddgz  }t        | j                  |dd|      }t        |j                  t	        j
                  ddg      d       y )	Nr1   )rj          @)r   r   i  )argsmaxfunr7   ri   r>   r<   )r   rp   r   r!   r   array)r   r7   rU   rE   s       r   test_f_circle_with_argsz"TestDIRECT.test_f_circle_with_args   sE    K=T**F$24rxxR1=r   c                     d}t        | j                  | j                  ||      }|j                  du sJ |j                  dk(  sJ |j
                  |k\  sJ d| }|j                  |k(  sJ y )Nd   )rt   r7   Fr   z:Number of function evaluations done is larger than maxfun=)r   r6   r   rN   rM   rB   rO   )r   r7   rt   resultrO   s        r   test_failure_maxfunzTestDIRECT.test_failure_maxfun   s    
 ,,d.H.H%nF~~&&&}}!!!{{f$$$))/2~~(((r   c                     d}t        | j                  | j                  ||      }|j                  du sJ |j                  dk(  sJ |j
                  |k\  sJ d| }|j                  |k(  sJ y )NrT   )r   r7   Fr1   z,Number of iterations is larger than maxiter=)r   r6   r   rN   rM   rD   rO   )r   r7   r   rz   rO   s        r   test_failure_maxiterzTestDIRECT.test_failure_maxiter   s}    
 ,,d.H.H 'H~~&&&}}!!!zzW$$$@	J~~(((r   c                 ,   g d}g d}t        j                  g d      }t        t        ||            }t	        ||      }t        | j                  ||      }t        | j                  ||      }|j                  |j                  k(  sJ |j                  |j                  k(  sJ |j                  |j                  k(  sJ |j                  |j                  k(  sJ t        |j                  |j                         t        |j                  |d       y )N)g      ri         )      g      @      @)r   ri   r   r9   g{Gz?ru   )r   rv   listzipr   r   r"   rB   rO   rN   rD   r   r!   )	r   r7   lbubx_opt
bounds_old
bounds_newres_old_boundsres_new_boundss	            r   test_bounds_variantszTestDIRECT.test_bounds_variants   s     '#b"+&
B^
Z/=?Z/=? ""n&9&9999%%)?)????%%)?)????!!^%7%7777((.*:*:;((%d;r   eps)r>   r]   r:   c                     t        | j                  | j                  |d|      }|j                  dk(  sJ |j                  sJ y )Nrd   )r   ra   r7   r
   )r   r6   r   rM   rN   )r   r   r7   rz   s       r   test_epsilonzTestDIRECT.test_epsilon   sF     ,,d.H.H'57 }}!!!~~~r   c                 L    dgdz  }t        | j                  |dd|      }|J y )N)r   g      4@ry   i i@B )rt   r   r7   )r   r"   r   r7   rU   rz   s       r   test_no_segmentation_faultz%TestDIRECT.test_no_segmentation_fault   s6    
 #VH 'H!!!r   c                 H    dgdz  }t        | j                  ||      }|J y )N)r   r   r1   r9   )r   r.   r   s       r   test_inf_funzTestDIRECT.test_inf_fun   s0     qf'57!!!r   r1   c                     d}t        j                  t        |      5  t        | j                  | j
                  |       d d d        y # 1 sw Y   y xY w)Nz len_tol must be between 0 and 1.match)r\   rV   rW   
ValueErrorr   r6   r   )r   r\   	error_msgs      r   test_len_tol_validationz"TestDIRECT.test_len_tol_validation   <    6	]]:Y74'')C)C"$ 877   #A

Ac                     d}t        j                  t        |      5  t        | j                  | j
                  |       d d d        y # 1 sw Y   y xY w)Nz vol_tol must be between 0 and 1.r   )ra   r   )r   ra   r   s      r   test_vol_tol_validationz"TestDIRECT.test_vol_tol_validation   r   r   c                     d}t        j                  t        |      5  t        | j                  | j
                  |d       d d d        y # 1 sw Y   y xY w)Nz#f_min_rtol must be between 0 and 1.r   r   )rg   rk   r   )r   rg   r   s      r   test_fmin_rtol_validationz$TestDIRECT.test_fmin_rtol_validation  s>    9	]]:Y74'')C)C(4 877s   $AArt   )g      ?string)r   r1   c                     d}t        j                  t        |      5  t        | j                  | j
                  |       d d d        y # 1 sw Y   y xY w)Nzmaxfun must be of type int.r   rt   r   )r   rt   r   s      r   test_maxfun_wrong_typez!TestDIRECT.test_maxfun_wrong_type  s<    1	]]:Y74'')C)C " 877r   r   c                     d}t        j                  t        |      5  t        | j                  | j
                  |       d d d        y # 1 sw Y   y xY w)Nzmaxiter must be of type int.r   r   r   )r   r   r   s      r   test_maxiter_wrong_typez"TestDIRECT.test_maxiter_wrong_type  s<    2	]]:Y74'')C)C"$ 877r   c                     d}t        j                  t        |      5  t        | j                  | j
                  d       d d d        y # 1 sw Y   y xY w)Nzmaxiter must be > 0.r   r   r   r   r   r   s     r   test_negative_maxiterz TestDIRECT.test_negative_maxiter  s<    *	]]:Y74'')C)C 877r   c                     d}t        j                  t        |      5  t        | j                  | j
                  d       d d d        y # 1 sw Y   y xY w)Nzmaxfun must be > 0.r   r   r   r   r   s     r   test_negative_maxfunzTestDIRECT.test_negative_maxfun  s<    )	]]:Y74'')C)C 877r   rU   )rU   rr   r   c                     d}t        j                  t        |      5  t        | j                  |       d d d        y # 1 sw Y   y xY w)Nz5bounds must be a sequence or instance of Bounds classr   rV   rW   r   r   r6   r   rU   r   s      r   test_invalid_bounds_typez#TestDIRECT.test_invalid_bounds_type"  s3    0	]]:Y74''0 877	   >Ar   r   r   c                     d}t        j                  t        |      5  t        | j                  |       d d d        y # 1 sw Y   y xY w)Nz#Bounds are not consistent min < maxr   r   r   s      r   test_incorrect_boundsz TestDIRECT.test_incorrect_bounds)  s3     :	]]:Y74''0 877r   c                     d}t        t        j                   dgdt        j                  g      }t        j                  t
        |      5  t        | j                  |       d d d        y # 1 sw Y   y xY w)NzBounds must not be inf.r   r   r   )r   r   r-   rV   rW   r   r   r6   )r   r   rU   s      r   test_inf_boundszTestDIRECT.test_inf_bounds3  sO    -	"&&"BFF|4]]:Y74''0 877s   A++A4biasr   rr   c                     d}t        j                  t        |      5  t        | j                  | j
                  |       d d d        y # 1 sw Y   y xY w)Nz%locally_biased must be True or False.r   r9   r   )r   r7   r   s      r   test_locally_biased_validationz)TestDIRECT.test_locally_biased_validation9  s<    ;	]]:Y74'')C)C"02 877r   N)*__name__
__module____qualname__r   r"   r&   r*   r.   r6   rV   markparametrizerG   rQ   rY   r[   rc   rf   rl   rp   rw   r{   r}   r   r   xslowr   r   r   r   r   r   r   r   r   r   r   r   r)   r   r   r   r3   r   r   r   r      sz   "
K [[-e}=' >'. [[-e}=. >.> [[-e}=2 >2 [[-e}=. >.
 [[Yt5[[-e}=
& > 6
& [[Yt5[[-e}=	& > 6	& [[\+=>[[-e}=& > ?&? [[-e}=> >> [[-e}=) >) [[-e}=) >) [[-e}=< ><* [[-e}=[[U$67 8 > [[[[-e}=" > " [[-e}=" >" [[YQ0$ 1$ [[YQ0$ 1$ [[\B734 44 [[X'>?" @" [[Y(?@$ A$ [[X'891 :1 [[X$c2YQ8$rvvgr]RLA 
1
1
1 [[-A/CD2 E2r   r   )__doc__numpy.testingr   r   rV   numpyr   scipy.optimizer   r   r   r3   r   r   <module>r      s$   .   )s2 s2r   