a
    Rej                     @   s>  d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZm	Z	 d dl
Z
d dl
mZmZ d dlmZmZmZmZmZmZmZmZ d dlmZ d dlmZ G dd	 d	Zd
d ZG dd deZeddgd d gdZeddgd d gdZeddgd d gdZ G dd deZ!e!dgdgde"dgdgdgdgdgdgdgdgdgdgg
e"g d d!Z#e!d"gdgdge"dgge"dgd!Z$G d#d$ d$eZ%e%d%d&gd'd(gd)d*Z&G d+d, d,eZ'e'd-gd. g d/d0d*Z(G d1d2 d2eZ)e)d3d3gd4gd5d6gd7Z*G d8d9 d9eZ+d:Z,e-e.d;gd: d<gd: Z/e+e/d=gdd7Z0G d>d? d?eZ1e1d@gdA dBe2dAdC d7Z3G dDdE dEeZ4e4d-d-gdFgdGdGgd7Z5G dHdI dIeZ6e6d%dJgddd7Z7e
j89dKdfdQdRZ:G dSdT dTZ;G dUdV dVZ<G dWdX dXZ=G dYdZ dZZ>G d[d\ d\Z?d]d^ Z@e
j8Ad_d`da ZBdbdc ZCddde ZDdS )g    N)Pool)assert_allcloseIS_PYPY)raiseswarns)shgoBoundsminimize_scalarminimizerosen	rosen_der
rosen_hessNonlinearConstraint)new_constraint_to_old)SHGOc                   @   s   e Zd ZdddZdS )StructTestFunctionNc                 C   s"   || _ || _|| _|| _|| _d S Nbounds
expected_xexpected_funexpected_xlexpected_funl)selfr   r   r   r   r    r   S/var/www/sunrise/env/lib/python3.9/site-packages/scipy/optimize/tests/test__shgo.py__init__   s
    zStructTestFunction.__init__)NNN)__name__
__module____qualname__r   r   r   r   r   r      s     r   c                 C   sX   g }| d urPt | tur,t | tur,| f} n | D ]} |d| d q0t|}nd }|S )Nineqtypefun)r"   tuplelistappend)gconsr   r   r   wrap_constraints   s    

r)   c                   @   s$   e Zd Zdd Zdd ZeeZdS )StructTest1c                 C   s   |d d |d d  S Nr         r   r   xr   r   r   f,   s    zStructTest1.fc                 C   s   t j| ddd  S )Nr   Zaxisg      @numpysumr/   r   r   r   r'   /   s    zStructTest1.gNr   r   r   r0   r'   r)   r(   r   r   r   r   r*   +   s   r*   )   )r   r   r   r-   NNc                   @   s(   e Zd ZdZdd Zdd ZeeZdS )StructTest2zN
    Scalar function with several minima to test all minimiser retrievals
    c                 C   s   |d t | S )N   )r3   sinr.   r   r   r   r0   B   s    zStructTest2.fc                 C   s   dt j| dd S )N:   r   r1   r2   r5   r   r   r   r'   E   s    zStructTest2.gNr   r   r   __doc__r0   r'   r)   r(   r   r   r   r   r;   =   s   r;   )r   <   g⡽4$?0_r<g GK@g
]<@gI{_H@gi<&,@g>E@gqŤCQ4@g8$B@go.n:@g^5	>@)
rB   gs8g^V&+6g+?-2g+!/gN|1(gc=C#g^\gNg自ݿr   )r   g      @c                   @   sD   e Zd ZdZdd Zdd Zdd Zdd	 Zeed
e	j
ZefZdS )StructTest3a  
    Hock and Schittkowski 18 problem (HS18). Hoch and Schittkowski (1981)
    http://www.ai7.uni-bayreuth.de/test_problem_coll.pdf
    Minimize: f = 0.01 * (x_1)**2 + (x_2)**2

    Subject to: x_1 * x_2 - 25.0 >= 0,
                (x_1)**2 + (x_2)**2 - 25.0 >= 0,
                2 <= x_1 <= 50,
                0 <= x_2 <= 50.

    Approx. Answer:
        f([(250)**0.5 , (2.5)**0.5]) = 5.0


    c                 C   s   d|d d  |d d  S )Ng{Gz?r   r,   r-   r   r.   r   r   r   r0   |   s    zStructTest3.fc                 C   s   | d | d  d S )Nr   r-         9@r   r5   r   r   r   g1   s    zStructTest3.g1c                 C   s   | d d | d d  d S )Nr   r,   r-   rD   r   r5   r   r   r   g2   s    zStructTest3.g2c                 C   s0   | d | d  d | d d | d d  d fS )Nr   r-   rD   r,   r   r5   r   r   r   r'      s    zStructTest3.gr   N)r   r   r   r@   r0   rE   rF   r'   r   npinfZ_StructTest3__nlcr(   r   r   r   r   rC   j   s   rC   )r,   2   )r   rI   g'In/@gS[:XL?      @)r   r   r   c                   @   sL   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZeeeefZ	e
e	ZdS )StructTest4z
    Hock and Schittkowski 11 problem (HS11). Hoch and Schittkowski (1981)

    NOTE: Did not find in original reference to HS collection, refer to
          Henderson (2015) problem 7 instead. 02.03.2016
    c                 C   s   |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-               r8         r   r.   r   r   r   r0      s&    .

zStructTest4.fc                 C   sJ   d| d d  d| d d   | d  d| d d   d| d   d  S )Nr,   r   rP   r-   rO   rM      r   r5   r   r   r   rE      s
    6
zStructTest4.g1c                 C   s>   d| d  d| d   d| d d   | d  | d  d  S )	NrR   r   rP   r-   rL   r,   rO   g     q@r   r5   r   r   r   rF      s    zStructTest4.g2c                 C   s:   d| d  | d d  d| d d   d| d   d  S )	N   r   r-   r,   r8   rM   rS      r   r5   r   r   r   g3   s    zStructTest4.g3c                 C   sZ   d| d d  | d d  d| d  | d   d| d d   d| d   d| d    S )	NrO   r   r,   r-   rP   rM   rQ   r8   r   r5   r   r   r   g4   s
    >

zStructTest4.g4Nr   r   r   r@   r0   rE   rF   rW   rX   r'   r)   r(   r   r   r   r   rK      s   rK   )irL   rR   )gܤ@g$H8?g	޿g0ev@gn8)gk_@/?g0g+?gP{[
E@c                   @   s    e Zd Zdd ZdZeeZdS )StructTest5c              
   C   sb   |d d  t t t|d d |d d   |d t t t|d |d d    S Nr-   g     G@r          @r3   r=   sqrtabsr.   r   r   r   r0      s
    &*zStructTest5.fNr6   r   r   r   r   rZ      s   rZ   )i i   g g      @gyCy@)r   r   r   c                   @   s$   e Zd ZdZdd ZdZeeZdS )StructTestLJzZ
    LennardJones objective function. Used to test symmetry constraints
    settings.
    c                 G   s   t d|  |d | _t| jd }d}t|d D ]}t|d |D ]}d| }d| }|| ||  }	||d  ||d   }
||d  ||d   }|	|	 |
|
  ||  }|| | }|dkrH|d| d | 7 }qHq6|S )	Nzx = r   rP           r-   r,         ?r\   )printNintrange)r   r/   argsksijabZxdZydZzdZedZudr   r   r   r0      s     
zStructTestLJ.fNr?   r   r   r   r   r`      s   r`   r8   g            @      c                   @   s    e Zd Zdd ZdZeeZdS )StructTestSc                 C   s@   |d d d |d d d  |d d d  |d d d  S )Nr         ?r,   r-   rP   r   r.   r   r   r   r0      s
    zStructTestS.fNr6   r   r   r   r   rp      s   rp   )r   r\   rO   ra   rq   c                   @   s    e Zd Zdd ZdZeeZdS )StructTestTablec                 C   s$   |d dkr|d dkrdS dS d S )Nr         @r-   rI   d   r   r.   r   r   r   r0     s    zStructTestTable.fNr6   r   r   r   r   rr   
  s   rr   rI   rs   c                   @   sL   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZeeeefZ	e
e	ZdS )StructTestInfeasiblez0
    Test function with no feasible domain.
    c                 G   s   |d d |d d  S r+   r   )r   r/   rg   r   r   r   r0     s    zStructTestInfeasible.fc                 C   s   | d | d  d S Nr   r-   r   r5   r   r   r   rE   "  s    zStructTestInfeasible.g1c                 C   s   | d | d  d  S rv   r   r5   r   r   r   rF   %  s    zStructTestInfeasible.g2c                 C   s   | d  | d  d S rv   r   r5   r   r   r   rW   (  s    zStructTestInfeasible.g3c                 C   s   | d  | d  d  S rv   r   r5   r   r   r   rX   +  s    zStructTestInfeasible.g4NrY   r   r   r   r   ru     s   ru   )r7   r-   
Not a testr   h㈵>rt   sobolr-   c
                 C   s   t | j| j|| j|||||||	d}
td|
  td|
  | jd urdtj	j
|
j| j||d | jd urtj	j
|
j| j|d | jd urtj	j
|
j| j|d | jd urtj	j
|
j| j|d d S )N)	rg   constraintsniterscallbackminimizer_kwargsoptionssampling_methodworkerszres = rtolatolr   )r   r0   r   r(   rc   logginginfor   r3   testingr   r/   r   r#   r   Zxlr   Zfunl)testrg   	test_atolr{   r|   r}   r~   r   r   r   resr   r   r   run_test8  s8    






r   c                   @   s`   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	e
jjdd Zdd Zdd ZdS )TestShgoSobolTestFunctionsz8
    Global optimisation tests with Sobol sampling:
    c                 C   s   t t dS )VMultivariate test function 1:
        x[0]**2 + x[1]**2 with bounds=[(-1, 6), (-1, 6)]Nr   test1_1r   r   r   r   test_f1_1_sobola  s    z*TestShgoSobolTestFunctions.test_f1_1_sobolc                 C   s   t t dS )zUMultivariate test function 1:
         x[0]**2 + x[1]**2 with bounds=[(0, 1), (0, 1)]Nr   test1_2r   r   r   r   test_f1_2_sobolf  s    z*TestShgoSobolTestFunctions.test_f1_2_sobolc                 C   s   ddi}t t|d dS )z_Multivariate test function 1:
        x[0]**2 + x[1]**2 with bounds=[(None, None),(None, None)]dispT)r   Nr   test1_3r   r   r   r   r   test_f1_3_sobolk  s    z*TestShgoSobolTestFunctions.test_f1_3_sobolc                 C   s   t t dS )RUnivariate test function on
        f(x) = (x - 30) * sin(x) with bounds=[(0, 60)]Nr   test2_1r   r   r   r   test_f2_1_sobolq  s    z*TestShgoSobolTestFunctions.test_f2_1_sobolc                 C   s   t t dS )NUnivariate test function on
        f(x) = (x - 30) * sin(x) bounds=[(0, 4.5)]Nr   test2_2r   r   r   r   test_f2_2_sobolv  s    z*TestShgoSobolTestFunctions.test_f2_2_sobolc                 C   s   t t dS )%NLP: Hock and Schittkowski problem 18Nr   test3_1r   r   r   r   test_f3_sobol{  s    z(TestShgoSobolTestFunctions.test_f3_sobolc                 C   s   ddi}t td|d dS )?NLP: (High dimensional) Hock and Schittkowski 11 problem (HS11)infty_constraintsFi  r{   r   Nr   test4_1r   r   r   r   test_f4_sobol  s    z(TestShgoSobolTestFunctions.test_f4_sobolc                 C   s   t tdd dS )NLP: Eggholder, multimodalrA   )r{   Nr   test5_1r   r   r   r   test_f5_1_sobol  s    z*TestShgoSobolTestFunctions.test_f5_1_sobolc                 C   s   t tddd dS )r   rA   rM   )r{   r|   Nr   r   r   r   r   test_f5_2_sobol  s    z*TestShgoSobolTestFunctions.test_f5_2_sobolN)r   r   r   r@   r   r   r   r   r   r   pytestmarkslowr   r   r   r   r   r   r   r   [  s   
r   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	e
jjdd Zdd Zdd Zdd Zdd Ze
jddd Ze
jddd ZdS )TestShgoSimplicialTestFunctionsz=
    Global optimisation tests with Simplicial sampling:
    c                 C   s   t tddd dS )r   r-   
simplicialr{   r   Nr   r   r   r   r   test_f1_1_simplicial  s    z4TestShgoSimplicialTestFunctions.test_f1_1_simplicialc                 C   s   t tddd dS )zTMultivariate test function 1:
        x[0]**2 + x[1]**2 with bounds=[(0, 1), (0, 1)]r-   r   r   Nr   r   r   r   r   test_f1_2_simplicial  s    z4TestShgoSimplicialTestFunctions.test_f1_2_simplicialc                 C   s   t tddd dS )z_Multivariate test function 1: x[0]**2 + x[1]**2
        with bounds=[(None, None),(None, None)]rM   r   r   Nr   r   r   r   r   test_f1_3_simplicial  s    z4TestShgoSimplicialTestFunctions.test_f1_3_simplicialc                 C   s   ddi}t tdd|dd dS )r   minimize_every_iterF   rR   r   r{   r|   r   r   Nr   r   r   r   r   test_f2_1_simplicial  s    
z4TestShgoSimplicialTestFunctions.test_f2_1_simplicialc                 C   s   t tddd dS )r   r-   r   r   Nr   r   r   r   r   test_f2_2_simplicial  s    z4TestShgoSimplicialTestFunctions.test_f2_2_simplicialc                 C   s   t tddd dS )r   r-   r   r   Nr   r   r   r   r   test_f3_simplicial  s    z2TestShgoSimplicialTestFunctions.test_f3_simplicialc                 C   s   t tddd dS )r   r-   r   r   Nr   r   r   r   r   test_f4_simplicial  s    z2TestShgoSimplicialTestFunctions.test_f4_simplicialc                 C   s&   ddd}d}t t|d|ddd dS )	z&LJ: Symmetry-constrained test functionTZsymmetryr   r8   ,  r-   r   rg   r{   r   r|   r   Nr   testLJr   r   rg   r   r   r   test_lj_symmetry_old  s    z4TestShgoSimplicialTestFunctions.test_lj_symmetry_oldc                 C   s,   dgd dd}d}t t|d|ddd	 d
S )z&LJ: Symmetry constrained test functionr   r8   Tr   r   r   r-   r   r   Nr   r   r   r   r   test_f5_1_lj_symmetry  s    z5TestShgoSimplicialTestFunctions.test_f5_1_lj_symmetryc                 C   s$   ddgdd}t td|ddd dS )	z"Symmetry constrained test functionr   Tr   r   r-   r   r{   r   r|   r   Nr   r   r   r   r   test_f5_2_cons_symmetry  s    z7TestShgoSimplicialTestFunctions.test_f5_2_cons_symmetryc                 C   s$   g ddd}t td|ddd dS )	z)Assymmetrically constrained test function)r   r   r   rP   Tr   i'  r-   r   r   N)r   test_sr   r   r   r   test_f5_3_cons_symmetry  s    z7TestShgoSimplicialTestFunctions.test_f5_3_cons_symmetryrw   c                    sV   d d fddd}t tjddg |d}|js6J t|jd	d
d t|jd dS )zOReturn a minimum on a perfectly symmetric problem, based on
            gh10429rq   eqc                    s   t |   S r   )r3   meanr5   avgr   r   <lambda>      zFTestShgoSimplicialTestFunctions.test_f0_min_variance.<locals>.<lambda>r!   r8   r9   r   rz   r   V瞯<r   N)r   r3   varsuccessr   r#   r/   )r   r(   r   r   r   r   test_f0_min_variance  s    
z4TestShgoSimplicialTestFunctions.test_f0_min_variancec                 C   sX   dd }dg}t ||d}t||d d}|js4J t|j|j t|j|jdd dS )	zRReturn a minimum on a perfectly symmetric 1D problem, based on
            gh10538c                 S   s   | | d  | d  S )Nrb   rq   r   r5   r   r   r   r#     s    zDTestShgoSimplicialTestFunctions.test_f0_min_variance_1D.<locals>.funr9   )r   r   ư>)r   N)r   r	   r   r   r#   r/   )r   r#   r   r   refr   r   r   test_f0_min_variance_1D  s    
z7TestShgoSimplicialTestFunctions.test_f0_min_variance_1DN)r   r   r   r@   r   r   r   r   r   r   r   r   r   r   r   r   r   r   skipr   r   r   r   r   r   r     s"   
	
	



r   c                   @   sf  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	e
jjdd Ze
jj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e
jjeoejd'kd(d)d*d+ Zd,d- Ze
jjd.d)d/d0 Z d1d2 Z!d3d4 Z"d5d6 Z#d7d8 Z$d9d: Z%d;d< Z&d=d> Z'd?d@ Z(dAdB Z)e
j*dCg dDdEdF Z+dGdH Z,dIdJ Z-dKS )LTestShgoArgumentsc                 C   s   t tdddd dS )z>Iterative simplicial sampling on TestFunction 1 (multivariate)Nr,   r   r{   r|   r   r   r   r   r   r   test_1_1_simpl_iter  s    z%TestShgoArguments.test_1_1_simpl_iterc                 C   s   ddi}t tdd|dd dS )z3Iterative simplicial on TestFunction 2 (univariate)r   FN	   r   r   r   r   r   r   r   test_1_2_simpl_iter  s    
z%TestShgoArguments.test_1_2_simpl_iterc                 C   s   t tdddd dS )z9Iterative Sobol sampling on TestFunction 1 (multivariate)Nr-   ry   r   r   r   r   r   r   test_2_1_sobol_iter  s    z%TestShgoArguments.test_2_1_sobol_iterc                 C   sL   t tjtjtjdddd}tjj|jtj	ddd tjj|j
tjdd dS )z7Iterative Sobol sampling on TestFunction 2 (univariate)Nr-   ry   )rz   r{   r|   r   rx   r   r   )r   r   r0   r   r(   r3   r   r   r/   r   r#   r   r   r   r   r   r   test_2_2_sobol_iter  s    z%TestShgoArguments.test_2_2_sobol_iterc              	   C   sR   dd }t tfD ]<}t|j|jdd|ddid t|j|jdd|ddid qd	S )
zKIterative sampling on TestFunction 1 and 2  (multi and univariate)
        c                 S   s   t d d S Nz Local minimization callback testrc   r5   r   r   r   callback_func  s    zATestShgoArguments.test_3_1_disp_simplicial.<locals>.callback_funcr-   r   r   Tr|   r   r}   r   r{   r   r}   r   Nr   r   r   r0   r   r   r   r   r   r   r   test_3_1_disp_simplicial  s    z*TestShgoArguments.test_3_1_disp_simplicialc              	   C   sR   dd }t tfD ]<}t|j|jdd|ddid t|j|jdd|ddid	 qd
S )zAIterative sampling on TestFunction 1 and 2 (multi and univariate)c                 S   s   t d d S r   r   r5   r   r   r   r   *  s    z<TestShgoArguments.test_3_2_disp_sobol.<locals>.callback_funcr-   ry   r   Tr   r   r   Nr   r   r   r   r   test_3_2_disp_sobol'  s    z%TestShgoArguments.test_3_2_disp_sobolc                 C   sF   t dd dgdd}t dd dgd}t|j|j t|j|j dS )	zMUsing `args` used to cause `shgo` to fail; see #14589, #15986,
        #16506c                 S   s   | | | S r   r   )r/   yzr   r   r   r   7  r   z5TestShgoArguments.test_args_gh14589.<locals>.<lambda>)r   rP   )r-   r,   )funcr   rg   c                 S   s   d|  d S )Nr,   r-   r   r5   r   r   r   r   9  r   )r   r   N)r   r   r#   r/   )r   r   r   r   r   r   test_args_gh145894  s    z#TestShgoArguments.test_args_gh14589c                 C   s$   t jddd}tt dd|dd dS )z,Test known function minima stopping criteriar   T)f_minf_tolr   Nrx   r   r{   r   r   r   r   r   r   r   r   r   r   test_4_1_known_f_min=  s    
z&TestShgoArguments.test_4_1_known_f_minc                 C   s&   t jdddd}tt dd|dd dS )	z*Test Global mode limiting local evalutionsr   Tr-   )r   r   r   
local_iterNrx   r   r   r   r   r   r   r   test_4_2_known_f_minH  s    
z&TestShgoArguments.test_4_2_known_f_minc              	   C   sJ   t jddddd}tt jt jt jdd|dd}tjj|j	t j
d	d	d
 dS )z8Test Global mode limiting local evaluations for 1D funcsr   Tr-   Fr   r   r   r   r   Nry   rz   r{   r|   r   r   rx   r   )r   r   r   r0   r   r(   r3   r   r   r/   r   r   r   r   r   r   r   test_4_4_known_f_minU  s    z&TestShgoArguments.test_4_4_known_f_minc                 C   s0   t tjtjtjd}tjj|jtj	ddd dS )z;Test Default simplicial sampling settings on TestFunction 1rz   rx   r   N
r   r   r0   r   r(   r3   r   r   r/   r   r   r   r   r   test_5_1_simplicial_arglesse  s    z-TestShgoArguments.test_5_1_simplicial_arglessc                 C   s2   t tjtjtjdd}tjj|jtj	ddd dS )z6Test Default sobol sampling settings on TestFunction 1ry   rz   r   rx   r   Nr   r   r   r   r   test_5_2_sobol_arglessk  s    z(TestShgoArguments.test_5_2_sobol_arglessc                 C   sR   ddi}t tjtjtj|dd}tjj|jtj	ddd tjj|j
tjdd dS )	:Test that maximum iteration option works on TestFunction 3Zmax_iterr,   r   rz   r   r   rx   r   r   Nr   r   r0   r   r(   r3   r   r   r/   r   r#   r   r   r   r   r   test_6_1_simplicial_max_iterr  s    z.TestShgoArguments.test_6_1_simplicial_max_iterc                 C   sR   ddi}t tjtjtj|dd}tjj|jtj	ddd tjj|j
tjdd dS )	r  Zmin_iterr,   r   r  rx   r   r   Nr  r   r   r   r   test_6_2_simplicial_min_iter{  s    z.TestShgoArguments.test_6_2_simplicial_min_iterc                 C   s,   dD ]"}|t jd}tt dd|dd qdS )z@Test the minimizer_kwargs arguments for solvers with constraints)COBYLASLSQP)methodrz   rt   MbP?ry   r{   r   r~   r   N)r   r(   r   )r   solverr~   r   r   r   test_7_1_minkwargs  s    z$TestShgoArguments.test_7_1_minkwargsc                 C   s0   ddi}ddi}t tjtjtjd ||d dS )z'Test the minimizer_kwargs default initsZftolrx   r   Tr   )rz   r~   r   Nr   r   r0   r   r(   )r   r~   r   r   r   r   test_7_2_minkwargs  s
    z$TestShgoArguments.test_7_2_minkwargsc                 C   sV   dD ]L}dd }dd }|||d}t d|  t d ttd	d
|dd qdS )z?Test minimizer_kwargs arguments for solvers without constraints)zNelder-MeadZPowellZCGZBFGS	Newton-CGzL-BFGS-BZTNCZdoglegz	trust-ncgztrust-exactztrust-krylovc                 S   s    t d| d  d| d  gjS )Nr,   r   r-   )r3   arrayTr5   r   r   r   jac  s    z1TestShgoArguments.test_7_3_minkwargs.<locals>.jacc                 S   s   t ddgddggS Nr,   r   )r3   r  r5   r   r   r   hess  s    z2TestShgoArguments.test_7_3_minkwargs.<locals>.hess)r
  r  r  z	Solver = zd====================================================================================================rt   r  ry   r  N)r   r   r   r   )r   r  r  r  r~   r   r   r   test_7_3_minkwargs  s    
z$TestShgoArguments.test_7_3_minkwargsc                 C   s    ddd}t td d |dd d S )Nr-   T)Zminhgrdr   r   r   r   r   r   r   r   test_8_homology_group_diff  s    
z,TestShgoArguments.test_8_homology_group_diffc                 C   s   t tjtjtjd d dS )'Test single function constraint passingr   r   Nr  r   r   r   r   test_9_cons_g  s    zTestShgoArguments.test_9_cons_gwin32z2Failing and fix in PyPy not planned (see gh-18632))reasonc                 C   s4   ddi}dd }t |tjd|d}|jdks0J dS )	r  maxtimer   c                 S   s   t d dS )Ng+=ra   )timesleepr5   r   r   r   r0     s    
z0TestShgoArguments.test_10_finite_time.<locals>.frM   )r|   r   r-   N)r   r   r   Znit)r   r   r0   r   r   r   r   test_10_finite_time  s    z%TestShgoArguments.test_10_finite_timec                 C   sN   ddd}t tjtjdd|dd}tjd|jd  tjd|jd	  dS )
*Test to cover the case where f_lowest == 0ra   T)r   r   rL   Nry   r   r   r-   )r   r   r0   r   r3   r   assert_equalr/   r   r   r   r   test_11_f_min_0  s    z!TestShgoArguments.test_11_f_min_0z no way of currently testing thisc                 C   s6   ddd}t tjtjdd|dd}tjd|j dS )r!  r   ra   )r  r   r-   Nry   r   )r   r   r0   r   r3   r   r"  r#   r   r   r   r   test_12_sobol_inf_cons  s    z(TestShgoArguments.test_12_sobol_inf_consc                 C   s0   dd }dgd }t ||dd}|dd d	S )
z-Test init of high-dimensional sobol sequencesc                 S   s   dS Nr   r   r5   r   r   r   r0     s    z/TestShgoArguments.test_13_high_sobol.<locals>.fr:   )   ry   r   r,   rI   N)r   Zsampling_function)r   r0   r   ZSHGOcr   r   r   test_13_high_sobol  s    
z$TestShgoArguments.test_13_high_sobolc                 C   s   ddi}t td|d dS )z6Test limited local iterations for a pseudo-global moder   rO   rA   r   Nr   r   r   r   r   test_14_local_iter  s    z$TestShgoArguments.test_14_local_iterc                 C   s   ddi}t tdd|dd dS )z9Test minimize every iter options and cover function cacher   Tr-   rR   ry   r   Nr   r   r   r   r   test_15_min_every_iter  s    
z(TestShgoArguments.test_15_min_every_iterc                 C   s$   ddi}ddi}t td||d dS )z:Test disp=True with minimizers that do not support bounds r   Tr
  znelder-meadr   )r   r   r~   Nr   )r   r   r~   r   r   r   test_16_disp_bounds_minimizer  s
    z/TestShgoArguments.test_16_disp_bounds_minimizerc                 C   s   dd }t td|d dS )=Test the functionality to add custom sampling methods to shgoc                 S   s   t jj| |fdS )N)size)r3   randomuniform)r{   dr   r   r   sample  s    z9TestShgoArguments.test_17_custom_sampling.<locals>.sampler<   r   Nr   )r   r1  r   r   r   test_17_custom_sampling  s    z)TestShgoArguments.test_17_custom_samplingc           	      C   s   dd }g d}g d}t t||}t||}t||}t||}|j|jksTJ |j|jksdJ |j|jkstJ tg d}tj	
|j| tj	
|j|j d S )Nc                 S   s   t |  S r   )r3   Zsquarer4   r5   r   r   r   r0     s    z1TestShgoArguments.test_18_bounds_class.<locals>.f)g      rb   g      )ro   rs   rJ   )ro   rb   ra   )r%   zipr   r   nfevmessager   r3   r  r   r   r/   )	r   r0   ZlbZubZ
bounds_oldZ
bounds_newZres_old_boundsZres_new_boundsZx_optr   r   r   test_18_bounds_class  s    



z&TestShgoArguments.test_18_bounds_classc                 C   s   t d }ttd|jd W d   n1 s.0    Y  ttdtd t d }ttd|jd W d   n1 st0    Y  ttdtd dS )r,  r,   r<   )r{   r   N)r   r   r   mapr   )r   pr   r   r   test_19_parallelization  s    
.
.z)TestShgoArguments.test_19_parallelizationc                 C   sz   dd }dd }dgd }dd }d	d
 }dd }d|ddd|dd|df}t ||dd|d t ||dd|dd dS )z0Test that constraints can be passed to argumentsc              
   S   sb   | d d  t t t| d d | d d   | d t t t| d | d d    S r[   r]   r5   r   r   r   	eggholder!  s
    &*z=TestShgoArguments.test_20_constrained_args.<locals>.eggholderc                 S   s0   d| d  d| d   d| d   d| d   S )	Nǧ8@r   g     :@r-   '   r,   g     @D@rP   r   r5   r   r   r   r0   '  s    z5TestShgoArguments.test_20_constrained_args.<locals>.fr   rb   rO   c                 S   s<   |d | d  |d | d   d| d   d| d   d	 S )
Ngffffff@r   gffffff@r-   g333333&@r,   g?rP   rM   r   )r/   rj   r   r   r   g1_modified,  s
    .z?TestShgoArguments.test_20_constrained_args.<locals>.g1_modifiedc                 S   s~   d| d  d| d   d| d   d| d   d	 d
t d| d d  d| d d   d| d d   d| d d     S )NrN   r   g'@r-   gfffffD@r,   gJ@rP      gRQ?gQ?gRQ?g     4@gףp=
?)r3   r^   r5   r   r   r   rF   0  s    2$z6TestShgoArguments.test_20_constrained_args.<locals>.g2c                 S   s$   | d | d  | d  | d  d S Nr   r-   r,   rP   r   r5   r   r   r   h16  s    z6TestShgoArguments.test_20_constrained_args.<locals>.h1r    )r   )r"   r#   rg   r!   r   r   r-   )r{   r|   rz   ry   )r{   r|   rz   r   N)r   )r   r:  r0   r   r=  rF   r@  r(   r   r   r   test_20_constrained_args  s    

z*TestShgoArguments.test_20_constrained_argsc              	   C   s   dd }t |ddgddggdddd	d
dd dd }ddgddgddgddgddgg}t ||dd	d
dd}t|g d|d
d}|jsJ t|j|j t|j|jdd dS )z|Test that shgo can handle objective functions that return the
        gradient alongside the objective value. Fixes gh-13547c                 S   s   t t | dd|  fS Nr,   )r3   r4   powerr5   r   r   r   r   F  s    z2TestShgoArguments.test_21_1_jac_true.<locals>.funcr7   r-   r,   rt   rM   ry   r	  T)r
  r  )r   r{   r|   r   r~   c                 S   s   t | d d|  fS rB  r2   r5   r   r   r   r   R  s    r   rP   )r   r   r~   )r-   r-   r-   r-   r-   )x0r   r  r   r   N)r   r
   r   r   r#   r/   )r   r   r   r   r   r   r   r   test_21_1_jac_trueB  s&    	"
z$TestShgoArguments.test_21_1_jac_true
derivativer  r  hesspc                 C   s   dd }dd }dd }dd }|||d	}||| i}d
di}dg}	t ||	||d}
t|fdg|	d||}|
js|J tj|
j|j tj|
j|j dS )zzshgo used to raise an error when passing `options` with 'jac'
        # see gh-12963. check that this is resolved
        c                 S   s$   d| d  | d  d| d   d S )NrP   r   r,   rM   r   r5   r   r   r   	objectivee  s    zATestShgoArguments.test_21_2_derivative_options.<locals>.objectivec                 S   s   d| d  d S )Nr8   r   r,   r   r5   r   r   r   gradienth  s    z@TestShgoArguments.test_21_2_derivative_options.<locals>.gradientc                 S   s   dS Nr8   r   r5   r   r   r   r  k  s    z<TestShgoArguments.test_21_2_derivative_options.<locals>.hessc                 S   s   d| S rK  r   )r/   r8  r   r   r   rH  n  s    z=TestShgoArguments.test_21_2_derivative_options.<locals>.hessprG  r
  trust-constr)irt   r~   r   r   )rD  r   N)r   r
   r   r3   r   r   r#   r/   )r   rF  rI  rJ  r  rH  Zderivative_funcsr   r~   r   r   r   r   r   r   test_21_2_derivative_options_  s"    
z.TestShgoArguments.test_21_2_derivative_optionsc                 C   st   g d}t td}ddi}tt|||d}tttdfddi|}|jsPJ t|j	|j	 t|j
|j
dd d	S )
zsEnsure the Hessian gets passed correctly to the local minimizer
        routine. Previous report gh-14533.
        )r   g?rO  r   gffffff?rP  rP  )r  r  r
  r  rM  rM   r   r   N)r   r   r   r   r
   r3   zerosr   r   r#   r/   )r   r   r   r~   r   r   r   r   r   test_21_3_hess_options_rosen  s    

z.TestShgoArguments.test_21_3_hess_options_rosenc                 C   sp   dd }ddd d}dg}t ||d|d	d
}t|td|d|d}|jsPJ t|j|j t|j|j dS )zwshgo used to raise an error when passing `args` with Sobol sampling
        # see gh-12114. check that this is resolvedc                 S   s   | d | S r%  r   )r/   rh   r   r   r   r#     s    z6TestShgoArguments.test_21_arg_tuple_sobol.<locals>.funr    c                 S   s   | d d S rv   r   r5   r   r   r   r     r   z;TestShgoArguments.test_21_arg_tuple_sobol.<locals>.<lambda>r!   r   rL   )r-   ry   )rg   rz   r   r-   )r   rg   rz   N)r   r
   r3   rQ  r   r   r#   r/   )r   r#   rz   r   r   r   r   r   r   test_21_arg_tuple_sobol  s    

z)TestShgoArguments.test_21_arg_tuple_sobolN).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  Zxfailr   sysplatformr   r#  r   r$  r(  r)  r*  r+  r2  r6  r9  rA  rE  ZparametrizerN  rR  rT  r   r   r   r   r     sR   		


		



$
r   c                   @   sd   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S )TestShgoFailuresc                 C   sD   ddi}t tjtjdd|dd}tjd|j tjd|j dS )z'Test failure on insufficient iterationsmaxiterr,   Nry   r   FrO   )	r   r   r0   r   r3   r   r"  r   Ztnevr   r   r   r   test_1_maxiter  s    zTestShgoFailures.test_1_maxiterc                 C   s   t tttjtjdd dS )z$Rejection of unknown sampling methodZ	not_Sobolr'  N)assert_raises
ValueErrorr   r   r0   r   r   r   r   r   test_2_sampling  s    z TestShgoFailures.test_2_samplingc                 C   sD   ddd}t tjtjd|dd}tjd|j tjd|j d	S )
zoCheck that the routine stops when no minimiser is found
           after maximum specified function evaluationsrL   T)Zmaxfevr   rP   ry   r{   r   r   FrN   N)	r   
test_tabler0   r   r3   r   r"  r   r4  r   r   r   r   test_3_1_no_min_pool_sobol  s    z+TestShgoFailures.test_3_1_no_min_pool_sobolc                 C   s4   ddd}t tjtjd|dd}tjd|j dS )	zoCheck that the routine stops when no minimiser is found
           after maximum specified sampling evaluationsrL   Tmaxevr   rP   r   r]  FN)r   r^  r0   r   r3   r   r"  r   r   r   r   r   test_3_2_no_min_pool_simplicial  s    z0TestShgoFailures.test_3_2_no_min_pool_simplicialc                 C   s   ddg}t tttj| dS )zSpecified bounds ub > lb)r8   rP   rP   rM   NrZ  r[  r   r   r0   r   r   r   r   r   test_4_1_bound_err  s    z#TestShgoFailures.test_4_1_bound_errc                 C   s   ddg}t tttj| dS )z)Specified bounds are of the form (lb, ub))rP   rM   rM   rc  Nrd  re  r   r   r   test_4_2_bound_err  s    z#TestShgoFailures.test_4_2_bound_errc                 C   s8   ddd}t tjtjtjd|dd}tjd|j dS )zxEnsures the algorithm terminates on infeasible problems
           after maxev is exceeded. Use infty constraints optionrt   Tr`  ry   rz   r{   r   r   FN	r   test_infeasibler0   r   r(   r3   r   r"  r   r   r   r   r   test_5_1_1_infeasible_sobol  s    
z,TestShgoFailures.test_5_1_1_infeasible_sobolc                 C   s:   dddd}t tjtjtjd|dd}tjd|j dS )zEnsures the algorithm terminates on infeasible problems
           after maxev is exceeded. Do not use infty constraints optionrt   TF)ra  r   r   ry   rh  Nri  r   r   r   r   test_5_1_2_infeasible_sobol  s    
z,TestShgoFailures.test_5_1_2_infeasible_sobolc                 C   s8   ddd}t tjtjtjd|dd}tjd|j dS )z[Ensures the algorithm terminates on infeasible problems
           after maxev is exceeded.i  Fr`  rt   r   rh  Nri  r   r   r   r   test_5_2_infeasible_simplicial  s    
z/TestShgoFailures.test_5_2_infeasible_simplicialc                 C   sP   t jd ddddd}t jt jf}t jdd|dd	}tttg|R i | dS )
z<Test Global mode limiting local evaluations with f* too highr\   r   Tr-   Fr   Nry   r   )r   r   r0   r   r(   r   UserWarningr   )r   r   rg   kwargsr   r   r   test_6_1_lower_known_f_min  s    z+TestShgoFailures.test_6_1_lower_known_f_minc                    sL   ddl mm} g d} fdd d _| |}t|j|j j d S )Nr   )r   r   r   r,   rr  rr  rr  rr  c                    s     j d7  _ | S Nr-   )r4  r5   r#   r   r   r   r#     s    z"TestShgoFailures.test.<locals>.fun)scipy.optimizer   r   r4  rc   r/   r#   )r   r   r   resultr   rt  r   r     s    
zTestShgoFailures.testN)r   r   r   rY  r\  r_  rb  rf  rg  rk  rl  rm  rp  r   r   r   r   r   rW    s   
	rW  c                   @   s   e Zd Zdd Zdd ZdS )TestShgoReturnsc                    s:   g d} fdd d _ t |}tj j |j  d S )Nrq  c                    s     j d7  _ t| S rs  r4  r   r5   r#   r   r   r#     s    z3TestShgoReturns.test_1_nfev_simplicial.<locals>.funr   r4  r   r3   r   r"  r   r   rv  r   ry  r   test_1_nfev_simplicial  s
    
z&TestShgoReturns.test_1_nfev_simplicialc                    s>   g d} fdd d _ t |dd}tj j |j  d S )Nrq  c                    s     j d7  _ t| S rs  rx  r5   ry  r   r   r#   '  s    z.TestShgoReturns.test_1_nfev_sobol.<locals>.funr   ry   r'  rz  r{  r   ry  r   test_1_nfev_sobol$  s
    z!TestShgoReturns.test_1_nfev_sobolN)r   r   r   r|  r}  r   r   r   r   rw    s   rw  c                  C   s   dd } t | dgdg}t|tddg}ttddg|dd}tt|jd	 dks^J tt|jd	 d
ks|J |j	sJ d S )Nc                 S   s   t | } t | d gS rB  rG   Zasarrayr4   r5   r   r   r   quad3  s    
z$test_vector_constraint.<locals>.quadg@rP   rb   rS  ry   r  r,   rs   )
r   r   rG   r  r   r   allr4   r/   r   )r  nlcZoldcr   r   r   r   test_vector_constraint1  s    r  zignore:delta_gradc                  C   s   dd } t | dgdg}ddi}ttddg|d|d	}tt|jd
 dksTJ tt|jd
 dksrJ |js|J d S )Nc                 S   s   t | } t | d gS rB  r~  r5   r   r   r   r  B  s    
ztest_trust_constr.<locals>.quadg@rP   r
  rL  rS  ry   )rz   r   r~   r,   rs   )r   r   r   rG   r  r4   r/   r   )r  r  r~   r   r   r   r   test_trust_constr@  s    r  c                  C   s   dgd } dd }t |dd}tt| |d}tt|jd dd }d|d	}tt| |d}tt|jd d
gd } dd }d|d	}tdd | |dd}tt|jd d S )N)g?rn   r,   c                 S   s   | d | d  S rv   r   r5   r   r   r   faulty[  s    z)test_equality_constraints.<locals>.faulty333333@r   c                 S   s   | d | d  d S )Nr   r-   r  r   r5   r   r   r   r  b  s    r   r!   r<  rO   c                 S   s$   | d | d  | d  | d  d S r?  r   r5   r   r   r   r  k  s    c                 S   s   t |  S r   )rG   prodr5   r   r   r   r   q  r   z+test_equality_constraints.<locals>.<lambda>ry   )r   rz   r   rb   )r   r   r   r   rG   r4   r/   )r   r  r  r   rz   r   r   r   test_equality_constraintsW  s&    



r  c                  C   sj   dd } | dd}ddddd	d
}t tdgd ||d}|jd  dksPJ |jd d dksfJ d S )Nc                 S   s   t | d d S r  )rG   r4   r5   r   r   r   r(   y  s    ztest_gh16971.<locals>.consr    )r#   r"   r  rM   rq   g?)ZrhobegZtolcatol)r
  r   rS  r,   )rz   r~   r
  Zcobylar   r  )r   r   r~   lower)r(   cr~   ri   r   r   r   test_gh16971x  s    

r  )	r   rx   rt   NNNNry   r-   )Er   rU  r3   rG   r  multiprocessingr   Znumpy.testingr   r   r   r   rZ  r   ru  r   r   r	   r
   r   r   r   r   Zscipy.optimize._constraintsr   Zscipy.optimize._shgor   r   r)   r*   r   r   r   r;   r  r   r   rC   r   rK   r   rZ   r   r`   rd   r%   r3  ZboundsLJr   rp   Zonesr   rr   r^  ru   rj  r   r   r   r   r   r   rW  rw  r  filterwarningsr  r  r  r   r   r   r   <module>   s   (


%"
	

   ":l   $u

!