o
    h A                     @   s   d Z ddlmZmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
mZ dd	lZdd	lZdd
lmZmZmZ ddlmZ ddlmZ dd	lZG dd dZd	S )z4
Unit tests for the dual annealing global optimizer
    )dual_annealingBounds)EnergyState)LocalSearchWrapper)ObjectiveFunWrapper)StrategyChain)VisitingDistribution)rosen	rosen_derN)assert_equalassert_allcloseassert_array_less)raises)check_random_statec                   @   s  e Zd Zdd Zdd ZdMddZdMdd	Zej	d
g ddd Z
ej	d
g ddd Zdd Zdd Zejddd Zdd Zejddd Zdd Zdd Zd d! Zejdd"d# Zd$d% Zd&d' Zd(d) Zejjd*d+ Zejjd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Z d8d9 Z!ej	d:g d;d<d= Z"d>d? Z#d@dA Z$ejddBdC Z%ej	dDg dEdFdG Z&ejddHdI Z'ejddJdK Z(dLS )NTestDualAnnealingc                 C   sd   dd | _ dgd | _| jd | _d| _d| _d| _d	| _d
| _t| j| _	t
 | _t
 | _d S )Nc                 S   s   t jS N)npinfx r   ]/var/www/vscode/kcb/lib/python3.10/site-packages/scipy/optimize/tests/test__dual_annealing.py<lambda>   s    z0TestDualAnnealing.setup_method.<locals>.<lambda>{Gz{Gz@      i  in  g?(\@  )	weirdfunc	ld_bounds	hd_boundsnbtestvalueshigh_temperaturelow_temperatureqvseedr   rng	threadinglocalnb_fun_callngevselfr   r   r   setup_method   s   

zTestDualAnnealing.setup_methodc                 C   s   |dkrdS d S )N      ?Tr   )r.   r   fcontextr   r   r   callback,   s   zTestDualAnnealing.callbackr   c              	   C   sv   |r|}nd}t || d dt dt j ||    dt |  | }t| jds1d| j_| j jd7  _|S )Nr   r   
   c   )r   sumcospisizehasattrr+   r5   )r.   r   argsshiftyr   r   r   func2   s   
zTestDualAnnealing.funcc                 C   s4   t | jds
d| j_| j jd7  _t|g|R  S )Nr5   r   r6   )r;   r,   r5   r
   )r.   r   r<   r   r   r   rosen_der_wrapper?   s   z#TestDualAnnealing.rosen_der_wrapperr&   )g?g(\?r   r   333333@c           
      C   s   t t| j }t|d }t|d }|j}t|||| j}t|}|	|d| j
}tt|dd t|}|	||| j
}	tt|	d dd d S )Nr   r6   T)listzipr!   r   arrayr:   r   r(   zerosvisitingr$   r   	not_equal)
r.   r&   lulowerupperdimvdvalues
x_step_lowx_step_highr   r   r   test_visiting_steppingG   s   

z(TestDualAnnealing.test_visiting_stepping)g      @r   rA   c                 C   sn   t t| j }t|d }t|d }t|||| j}|| j| j	}t
t|d t
dt| d S )Nr   r6   g|=g    _B)rB   rC   r!   r   rD   r   r(   visit_fnr$   r#   r   minmax)r.   r&   rH   rI   rJ   rL   rM   r   r   r   #test_visiting_dist_high_temperatureW   s   z5TestDualAnnealing.test_visiting_dist_high_temperaturec                 C   sV   t | j}tt| j }t|d }t|d }t||}tt	|j
|td  d S )Nr   r6   )r   r    rB   rC   r!   r   rD   r   assert_raises
ValueErrorresetr   )r.   owfrH   rI   rJ   esr   r   r   
test_reseti   s   

zTestDualAnnealing.test_resetc                 C   2   t | j| j| jd}t|jddd |jsJ d S Nr(           g-q=atol)r   r?   r!   r'   r   funsuccessr.   retr   r   r   test_low_dimq   s
   zTestDualAnnealing.test_low_dimr4   c                 C   r[   r\   )r   r?   r"   r'   r   ra   rb   rc   r   r   r   test_high_dimw   s   zTestDualAnnealing.test_high_dimc                 C   *   t | j| jd| jd}t|jddd d S )NT)no_local_searchr'   r^   -C6?r_   r   r?   r!   r'   r   ra   rc   r   r   r   test_low_dim_no_ls}      
z$TestDualAnnealing.test_low_dim_no_lsc                 C   rg   )NTrh   r(   r^   giUMu?r_   )r   r?   r"   r'   r   ra   rc   r   r   r   test_high_dim_no_ls   s   
z%TestDualAnnealing.test_high_dim_no_lsc                 C   s0   d| j _t| j| j| jd}t| j j|j d S )Nr   r]   r+   r5   r   r?   r!   r'   r   nfevrc   r   r   r   test_nb_fun_call   s   z"TestDualAnnealing.test_nb_fun_callc                 C   s2   d| j _t| j| jd| jd}t| j j|j d S )Nr   Trm   ro   rc   r   r   r   test_nb_fun_call_no_ls   s
   
z(TestDualAnnealing.test_nb_fun_call_no_lsc                 C   s   t tt| j| j d S r   )rU   rV   r   r    r!   r-   r   r   r   test_max_reinit   s   
z!TestDualAnnealing.test_max_reinitc                 C   s\   t | j| j| jd}t | j| j| jd}t | j| j| jd}t|j|j t|j|j d S )Nr]   )r   r?   r!   r'   r   r   )r.   res1res2res3r   r   r   test_reproduce   s
   z TestDualAnnealing.test_reproducec                 C   sN   t jd}t| j| j|d}t jd}t| j| j|d}t|j|j d S )Nr6   r]   )r   randomdefault_rngr   r?   r!   r   r   )r.   r(   rt   ru   r   r   r   test_rand_gen   s
   zTestDualAnnealing.test_rand_genc                 C   s   g d}t tt| j| d S )N)r   )r6   r   )r   r   )rU   rV   r   r?   )r.   wrong_boundsr   r   r   test_bounds_integrity   s   
z'TestDualAnnealing.test_bounds_integrityc                 C   sf   dt j dfdg}ttt| j| ddt jfdg}ttt| j| ddt jfdg}ttt| j| d S )N)   r   )r   r   rU   rV   r   r?   nan)r.   invalid_boundsr   r   r   test_bound_validity   s   


z%TestDualAnnealing.test_bound_validityc                 C   sf   dd }t tddgddg}tjtdd t||d	|d
d W d    d S 1 s,w   Y  d S )Nc                 S      t | d | d  S Nr~   r6   r   r7   r   r   r   r   r?         zKTestDualAnnealing.test_deprecated_local_search_options_bounds.<locals>.funcr}      r~   Method CG cannot handle matchCGmethodboundsr   minimizer_kwargs)rB   rC   pytestwarnsRuntimeWarningr   r.   r?   r   r   r   r   +test_deprecated_local_search_options_bounds   s   "z=TestDualAnnealing.test_deprecated_local_search_options_boundsc                 C   sz   dd }t tddgddg}t||d|dd	 tjtd
d t||d|dd	 W d    d S 1 s6w   Y  d S )Nc                 S   r   r   r   r   r   r   r   r?      r   z<TestDualAnnealing.test_minimizer_kwargs_bounds.<locals>.funcr   r}   r   r~   SLSQPr   r   r   r   r   )rB   rC   r   r   r   r   r   r   r   r   test_minimizer_kwargs_bounds   s   "z.TestDualAnnealing.test_minimizer_kwargs_boundsc                 C   sV   t | j| jd| jd}ttt| jtj tj	tj
}|jd| ks$J |jr)J d S )Nd   )maxfunr(   )r   r?   r!   r'   rR   rS   lenr   LS_MAXITER_RATIOLS_MAXITER_MINLS_MAXITER_MAXrp   rb   )r.   rd   ls_max_iterr   r   r   test_max_fun_ls   s   z!TestDualAnnealing.test_max_fun_lsc                 C   s4   t | j| jdd| jd}|jdksJ |jrJ d S )NTi  )rh   r   r(   )r   r?   r!   r'   rp   rb   rc   r   r   r   test_max_fun_no_ls   s
   
z$TestDualAnnealing.test_max_fun_no_lsc                 C   s(   t | j| jd| jd}|jdksJ d S )Ni  )maxiterr(   )r   r?   r!   r'   nitrc   r   r   r   test_maxiter   s   zTestDualAnnealing.test_maxiterc                 C   rg   )Nn!	@)r<   r(   r   ư>r_   rj   rc   r   r   r   test_fun_args_ls   rl   z"TestDualAnnealing.test_fun_args_lsc                 C   s,   t | j| jdd| jd}t|jddd d S )Nr   T)r<   rh   r(   r   ri   r_   rj   rc   r   r   r   test_fun_args_no_ls   s
   
z%TestDualAnnealing.test_fun_args_no_lsc                 C   sF   t | j| j| j| jd}|jdksJ d|jd v sJ |jr!J d S )N)r3   r(   r0   z
stop earlyr   )r   r?   r!   r3   r'   ra   messagerb   rc   r   r   r   test_callback_stop   s   
z$TestDualAnnealing.test_callback_stopzmethod, atol))zNelder-Meadgh㈵>)COBYLAgh㈵>)COBYQA:0yE>)Powellr   )r   r   )BFGSr   )TNCr   )r   gH׊>c                 C   s0   t | j| jt|d| jd}t|jd|d d S )Nr   r   r(   r^   r_   )r   r?   r!   dictr'   r   ra   )r.   r   r`   rd   r   r   r   test_multi_ls_minimizer  s
   
z)TestDualAnnealing.test_multi_ls_minimizerc                 C   s0   t tt| j| jdd t tt| j| jdd d S )Nr6   )restart_temp_ratior   )rU   rV   r   r?   r!   r-   r   r   r   test_wrong_restart_temp  s   


z)TestDualAnnealing.test_wrong_restart_tempc                 C   s4   d| j i}tt| j|| jd}|j| jjksJ d S )Njacr   )r@   r   r	   r!   r'   njevr,   r5   )r.   minimizer_optsrd   r   r   r   test_gradient_gnev  s   z$TestDualAnnealing.test_gradient_gnevc                 C   s\   dd }dgd }dgd }t |tt||dd}t|jg dd	d
 t|jddd
 d S )Nc                 S   s4   t | |  dt dt j |    dt |   S Nr4   r   r   r7   r8   r9   r:   r   r   r   r   r?   )  s   4z3TestDualAnnealing.test_from_docstring.<locals>.funcr   r4   r   r   r   r(   )
g[tP2gf0g˯>g,1gft;gh<g#0g(<geԑ:gi5g:0ye>r_   r^   g-a=)r   rB   rC   r   r   ra   )r.   r?   lwuprd   r   r   r   test_from_docstring'  s   

z%TestDualAnnealing.test_from_docstringz'new_e, temp_step, accepted, accept_rate))r   r     g]'?)r   r   r   gr(?)r4   r   in  gD?)r4   <   i  gt%?)r   r   i  gw?c                 C   s   t d}d}d}d}d}	t|D ]-}
td d d}||	dg t|d d d ||}||_|jd|dgd |j|kr=|d7 }q||ksDJ dd| ||	  |  }|dkrVdnt	t
|d|  }t|| d S )	N{   r   r   r}   r6   )rI   rJ   r   )jex_visit)r   ranger   update_currentr   temperature_stepaccept_rejectcurrent_energyr   explogr   )r.   new_e	temp_stepacceptedaccept_raterscount_accepted
iterationsaccept_paramr   _energy_statechainpqvrater   r   r    test_accept_reject_probabilistic5  s(   
$z2TestDualAnnealing.test_accept_reject_probabilisticc                 C   s   dd }dgd }dgd }d|d< d|d	< d
|d< d|d< t ||}t||dd}tt||}t||dd}t|j|jdd t|jtdddd t|j|jdd |j	|j	ks^J d S )Nc                 S   s8   t | |  dt dt j |    dt |   }|S r   r   )r   r1   r   r   r   r?   b  s   4z1TestDualAnnealing.test_bounds_class.<locals>.funcr   r~   r   g       r   g      r6   r0      g       @r   r   r   r   r_   gHz>g&.>)
r   r   rB   rC   r   r   r   arangera   rp   )r.   r?   r   r   r   ret_bounds_class
bounds_oldret_bounds_listr   r   r   test_bounds_class_  s   


z#TestDualAnnealing.test_bounds_classc           	   
      s  t jd}dd }dd }dd   fdd	}t|d
ddgddgg|tddd}t|d
ddgddgg|td|dd}t|d
ddgddgg|td| dd}t|d
ddgddgg|td||dd}t|j|jdd t|j|jdd t|j|jdd d S )Nl   ]k@A`Q c                 S   s   t t | | S r   )r   r7   r   r   powerr   r   r   r1     s   zETestDualAnnealing.test_callable_jac_hess_with_args_gh11052.<locals>.fc                 S   s   t | | | | |d   S )Nr6   )r   r   r   r   r   r   r     s   zGTestDualAnnealing.test_callable_jac_hess_with_args_gh11052.<locals>.jacc                 S   s8   t |t | |  | |d   || |  | d  S )Nr   r6   )r   diagr   r   r   r   r   hess  s
   zHTestDualAnnealing.test_callable_jac_hess_with_args_gh11052.<locals>.hessc                    s    | || S r   r   )r   pr   r   r   r   hessp  s   zITestDualAnnealing.test_callable_jac_hess_with_args_gh11052.<locals>.hessp)r   r   r6   zL-BFGS-Br   )r<   r   r(   r   )r   r   z	newton-cg)r   r   r   )r   r   r   r   )rtol)r   rx   ry   r   r   r   ra   )	r.   r(   r1   r   r   rt   ru   rv   res4r   r   r   (test_callable_jac_hess_with_args_gh11052|  s4   z:TestDualAnnealing.test_callable_jac_hess_with_args_gh11052N)r   ))__name__
__module____qualname__r/   r3   r?   r@   r   markparametrizerP   rT   rZ   re   	fail_slowrf   rk   rn   rq   rr   rs   rw   rz   r|   r   thread_unsafer   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s\    









	

	


	


#

r   )__doc__scipy.optimizer   r   scipy.optimize._dual_annealingr   r   r   r   r   r	   r
   r   numpyr   numpy.testingr   r   r   r   rU   scipy._lib._utilr   r)   r   r   r   r   r   <module>   s   