o
    4Bi                    @   s   d Z ddlZddlZddlmZmZ ddlmZmZ ddl	m
Z
mZmZ ddlmZmZ ddlmZ ddlmZ ddlZdd	lmZmZmZmZmZmZ dd
lmZmZ ddlZG dd dZ dS )z@
Unit tests for the differential global minimization algorithm.
    N)DifferentialEvolutionSolver_ConstraintWrapper)differential_evolutionOptimizeResult)BoundsNonlinearConstraintLinearConstraint)rosenminimize)
csr_matrix)stats)assert_equalassert_allcloseassert_almost_equalassert_string_equalassert_suppress_warnings)raiseswarnsc                   @   s4  e Z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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d9d: Zd;d< Z d=d> Z!d?d@ Z"dAdB Z#dCdD Z$dEdF Z%dGdH Z&dIdJ Z'dKdL Z(dMdN Z)dOdP Z*dQdR Z+dSdT Z,dUdV Z-dWdX Z.dYdZ Z/d[d\ Z0d]d^ Z1d_d` Z2dadb Z3dcdd Z4dedf Z5dgdh Z6didj Z7dkdl Z8dmdn Z9dodp Z:dqdr Z;e<j=j>e<j=j?e@A dskdtdudvdw ZBdxdy ZCdzd{ ZDd|d} ZEd~d ZFdd ZGdd ZHdd ZIdS )TestDifferentialEvolutionSolverc                 C   sx   t jdd| _t ddgddgg| _ddg| _t| jdg| _t| jdgdd	d
| _	t 
t dddj}|| j	_d S )Nraiseinvalid               @r   r   r   d   r               ?)popsizemutation皙?皙?)npseterr
old_seterrarraylimitsboundsr   	quadraticdummy_solverdummy_solver2
atleast_2darangeT
population)selfr2    r4   q/var/www/html/Trade-python/venv/lib/python3.10/site-packages/scipy/optimize/tests/test__differential_evolution.pysetup_method   s   

z,TestDifferentialEvolutionSolver.setup_methodc                 C   s   t jdi | j d S )Nr4   )r&   r'   r(   r3   r4   r4   r5   teardown_method,   s   z/TestDifferentialEvolutionSolver.teardown_methodc                 C   s   |d d S )Nr      r4   )r3   xr4   r4   r5   r,   /   s   z)TestDifferentialEvolutionSolver.quadraticc                 C   s  t t| jdd}t|jd t|jjd t t| jdd}t|jd t|jjd t t| jdd}t|jd t|jjd t t| jdd}t|jd t|jjd t t| jdd}t|jd t|jjd	 t t| jd
d}t|jd
 t|jjd t t| jdd}t|jd t|jjd	 t t| jdd}t|jd t|jjd	 t t| jdd}t|jd t|jjd t t| jdd}t|jd t|jjd t t| jdd}t|jd t|jjd t t| jdd}t|jd t|jjd d S )Nbest1expstrategy_best1best1binrand1bin_rand1rand1exprand2exp_rand2best2bin_best2rand2binrandtobest1bin_randtobest1randtobest1expcurrenttobest1bin_currenttobest1currenttobest1exp)r   r	   r+   r   r=   mutation_func__name__r3   solverr4   r4   r5   test__strategy_resolves2   s   z7TestDifferentialEvolutionSolver.test__strategy_resolvesc                 C   H   t dg}| jd}t|| t dg}| jd}t|| d S )Ng?r9                     ?)r&   r)   r.   r>   r   rA   r3   resulttrialr4   r4   r5   test__mutate1}   s   
z-TestDifferentialEvolutionSolver.test__mutate1c                 C   rS   )NgrT   r$   )r&   r)   r.   rF   r   rD   rZ   r4   r4   r5   test__mutate2   s   
z-TestDifferentialEvolutionSolver.test__mutate2c                 C   s&   t dg}| jd}t|| d S )Ng333333?rT   )r&   r)   r.   rI   r   rZ   r4   r4   r5   test__randtobest1   s   z1TestDifferentialEvolutionSolver.test__randtobest1c                 C   s(   t dg}| jdd}t|| d S )Nr$   r   rT   )r&   r)   r.   rL   r   rZ   r4   r4   r5   test__currenttobest1   s   z4TestDifferentialEvolutionSolver.test__currenttobest1c                 C   s*   d}t | j| j|d}t|jt| d S )Nr!   r   r#   )r   r,   r+   r   ditherlist)r3   r#   rQ   r4   r4   r5   test_can_init_with_dithering   s   z<TestDifferentialEvolutionSolver.test_can_init_with_ditheringc                 C   s   t }d}ttt|| j|d d}ttt|| j|d dtjf}ttt|| j|d d}t|| j|d}td|j td |j	 d S )N)r!   rU   rb   r   r$   r!   )
r	   assert_raises
ValueErrorr   r+   r&   nanr   scalerc   )r3   funcr#   rQ   r4   r4   r5   +test_invalid_mutation_values_arent_accepted   s:   
zKTestDifferentialEvolutionSolver.test_invalid_mutation_values_arent_acceptedc                 C   sH   dd }t tdd t|ddg W d    d S 1 sw   Y  d S )Nc                 S   s   t t | d t | gS Nr9   )r&   r)   sumr:   r4   r4   r5   rl      s   zETestDifferentialEvolutionSolver.test_invalid_functional.<locals>.funcz,func\(x, \*args\) must return a scalar valuematchr9   )rh   RuntimeErrorr   )r3   rl   r4   r4   r5   test_invalid_functional   s   "z7TestDifferentialEvolutionSolver.test_invalid_functionalc                 C   J   t dg}td| j| t dgdgg| j_td| j| d S )N333333?   r   r   )r&   r)   r   r-   _scale_parametersr*   r3   r\   r4   r4   r5   test__scale_parameters      z6TestDifferentialEvolutionSolver.test__scale_parametersc                 C   rw   )Nry   rx   r   r   )r&   r)   r   r-   _unscale_parametersr*   r{   r4   r4   r5   test__unscale_parameters   r}   z8TestDifferentialEvolutionSolver.test__unscale_parametersc                 C   sz   t jdd t| jddgd}|ddg}t|d W d    n1 s&w   Y  t| jddg}t|jdd	g d S )
Nr   r   r   r   )      ?      @)r+   r   r!   )r   r   r   )	r&   errstater   r,   r~   r   r   r   r:   )r3   rQ   vresr4   r4   r5   test_equal_bounds   s   z1TestDifferentialEvolutionSolver.test_equal_boundsc                 C   sH   t g d}| j| t|d d tt |dk|dk  d S )N)皙??r   g     r@gh㈵r9   r   r   r   )r&   r)   r-   _ensure_constraintr   r   logical_andallr{   r4   r4   r5   test__ensure_constraint   s    z7TestDifferentialEvolutionSolver.test__ensure_constraintc                 C   sd   t | jdgddd}| }t|j| |j t | jdgddd}| }t|j| |j d S )Nrs   r   F)maxiterpolishT)r   r,   solver   funr:   r3   rQ   r[   r4   r4   r5   test_differential_evolution   s   z;TestDifferentialEvolutionSolver.test_differential_evolutionc                 C   s(   t | jdg}| }t|j|j d S )Nrs   )r   r,   r   r   r:   r   r4   r4   r5   test_best_solution_retrieval   s   z<TestDifferentialEvolutionSolver.test_best_solution_retrievalc                    sX  dfddd _ tj_ fdd d _d  _ _ddg}t|dd	d
}tdi |d i}tdi |di}|j	|j	  u rNd	u sQJ  J  j|j  kr^ksaJ  J |j
dkshJ |j
dksoJ | D ]\}}|dv r|qst j| | t|| | qsd _tj_d|d< tdi |d i}|j|jk sJ d S )N
   c                    s"   t | }| jk r|  _| _|S N)r	   valr:   )r:   r   rl   r4   r5   rl     s
   
zFTestDifferentialEvolutionSolver.test_intermediate_result.<locals>.funcc                    s    j d7  _ |  _| jjdksJ | jjd dksJ | j  j ks$J t| j jj t| j jj	 t| jt
| j tt| jD ]}| j| }t
| j| }t|| qDt| j| jd  t| j| jd  | jdksqJ | jdu sxJ t| tsJ  j krtd S )Nr   r9   r   zin progressT)nitintermediate_resultr2   ndimshaper   r:   rl   r   r   r	   rangelenpopulation_energiesmessagesuccess
isinstancer   StopIteration)r   ir   ref)callbackr   r4   r5   r     s0   

zJTestDifferentialEvolutionSolver.test_intermediate_result.<locals>.callbackr   r   r9   ie F)rl   r+   seedr   r   r   &callback function requested stop early/Maximum number of iterations has been exceeded.>   r   r   Tr   r4   )r:   r&   infr   r   r   rl   dictr   r   r   itemsr   r   )r3   r+   kwargsr   r   fieldr   r4   )r   rl   r   r5   test_intermediate_result  s4     z8TestDifferentialEvolutionSolver.test_intermediate_resultc                 C   s   ddg}d}ddd}t t||d}t|j| dd }t t||d}|jr(J dd	d
}t t||d}t|j| |jr?J ddd}t t||d}|jsPJ d S )Nr   r   r   c                 S   s   dS )NTr4   paramconvergencer4   r4   r5   callback_python_trueS     zVTestDifferentialEvolutionSolver.test_callback_terminates.<locals>.callback_python_true)r   c                 S   s   t r   )r   )r   r4   r4   r5   callback_stop\  r   zOTestDifferentialEvolutionSolver.test_callback_terminates.<locals>.callback_stopc                 S   s   dgS )Nr   r4   r   r4   r4   r5   callback_evaluates_trueb  s   zYTestDifferentialEvolutionSolver.test_callback_terminates.<locals>.callback_evaluates_truec                 S   s   g S r   r4   r   r4   r4   r5   callback_evaluates_falsej  r   zZTestDifferentialEvolutionSolver.test_callback_terminates.<locals>.callback_evaluates_false)r   )r   r	   r   r   r   )r3   r+   expected_msgr   r[   r   r   r   r4   r4   r5   test_callback_terminatesO  s&   




z8TestDifferentialEvolutionSolver.test_callback_terminatesc                 C   s2   dg}d}dd }t |||dd}t|jd d S )Nr   )r   r   r   c                 W   s8   t |tkr
td|d |d |   |d | d   S )Nzargs should be a tupler   r   r9   r   )typetupleri   )r:   argsr4   r4   r5   r,   v  s   $zLTestDifferentialEvolutionSolver.test_args_tuple_is_passed.<locals>.quadraticT)r   r   gUUUUUU?)r   r   r   )r3   r+   r   r,   r[   r4   r4   r5   test_args_tuple_is_passedq  s   z9TestDifferentialEvolutionSolver.test_args_tuple_is_passedc                 C   s    t }dg}ttt||dd d S )NrU   abcr<   )r	   rh   ri   r   )r3   rl   r+   r4   r4   r5   test_init_with_invalid_strategy  s   
z?TestDifferentialEvolutionSolver.test_init_with_invalid_strategyc                 C   sV   t }dg}ttt|| ddg}ttt|| tt tddgddg}t|jd d S )Nr   r   )rU   rV   rW   r   r9   )r   r   )r	   rh   ri   r   r   r   r:   )r3   rl   r+   r[   r4   r4   r5   test_bounds_checking  s   z4TestDifferentialEvolutionSolver.test_bounds_checkingc           
      C   s   t jddddd}tt|dd d f |dd d f }td |dd}d}||d	\}}}}}	ttt 	t 
||||||	gd d S )
Ng      (@float64)dtyper9   rX   r   r   r"   rW   )r&   r0   reshaperd   zipr   _select_samplesr   r   uniquer)   )
r3   r*   r+   rQ   	candidater1r2r3r4r5r4   r4   r5   test_select_samples  s   & z3TestDifferentialEvolutionSolver.test_select_samplesc                 C   s4   t t| jdd}| }t|jd t|jd d S )Nr   )r   Fr   )r   r	   r+   r   r   r   r   r   r4   r4   r5   test_maxiter_stops_solve  s   z8TestDifferentialEvolutionSolver.test_maxiter_stops_solvec                 C   s   t t| jddd}| }t|jd t|jd t|jd t t| jdddd}| }t|jd	 t|jd t|jd t t| jddd
dd}| }t|jd
 t|jd t|jd d S )Nr   F)maxfunr   r9   z9Maximum number of function evaluations has been exceeded.rW   (   )r"   r   r   )   /   deferred)r"   r   r   updatingz8Maximum number of function evaluations has been reached.)r   r	   r+   r   r   nfevr   r   r   r4   r4   r5   test_maxfun_stops_solve  sD   
	z7TestDifferentialEvolutionSolver.test_maxfun_stops_solvec                 C   s0   t | jdgdd}|  tt|jd d S )Nr   r   {Gz?tolr   )r   r,   r   r   r&   argminr   rP   r4   r4   r5   test_quadratic  s   z.TestDifferentialEvolutionSolver.test_quadraticc                 C   s   t | jdgdd d S )Nr   r   r   )r   r,   r7   r4   r4   r5   test_quadratic_from_diff_ev  s   
z;TestDifferentialEvolutionSolver.test_quadratic_from_diff_evc                 C   sL   t | jdgdddd}t | jdgdddd}t|j|j t|j|j d S )Nr   Fr   r!   )r   r   r   )r   r,   r   r:   r   )r3   r[   result2r4   r4   r5   test_seed_gives_repeatability  s   z=TestDifferentialEvolutionSolver.test_seed_gives_repeatabilityc              	   C   s8   t j }g d}|D ]}t| jdgd|d|d qd S )N)randomlatinhypercubesobolhaltonr   Fr!   )r   r   r   init)r&   r   default_rngr   r,   )r3   rnginitsr   r4   r4   r5   test_random_generator  s   
z5TestDifferentialEvolutionSolver.test_random_generatorc                 C   s   t t| jddd}|  d S )Nr;   r   )r=   r   r   r	   r+   r   rP   r4   r4   r5   test_exp_runs
  s   z-TestDifferentialEvolutionSolver.test_exp_runsc                 C   s   ddg}t t|ddd d S )N)rW   1   r   )r"   r   )r   r	   )r3   r+   r4   r4   r5   test_gh_4511_regression  s   	z7TestDifferentialEvolutionSolver.test_gh_4511_regressionc                 C   sF   t t| jdd}||j |  tt|j	d t|j
d d S )NrU   r   r   rX   )r   r	   r+   _calculate_population_energiesr2   _promote_lowest_energyr   r&   r   r   _nfevrP   r4   r4   r5   "test_calculate_population_energies#  s
   zBTestDifferentialEvolutionSolver.test_calculate_population_energiesc           
      C   s   t t| jddd}t|\}}tt|dd t|jd tt	t| t t| j}t|\}}t
|D ]\}}|\}}	||	ksBJ ||	}}|dkrN d S q4d S )NrU      )r"   r   r   r9   2   )r   r	   r+   nextr   r&   sizer   rh   r   	enumerate)
r3   rQ   r:   r   _fun_prevr   soln	x_currentfun_currentr4   r4   r5   test_iteration-  s"   

z.TestDifferentialEvolutionSolver.test_iterationc                 C   s,   t t| jddd}|  t|jdk  d S )N皙?F)r   r   )r   r	   r+   r   r   r   rP   r4   r4   r5   test_convergenceG  s
   
z0TestDifferentialEvolutionSolver.test_convergencec                 C   s   t t| jd d d}|  d S )N)r   r   r   rP   r4   r4   r5   test_maxiter_none_GH5731M  s   
z8TestDifferentialEvolutionSolver.test_maxiter_none_GH5731c              	   C   s(  t ttgt| jfR i ddi tt| j}|  t|jd tt	
t	|j |  t|jd tt	
t	|j |jdd t|jd tt	
t	|j tt| jdd}|jdd t|jd tt	
t	|j t	dd	d
dd}tt| j|ddddd}t|jd tt	
t	|j t|jdk t|jdk t	||dd}t|jd d | tt	|jd d d tt	|jd d d t	dd	ddd	}t ttgt| jfR i d|i t	jjdddd}tt| j|d}t|jd |d  d S )Nr   rubbishr   r   )
qmc_enginer   )r   rg   rU   r   rW   r9   rE   {Gz?r   )r   r=   atolr   r"   )rW   r9      r   r   )lowhighr   x0)rh   ri   r   r	   r+   init_population_randomr   r   r   r&   r   isinfr   init_population_lhsinit_population_qmclinspacer   num_population_memberspopulation_shapeclipr~   r   r2   minmaxr   uniformr   )r3   rQ   r2   unscaled_populationr  r4   r4   r5   test_population_initiationV  sd   z:TestDifferentialEvolutionSolver.test_population_initiationc                 C   s`   t t| jddgd}|jsJ tt t t| jddgd W d    d S 1 s)w   Y  d S )Nr  r%   r   @)r   r	   r+   r   rh   ri   )r3   r   r4   r4   r5   test_x0  s
   

"z'TestDifferentialEvolutionSolver.test_x0c                 C   s"   dd }ddg}t ||dd d S )Nc                 S   s   | d dk r	t jS | d S )Nr   r!   r   )r&   r   rp   r4   r4   r5   sometimes_inf  s   zWTestDifferentialEvolutionSolver.test_infinite_objective_function.<locals>.sometimes_infr   F)r+   disp)r   )r3   r  r+   r4   r4   r5    test_infinite_objective_function  s   z@TestDifferentialEvolutionSolver.test_infinite_objective_functionc                 C   s@   ddg}t t|dd}t|jdk t|jjtu  |  d S )Nr   r   )r   )r   r	   r   	_updating_mapwrapper_mapfuncmapr   )r3   r+   rQ   r4   r4   r5   test_deferred_updating  s
   z6TestDifferentialEvolutionSolver.test_deferred_updatingc              
   C   s   ddg}t t|}t|jdk td}|d<}tt  t t||j	d}W d    n1 s3w   Y  W d    n1 sBw   Y  |jdksNJ W d    d S 1 sYw   Y  d S )Nr   	immediatespawnr9   )workersr   )
r   r	   r   r!  multiprocessingget_contextPoolr   UserWarningr$  )r3   r+   rQ   ctxpsr4   r4   r5   test_immediate_updating  s   


"z7TestDifferentialEvolutionSolver.test_immediate_updatingc              	   C   s   ddg}t d4}tt|d|jd}t|jjd u t|jdk |	  W d    n1 s1w   Y  W d    n1 s@w   Y  tt|ddd}t|jjd u t|jdk |	  W d    d S 1 slw   Y  d S )Nr   r9   r   )r   r(  )
r)  r+  r   r	   r$  r   r"  poolr!  r   )r3   r+   r.  rQ   r4   r4   r5   test_parallel  s&   

 
"z-TestDifferentialEvolutionSolver.test_parallelc                 C   s&   t tddg}|  t|  d S )Nr   )r   r	   r   r   	convergedrP   r4   r4   r5   test_converged  s   z.TestDifferentialEvolutionSolver.test_convergedc                    s  dd }dd  t |tj d}ttddg|dtddg}t|d	 t  tj d
}ttddg||fdg d}g d}t||D ]\}}t|}t	|t
| qHt	t|t| tfddt|D }	|	jdksJ  fdd}
t |
tj d
}ttddg||fddd_tjtdd t| W d    d S 1 sw   Y  d S )Nc                 S      | d | d  gS Nr   r   r4   rp   r4   r4   r5   constr_f     zNTestDifferentialEvolutionSolver.test_constraint_violation_fn.<locals>.constr_fc                 S   *   t | d d | d  | d | d  gS Nr   r9   r   r&   r)   rp   r4   r4   r5   	constr_f2     *zOTestDifferentialEvolutionSolver.test_constraint_violation_fn.<locals>.constr_f2ffffff?r   constraintsr   r$   ?))333333?r   r   )r!   r!   ))rx   {Gz?r   )r  @r   )r   r   r   c                    s   g | ]}  |qS r4   )_constraint_violation_fn).0r:   )rQ   r4   r5   
<listcomp>  s    zPTestDifferentialEvolutionSolver.test_constraint_violation_fn.<locals>.<listcomp>)rU   r   rU   c                    s
    | j S r   )r1   rp   )r<  r4   r5   	constr_f3	  s   
zOTestDifferentialEvolutionSolver.test_constraint_violation_fn.<locals>.constr_f3F)r@  
vectorizedTz#An array returned from a Constraintrq   )r   r&   r   r   r	   rE  r)   r   r   r   r/   r   rI  pytestr   ru   )r3   r7  nlccvnlc2xsvsr:   r   constraint_violationrH  r4   )r<  rQ   r5   test_constraint_violation_fn  sH   



"z<TestDifferentialEvolutionSolver.test_constraint_violation_fnc           	      C   s  dd }dd }t |tj d}ttddg|d}|tddgd	d	gg\}}t|d
d
g t|tdgdgg |j	dksCJ t |tj d}dD ]}ttddg||f|dd}|tddgddgg\}}t|d
d
g t|tg dg dg |tddgd	d	gg\}}t|d
d
g t|tg dg dg |j	dksJ |tddgd	d	gg\}}t|dd
g t|tg dg dg |j	dksJ qMd S )Nc                 S   r5  r6  r4   rp   r4   r4   r5   r7    r8  zZTestDifferentialEvolutionSolver.test_constraint_population_feasibilities.<locals>.constr_fc                 S   s$   | d d | d  | d | d  gS r:  r4   rp   r4   r4   r5   r<    s   $z[TestDifferentialEvolutionSolver.test_constraint_population_feasibilities.<locals>.constr_f2r>  r   r?  r!   r   Fr$   r  )r9   r   rA  )FTr   )r@  rI  r   g333333?)r$   r  r   )rx   rC  r   )r  rD  r   )r9   rU   rY   T)r   r   r   )
r   r&   r   r   r	   #_calculate_population_feasibilitiesr)   r   r   r   )	r3   r7  r<  rK  rQ   feasrL  rM  	vectorizer4   r4   r5   (test_constraint_population_feasibilities  sJ   

zHTestDifferentialEvolutionSolver.test_constraint_population_feasibilitiesc                 C   sz   dd }t |tj d}ttddg|d}tt | }W d    n1 s(w   Y  ||jdks6J |j	s;J d S )Nc                 S      t | d | d  gS r6  r;  rp   r4   r4   r5   r7  E     zGTestDifferentialEvolutionSolver.test_constraint_solve.<locals>.constr_fr>  r   r?  )
r   r&   r   r   r	   r   r,  r   r:   r   )r3   r7  rK  rQ   r   r4   r4   r5   test_constraint_solveD  s   


z5TestDifferentialEvolutionSolver.test_constraint_solvec                 C   sF  dd }t |tj d}ttddg|ddd}tt | }W d    n1 s*w   Y  |jdks6J |j	r;J ttddg|d	d
}t
| |j rPJ t|j rZJ d}|jd }|j|dg |jd|g< |j|dgd d f |jd|gd d f< |j|dgd d f |jd|gd d f< |  t|jd | d S )Nc                 S   rV  r6  r;  rp   r4   r4   r5   r7  U  rW  zLTestDifferentialEvolutionSolver.test_impossible_constraint.<locals>.constr_frg   r   rU   r   )r@  r"   r   r   F)r@  r      )r   r&   r   r   r	   r   r,  r   maxcvr   r   feasibler   isfiniter   rP  r2   r   r   )r3   r7  rK  rQ   r   lrL  r4   r4   r5   test_impossible_constraintT  s2   





(z:TestDifferentialEvolutionSolver.test_impossible_constraintc              
   C   sN  dd }t |tj d}ttddg|d}|j}|ddtdgd	dtdgs+J |d	dtdgddtdgd
u s@J |ddtdgddtdgsSJ |ddtdgd	d
td	gsfJ |dd
tddgd	d
td	d	gs{J |dd
tddgd	d
td	dgsJ |d	d
tddgd	d
td	dgrJ d S )Nc                 S   r5  r6  r4   rp   r4   r4   r5   r7  ~  r8  zCTestDifferentialEvolutionSolver.test_accept_trial.<locals>.constr_fr>  r   r?  r$   Tr   r   Fg#@r!   g?)r   r&   r   r   r	   _accept_trialr)   )r3   r7  rK  rQ   fnr4   r4   r5   test_accept_trial{  s&   
&*&&z1TestDifferentialEvolutionSolver.test_accept_trialc           
         s*  t g d}t dt jdg}t g d}tt|||}||dk s*J |g ddk s7J t dd	d	d
}g }|D ]
}|
|| qEt j||jt |j t g d}t g dg dg dg tt t j d|}||dk sJ |g ddk sJ t dd	dd}g }|D ]
}|
|| qt j||jt |j ttt t j d|}||dk sJ |g ddk sJ  fdd}t|t j d}	t|	g d}||dk sJ |g ddk sJ d S )N)r   rY  ry   r!   F   )r   r9   rU   r   )rY         r      rW   rU   )r   r9   rU   rV   )rW   r   r   rX   )r    r      r   )r   r9   r   rV      r    rV   c                    s
     | S r   )dotrp   Ar4   r5   r     s   
zDTestDifferentialEvolutionSolver.test_constraint_wrapper.<locals>.fun)r&   r)   r   r   r   	violationanyr   r0   r   appendtestingr   r1   r   r   r   )
r3   lbubr  pcrN  
violationsr:   r   	nonlinearr4   ri  r5   test_constraint_wrapper  s>    z7TestDifferentialEvolutionSolver.test_constraint_wrapperc                 C   s   dd }t |ddgddg}t|ddg}t|jd dks!J g d	}g d
}t||D ]\}}t||| q.t|t|j	t|j	 |
t|j	jdt|fks]J |t|j	jdt|fksoJ |jdksvJ |jdks}J d S )Nc                 S   s.   t | d d | d  | d d | d  gS r:  r;  rp   r4   r4   r5   cons_f  s   zQTestDifferentialEvolutionSolver.test_constraint_wrapper_violation.<locals>.cons_frg   g333333r9   r!   r   r   )ra   )r!   rB  )rB  rB  )r$   333333)r$   r   ))r   r   )r   r$   )rC  r   )gRQ?r   )r  g=
ףp=?)r   r   r&   r   r+   r   r   rk  r)   r1   r   r   r   
num_constrparameter_count)r3   ru  rK  rq  rN  rO  r:   r   r4   r4   r5   !test_constraint_wrapper_violation  s   	
$$zATestDifferentialEvolutionSolver.test_constraint_wrapper_violationc                 C   s   t  }|t tg dg dg}W d    n1 sw   Y  t|dd}td}t||}||j	dks=J t
ddd}||j	d	ksPJ d S )
N)r   r   r   r   )r9   r9   r9   r   r   r   rV   )r9   rY  rW   )r9   rW   )r   filterPendingDeprecationWarningr&   matrixr   onesr   rk  r   r0   r   )r3   supr|  lcr  cwxtrialr4   r4   r5   test_matrix_linear_constraint  s   



z=TestDifferentialEvolutionSolver.test_matrix_linear_constraintc              	   C   s  dd }t d}d|dg df< d|ddd	gf< d
|dg df< d|dg df< d|dddgf< d
|dg df< d|dg df< d|dddgf< d
|dg df< |dd dd f }t g d}t|t j |}dgd dgd  dg }t||dd|dd}d}d }t|||d!d" |jsJ t|j|d!d" t|j	|d#d" t
t ||j |k t
t |jt |d d d$f k t
t |jt |d d df k tt|t j |}t||dd|dd}t||| |jsJ t|j|d%d" t|j	|d#d" t
t ||j |k t
t |jt |d d d$f k t
t |jt |d d df k d&d' }	d(d) }
t|d dd d f t j |d d }t|ddd d f t j |dd }t|	t j |dd }t|
t j |dd }||||f}t }|t t||d*d|dd}W d    n	1 sw   Y  t|j|d!d" t|j	|d#d" t
t ||j |k t
t |jt |d d d$f k t
t |jt |d d df k d S )+Nc                 S   sX   t dg| f} t d| dd  d| dd  | dd   t | dd   }|S )Nr   rW   r   )r&   hstackro   r:   r   r4   r4   r5   f  s   Dz2TestDifferentialEvolutionSolver.test_L1.<locals>.f)r      )r9   r9   r   r   r   )r   r9   r      )r   r9   r   )rt   rg   r   rU   )rV   rW   r   rV   )r   rU   r   r  rW   r  rX   )rX   r    r  r    )r9   rU   r  r   rf  r   	   )rf  r  r   )	r   r   r   r   r   r   r   r   r   r   r   r?     r=   r   r@  r"   )r   r   r   r   r   r   r   r   r   rU   rU   rU   r   iga2U0*C?r	  g{Gzt?r   gMb@?c                 S   sL   t dg| f} d| d  d| d   | d  | d  d| d  | d  gS )Nr   r9   rU   r  r   r  r&   r  rp   r4   r4   r5   c1/  s   &z3TestDifferentialEvolutionSolver.test_L1.<locals>.c1c                 S   s,   t dg| f} d| d  | d  | d  S )Nr   rt   rf  r  r   r  rp   r4   r4   r5   c24     z3TestDifferentialEvolutionSolver.test_L1.<locals>.c2r@   )r&   zerosr)   r   r   r   r   r   r:   r   r   r   r   r   r   rz  r,  )r3   r  rj  bLr+   r   x_optf_optr  r  L2NN2r@  r~  r4   r4   r5   test_L1  sr   


&&

&&((
&*z'TestDifferentialEvolutionSolver.test_L1c           
      C   s  dd }dd }t |dtj}dgd }|}t }|t t||dd	|d
}W d    n1 s2w   Y  d}d}	t||	| t|j| t|j	|	dd |j
sUJ ttt||j	dk tt|j	t|d d df k tt|j	t|d d df k d S )Nc                 S   s   t dg| f} | d d d d| d d d   | d d  d| d d	 d   d| d d
   d| d
 d   | d d  d| d
  | d   d| d
   d| d   }|S )Nr   r   r   r9   rW   r   rU   rV   r  rX   r    rf  r  r  r4   r4   r5   r  M  s   B


z2TestDifferentialEvolutionSolver.test_L2.<locals>.fc                 S   s   t dg| f} dd| d d   d| d d   | d  d| d d   d| d   dd	| d   | d d  d
| d
 d   d| d   dd| d   d| d   d| d d   | d  | d  d| d d  | d d  d| d  | d   d| d d   d| d
   d| d   gS )Nr      r9   r   rU   rV   rW         rX   rf  r    i  r   r  r  rp   r4   r4   r5   r  T  s   F6:>

z3TestDifferentialEvolutionSolver.test_L2.<locals>.c1r   r   r    r@   r  r=   r   r@  gy\
E@)gܤ@g$H8?g	޿g0ev@gn8)gk_@/?g0g+?h㈵>r  r   )r   r&   r   r   rz  r,  r   r   r   r:   r   r   r   r)   )
r3   r  r  r  r+   r@  r~  r   r  r  r4   r4   r5   test_L2J  s(   


&*z'TestDifferentialEvolutionSolver.test_L2c                 C   s  dd }t d}d|dg df< d|dg df< d	|d
g df< |dd dd f }t g d}dd }t||t j}t|dt j}dgd }||f}t }	|	t t	||d|d
d}
W d    n1 siw   Y  d}d}t
|||dd t
|
j|dd t
|
j|dd |
jsJ tt ||
j |k tt t ||
jdk tt |
jt |d d df k tt |
jt |d d df k d S )Nc                 S   s   t dg| f} | d d | d d  | d | d   d| d   d| d   | d d d  d| d d	 d   | d	 d d  d| d
 d d   d	| d d   d| d d d   d| d d d   | d d d  d }|S )Nr   r   r9   r  re  rU   r   rV   rW   rX   r    rf  r  r  -   r  r  r4   r4   r5   r  t  s*   >z2TestDifferentialEvolutionSolver.test_L3.<locals>.f)rV   r  )r  r   rU   ir   )r   r9   r    rf  )r   rf     rt   r9   )rf  rt   r   r9   rU   )r   r9   r  r   )ir   ic                 S   sX  t dg| f} d| d  d| d   d| d d d   d	| d
   d| d d d  d| d d d   d| d d   d	| d   d | d d  d| d d d   d| d  | d   d| d   d| d   d| d d  d| d   | d d d  d| d   d d| d d d  d| d d d   d| d d   | d  d gS )Nr   rU   r   rX   r9   r   r  rf  r    r   r   rV   x   r  rW   r   r         ry   r  rp   r4   r4   r5   r    s   6FL:Bz3TestDifferentialEvolutionSolver.test_L3.<locals>.c1r   r   r   r  )r   r@  r"   )
gWXp?`@g@gVW@!@g3Ib@grq?g` ?g$F-t%?g6N#@g7h @g]y @g<6cN8@r  r  ư>)r&   r  r)   r   r   r   r   rz  r,  r   r   r:   r   r   r   r   r3   r  rj  r  r  r  r  r+   r@  r~  r   r  r  r4   r4   r5   test_L3q  s8   
	


&*z'TestDifferentialEvolutionSolver.test_L3c              	   C   s  dd }t d}d|dddgf< d|d	g d
f< d|dddgf< |dd dd f }t g d}dd }t|t j d}t|dt j}dgdgd	  dgd  }||f}t }	|	t t	||dd|dd}
W d    n1 ssw   Y  d}g d}t
|||dd t
|
j|dd t dkrt t jjdk rt
|
j|ddd  n	t
|
j|d!d"d  |
jsJ tt ||
j |k tt t ||
jdk tt |
jt |d d df k tt |
jt |d d df k d S )#Nc                 S   s   t | d d S )NrU   r&   ro   rp   r4   r4   r5   r    r8  z2TestDifferentialEvolutionSolver.test_L4.<locals>.f)rV   r  ){Gzd?r  r   rV   rX   )r  r  g{Gzdr9   )rW   r    rV   )r  g{GzrU   rf  rW   )r   r   r   c                 S   s   t dg| f} | d | d  d| d   d| d   d | d | d	  d
| d   | d | d   d
| d   | d | d  d | d | d   d| d   gS )Nr   r   rX   g!> 
@rV   r   gSUX@r9   r    i  rW   rU   rf  i i	  r  rp   r4   r4   r5   r    s
   *6.z3TestDifferentialEvolutionSolver.test_L4.<locals>.c1r   )r   '  )  r  )r   r  r@   r  r  gh|?@)got@g?@g:@gt$f@gVfyr@gh o?k@gCq@gVfx@MbP?r  Windowsgv!>gy&1l?)rtolr	  gh㈵>ga2U0*c?)r&   r  r)   r   r   r   r   rz  r,  r   r   r   platformsystemr   intpitemsizer:   r   r   r   )r3   r  rj  r  r  r  r  r+   r@  r~  r   r  r  r4   r4   r5   test_L4  s>   


&*z'TestDifferentialEvolutionSolver.test_L4c           	      C   s   dd }dd }t |tj d}dgd }|}t||dd	|d
}d}d}t|||dd t|j|dd |js:J ttt	||j
dk tt|j
t	|d d df k tt|j
t	|d d df k d S )Nc                 S   sf   t dg| f} t dt j | d  d t dt j | d   | d d | d | d    }| S )Nr   r9   r   rU   )r&   r  sinpir  r4   r4   r5   r    s
   2z2TestDifferentialEvolutionSolver.test_L5.<locals>.fc                 S   sD   t dg| f} | d d | d  d d| d  | d d d  gS )Nr   r   r9   rV   r  rp   r4   r4   r5   r    s   z3TestDifferentialEvolutionSolver.test_L5.<locals>.c1r   r   r   r9   r@   r  r  )gJIť?gU(C@g#gh㈵>r  -C6?r   )r   r&   r   r   r   r   r   r   r   r)   r:   	r3   r  r  r  r+   r@  r   r  r  r4   r4   r5   test_L5  s    


&*z'TestDifferentialEvolutionSolver.test_L5c           	      C   s   dd }dd }t |dtj}ddg}|}t||dd	|d
d}d}d}t|||dd t|j|dd t|j|dd |jsAJ tt	t
||jdk tt	|jt
|d d df k tt	|jt
|d d df k d S )Nc                 S   s4   t dg| f} | d d d | d d d  }|S )Nr   r   r   rU   r9   rY  r  r  r4   r4   r5   r    s    z2TestDifferentialEvolutionSolver.test_L6.<locals>.fc                 S   sZ   t dg| f} | d d d | d d d  d | d d d  | d d d  d gS )Nr   r   rW   r9   r   rX   gp=
׳T@r  rp   r4   r4   r5   r    s   "$z3TestDifferentialEvolutionSolver.test_L6.<locals>.c1r   )   r   r   r@   r  gHz>)r=   r   r@  r   )gq=
ף0,@g??g1r  r  r  r  r   )r   r&   r   r   r   r   r:   r   r   r   r)   r  r4   r4   r5   test_L6  s"   

&*z'TestDifferentialEvolutionSolver.test_L6c           	      C   s0  dd }dd }t |g dg d}ddgd	gd
  }|}t||dd|d}g d}d}t||| t|j|dd t|j|dd |jsHJ ttt	||jt	g dk ttt	||jt	g dk tt|jt	|d d df k tt|jt	|d d df k d S )Nc                 S   sH   t dg| f} d| d d  d| d  | d   d| d   d	 }|S )
Nr   g+dvqn@rU   r9   g"C?r   rW   gtۈB@gn@r  r  r4   r4   r5   r    s   "
z2TestDifferentialEvolutionSolver.test_L7.<locals>.fc                 S   s   t dg| f} dd| d  | d   d| d  | d   d	| d
  | d   dd| d  | d   d| d  | d   d| d
 d   dd| d
  | d   d| d  | d
   d| d
  | d   gS )Nr   gl#fUU@g+WPIw?r9   rW   g=BD?r   rV   gOlb?rU   gaۢ T@g+<$!6}?g h?g^5a?g/h"@gxLCs?gݗT?g^gED_?r  rp   r4   r4   r5   r    s   ***z3TestDifferentialEvolutionSolver.test_L7.<locals>.c1)r   Z   rY  )\   n      )N   f   )!   r  )   r  rU   r@   r  r  )gq S@g] @@g˻f=@g:F@g%ShMcB@gCgbr  r  r   r   )
r   r   r   r:   r   r   r   r&   r   r)   r  r4   r4   r5   test_L7  s$   

((&*z'TestDifferentialEvolutionSolver.test_L7ppc64lezfails on ppc64le)reasonc              	   C   s  dd }t d}d|dddgf< d|dddgf< |dd dd f }t d	d	g}d
d }t||t j}t|t ddt dd}dgd dgd  }||f}t }	|	t	 t
||dd|dd}
W d    n1 snw   Y  d}d}t|||dd t|
jd d |d d dd t|
jdd  |dd  dd t|
j|dd |
jsJ tt ||
j |k tt t ||
jdk tt t ||
jdk tt |
jt |d d df k tt |
jt |d d df k d S )Nc                 S   sL   t dg| f} d| d  d| d d   d| d   d| d d   }|S )Nr   rU   r   r  r9   g^>r  r  r4   r4   r5   r  B  s   8z2TestDifferentialEvolutionSolver.test_L8.<locals>.f)rU   rW   )r   rg   r   rV   rU   r9   皙c              	   S   s   t dg| f} dt | d  d  dt | d  d   d | d  dt | d d  dt | d | d  d   d | d  dt | d d  dt | d | d  d   d	 gS )
Nr   r  rU   rY   rV   gfffff@r   r9   g33333;@)r&   r  r  rp   r4   r4   r5   r  M  s   .22z3TestDifferentialEvolutionSolver.test_L8.<locals>.c1MbPr  )r   i  )r  g?r?   r  i  )r=   r   r@  r   )gEGr?@gT㥛D@g&D
n?gd,[ٿg#J{@r  gMb`?r   r   )r&   r  r)   r   r   r   fullr   rz  r,  r   r   r:   r   r   r   r   r  r4   r4   r5   test_L8>  s<   


	  
&*z'TestDifferentialEvolutionSolver.test_L8c           	      C   s   dd }dd }t |dgdg}dgd }|}t||d	d
|d}tdd dg}d}t||| tt|j|dd t|j|dd |jsJJ t	t
t||jdk t	t
t||jdk t	t
|jt|d d df k t	t
|jt|d d df k d S )Nc                 S   s,   t dg| f} | d d | d d d  S )Nr   r   r9   r  rp   r4   r4   r5   r  w  r  z2TestDifferentialEvolutionSolver.test_L9.<locals>.fc                 S   s&   t dg| f} | d | d d  gS r:  r  rp   r4   r4   r5   r  {  s   z3TestDifferentialEvolutionSolver.test_L9.<locals>.c1r  r  rf   r9   r@   r  r  r!   g      ?r  r   r   )r   r   r&   sqrtr   absr:   r   r   r   r   r)   r  r4   r4   r5   test_L9t  s$   


&*z'TestDifferentialEvolutionSolver.test_L9c                    s   t jd}tj}d}|j|d|d}dd  ddg}d	d
g}t |||f|d|d}|jd dks5J t|j|dd t |||f|d|d} fdd}	t	|	|jd d||fd
gd}
t|jd |
j |j
|j
ksoJ d S )Nl   b	)rW   r!   r  )r   random_statec                 W   s>   |\}}t |j|g| R  jdd }t |rt j}|S )Nrg   axis)r&   logpmfro   isnanr   )r.  r   distr:   llr4   r4   r5   rl     s
   "
z>TestDifferentialEvolutionSolver.test_integrality.<locals>.funcTF)r      )r   gffffff?)r   integralityr   r   r   rW   g?)r  c                    s$   |\}}} t || d g||S )Nr   r;  )r.  r   nr  r:   r   r4   r5   func2  s   
z?TestDifferentialEvolutionSolver.test_integrality.<locals>.func2r   )r   r+   )r&   r   r   r   nbinomrvsr   r:   r   r
   r   )r3   r   r  shapesr:   r  r+   r   res2r  LBFGSBr4   r   r5   test_integrality  s.   z0TestDifferentialEvolutionSolver.test_integralityc                 C   s  dd }g d}g d}t ||ddd}t|jd g d t|jd	 g d
 t ||d|d}t|jd g d t|jd	 g d t|jg d |jdu sSJ g d}t ||d|d}t|jd g d t|jd	 g d tt|jd g d tt|jd	 g d g d}t ||d|d}t|jd g d t|jd	 g d g d}tj	t
dd t ||d|d W d    d S 1 sw   Y  d S )Nc                 S   s   | S r   r4   rp   r4   r4   r5   r    r   zBTestDifferentialEvolutionSolver.test_integrality_limits.<locals>.f)TFT))r  r   r   皙@)ffffff
@皙@F)r+   r   r  r   )r  r   r  r   )r   r  r  )r!   r   g      @)g      ?r        @))rv  gr  )g$gffffff@)g      r         %)r  r  r  )      r   g      $)r  r   g      @))ffffff$g333333 r  g%gr#)r  r   r  )g      !r  g      #))r  g333333$r  r  zOne of the integralityrq   )r   r   r*   r   r  r   r&   roundrJ  r   ri   )r3   r  r  r+   rQ   r4   r4   r5   test_integrality_limits  sF   "z7TestDifferentialEvolutionSolver.test_integrality_limitsc                    s<  dd }dd }t jtdd t|| jddd	 W d    n1 s#w   Y  ttd
d t|| jdd W d    n1 sAw   Y  ttdd t|| jdtdd W d    n1 saw   Y  dg  fdd}ddg}tt|ddd}t||dddd}t	|j
|j
  d |jksJ |j|jksJ d S )Nc                 S   s   t | d S rn   r  rp   r4   r4   r5   r,        zBTestDifferentialEvolutionSolver.test_vectorized.<locals>.quadraticc                 S   s   t j| d ddS )Nr9   r   r  r  rp   r4   r4   r5   quadratic_vec  r8  zFTestDifferentialEvolutionSolver.test_vectorized.<locals>.quadratic_veczThe vectorized functionrq   Tr   )rI  r   z#differential_evolution: the 'vector)rI  z$differential_evolution: the 'workers)rI  r(  r   r   c                    s    d  d7  < t | S r6  )r	   rp   ncallsr4   r5   	rosen_vec  s   zBTestDifferentialEvolutionSolver.test_vectorized.<locals>.rosen_vecr  r   )r   r   )rI  r   r   )rJ  r   ru   r   r+   r   r,  r$  r	   r   r:   r   r   )r3   r,   r  r  r+   res1r  r4   r  r5   test_vectorized  s<   z/TestDifferentialEvolutionSolver.test_vectorizedc           	   	   C   s   dd }dd }t |tj d}t |dd}dd	 }d
d
g}tt|dd||gdd}t||ddd||gdd}t|j|j d S )Nc                 S   rV  r6  r;  rp   r4   r4   r5   r7    rW  zMTestDifferentialEvolutionSolver.test_vectorized_constraints.<locals>.constr_fc                 S   r9  r:  r;  rp   r4   r4   r5   r<    r=  zNTestDifferentialEvolutionSolver.test_vectorized_constraints.<locals>.constr_f2r>  )r   r!   r   c                 S   sF   d| dd  | d d d  d  }|d| d d  d 7 }t |S )Nr   r   rg   r   )r&   squeeze)r:   r   r4   r4   r5   r    s   $
zNTestDifferentialEvolutionSolver.test_vectorized_constraints.<locals>.rosen_vecr  r   r   F)r   r   r@  r   T)rI  r   r   r@  r   )r   r&   r   r   r	   r   r:   )	r3   r7  r<  nlc1rM  r  r+   r  r  r4   r4   r5   test_vectorized_constraints  s    z;TestDifferentialEvolutionSolver.test_vectorized_constraintsc                 C   sj   dd }t dd dtj}t dd tj d}t|ddg||gd	d
dd}|jd
u s,J d|jv s3J d S )Nc                 S   s   t | d t | d  S r6  )r&   cosr  rp   r4   r4   r5   rl   1  s   zUTestDifferentialEvolutionSolver.test_constraint_violation_error_message.<locals>.funcc                 S   s   | d | d d d  S Nr   r   r9   r4   rp   r4   r4   r5   <lambda>5  s    zYTestDifferentialEvolutionSolver.test_constraint_violation_error_message.<locals>.<lambda>r   c                 S   s   | d | d d  S r  r4   rp   r4   r4   r5   r  6  s    )rg   r9   rf   r   Fi3)r+   r@  r   r   r   zMAXCV = 0.414)r   r&   r   r   r   r   )r3   rl   c0r  r[   r4   r4   r5   'test_constraint_violation_error_message/  s   zGTestDifferentialEvolutionSolver.test_constraint_violation_error_messagec                    s   dd}dg }| d dd fdd	}t t|| d|dd	d
	}|j|u s/J | }tt| |ddd	dd	}t|j|j t|j|j ddd}tj	t
dd tt||d W d    d S 1 smw   Y  d S )NrV   r   )r   g      $@r%   gffffff?c                    s   t ||  }|}t }|| g }t|dk rCt|dkrC|d }|dd  }|| kr7|| t|dk rCt|dks$|d d \}}	|d  || ||	    }
|jd}|k }d||< t ||
|}|S )Nr9   r   r   )r   T)	r&   copychoicer0   shuffler   rm  r  where)r   r2   r   r\   
fill_pointr1  idxsidxr0r   bprime
crossoversr#   rx  recombinationtotal_popsizer4   r5   custom_strategy_fnP  s(   



zLTestDifferentialEvolutionSolver.test_strategy_fn.<locals>.custom_strategy_fnr9   F)r"   r  r#   r   r=   r   r   r?   )r#   r"   r  r   r=   r   r   c                 S   s   t ddgS )Nr   r   r;  )r   r2   r   r4   r4   r5   r    r  z	strategy*rq   r<   r   )r   r	   r=   r   r   r   r2   r:   rJ  r   ru   )r3   r"   r+   r  rQ   r   r  r4   r  r5   test_strategy_fnF  sR   

"z0TestDifferentialEvolutionSolver.test_strategy_fnN)JrO   
__module____qualname__r6   r8   r,   rR   r]   r^   r_   r`   re   rm   rv   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r   r%  r0  r2  r4  rQ  rU  rX  r^  ra  rt  ry  r  r  r  r  r  r  r  r  rJ  markslowxfailr  machiner  r  r  r  r  r  r  r  r4   r4   r4   r5   r      s    K
	I"

	0	
	F	
9-',Y'23.3,-(r   )!__doc__r)  r  %scipy.optimize._differentialevolutionr   r   scipy.optimizer   r   scipy.optimize._constraintsr   r   r   r	   r
   scipy.sparser   scipyr   numpyr&   numpy.testingr   r   r   r   r   r   rJ  r   rh   r   r   r4   r4   r4   r5   <module>   s     