o
    i 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   c/home/kim/smarthome/.venv/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_temperatureZ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*   r4   )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 )Nr4   r   r5   )r:   r+   r4   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   r5   T)listzipr!   r   arrayr9   r   r'   ZzerosZvisitingr$   r   	not_equal)
r-   r%   lulowerupperdimvdvaluesZ
x_step_lowZx_step_highr   r   r   test_visiting_steppingG   s   

z(TestDualAnnealing.test_visiting_stepping)g      @r   r@   c                 C   sn   t t| j }t|d }t|d }t|||| j}|| j| j	}t
t|d t
dt| d S )Nr   r5   g|=g    _B)rA   rB   r!   r   rC   r   r'   Zvisit_fnr$   r#   r   minmax)r-   r%   rE   rF   rG   rI   rJ   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   r5   )r   r    rA   rB   r!   r   rC   r   assert_raises
ValueErrorresetr   )r-   ZowfrE   rF   rG   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_dimr3   c                 C   rT   rU   )r   r>   r"   r&   r   rZ   r[   r\   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&   rW   -C6?rX   r   r>   r!   r&   r   rZ   r\   r   r   r   test_low_dim_no_ls}      
z$TestDualAnnealing.test_low_dim_no_lsc                 C   r`   )NTra   r'   rW   giUMu?rX   )r   r>   r"   r&   r   rZ   r\   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   rV   r*   r4   r   r>   r!   r&   r   nfevr\   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   Trf   rh   r\   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   )rO   rP   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 )NrV   )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 )Nr5   rV   )r   randomdefault_rngr   r>   r!   r   r   )r-   r'   rm   rn   r   r   r   test_rand_gen   s
   zTestDualAnnealing.test_rand_genc                 C   s   g d}t tt| j| d S )N)r   )r5   r   )r   r   )rO   rP   r   r>   )r-   Z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   rO   rP   r   r>   nan)r-   Z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 Nrv   r5   r   r6   r   r   r   r   r>         zKTestDualAnnealing.test_deprecated_local_search_options_bounds.<locals>.funcru      rv   Method CG cannot handle matchCGmethodboundsr   minimizer_kwargs)rA   rB   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   ry   rz   r{   r   r   r   r   r>      r|   z<TestDualAnnealing.test_minimizer_kwargs_bounds.<locals>.funcr}   ru   r~   rv   SLSQPr   r   r   r   r   )rA   rB   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&   rL   rM   lenr   ZLS_MAXITER_RATIOZLS_MAXITER_MINZLS_MAXITER_MAXri   r[   )r-   r]   Z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  )ra   r   r'   )r   r>   r!   r&   ri   r[   r\   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&   nitr\   r   r   r   test_maxiter   s   zTestDualAnnealing.test_maxiterc                 C   r`   )Nn!	@)r;   r'   r   ư>rX   rc   r\   r   r   r   test_fun_args_ls   re   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;   ra   r'   r   rb   rX   rc   r\   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)r2   r'   r/   z
stop earlyr   )r   r>   r!   r2   r&   rZ   messager[   r\   r   r   r   test_callback_stop   s   
z$TestDualAnnealing.test_callback_stopzmethod, atol))zNelder-Meadgh㈵>)ZCOBYLAgh㈵>)ZCOBYQA:0yE>)ZPowellr   )r   r   )ZBFGSr   )ZTNCr   )r   gH׊>c                 C   s0   t | j| jt|d| jd}t|jd|d d S )Nr   r   r'   rW   rX   )r   r>   r!   dictr&   r   rZ   )r-   r   rY   r]   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 )Nr5   )Zrestart_temp_ratior   )rO   rP   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&   Znjevr+   r4   )r-   Zminimizer_optsr]   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 Nr3   r   r   r6   r7   r8   r9   r   r   r   r   r>   )  s   4z3TestDualAnnealing.test_from_docstring.<locals>.funcr   r3   r   r   r   r'   )
g[tP2gf0g˯>g,1gft;gh<g#0g(<geԑ:gi5g:0ye>rX   rW   g-a=)r   rA   rB   r   r   rZ   )r-   r>   lwupr]   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(?)r3   r   in  gD?)r3   <   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   ru   r5   )rF   rG   r   )jeZx_visit)r   ranger   Zupdate_currentr   Ztemperature_stepZaccept_rejectcurrent_energyr   explogr   )r-   Znew_eZ	temp_stepacceptedZaccept_ratersZcount_acceptedZ
iterationsZaccept_paramr   _Zenergy_statechainZpqvZ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   r0   r   r   r   r>   b  s   4z1TestDualAnnealing.test_bounds_class.<locals>.funcr   rv   r   g       r   g      r5   r/      g       @r   r   r   r   rX   gHz>g&.>)
r   r   rA   rB   r   r   r   ZarangerZ   ri   )r-   r>   r   r   r   Zret_bounds_classZ
bounds_oldZ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   r6   r   r   powerr   r   r   r0     s   zETestDualAnnealing.test_callable_jac_hess_with_args_gh11052.<locals>.fc                 S   s   t | | | | |d   S )Nr5   )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   r5   )r   Z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   r5   zL-BFGS-Br   )r;   r   r'   r   )r   r   z	newton-cg)r   r   r   )r   r   r   r   )Zrtol)r   rq   rr   r   r   r   rZ   )	r-   r'   r0   r   r   rm   rn   ro   Z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.   r2   r>   r?   r   markZparametrizerK   rN   rS   r^   Z	fail_slowr_   rd   rg   rj   rk   rl   rp   rs   rt   rx   Zthread_unsafer   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s\    









	

	

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