o
    iZ                     @   sp   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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   n/var/www/html/eduruby.in/lip-sync/lip-sync-env/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dfddZdfddZdfd	d
ZdfddZdfddZ	dfddZ
dfddZdfddZdfd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 Z4deS )g	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%   ZdfdxZdfdyr   r   r   jac5   s
   zTestSLSQP.jacc                 C   s   |  ||| ||fS N)r&   r,   )r   r#   r$   r   r   r   fun_and_jacA      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_eqconD      zTestSLSQP.f_eqconc                 C      t ddggS )z! Equality constraint, derivative r   r1   r2   r   r   r   fprime_eqconH      zTestSLSQP.fprime_eqconc                 C   s   |  ||d S )z Scalar equality constraint r   )r3   r2   r   r   r   f_eqcon_scalarL   r8   zTestSLSQP.f_eqcon_scalarc                 C   s   |  ||d  S )z( Scalar equality constraint, derivative r   )r7   tolistr2   r   r   r   fprime_eqcon_scalarP   s   zTestSLSQP.fprime_eqcon_scalarc                 C   s   t |d |d  d gS )z Inequality constraint r   r   r!   r1   r2   r   r   r   f_ieqconT   s   zTestSLSQP.f_ieqconc                 C   r5   )z# Inequality constraint, derivative r   r6   r1   r2   r   r   r   fprime_ieqconX   r8   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_ieqcon2`   r8   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!   rD   SLSQPargsr,   methodoptionssuccessmessager"   r   )r   r&   r   r   r   r   r   Zjacsr,   resr   r   r   $test_minimize_unbounded_approximatede   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 )
NrD   r!   rE   rF   rG   rK   rL   r"   r   )r   r&   r,   r   r   r   r   r   rN   r   r   r   test_minimize_unbounded_giveno   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 )NrC   ignore)invalidrD   r!   rE   ))      @N)N      ?rF   )rH   r,   boundsrI   rJ   rK   rL   rT   rU   r   r   )r)   Zerrstater   r&   r   r   r   r   rM   r   r   r   "test_minimize_bounded_approximatedv   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 )NrD   r!   rE   TrF   rG   rK   rL   r"   r   )r   r.   r   r   r   r   rP   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 )NrC   rD   r!   rE   eqtyper&   rH   rF   )rH   r,   constraintsrI   rJ   rK   rL   r   )r   r&   r3   r   r   r   r   rM   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 )NrD   r!   rF   rE   rY   rZ   r,   rI   rH   r\   rJ   rK   rL   r   )r   r&   r,   r3   r   r   r   r   rP   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 NrD   r!   rF   rE   rY   r[   r&   rH   r,   rI   r,   rH   r\   rJ   rK   rL   r   	r   r&   r,   r3   r7   r   r   r   r   rP   r   r   r   test_minimize_equality_given2      z'TestSLSQP.test_minimize_equality_given2c                 C   r`   ra   )	r   r&   r,   r9   r;   r   r   r   r   rP   r   r   r   (test_minimize_equality_given_cons_scalar   rf   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 )NrD   r!   rF   rE   ineqrZ   rc   rK   rL   r"   r   MbP?Zatol)r   r&   r,   r<   r   r   r   r   rP   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 )NrD   r!   rF   rE   rh   )r[   r&   r,   r^   rK   rL   r"   r   )	r   r&   r,   r?   rB   r   r   r   r   rP   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   rU   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   rm   r   r   r   f   s   Jz5TestSLSQP.test_minimize_bounded_constraint.<locals>.fr   g      ?g?rU   g        r!   rF   rI   rV   r\   )r
   r)   r>   r	   r   )r   rn   rp   Zcnsx0Z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 )NrD   r!   rF   rE   皙r!   r6   皙?rY   rb   )rI   r,   rH   rV   r\   rJ   rK   rL   rw   ri   rj   ru   r   r   r6   rd   rP   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 )NrD   r!   rE   r   r   )rH   iprintfull_outputr"   )r   r&   r   r   r   rN   r   ZfxZitsZimodeZ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 )NrD   r!   rE   r   r   )rH   fprimery   rz   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 )NrD   r!   rE   r   r   )rH   eqconsry   rz   )r   r&   r3   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 )NrD   r!   rE   r   r   )r}   rH   r   ry   rz   )r   r&   r,   r3   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 )NrD   r!   rE   r   r   )r}   rH   f_eqconsfprime_eqconsry   rz   r   r&   r,   r3   r7   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 )
NrD   r!   rE   r   r   )r}   rH   ieqconsry   rz   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 )NrD   r!   rE   rt   rv   r   r   )r}   rH   rV   r   r   ry   rz   rw   r   r   ru   r6   r   r{   r   r   r   test_bound_equality_given20  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>@      z3TestSLSQP.test_scalar_constraints.<locals>.<lambda>g      @c                 S      | d d S Nr   r   r   r   r   r   r   r   A      r   )r   ry   r!   c                 S   r   r   r   r   r   r   r   r   E  r   c                 S   s   | d d gS r   r   r   r   r   r   r   F  s    )Z	f_ieqconsry   )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   L  r   z/TestSLSQP.test_integer_bounds.<locals>.<lambda>r   r   rV   ry   r   r   r   r   r   test_integer_boundsJ  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   S  s    z-TestSLSQP.test_array_bounds.<locals>.<lambda>rT   r   r   )r)   infr*   r   r   )r   rV   r   r   r   r   test_array_boundsN  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   rm   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_scalarW  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   rm   r   r   r   r   a  s    z;TestSLSQP.test_obj_returns_scalar_in_list.<locals>.<lambda>r   r   )ry   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 )	NrD   r!   rE   rF   )rH   rI   callbackrJ   rK   rL   nit)r   r   r&   r   r   r   r   r   )r   r   rN   r   r   r   test_callbackc  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 ro   r   rm   r   r   r   f1w     z5TestSLSQP.test_inconsistent_linearization.<locals>.f1c                 S   s   | d d d S Nr   r"   r   r   rm   r   r   r   f2y     z5TestSLSQP.test_inconsistent_linearization.<locals>.f2c                 S      | d d | d d  S r   r   rm   r   r   r   r   |      z;TestSLSQP.test_inconsistent_linearization.<locals>.<lambda>rY   r[   r&   rh   r   Nr   rF   r\   rV   rI   g:0yE>rj   g:0yE)r   r   r   r   rK   )r   r   r   r   solr   r   r   test_inconsistent_linearizationl  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   rm   r   r   r   r     r   z0TestSLSQP.test_regression_5743.<locals>.<lambda>rY   c                 S      | d | d  d S r   r   rm   r   r   r   r         r   rh   c                 S   r   )Nr   r"   r   rm   r   r   r   r     r   r   rF   r   )r   r   rK   )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"   rU   r   rm   r   r   r   func  s   8z$TestSLSQP.test_gh_6676.<locals>.funcr   r   r   rF   rI   )r   )r   r   r,   rA   )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   rD   r!   rF   )rV   rI   )r)   r   r   r   r   r&   )r   Zbounds_listrV   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 ro   r   rm   r   r   r   rp     r   z)TestSLSQP.test_bounds_clipping.<locals>.f
   slsqpr   rI   rV   r   绽|=rj   )r"   Nr"         )r6   r   r   r   rK   r   r   )r   rp   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   rm   r   r   r   rp     s   z,TestSLSQP.test_infeasible_initial.<locals>.frh   c                 S      d|  S r   r   rm   r   r   r   r     r   z3TestSLSQP.test_infeasible_initial.<locals>.<lambda>r   c                 S   s   | d S r   r   rm   r   r   r   r     r   c                 S   r   r   r   rm   r   r   r   r     r   c                 S   s   | d S Nr   r   rm   r   r   r   r     r   r   r   )rI   r\   r   r   rj   r   r"   r   r   )r   rp   Zcons_uZcons_lZcons_ulr   r   r   r   test_infeasible_initial  s0   





z!TestSLSQP.test_infeasible_initialc                 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 )Nr6   r   r(   r   r   rm   r   r   r   cost  r/   z6TestSLSQP.test_inconsistent_inequalities.<locals>.costc                 S   s   | d | d  d S )Nr   r   r   rm   r   r   r   	ineqcons1  r   z;TestSLSQP.test_inconsistent_inequalities.<locals>.ineqcons1c                 S   s   | d | d  S r   r   rm   r   r   r   	ineqcons2  r   z;TestSLSQP.test_inconsistent_inequalities.<locals>.ineqcons2)r      )r   r   rh   r   rF   rq   )dictr   r   rK   )r   r   r   r   rr   rV   consrN   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   rm   r   r   r   rp     r/   z)TestSLSQP.test_new_bounds_type.<locals>.fr   r   r   r   )r	   r)   r   r   r   rK   r   r   )r   rp   rV   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(   rF   r   r   r"   )r   	Exceptionr   F_innerr   rK   r   r   )r   r   Z	inner_resr   r   r   F_outer  s   

$zATestSLSQP.test_nested_minimization.<locals>.NestedProblem.F_outerc                 S   s    |d d d |d d d  S ro   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   rF   r   r   )r   r   r   rK   r   r   )r   Z	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)   sqrtrm   r   r   r   r&   +  r    z"TestSLSQP.test_gh1758.<locals>.func                 S   s   | d d| d  d  S )r0   r   r"   r   r   r   rm   r   r   r   r3   .  r4   z&TestSLSQP.test_gh1758.<locals>.f_eqconc                 S   s   | d | d  d d  S )r0   r   r   r   r   rm   r   r   r   f_eqcon22  s   z'TestSLSQP.test_gh1758.<locals>.f_eqcon2rY   r      g      ?rF   )r   r   )r   r   )rI   r\   rV   g8r](k?gQUU?gc@?)r   r)   testingr   r&   r   rK   )r   r&   r3   r   c1c2rN   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   rh   c                 S   s   | d  | d  d S )Nr   r   r   r   rm   r   r   r   r   B  s    z'TestSLSQP.test_gh9640.<locals>.<lambda>r   c                 S   r   )Nr   r"   r   rm   r   r   r   r   C  r   )r'   r"   r   r   c                 S   s   dS r   r   rm   r   r   r   targetF  s   z%TestSLSQP.test_gh9640.<locals>.target)g51gУX{gP(rF   Fi'  )r   maxiter)rI   rV   r\   rJ   )r)   randomseedr   rK   )r   r   Zbndsr   rr   rN   r   r   r   test_gh9640@  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)   ZlinalgZnormrm   rV   r   r   rp   Z  s   z7TestSLSQP.test_parameters_stay_within_bounds.<locals>.fzx were outside bounds)matchrF   r   )r)   r   r   r	   r*   lenr   ZubpytestZwarnsRuntimeWarningr   rK   )r   Zn_inputsrr   rp   rN   r   r   r   "test_parameters_stay_within_boundsO  s   

"z,TestSLSQP.test_parameters_stay_within_boundsN)r!   )5r   r   r   r   r   r&   r,   r.   r3   r7   r9   r;   r<   r=   r?   rB   rO   rQ   rW   rX   r]   r_   re   rg   rk   rl   rs   rx   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   r   r      sf    










		

		#r   )r   Znumpy.testingr   r   r   r   r   r   r   numpyr)   Zscipy.optimizer   r   r	   r
   r   r   r   r   r   r   <module>   s    