o
    g7                     @   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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_state)Versionc                   @   sX  e Zd Zdd Zdd ZdJddZdJd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%dIS )K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   g/home/ubuntu/cloudmapper/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?(\@  r   )	weirdfunc	ld_bounds	hd_boundsnbtestvalueshigh_temperature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   func1   s   
zTestDualAnnealing.funcc                 C   s   |  j d7  _ t|g|R  S )Nr4   )r+   r
   )r-   r   r9   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   r4   T)listzipr"   r   arrayr8   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_steppingB   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   r4   g|=g    _B)r?   r@   r"   r   rA   r   r)   visit_fnr%   r$   r   minmax)r-   r'   rE   rF   rG   rI   rJ   r   r   r   #test_visiting_dist_high_temperatureR   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   r4   )r   r!   r?   r@   r"   r   rA   r   assert_raises
ValueErrorresetr   )r-   owfrE   rF   rG   esr   r   r   
test_resetd   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_diml   s
   zTestDualAnnealing.test_low_dimc                 C   rX   rY   )r   r<   r#   r(   r   r^   r_   r`   r   r   r   test_high_dimr   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   r^   r`   r   r   r   test_low_dim_no_lsw      
z$TestDualAnnealing.test_low_dim_no_lsc                 C   rd   re   )r   r<   r#   r(   r   r^   r`   r   r   r   test_high_dim_no_ls|   rk   z%TestDualAnnealing.test_high_dim_no_lsc                 C   s&   t | j| j| jd}t| j|j d S NrZ   r   r<   r"   r(   r   r*   nfevr`   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 )NTrf   rn   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   )rR   rS   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 rm   )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 )Nr4   rZ   )r   randomdefault_rngr   r<   r"   r   r   )r-   rngrs   rt   r   r   r   test_rand_gen   s
   zTestDualAnnealing.test_rand_genc                 C   s   g d}t tt| j| d S )N)r   )r4   r   )r   r   )rR   rS   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   rR   rS   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~   r4   r   r5   r   r   r   r   r          zOTestDualAnnealing.test_deprecated_local_search_options_bounds.<locals>.<lambda>r}      r~   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   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>.<lambda>r   r}   r   r~   SLSQPr   r   r   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s$J |jr)J d S )Nd   )maxfunr(   )r   r<   r"   r(   rO   rP   lenr   LS_MAXITER_RATIOLS_MAXITER_MINLS_MAXITER_MAXro   r_   )r-   ra   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  )rg   r   r(   )r   r<   r"   r(   ro   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   rd   )Nn!	@)r9   r(   r   gư>r\   ri   r`   r   r   r   test_fun_args_ls   rk   z"TestDualAnnealing.test_fun_args_lsc                 C   s,   t | j| jdd| jd}t|jddd d S )Nr   T)r9   rg   r(   r   rh   r\   ri   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(   r^   messager_   r`   r   r   r   test_callback_stop   s   
z$TestDualAnnealing.test_callback_stopzmethod, atol))zNelder-Meadgh㈵>)COBYLAgh㈵>)Powell:0yE>)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   r^   )r-   r   r]   ra   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 )Nr4   )restart_temp_ratior   )rR   rS   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(   njevr+   )r-   minimizer_optsra   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   r5   r6   r7   r8   r   r   r   r   r     s   4 z7TestDualAnnealing.test_from_docstring.<locals>.<lambda>r   r3   r   r    r   r(   )
g[tP2gf0g˯>g,1gft;gh<g#0g(<geԑ:gi5g:0ye>r\   r[   g-a=)r   r?   r@   r   r   r^   )r-   r<   lwupra   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   r}   r4   )rF   rG   r   )jex_visit)r   ranger   update_currentr   temperature_stepaccept_rejectcurrent_energyr   explogr   )r-   new_e	temp_stepacceptedaccept_rater)   count_accepted
iterationsaccept_paramr   _energy_statechainpqv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s^J d S )Nc                 S   s8   t | |  dt dt j |    dt |   }|S r   r   )r   r0   r   r   r   r<   P  s   4z1TestDualAnnealing.test_bounds_class.<locals>.funcr   r~   r   g       r   g      r4   r/      g       @r   r    r   r   r\   gHz>g&.>)
r   r   r?   r@   r   r   r   aranger^   ro   )r-   r<   r   r   r   ret_bounds_class
bounds_oldret_bounds_listr   r   r   test_bounds_classN  s   


z#TestDualAnnealing.test_bounds_classN)r   )&__name__
__module____qualname__r.   r2   r<   r=   r   markparametrizerM   rQ   rW   rb   rc   rj   rl   rp   rq   rr   rv   rz   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      sJ    



		
		
#r   )__doc__scipy.optimizer   r   scipy.optimize._dual_annealingr   r   r   r   r   r	   r
   r   numpyr   numpy.testingr   r   r   r   rR   scipy._lib._utilr   scipy._lib._pep440r   r   r   r   r   r   <module>   s   