o
    ?Hh[                     @   sx   d Z ddlmZmZmZmZ ddlmZ ddlZddl	Z
ddlZddlmZmZmZmZ G dd dZG dd	 d	ZdS )
z#
Unit test for SLSQP optimization.
    )assert_assert_array_almost_equalassert_allcloseassert_equal)raisesN)
fmin_slsqpminimizeBoundsNonlinearConstraintc                   @   s    e Zd ZdZdd Zdd ZdS )
MyCallBackzJpass a custom callback function

    This makes sure it's being used.
    c                 C   s   d| _ d| _d S )NFr   been_calledncallsself r   _/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/scipy/optimize/tests/test_slsqp.py__init__   s   
zMyCallBack.__init__c                 C   s   d| _ |  jd7  _d S )NT   r   r   xr   r   r   __call__   s   zMyCallBack.__call__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s    r   c                   @   s  e Zd ZdZdd ZdnddZdnddZdnd	d
ZdnddZdnddZ	dnddZ
dnddZdnddZdnd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.e/j0j1e2j3dYdZd[ d\ d] d^kd_d`dadb Z4dcdd Z5dedf Z6dgdh Z7didj Z8e/j0j9dkdl Z:dmS )o	TestSLSQPz
    Test SLSQP algorithm using Example 14.4 from Numerical Methods for
    Engineers by Steven Chapra and Raymond Canale.
    This example maximizes the function f(x) = 2*x*y + 2*x - x**2 - 2*y**2,
    which has a maximum at x=2, y=1.
    c                 C   s   ddi| _ d S )NdispF)optsr   r   r   r   setup_method#      zTestSLSQP.setup_method      ?c                 C   s<   |d }|d }|d| | d|  |d  d|d    S )a  
        Arguments:
        d     - A list of two elements, where d[0] represents x and d[1] represents y
                 in the following equation.
        sign - A multiplier for f. Since we want to optimize it, and the SciPy
               optimizers can only minimize functions, we need to multiply it by
               -1 to achieve the desired solution
        Returns:
        2*x*y + 2*x - x**2 - 2*y**2

        r   r      r   )r   dsignr   yr   r   r   fun&   s   ,zTestSLSQP.func                 C   sL   |d }|d }|d| d|  d  }|d| d|   }t ||gtS )zo
        This is the derivative of fun, returning a NumPy array
        representing df/dx and df/dy.

        r   r   r"      )nparrayfloat)r   r#   r$   r   r%   dfdxdfdyr   r   r   jac6   s
   zTestSLSQP.jacc                 C   s   |  ||| ||fS N)r&   r.   )r   r#   r$   r   r   r   fun_and_jacB      zTestSLSQP.fun_and_jacc                 C   s   t |d |d  gS ) Equality constraint r   r   r)   r*   r   r   r$   r   r   r   f_eqconE      zTestSLSQP.f_eqconc                 C      t ddggS )z! Equality constraint, derivative r   r3   r4   r   r   r   fprime_eqconI      zTestSLSQP.fprime_eqconc                 C   s   |  ||d S )z Scalar equality constraint r   )r5   r4   r   r   r   f_eqcon_scalarM   r:   zTestSLSQP.f_eqcon_scalarc                 C   s   |  ||d  S )z( Scalar equality constraint, derivative r   )r9   tolistr4   r   r   r   fprime_eqcon_scalarQ   s   zTestSLSQP.fprime_eqcon_scalarc                 C   s   t |d |d  d gS )z Inequality constraint r   r   r!   r3   r4   r   r   r   f_ieqconU   s   zTestSLSQP.f_ieqconc                 C   r7   )z# Inequality constraint, derivative r   r8   r3   r4   r   r   r   fprime_ieqconY   r:   zTestSLSQP.fprime_ieqconc                 C   s
   t |S )z Vector inequality constraint )r)   asarrayr   r   r   r   	f_ieqcon2]   s   
zTestSLSQP.f_ieqcon2c                 C   s   t |jd S )z* Vector inequality constraint, derivative r   )r)   identityshaper   r   r   r   fprime_ieqcon2a   r:   zTestSLSQP.fprime_ieqcon2c              	   C   sT   g d}|D ]!}t | jddgd|d| jd}t|d |d  t|jd	d
g qd S )NNFz2-pointz3-point      r!   rF   SLSQPargsr.   methodoptionssuccessmessager"   r   )r   r&   r   r   r   r   r   jacsr.   resr   r   r   $test_minimize_unbounded_approximatedf   s   z.TestSLSQP.test_minimize_unbounded_approximatedc                 C   sD   t | jddgd| jd| jd}t|d |d  t|jdd	g d S )
NrF   r!   rG   rH   rI   rM   rN   r"   r   )r   r&   r.   r   r   r   r   r   rQ   r   r   r   test_minimize_unbounded_givenp   s
   
z'TestSLSQP.test_minimize_unbounded_givenc                 C   s   g d}|D ]J}t jdd t| jddgd|dd| jd	}W d    n1 s(w   Y  t|d
 |d  t|jddg td|jd k t|jd dk qd S )NrE   ignore)invalidrF   r!   rG   ))      @N)N      ?rH   )rJ   r.   boundsrK   rL   rM   rN   rW   rX   r   r   )r)   errstater   r&   r   r   r   r   rO   r   r   r   "test_minimize_bounded_approximatedw   s   z,TestSLSQP.test_minimize_bounded_approximatedc                 C   sB   t | jddgddd| jd}t|d |d  t|jd	d
g d S )NrF   r!   rG   TrH   rI   rM   rN   r"   r   )r   r0   r   r   r   r   rS   r   r   r    test_minimize_unbounded_combined   s
   z*TestSLSQP.test_minimize_unbounded_combinedc              
   C   s`   g d}|D ]'}t | jddgd|d| jddd| jd}t|d	 |d
  t|jddg qd S )NrE   rF   r!   rG   eqtyper&   rJ   rH   )rJ   r.   constraintsrK   rL   rM   rN   r   )r   r&   r5   r   r   r   r   rO   r   r   r   #test_minimize_equality_approximated   s   z-TestSLSQP.test_minimize_equality_approximatedc              
   C   sP   t | jddg| jddd| jdd| jd}t|d |d	  t|jd
d
g d S )NrF   r!   rH   rG   r]   r^   r.   rK   rJ   r`   rL   rM   rN   r   )r   r&   r.   r5   r   r   r   r   rS   r   r   r   test_minimize_equality_given   s   z&TestSLSQP.test_minimize_equality_givenc                 C   T   t | jddgd| jdd| jd| jd| jd}t|d |d	  t|jd
d
g d S NrF   r!   rH   rG   r]   r_   r&   rJ   r.   rK   r.   rJ   r`   rL   rM   rN   r   	r   r&   r.   r5   r9   r   r   r   r   rS   r   r   r   test_minimize_equality_given2      z'TestSLSQP.test_minimize_equality_given2c                 C   rd   re   )	r   r&   r.   r;   r=   r   r   r   r   rS   r   r   r   (test_minimize_equality_given_cons_scalar   rj   z2TestSLSQP.test_minimize_equality_given_cons_scalarc              
   C   sT   t | jddgd| jdd| jdd| jd}t|d |d	  t|jd
dgdd d S )NrF   r!   rH   rG   ineqr^   rg   rM   rN   r"   r   MbP?atol)r   r&   r.   r>   r   r   r   r   rS   r   r   r   test_minimize_inequality_given   s   z(TestSLSQP.test_minimize_inequality_givenc              
   C   sR   t | jddg| jddd| j| jd| jd}t|d |d	  t|jd
dg d S )NrF   r!   rH   rG   rl   )r_   r&   r.   rb   rM   rN   r"   r   )	r   r&   r.   rA   rD   r   r   r   r   rS   r   r   r   1test_minimize_inequality_given_vector_constraints   s   z;TestSLSQP.test_minimize_inequality_given_vector_constraintsc                 C   sT   dd }dd }t |ddg}tddg}td	d	gd
d
g}t||d||d d S )Nc                 S   s^   d| d   krdkr!n J | d| d   kr dks%J |  J | | d d | d  S )Nr   r   rX   r   r   r   r   r   c   s   Jz5TestSLSQP.test_minimize_bounded_constraint.<locals>.cc                 S   sd   d| d   krdkr!n J | d| d   kr dks%J |  J | | d d  | d d  S Nr   r   r"   r   rr   r   r   r   f   s   Jz5TestSLSQP.test_minimize_bounded_constraint.<locals>.fr   g      ?g?rX   g        r!   rH   rK   rY   r`   )r
   r)   r@   r	   r   )r   rs   ru   cnsx0bndr   r   r    test_minimize_bounded_constraint   s   z*TestSLSQP.test_minimize_bounded_constraintc                 C   s   t | jddgd| jdddgd| jd| jd| jd	}t|d
 |d  t|jddgdd td|jd   ko:dkn   td|jd   koNdk d S    d S )NrF   r!   rH   rG   皙r!   r8   皙?r]   rf   )rK   r.   rJ   rY   r`   rL   rM   rN   r~   rm   rn   r|   r   r   r8   rh   rS   r   r   r   #test_minimize_bound_equality_given2   s   ",z-TestSLSQP.test_minimize_bound_equality_given2c                 C   sF   t | jddgdddd}|\}}}}}t|dk| t|ddg d S )NrF   r!   rG   r   r   )rJ   iprintfull_outputr"   )r   r&   r   r   r   rQ   r   fxitsimodesmoder   r   r   test_unbounded_approximated   s   z%TestSLSQP.test_unbounded_approximatedc                 C   sJ   t | jddgd| jddd}|\}}}}}t|dk| t|ddg d S )NrF   r!   rG   r   r   )rJ   fprimer   r   r"   )r   r&   r.   r   r   r   r   r   r   test_unbounded_given   s   zTestSLSQP.test_unbounded_givenc                 C   sL   t | jddgd| jgddd}|\}}}}}t|dk| t|ddg d S )NrF   r!   rG   r   r   )rJ   eqconsr   r   )r   r&   r5   r   r   r   r   r   r   test_equality_approximated  s   z$TestSLSQP.test_equality_approximatedc              	   C   sP   t | jddg| jd| jgddd}|\}}}}}t|dk| t|ddg d S )NrF   r!   rG   r   r   )r   rJ   r   r   r   )r   r&   r.   r5   r   r   r   r   r   r   test_equality_given  s   zTestSLSQP.test_equality_givenc              
   C   sR   t | jddg| jd| j| jddd}|\}}}}}t|dk| t|ddg d S )NrF   r!   rG   r   r   )r   rJ   f_eqconsfprime_eqconsr   r   r   r&   r.   r5   r9   r   r   r   r   r   r   test_equality_given2  s   zTestSLSQP.test_equality_given2c              	   C   sT   t | jddg| jd| jgddd}|\}}}}}t|dk| t|ddgdd	 d S )
NrF   r!   rG   r   r   )r   rJ   ieqconsr   r   r"      decimal)r   r&   r.   r>   r   r   r   r   r   r   test_inequality_given'  s   zTestSLSQP.test_inequality_givenc                 C   s   t | jddg| jdddg| j| jddd	}|\}}}}}t|dk| t|d	d	gd
d td|d   ko8dkn   td|d   koKd	k d S    d S )NrF   r!   rG   r{   r}   r   r   )r   rJ   rY   r   r   r   r   r~   r   r   r|   r8   r   r   r   r   r   test_bound_equality_given21  s    *z$TestSLSQP.test_bound_equality_given2c                 C   sR   t dd dgdd gdd}t|dg t dd dgd	d dd
}t|dg d S )Nc                 S      | d S Nr"   r   zr   r   r   <lambda>A      z3TestSLSQP.test_scalar_constraints.<locals>.<lambda>g      @c                 S      | d d S Nr   r   r   r   r   r   r   r   B      r   )r   r   r!   c                 S   r   r   r   r   r   r   r   r   F  r   c                 S   s   | d d gS r   r   r   r   r   r   r   G  s    )	f_ieqconsr   )r   r   r   r   r   r   test_scalar_constraints?  s   z!TestSLSQP.test_scalar_constraintsc                 C   s    t dd dgddggdd d S )Nc                 S   s   | d d S Nr"   r   r   r   r   r   r   r   M  r   z/TestSLSQP.test_integer_bounds.<locals>.<lambda>r   r   rY   r   r   r   r   r   r   test_integer_boundsK  s    zTestSLSQP.test_integer_boundsc                 C   sP   t j t jft dgt dgfg}tdd ddg|dd}t|ddg d S )Nr"   r   c                 S   s   t | d d S r   )r)   sumr   r   r   r   r   T  s    z-TestSLSQP.test_array_bounds.<locals>.<lambda>rW   r   r   )r)   infr*   r   r   )r   rY   r   r   r   r   test_array_boundsO  s
   &zTestSLSQP.test_array_boundsc                 C   s@   t t tdd g d W d    d S 1 sw   Y  d S )Nc                 S   s   ddgS r   r   rr   r   r   r   r   \  r   z7TestSLSQP.test_obj_must_return_scalar.<locals>.<lambda>r   r"   r   )assert_raises
ValueErrorr   r   r   r   r   test_obj_must_return_scalarX  s   
"z%TestSLSQP.test_obj_must_return_scalarc                 C   s   t dd g ddd d S )Nc                 S   s   dgS Nr   r   rr   r   r   r   r   b  s    z;TestSLSQP.test_obj_returns_scalar_in_list.<locals>.<lambda>r   r   )r   r   r   r   r   r   test_obj_returns_scalar_in_list^  s   z)TestSLSQP.test_obj_returns_scalar_in_listc                 C   sR   t  }t| jddgdd|| jd}t|d |d  t|j t|j|d  d S )	NrF   r!   rG   rH   )rJ   rK   callbackrL   rM   rN   nit)r   r   r&   r   r   r   r   r   )r   r   rQ   r   r   r   test_callbackd  s   
zTestSLSQP.test_callbackc                 C   sv   ddg}dd }dd }t dd |d	|d
d|d
fddd}|j}t||ddd t||dk t|j| d S )Nr   r   c                 S   s   | d | d  d S rt   r   rr   r   r   r   f1x     z5TestSLSQP.test_inconsistent_linearization.<locals>.f1c                 S   s   | d d d S Nr   r"   r   r   rr   r   r   r   f2z     z5TestSLSQP.test_inconsistent_linearization.<locals>.f2c                 S      | d d | d d  S r   r   rr   r   r   r   r   }      z;TestSLSQP.test_inconsistent_linearization.<locals>.<lambda>r]   r_   r&   rl   r   Nr   rH   r`   rY   rK   g:0yE>rn   g:0yE)r   r   r   r   rM   )r   r   r   r   solr   r   r   test_inconsistent_linearizationm  s    
z)TestSLSQP.test_inconsistent_linearizationc                 C   sH   ddg}t dd |ddd ddd	d dfd
dd}t|j | d S )Nr   r"   c                 S   r   r   r   rr   r   r   r   r     r   z0TestSLSQP.test_regression_5743.<locals>.<lambda>r]   c                 S      | d | d  d S r   r   rr   r   r   r   r         r   rl   c                 S   r   )Nr   r"   r   rr   r   r   r   r     r   r   rH   r   )r   r   rM   )r   r   r   r   r   r   test_regression_5743  s   zTestSLSQP.test_regression_5743c                 C   s.   dd }t |g ddd}t|jjdk d S )Nc                 S   s8   | d d d d| d d d   d| d d d   S )Nr   r   r"   rX   r   rr   r   r   r   func  s   8z$TestSLSQP.test_gh_6676.<locals>.funcr   r   r   rH   rK   )r   )r   r   r.   rC   )r   r   r   r   r   r   test_gh_6676  s   zTestSLSQP.test_gh_6676c              	   C   sv   dddt jdft jdffdt j fdfg}|D ]!}tt t| jddg|d	d
 W d    n1 s3w   Y  qd S )N)r   r"   r"   r   )r   r   )r   r   r   r   )r   r   rF   r!   rH   )rY   rK   )r)   r   r   r   r   r&   )r   bounds_listrY   r   r   r   test_invalid_bounds  s   
zTestSLSQP.test_invalid_boundsc                 C   s   dd }t |dgddgd}t|j t|jddd	 t |d
gddgd}t|j t|jddd	 t |d
gddgd}t|j t|jddd	 t |dgddgd}t|j t|jddd	 t |dgddgd}t|j t|jddd	 t |dgddgd}t|j t|jddd	 d S )Nc                 S   s   | d d d S rt   r   rr   r   r   r   ru     r   z)TestSLSQP.test_bounds_clipping.<locals>.f
   slsqpr   rK   rY   r   绽|=rn   )r"   Nr"         )r8   r   r   r   rM   r   r   )r   ru   r   r   r   r   test_bounds_clipping  s&   





zTestSLSQP.test_bounds_clippingc                 C   sP  dd }ddd dg}ddd dg}ddd ddd	d dg}t |d
gd|d}t|j t|jddd t |dgd|d}t|j t|jddd t |dgd|d}t|j t|jddd t |d
gd|d}t|j t|jddd t |dgd|d}t|j t|jddd t |d
gd|d}t|j t|jddd d S )Nc                 S   s   | \} | |  d|   d S r   r   rr   r   r   r   ru     s   z,TestSLSQP.test_infeasible_initial.<locals>.frl   c                 S      d|  S r   r   rr   r   r   r   r     r   z3TestSLSQP.test_infeasible_initial.<locals>.<lambda>r   c                 S   s   | d S r   r   rr   r   r   r   r     r   c                 S   r   r   r   rr   r   r   r   r     r   c                 S   s   | d S Nr   r   rr   r   r   r   r     r   r   r   )rK   r`   r   r   rn   r   r"   r   r   )r   ru   cons_ucons_lcons_ulr   r   r   r   test_infeasible_initial  s0   





z!TestSLSQP.test_infeasible_initialdicts)mode	Compilersfortrannamez
intel-llvmz7Runtime warning due to floating point issues, not logic)reasonc                 C   sZ   dd }dd }dd }d}d}t d	|d
t d	|d
f}t||d||d}t|j  d S )Nc                 S   s   d| d  d| d   S )Nr8   r   r(   r   r   rr   r   r   r   cost  r1   z6TestSLSQP.test_inconsistent_inequalities.<locals>.costc                 S   s   | d | d  d S )Nr   r   r   rr   r   r   r   	ineqcons1  r   z;TestSLSQP.test_inconsistent_inequalities.<locals>.ineqcons1c                 S   s   | d | d  S r   r   rr   r   r   r   	ineqcons2  r   z;TestSLSQP.test_inconsistent_inequalities.<locals>.ineqcons2)r      )r   r   rl   r   rH   rv   )dictr   r   rM   )r   r   r   r   rx   rY   consrQ   r   r   r   test_inconsistent_inequalities  s   z(TestSLSQP.test_inconsistent_inequalitiesc                 C   sP   dd }t ddgtjtjg}t|ddgd|d}t|j t|jddg d S )Nc                 S   r   r   r   rr   r   r   r   ru   	  r1   z)TestSLSQP.test_new_bounds_type.<locals>.fr   r   r   r   )r	   r)   r   r   r   rM   r   r   )r   ru   rY   r   r   r   r   test_new_bounds_type  s
   
zTestSLSQP.test_new_bounds_typec                 C   s    G dd d}| }|   d S )Nc                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
z9TestSLSQP.test_nested_minimization.<locals>.NestedProblemc                 S   s
   d| _ d S r   )F_outer_countr   r   r   r   r     s   
zBTestSLSQP.test_nested_minimization.<locals>.NestedProblem.__init__c                 S   sn   |  j d7  _ | j dkrtdt| jddd}t|j t|jddg |d d |d d  |d d  S )	Nr   i  z(Nested minimization failed to terminate.)r   r(   rH   r   r   r"   )r   	Exceptionr   F_innerr   rM   r   r   )r   r   	inner_resr   r   r   F_outer  s   

$zATestSLSQP.test_nested_minimization.<locals>.NestedProblem.F_outerc                 S   s    |d d d |d d d  S rt   r   r   r   r   r   r      s    zATestSLSQP.test_nested_minimization.<locals>.NestedProblem.F_innerc                 S   s.   t | jddd}t|j t|jg d d S )N)r   r   r   rH   r   r   )r   r   r   rM   r   r   )r   	outer_resr   r   r   solve#  s   
z?TestSLSQP.test_nested_minimization.<locals>.NestedProblem.solveN)r   r   r   r   r   r   r   r   r   r   r   NestedProblem  s
    	r   )r   )r   r   problemr   r   r   test_nested_minimization  s   z"TestSLSQP.test_nested_minimizationc                 C   s|   dd }dd }dd }d|d}d|d}t |d	d
gd||gddgd}tj|jd tj|jddg |js<J d S )Nc                 S   s   t | d S r   )r)   sqrtrr   r   r   r   r&   /  r    z"TestSLSQP.test_gh1758.<locals>.func                 S   s   | d d| d  d  S )r2   r   r"   r   r   r   rr   r   r   r   r5   2  r6   z&TestSLSQP.test_gh1758.<locals>.f_eqconc                 S   s   | d | d  d d  S )r2   r   r   r   r   rr   r   r   r   f_eqcon26  s   z'TestSLSQP.test_gh1758.<locals>.f_eqcon2r]   r      g      ?rH   )r   r   )r   r   )rK   r`   rY   g8r](k?gQUU?gc@?)r   r)   testingr   r&   r   rM   )r   r&   r5   r   c1c2rQ   r   r   r   test_gh1758+  s   

zTestSLSQP.test_gh1758c              	   C   sd   t jd ddd dddd df}d}dd	 }g d
}t||d||dddd}|jr0J d S )Nr   rl   c                 S   s   | d  | d  d S )Nr   r   r   r   rr   r   r   r   r   F  s    z'TestSLSQP.test_gh9640.<locals>.<lambda>r   c                 S   r   )Nr   r"   r   rr   r   r   r   r   G  r   )r'   r"   r   r   c                 S   s   dS r   r   rr   r   r   r   targetJ  s   z%TestSLSQP.test_gh9640.<locals>.target)g51gУX{gP(rH   Fi'  )r   maxiter)rK   rY   r`   rL   )r)   randomseedr   rM   )r   r   bndsr   rx   rQ   r   r   r   test_gh9640D  s   zTestSLSQP.test_gh9640c                    s   t jd tt dgt dg t j}t  j j j t j|  } fdd}tj	t
dd t||d d	}|jsEJ W d    d S 1 sPw   Y  d S )
Nr   g?r!   c                    s   |  j k s	J tj| S r/   )lballr)   linalgnormrr   rY   r   r   ru   _  s   z7TestSLSQP.test_parameters_stay_within_bounds.<locals>.fzx were outside bounds)matchrH   r   )r)   r   r   r	   r*   lenr   ubpytestwarnsRuntimeWarningr   rM   )r   n_inputsrx   ru   rQ   r   r  r   "test_parameters_stay_within_boundsS  s   

"z,TestSLSQP.test_parameters_stay_within_boundsN)r!   );r   r   r   r   r   r&   r.   r0   r5   r9   r;   r=   r>   r?   rA   rD   rR   rT   r[   r\   ra   rc   ri   rk   rp   rq   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  markxfailscipyshow_configr   r   r   r   r   thread_unsafer  r   r   r   r   r      sr    










		

		#
r   )r   numpy.testingr   r   r   r   r  r   r   numpyr)   r  scipy.optimizer   r   r	   r
   r   r   r   r   r   r   <module>   s    