
    (ph3                     T    S r SSKJrJr  SSKrSSKrSSKJrJ	r	  SSK
r
 " S S5      rg)z.
Unit test for DIRECT optimization algorithm.
    )assert_allcloseassert_array_lessN)directBoundsc            	          \ rS rSrS rS rS rS rS rS r	\
R                  R                  SS	S
/5      S 5       r\
R                  R                  SS	S
/5      S 5       r\
R                  R                  SS	S
/5      S 5       r\
R                  R                  SS	S
/5      S 5       r\
R                  R                  SSS/5      \
R                  R                  SS	S
/5      S 5       5       r\
R                  R                  SSS/5      \
R                  R                  SS	S
/5      S 5       5       r\
R                  R                  S/ SQ5      \
R                  R                  SS	S
/5      S 5       5       rS r\
R                  R                  SS	S
/5      S 5       r\
R                  R                  SS	S
/5      S 5       r\
R                  R                  SS	S
/5      S 5       r\
R                  R                  SS	S
/5      S 5       r\
R                  R                  SS	S
/5      \
R                  R                  S/ S Q5      S! 5       5       r\
R                  R4                  \
R                  R                  SS	S
/5      S" 5       5       r\
R                  R                  SS	S
/5      S# 5       r\
R                  R                  SS$S%/5      S& 5       r\
R                  R                  SS$S%/5      S' 5       r\
R                  R                  SS$S%/5      S( 5       r\
R                  R                  S)/ S*Q5      S+ 5       r \
R                  R                  S,/ S*Q5      S- 5       r!S. r"S/ r#\
R                  R                  S0/ S1Q5      S2 5       r$\
R                  R                  S0\%" S3S$/S4S5/5      \%" \&RN                  * S$/S4\&RN                  /5      /5      S6 5       r(S7 r)\
R                  R                  SS8S9S9/S:/5      S; 5       r*S<r+g=)>
TestDIRECT   c                     [         R                  " 5       U l        SS/-  U l        [        R
                  " S5      U l        SU l        [        SS/SS/5      U l	        SU l
        g )N   )   r           g      g      @  )	threadinglocal	fun_callsbounds_spherenpzerosoptimum_sphere_posoptimum_spherer   bounds_stylinski_tangmaxiter)selfs    S/var/www/html/venv/lib/python3.13/site-packages/scipy/optimize/tests/test_direct.pysetup_methodTestDIRECT.setup_method   sT    "*y["$((5/!%+S#JR%A"    c                     [        U R                  S5      (       d  SU R                  l        U R                  =R                  S-  sl        [        R                  " U5      R                  5       $ )Ncr      )hasattrr   r!   r   squaresumr   xs     r   sphereTestDIRECT.sphere   sK    t~~s++ DNNAyy|!!r   c                 |    [         R                  " U5      S:X  a
  [        5       eS[         R                  " U5      -  $ Nr   r"   )r   r%   ZeroDivisionErrorr&   s     r   invTestDIRECT.inv   s,    66!9>#%%{r   c                 "    [         R                  $ N)r   nanr&   s     r   nan_funTestDIRECT.nan_fun"       vvr   c                 "    [         R                  $ r0   )r   infr&   s     r   inf_funTestDIRECT.inf_fun%   r4   r   c                 b    Uu  p#SUS-  SUS-  -  -
  SU-  -   US-  -   SUS-  -  -
  SU-  -   -  $ )Ng      ?r             )r   posr'   ys       r   styblinski_tangTestDIRECT.styblinski_tang(   sM    adR!Q$Y&Q.A5QT	AAEIJJr   locally_biasedTFc                 P   [        U R                  U R                  US9n[        UR                  U R
                  SSS9  [        UR                  U R                  SSS9  [        R                  " U R                  5      n[        US S 2S4   UR                  5        [        UR                  US S 2S4   5        UR                  S[        U R                  5      S-   -  ::  d   eUR                  U R                  R                  :X  d   eUR                  U R                   ::  d   eg )	NrB   MbP?rtolatolh㈵>rH   rG   r   r"   r   )r   r(   r   r   r'   r   funr   r   asarrayr   nfevlenr   r!   nitr   )r   rB   res_boundss       r   test_direct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                    [        U R                  U R                  US9nS n[        U R                  U R                  UUS9n[        UR                  UR                  5        UR
                  UR
                  :X  d   eUR                  UR                  :X  d   eUR                  UR                  :X  d   eUR                  UR                  :X  d   eUR                  UR                  :X  d   e[        UR                  UR                  5        UR                  UR                  :X  d   e[        UR                  U R                  SSS9  [        UR                  U R                  SSS9  g )NrD   c                 R    SU -  n [         R                  " U 5      n[        S5        U$ )Nr;   z+DIRECT minimization algorithm callback test)r   r$   print)r'   dummys     r   callback1TestDIRECT.test_direct_callback.<locals>.callbackJ   s&    !AIIaLE?@Lr   )rB   rX   rE   rF   rI   rJ   )r   r(   r   r   r'   rO   rM   statussuccessrK   messager   r   )r   rB   rP   rX   res_callbacks        r   test_direct_callbackTestDIRECT.test_direct_callbackD   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                     SS/-  n[         R                  " [        5         [        U R                  UUS9  S S S 5        g ! , (       d  f       g = fNr   )i
   )boundsrB   )pytestraisesr,   r   r-   r   rB   rc   s      r   test_exceptionTestDIRECT.test_exceptiond   s9    I;]],-488F"02 .--s   A  
Ac                 :    SS/-  n[        U R                  UUS9  g ra   )r   r2   rf   s      r   test_nanTestDIRECT.test_nank   s     I;t||F,	.r   len_tolrE   -C6?c                    SS/-  n[        U R                  X1SUS9nUR                  S:X  d   eUR                  (       d   e[	        UR
                  [        R                  " S5      5        SU 3nUR                  U:X  d   eg )Nr   g      $      $@gKH9)rc   rl   vol_tolrB   r<   r   zjThe side length measure of the hyperrectangle containing the lowest function value found is below len_tol=	r   r(   rZ   r[   r   r'   r   r   r\   )r   rl   rB   rc   rP   r\   s         r   test_len_tolTestDIRECT.test_len_tolq   s~     K=T[[">CzzQ{{{rxx/%Y( {{g%%%r   rq   ư>g:0yE>c                    SS/-  n[        U R                  X1SUS9nUR                  S:X  d   eUR                  (       d   e[	        UR
                  [        R                  " S5      5        SU 3nUR                  U:X  d   eg )Nr   ro   r   )rc   rq   rl   rB   r   z]The volume of the hyperrectangle containing the lowest function value found is below vol_tol=rr   )r   rq   rB   rc   rP   r\   s         r   test_vol_tolTestDIRECT.test_vol_tol   s}     K=T[[@zzQ{{{rxx/<<C9F{{g%%%r   
f_min_rtol)rE   rI   gHz>c                     SnSS/-  n[        U R                  XCUUS9nUR                  S:X  d   eUR                  (       d   eUR                  USU-   -  :  d   eSU S3nUR
                  U:X  d   eg )N      ?r   )       rp   )rc   f_minry   rB   r   z9The best function value found is within a relative error=z$ of the (known) global optimum f_min)r   r(   rZ   r[   rK   r\   )r   ry   rB   r}   rc   rP   r\   s          r   
test_f_minTestDIRECT.test_f_min   s    
 J<T[[ *$24 zzQ{{{ww"z/2222&<'KM{{g%%%r   c                     [         R                  " US   U-
  5      [         R                  " US   U-
  5      R                  5       -   $ r+   )r   r$   r%   )r   r'   abs       r   circle_with_argsTestDIRECT.circle_with_args   s8    yy1"RYYqtax%8%<%<%>>>r   c                     SS/-  n[        U R                  USSUS9n[        UR                  [        R
                  " SS/5      SS9  g )	Nr;   )r|          @)r"   r"   i  )argsmaxfunrB   r{   rI   rG   )r   r   r   r'   r   array)r   rB   rc   rP   s       r   test_f_circle_with_args"TestDIRECT.test_f_circle_with_args   sE    K=T**F$24rxxR1=r   c                     Sn[        U R                  U R                  X!S9nUR                  SL d   eUR                  S:X  d   eUR
                  U:  d   eSU 3nUR                  U:X  d   eg )Nd   )r   rB   Fr"   z:Number of function evaluations done is larger than maxfun=)r   r@   r   r[   rZ   rM   r\   )r   rB   r   resultr\   s        r   test_failure_maxfunTestDIRECT.test_failure_maxfun   s    
 ,,d.H.H%F~~&&&}}!!!{{f$$$))/2~~(((r   c                     Sn[        U R                  U R                  X!S9nUR                  SL d   eUR                  S:X  d   eUR
                  U:  d   eSU 3nUR                  U:X  d   eg )Nrb   )r   rB   Fr;   z,Number of iterations is larger than maxiter=)r   r@   r   r[   rZ   rO   r\   )r   rB   r   r   r\   s        r   test_failure_maxiterTestDIRECT.test_failure_maxiter   s{    
 ,,d.H.H 'H~~&&&}}!!!zzW$$$@	J~~(((r   c                     / SQn/ SQn[         R                  " / SQ5      n[        [        X#5      5      n[	        X#5      n[        U R                  UUS9n[        U R                  UUS9nUR                  UR                  :X  d   eUR                  UR                  :X  d   eUR                  UR                  :X  d   eUR                  UR                  :X  d   e[        UR                  UR                  5        [        UR                  USS9  g )N)g      r{         )      g      @      @)r   r{   r   rD   g{Gz?r   )r   r   listzipr   r   r(   rM   r\   r[   rO   r   r'   )	r   rB   lbubx_opt
bounds_old
bounds_newres_old_boundsres_new_boundss	            r   test_bounds_variantsTestDIRECT.test_bounds_variants   s     '#b+&
B^
Z/=?Z/=? ""n&9&9999%%)?)????%%)?)????!!^%7%7777((.*:*:;((%d;r   eps)rI   rm   rE   c                     [        U R                  U R                  USUS9nUR                  S:X  d   eUR                  (       d   eg )Nru   )r   rq   rB   r   )r   r@   r   rZ   r[   )r   r   rB   r   s       r   test_epsilonTestDIRECT.test_epsilon   sF     ,,d.H.H'57 }}!!!~~~r   c                 H    S/S-  n[        U R                  USSUS9nUc   eg )N)r   g      4@r   i i@B )r   r   rB   )r   r(   r   rB   rc   r   s       r   test_no_segmentation_fault%TestDIRECT.test_no_segmentation_fault   s6    
 #VH 'H!!!r   c                 D    S/S-  n[        U R                  UUS9nUc   eg )N)r   r   r;   rD   )r   r7   r   s       r   test_inf_funTestDIRECT.test_inf_fun   s0     qf'57!!!r   r;   c                     Sn[         R                  " [        US9   [        U R                  U R
                  US9  S S S 5        g ! , (       d  f       g = f)Nz len_tol must be between 0 and 1.match)rl   rd   re   
ValueErrorr   r@   r   )r   rl   	error_msgs      r   test_len_tol_validation"TestDIRECT.test_len_tol_validation   <    6	]]:Y74'')C)C"$ 877    A
Ac                     Sn[         R                  " [        US9   [        U R                  U R
                  US9  S S S 5        g ! , (       d  f       g = f)Nz vol_tol must be between 0 and 1.r   )rq   r   )r   rq   r   s      r   test_vol_tol_validation"TestDIRECT.test_vol_tol_validation   r   r   c                     Sn[         R                  " [        US9   [        U R                  U R
                  USS9  S S S 5        g ! , (       d  f       g = f)Nz#f_min_rtol must be between 0 and 1.r   r   )ry   r}   r   )r   ry   r   s      r   test_fmin_rtol_validation$TestDIRECT.test_fmin_rtol_validation  s>    9	]]:Y74'')C)C(4 877s   !A
Ar   )g      ?string)r"   r;   c                     Sn[         R                  " [        US9   [        U R                  U R
                  US9  S S S 5        g ! , (       d  f       g = f)Nzmaxfun must be of type int.r   r   r   )r   r   r   s      r   test_maxfun_wrong_type!TestDIRECT.test_maxfun_wrong_type  s<    1	]]:Y74'')C)C " 877r   r   c                     Sn[         R                  " [        US9   [        U R                  U R
                  US9  S S S 5        g ! , (       d  f       g = f)Nzmaxiter must be of type int.r   r   r   )r   r   r   s      r   test_maxiter_wrong_type"TestDIRECT.test_maxiter_wrong_type  s<    2	]]:Y74'')C)C"$ 877r   c                     Sn[         R                  " [        US9   [        U R                  U R
                  SS9  S S S 5        g ! , (       d  f       g = f)Nzmaxiter must be > 0.r   r   r   r   r   r   s     r   test_negative_maxiter TestDIRECT.test_negative_maxiter  s<    *	]]:Y74'')C)C 877r   c                     Sn[         R                  " [        US9   [        U R                  U R
                  SS9  S S S 5        g ! , (       d  f       g = f)Nzmaxfun must be > 0.r   r   r   r   r   s     r   test_negative_maxfunTestDIRECT.test_negative_maxfun  s<    )	]]:Y74'')C)C 877r   rc   )rc   r   r   c                     Sn[         R                  " [        US9   [        U R                  U5        S S S 5        g ! , (       d  f       g = f)Nz5bounds must be a sequence or instance of Bounds classr   rd   re   r   r   r@   r   rc   r   s      r   test_invalid_bounds_type#TestDIRECT.test_invalid_bounds_type%  s3    0	]]:Y74''0 877	   <
A
r   r   r"   c                     Sn[         R                  " [        US9   [        U R                  U5        S S S 5        g ! , (       d  f       g = f)Nz#Bounds are not consistent min < maxr   r   r   s      r   test_incorrect_bounds TestDIRECT.test_incorrect_bounds,  s3     :	]]:Y74''0 877r   c                     Sn[        [        R                  * S/S[        R                  /5      n[        R                  " [
        US9   [        U R                  U5        S S S 5        g ! , (       d  f       g = f)NzBounds must not be inf.r   r   r   )r   r   r6   rd   re   r   r   r@   )r   r   rc   s      r   test_inf_boundsTestDIRECT.test_inf_bounds6  sO    -	"&&"BFF|4]]:Y74''0 877s   	A))
A7biasr   r   c                     Sn[         R                  " [        US9   [        U R                  U R
                  US9  S S S 5        g ! , (       d  f       g = f)Nz%locally_biased must be True or False.r   rD   r   )r   rB   r   s      r   test_locally_biased_validation)TestDIRECT.test_locally_biased_validation<  s<    ;	]]:Y74'')C)C"02 877r   )r   r   r   r   r   r   N),__name__
__module____qualname____firstlineno__r   r(   r-   r2   r7   r@   rd   markparametrizerR   r^   rg   rj   rs   rw   r~   r   r   r   r   r   r   xslowr   r   r   r   r   r   r   r   r   r   r   r   r1   r   r   r   __static_attributes__r=   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   rd   numpyr   scipy.optimizer   r   r   r   r=   r   r   <module>r      s'   .   ) u2 u2r   