a
    ReE;                     @   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 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dLd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dd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zej	d9g d:d;d< Z d=d> Z!d?d@ Z"dAdB Z#ej	dCg dDdEdF Z$dGdH Z%dIdJ Z&dKS )NTestDualAnnealingc                 C   s\   dd | _ dgd | _| jd | _d| _d| _d| _d	| _d
| _t| j| _	d| _
d| _d S )Nc                 S   s   t jS N)npinfx r   ]/var/www/sunrise/env/lib/python3.9/site-packages/scipy/optimize/tests/test__dual_annealing.py<lambda>       z0TestDualAnnealing.setup_method.<locals>.<lambda>{Gz{Gz@      i  in  g?(\@  r   )	weirdfunc	ld_bounds	hd_boundsnbtestvalueshigh_temperatureZlow_temperatureqvseedr   rs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   s`   |r
|}nd}t || d dt dt j ||    dt |  | }|  jd7  _|S )Nr   r   
      )r   sumcospisizer)   )r,   r   argsshiftyr   r   r   func0   s    
zTestDualAnnealing.funcc                 C   s   |  j d7  _ t|g|R  S Nr3   )r*   r
   )r,   r   r8   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   r3   T)listzipr"   r   arrayr7   r   r(   zerosZvisitingr%   r   	not_equal)
r,   r&   lulowerupperZdimvdvaluesZ
x_step_lowZx_step_highr   r   r   test_visiting_steppingA   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   r3   g|=g    _B)r?   r@   r"   r   rA   r   r(   Zvisit_fnr%   r$   r   minmax)r,   r&   rD   rE   rF   rG   rH   r   r   r   #test_visiting_dist_high_temperatureQ   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   r3   )r   r!   r?   r@   r"   r   rA   r   assert_raises
ValueErrorresetr   )r,   ZowfrD   rE   rF   esr   r   r   
test_resetc   s    

zTestDualAnnealing.test_resetc                 C   s2   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_dimk   s
    zTestDualAnnealing.test_low_dimc                 C   s2   t | j| j| jd}t|jddd |js.J d S rR   )r   r;   r#   r'   r   rW   rX   rY   r   r   r   test_high_dimq   s    zTestDualAnnealing.test_high_dimc                 C   s*   t | j| jd| jd}t|jddd d S NTno_local_searchr'   rT   -C6?rU   r   r;   r"   r'   r   rW   rY   r   r   r   test_low_dim_no_lsv   s    
z$TestDualAnnealing.test_low_dim_no_lsc                 C   s*   t | j| jd| jd}t|jddd d S r]   )r   r;   r#   r'   r   rW   rY   r   r   r   test_high_dim_no_ls{   s    
z%TestDualAnnealing.test_high_dim_no_lsc                 C   s&   t | j| j| jd}t| j|j d S NrS   r   r;   r"   r'   r   r)   nfevrY   r   r   r   test_nb_fun_call   s    z"TestDualAnnealing.test_nb_fun_callc                 C   s(   t | j| jd| jd}t| j|j d S )NTr^   re   rY   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   )rM   rN   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 rd   )r   r;   r"   r'   r   r   )r,   res1res2Z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 )Nr3   rS   )r   randomdefault_rngr   r;   r"   r   r   )r,   rngrj   rk   r   r   r   test_rand_gen   s
    zTestDualAnnealing.test_rand_genc                 C   s   g d}t tt| j| d S )N)r   )r3   r   )r   r   )rM   rN   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   rM   rN   r   r;   nan)r,   Zinvalid_boundsr   r   r   test_bound_validity   s    


z%TestDualAnnealing.test_bound_validityc                 C   sd   dd }t tddgddg}tjtdd$ t||d	|d
d W d    n1 sV0    Y  d S )Nc                 S   s   t | d | d  S Nrs   r3   r   r4   r   r   r   r   r;      s    zKTestDualAnnealing.test_deprecated_local_search_options_bounds.<locals>.funcrr      rs   Method CG cannot handle matchCGmethodboundsr   minimizer_kwargs)r?   r@   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   sx   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    n1 sj0    Y  d S )Nc                 S   s   t | d | d  S rv   rw   r   r   r   r   r;      s    z<TestDualAnnealing.test_minimizer_kwargs_bounds.<locals>.funcrx   rr   ry   rs   SLSQPr~   r   rz   r{   r}   )r?   r@   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sHJ |jrRJ d S )Nd   )maxfunr'   )r   r;   r"   r'   rJ   rK   lenr   ZLS_MAXITER_RATIOZLS_MAXITER_MINZLS_MAXITER_MAXrf   rX   )r,   rZ   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r0J d S )NTi  )r_   r   r'   )r   r;   r"   r'   rf   rX   rY   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'   ZnitrY   r   r   r   test_maxiter   s    zTestDualAnnealing.test_maxiterc                 C   s*   t | j| jd| jd}t|jddd d S )Nn!	@)r8   r'   r   ư>rU   ra   rY   r   r   r   test_fun_args_ls   s    
z"TestDualAnnealing.test_fun_args_lsc                 C   s,   t | j| jdd| jd}t|jddd d S )Nr   T)r8   r_   r'   r   r`   rU   ra   rY   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 s8J |jrBJ d S )N)r1   r'   r.   z
stop earlyr   )r   r;   r"   r1   r'   rW   messagerX   rY   r   r   r   test_callback_stop   s    
z$TestDualAnnealing.test_callback_stopzmethod, atol))zNelder-Meadgh㈵>)ZCOBYLAgh㈵>)ZPowell:0yE>)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'   rT   rU   )r   r;   r"   dictr'   r   rW   )r,   r   rV   rZ   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 )Nr3   )Zrestart_temp_ratior   )rM   rN   r   r;   r"   r+   r   r   r   test_wrong_restart_temp
  s    

z)TestDualAnnealing.test_wrong_restart_tempc                 C   s2   d| j i}tt| j|| jd}|j| jks.J d S )Njacr   )r=   r   r	   r"   r'   Znjevr*   )r,   Zminimizer_optsrZ   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 Nr2   r   r   r4   r5   r6   r7   r   r   r   r   r;     s    z3TestDualAnnealing.test_from_docstring.<locals>.funcr   r2   r   r    r   r'   )
g[tP2gf0g˯>g,1gft;gh<g#0g(<geԑ:gi5g:0ye>rU   rT   g-a=)r   r?   r@   r   r   rW   )r,   r;   lwuprZ   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(?)r2   r   in  gD?)r2   <   i  gt%?)r   r   i  gw?c                 C   s   t d}d}d}d}d}	t|D ]Z}
td d d}||	dg t|d d d ||}||_|jd|dgd |j|kr |d7 }q ||ksJ dd| ||	  |  }|dkrdnt	t
|d|  }t|| d S )	N{   r   r   rr   r3   )rE   rF   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_rater(   Zcount_acceptedZ
iterationsZaccept_paramr   _Zenergy_statechainZpqvZrater   r   r    test_accept_reject_probabilistic&  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sJ d S )Nc                 S   s8   t | |  dt dt j |    dt |   }|S r   r   )r   r/   r   r   r   r;   R  s    4z1TestDualAnnealing.test_bounds_class.<locals>.funcr   rs   r   g       r   g      r3   r.      g       @r   r    r   r   rU   gHz>g&.>)
r   r   r?   r@   r   r   r   ZarangerW   rf   )r,   r;   r   r   r   Zret_bounds_classZ
bounds_oldZret_bounds_listr   r   r   test_bounds_classP  s    


z#TestDualAnnealing.test_bounds_classc              	   C   s|   t jd}d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 d S )Nl   ]k@A`Q c                 S   s   t t | | S r   )r   r4   r   r   powerr   r   r   r/   p  s    z@TestDualAnnealing.test_callable_jac_with_args_gh11052.<locals>.fc                 S   s   t | | | | |d   S r<   )r   r   r   r   r   r   r   s  s    zBTestDualAnnealing.test_callable_jac_with_args_gh11052.<locals>.jac)r   r   r3   zL-BFGS-Br   )r8   r   r'   r   )r   r   r   )Zrtol)r   rm   rn   r   r   r   rW   )r,   ro   r/   r   rj   rk   r   r   r   #test_callable_jac_with_args_gh11052l  s    z5TestDualAnnealing.test_callable_jac_with_args_gh11052N)r   )r   )'__name__
__module____qualname__r-   r1   r;   r=   r   markZparametrizerI   rL   rQ   r[   r\   rb   rc   rg   rh   ri   rl   rp   rq   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      sJ   



			
	
#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   rM   Zscipy._lib._utilr   r   r   r   r   r   <module>   s   