a
    ˜¥»aEQ  ã                   @   sx  d Z ddgZddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZ ddlmZmZmZmZmZ dd	lmZ dd
lmZmZ dZeee ƒj!ƒZ"dd„ Z#dddddddddddddde"dfdd„Z$dddddddde"ddfdd„Z%dd„ Z&dd„ Z'e(dk�rtg d¢fdd„Z)ee gd egd gƒj*Z+ddge+dd…df< d:dd „Z,d;d!d"„Z-d<d$d%„Z.d=d&d'„Z/d(e,e-d)d*œd+e.e/d,d*œfZ0e1d- 2d.d/¡ƒ e1d0ƒ e$e)ed1dgƒe+dd2d3�dd… \Z3Z4e1d4ƒ e%e)ed1dgƒfd5e+id6d2i¤ŽZ5e1d7 2d.d/¡ƒ e1d0ƒ e$e)ed1dgƒe,e-e.e/dd2d8�dd… \Z3Z4e1d4ƒ e%e)ed1dgƒfd9e0id6d2i¤ŽZ5dS )>a  
This module implements the Sequential Least Squares Programming optimization
algorithm (SLSQP), originally developed by Dieter Kraft.
See http://www.netlib.org/toms/733

Functions
---------
.. autosummary::
   :toctree: generated/

    approx_jacobian
    fmin_slsqp

Úapprox_jacobianÚ
fmin_slsqpé    N)Úslsqp)ÚzerosÚarrayÚlinalgÚappendÚasfarrayÚconcatenateÚfinfoÚsqrtÚvstackÚexpÚinfÚisfiniteÚ
atleast_1dé   )ÚOptimizeResultÚ_check_unknown_optionsÚ_prepare_scalar_functionÚ_clip_x_for_funcÚ_check_clip_x)Úapprox_derivative)Úold_bound_to_newÚ_arr_to_scalarzrestructuredtext enc                 G   s   t || d||d�}t |¡S )a“  
    Approximate the Jacobian matrix of a callable function.

    Parameters
    ----------
    x : array_like
        The state vector at which to compute the Jacobian matrix.
    func : callable f(x,*args)
        The vector-valued function.
    epsilon : float
        The perturbation used to determine the partial derivatives.
    args : sequence
        Additional arguments passed to func.

    Returns
    -------
    An array of dimensions ``(lenf, lenx)`` where ``lenf`` is the length
    of the outputs of `func`, and ``lenx`` is the number of elements in
    `x`.

    Notes
    -----
    The approximation is done using forward differences.

    ú2-point)ÚmethodÚabs_stepÚargs)r   ÚnpZ
atleast_2d)ÚxÚfuncÚepsilonr   Újac© r$   úN/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/scipy/optimize/slsqp.pyr   #   s    
ÿr$   éd   g�íµ ÷Æ°>c                    sØ   |dur|}||||dk||dœ}d}|t ‡ fdd„|D ƒƒ7 }|t ‡ fdd„|D ƒƒ7 }|rr|d||ˆ d	œf7 }|rŠ|d
||	ˆ d	œf7 }t| |ˆ f|||dœ|¤Ž}|rÌ|d |d |d |d |d fS |d S dS )a/  
    Minimize a function using Sequential Least Squares Programming

    Python interface function for the SLSQP Optimization subroutine
    originally implemented by Dieter Kraft.

    Parameters
    ----------
    func : callable f(x,*args)
        Objective function.  Must return a scalar.
    x0 : 1-D ndarray of float
        Initial guess for the independent variable(s).
    eqcons : list, optional
        A list of functions of length n such that
        eqcons[j](x,*args) == 0.0 in a successfully optimized
        problem.
    f_eqcons : callable f(x,*args), optional
        Returns a 1-D array in which each element must equal 0.0 in a
        successfully optimized problem. If f_eqcons is specified,
        eqcons is ignored.
    ieqcons : list, optional
        A list of functions of length n such that
        ieqcons[j](x,*args) >= 0.0 in a successfully optimized
        problem.
    f_ieqcons : callable f(x,*args), optional
        Returns a 1-D ndarray in which each element must be greater or
        equal to 0.0 in a successfully optimized problem. If
        f_ieqcons is specified, ieqcons is ignored.
    bounds : list, optional
        A list of tuples specifying the lower and upper bound
        for each independent variable [(xl0, xu0),(xl1, xu1),...]
        Infinite values will be interpreted as large floating values.
    fprime : callable `f(x,*args)`, optional
        A function that evaluates the partial derivatives of func.
    fprime_eqcons : callable `f(x,*args)`, optional
        A function of the form `f(x, *args)` that returns the m by n
        array of equality constraint normals. If not provided,
        the normals will be approximated. The array returned by
        fprime_eqcons should be sized as ( len(eqcons), len(x0) ).
    fprime_ieqcons : callable `f(x,*args)`, optional
        A function of the form `f(x, *args)` that returns the m by n
        array of inequality constraint normals. If not provided,
        the normals will be approximated. The array returned by
        fprime_ieqcons should be sized as ( len(ieqcons), len(x0) ).
    args : sequence, optional
        Additional arguments passed to func and fprime.
    iter : int, optional
        The maximum number of iterations.
    acc : float, optional
        Requested accuracy.
    iprint : int, optional
        The verbosity of fmin_slsqp :

        * iprint <= 0 : Silent operation
        * iprint == 1 : Print summary upon completion (default)
        * iprint >= 2 : Print status of each iterate and summary
    disp : int, optional
        Overrides the iprint interface (preferred).
    full_output : bool, optional
        If False, return only the minimizer of func (default).
        Otherwise, output final objective function and summary
        information.
    epsilon : float, optional
        The step size for finite-difference derivative estimates.
    callback : callable, optional
        Called after each iteration, as ``callback(x)``, where ``x`` is the
        current parameter vector.

    Returns
    -------
    out : ndarray of float
        The final minimizer of func.
    fx : ndarray of float, if full_output is true
        The final value of the objective function.
    its : int, if full_output is true
        The number of iterations.
    imode : int, if full_output is true
        The exit mode from the optimizer (see below).
    smode : string, if full_output is true
        Message describing the exit mode from the optimizer.

    See also
    --------
    minimize: Interface to minimization algorithms for multivariate
        functions. See the 'SLSQP' `method` in particular.

    Notes
    -----
    Exit modes are defined as follows ::

        -1 : Gradient evaluation required (g & a)
         0 : Optimization terminated successfully
         1 : Function evaluation required (f & c)
         2 : More equality constraints than independent variables
         3 : More than 3*n iterations in LSQ subproblem
         4 : Inequality constraints incompatible
         5 : Singular matrix E in LSQ subproblem
         6 : Singular matrix C in LSQ subproblem
         7 : Rank-deficient equality constraint subproblem HFTI
         8 : Positive directional derivative for linesearch
         9 : Iteration limit reached

    Examples
    --------
    Examples are given :ref:`in the tutorial <tutorial-sqlsp>`.

    Nr   )ÚmaxiterÚftolÚiprintÚdispÚepsÚcallbackr$   c                 3   s   | ]}d |ˆ dœV  qdS )Úeq©ÚtypeÚfunr   Nr$   ©Ú.0Úc©r   r$   r%   Ú	<genexpr>Ã   ó    zfmin_slsqp.<locals>.<genexpr>c                 3   s   | ]}d |ˆ dœV  qdS )Úineqr.   Nr$   r1   r4   r$   r%   r5   Ä   r6   r-   ©r/   r0   r#   r   r7   )r#   ÚboundsÚconstraintsr    r0   ÚnitÚstatusÚmessage)ÚtupleÚ_minimize_slsqp)r!   Úx0ZeqconsÚf_eqconsZieqconsÚ	f_ieqconsr9   ZfprimeÚfprime_eqconsÚfprime_ieqconsr   ÚiterÚaccr)   r*   Úfull_outputr"   r,   ÚoptsÚconsÚresr$   r4   r%   r   E   s8    pûÿ
ÿ
ÿÿ"Fc           C   !      sD  t |ƒ |d }|}|
‰ |	s d}t|ƒ ¡ ‰|du s@t|ƒdkrPtj tjf‰nt|ƒ‰t ˆˆd ˆd ¡‰t|t	ƒr~|f}dddœ}t
|ƒD �]0\}}z|d  ¡ }W nŽ tyÜ } ztd| ƒ|‚W Y d}~n€d}~0  t�y
 } ztdƒ|‚W Y d}~nRd}~0  t�y8 } ztd	ƒ|‚W Y d}~n$d}~0 0 |dv�rTtd
|d  ƒ‚d|v�rjtd| ƒ‚| d¡}|du �rœ‡ ‡‡‡fdd„}||d ƒ}||  |d || dd¡dœf7  < q�ddddddddddddœ}ttt‡fdd„|d  D ƒƒƒ}ttt‡fd!d„|d" D ƒƒƒ}|| }td|gƒ ¡ }tˆƒ}|d }|| | | }d#| | |d  || d |d$   d$|  || ||   d$|  | |d | d$  d$|  d#|  d#|  d }|} t|ƒ}!t| ƒ}"|du �süt|ƒdk�r2tj|td%�}#tj|td%�}$|# tj¡ |$ tj¡ nøtd&d„ |D ƒtƒ}%|%jd |k�r^td'ƒ‚tjd(d)��0 |%dd…df |%dd…df k}&W d  ƒ n1 �s¢0    Y  |& ¡ �rÒtd*d+ d,d-„ |&D ƒ¡ ƒ‚|%dd…df |%dd…df  }#}$t|%ƒ }'tj|#|'dd…df < tj|$|'dd…df < t | ˆˆ||
ˆˆd.�}(t!|(j"ˆƒ})t!|(j#ˆƒ}*tdt$ƒ}+t|tƒ}t|t$ƒ},d}-tdtƒ}.tdtƒ}/tdtƒ}0tdtƒ}1tdtƒ}2tdtƒ}3tdtƒ}4tdtƒ}5tdtƒ}6tdtƒ}7tdt$ƒ}8tdt$ƒ}9tdt$ƒ}:tdt$ƒ};tdt$ƒ}<tdt$ƒ}tdt$ƒ}=tdt$ƒ}>|d$k�rDt%d/d0 ƒ |)ˆƒ}?ztt &|?¡ƒ}?W n4 ttf�y’ } ztd1ƒ|‚W Y d}~n
d}~0 0 t'|*ˆƒd2ƒ}@t(ˆ|ƒ}At)ˆ||||||ƒ}Bt*||ˆ|#|$|?|A|@|B||,|+|!|"|.|/|0|1|2|3|4|5|6|7|8|9|:|;|<||=|>ƒ  |+dk�r"|)ˆƒ}?t(ˆ|ƒ}A|+d3k�rNt'|*ˆƒd2ƒ}@t)ˆ||||||ƒ}B|,|-k�r–|du�rp|t +ˆ¡ƒ |d$k�r–t%d4|,|(j,|?t- .|@¡f ƒ t/|+ƒdk�r¨�q´t$|,ƒ}-�qÀ|dk�r
t%|t$|+ƒ d5 t0|+ƒ d6 ƒ t%d7|?ƒ t%d8|,ƒ t%d9|(j,ƒ t%d:|(j1ƒ t2ˆ|?|@dd3… t$|,ƒ|(j,|(j1t$|+ƒ|t$|+ƒ |+dkd;�	S )<aª  
    Minimize a scalar function of one or more variables using Sequential
    Least Squares Programming (SLSQP).

    Options
    -------
    ftol : float
        Precision goal for the value of f in the stopping criterion.
    eps : float
        Step size used for numerical approximation of the Jacobian.
    disp : bool
        Set to True to print convergence messages. If False,
        `verbosity` is ignored and set to 0.
    maxiter : int
        Maximum number of iterations.
    finite_diff_rel_step : None or array_like, optional
        If `jac in ['2-point', '3-point', 'cs']` the relative step size to
        use for numerical approximation of `jac`. The absolute step
        size is computed as ``h = rel_step * sign(x0) * max(1, abs(x0))``,
        possibly adjusted to fit into the bounds. For ``method='3-point'``
        the sign of `h` is ignored. If None (default) then step is selected
        automatically.
    r   r   Nr$   )r-   r7   r/   z"Constraint %d has no type defined.z/Constraints must be defined using a dictionary.z#Constraint's type must be a string.zUnknown constraint type '%s'.r0   z&Constraint %d has no function defined.r#   c                    s   ‡‡‡ ‡‡fdd„}|S )Nc                    s>   t | ˆƒ} ˆdv r&tˆ| ˆ|ˆˆd�S tˆ| dˆ |ˆd�S d S )N)r   z3-pointÚcs)r   r   Zrel_stepr9   r   )r   r   r   r9   )r   r   )r    r   )r"   Úfinite_diff_rel_stepr0   r#   Ú
new_boundsr$   r%   Úcjac&  s    

þþz3_minimize_slsqp.<locals>.cjac_factory.<locals>.cjacr$   )r0   rN   )r"   rL   r#   rM   )r0   r%   Úcjac_factory%  s    z%_minimize_slsqp.<locals>.cjac_factoryr   )r0   r#   r   z$Gradient evaluation required (g & a)z$Optimization terminated successfullyz$Function evaluation required (f & c)z4More equality constraints than independent variablesz*More than 3*n iterations in LSQ subproblemz#Inequality constraints incompatiblez#Singular matrix E in LSQ subproblemz#Singular matrix C in LSQ subproblemz2Rank-deficient equality constraint subproblem HFTIz.Positive directional derivative for linesearchzIteration limit reached)éÿÿÿÿr   r   é   é   é   é   é   é   é   é	   c                    s(   g | ] }t |d  ˆ g|d ¢R Ž ƒ‘qS ©r0   r   ©r   r1   ©r    r$   r%   Ú
<listcomp>H  s   ÿz#_minimize_slsqp.<locals>.<listcomp>r-   c                    s(   g | ] }t |d  ˆ g|d ¢R Ž ƒ‘qS rY   rZ   r1   r[   r$   r%   r\   J  s   ÿr7   rR   rQ   )Zdtypec                 S   s    g | ]\}}t |ƒt |ƒf‘qS r$   )r   )r2   ÚlÚur$   r$   r%   r\   c  s   ÿzDSLSQP Error: the length of bounds is not compatible with that of x0.Úignore)Úinvalidz"SLSQP Error: lb > ub in bounds %s.z, c                 s   s   | ]}t |ƒV  qd S )N)Ústr)r2   Úbr$   r$   r%   r5   n  r6   z"_minimize_slsqp.<locals>.<genexpr>)r#   r   r"   rL   r9   z%5s %5s %16s %16s)ZNITZFCZOBJFUNZGNORMz'Objective function must return a scalarg        rP   z%5i %5i % 16.6E % 16.6Ez    (Exit mode ú)z#            Current function value:z            Iterations:z!            Function evaluations:z!            Gradient evaluations:)	r    r0   r#   r;   ÚnfevZnjevr<   r=   Úsuccess)3r   r	   ÚflattenÚlenr   r   r   ZclipÚ
isinstanceÚdictÚ	enumerateÚlowerÚKeyErrorÚ	TypeErrorÚAttributeErrorÚ
ValueErrorÚgetÚsumÚmapr   Úmaxr   ÚemptyÚfloatÚfillÚnanÚshapeÚ
IndexErrorZerrstateÚanyÚjoinr   r   r   r0   ZgradÚintÚprintZasarrayr   Ú_eval_constraintÚ_eval_con_normalsr   Úcopyrd   r   ZnormÚabsra   Zngevr   )Cr!   r@   r   r#   r9   r:   r'   r(   r)   r*   r+   r,   rL   Zunknown_optionsrE   rF   rI   ZicÚconÚctypeÚerN   rO   Z
exit_modesÚmeqÚmieqÚmÚlaÚnZn1ZmineqZlen_wZlen_jwÚwZjwZxlZxuÚbndsZbnderrZinfbndZsfZwrapped_funZwrapped_gradÚmodeZmajiterZmajiter_prevÚalphaZf0ZgsÚh1Úh2Úh3Úh4ÚtÚt0ZtolZiexactZinconsZiresetZitermxÚlineZn2Zn3ZfxÚgr3   Úar$   )r"   rL   r#   rM   r    r%   r?   ×   sR   

"ÿ 




þöÿ
ÿ
>ÿÿÿÿÿÿÿÿÿ@
ÿ"
þ





















 

ý







ÿ
 

þr?   c                    sh   |d r$t ‡ fdd„|d D ƒƒ}ntdƒ}|d rPt ‡ fdd„|d D ƒƒ}ntdƒ}t ||fƒ}|S )Nr-   c                    s(   g | ] }t |d  ˆ g|d ¢R Ž ƒ‘qS rY   rZ   ©r2   r‚   r[   r$   r%   r\   Ø  s   ÿz$_eval_constraint.<locals>.<listcomp>r   r7   c                    s(   g | ] }t |d  ˆ g|d ¢R Ž ƒ‘qS rY   rZ   r—   r[   r$   r%   r\   Þ  s   ÿ)r
   r   )r    rI   Zc_eqZc_ieqr3   r$   r[   r%   r~   Õ  s    ÿ
ÿ
r~   c           
         sœ   |d r$t ‡ fdd„|d D ƒƒ}nt||fƒ}|d rTt ‡ fdd„|d D ƒƒ}nt||fƒ}|dkrvt||fƒ}	nt ||fƒ}	t|	t|dgƒfdƒ}	|	S )Nr-   c                    s$   g | ]}|d  ˆ g|d ¢R Ž ‘qS ©r#   r   r$   r—   r[   r$   r%   r\   ë  s   ÿz%_eval_con_normals.<locals>.<listcomp>r7   c                    s$   g | ]}|d  ˆ g|d ¢R Ž ‘qS r˜   r$   r—   r[   r$   r%   r\   ñ  s   ÿr   r   )r   r   r
   )
r    rI   rˆ   r‰   r‡   r…   r†   Za_eqZa_ieqr–   r$   r[   r%   r   è  s    ÿ
ÿ
r   Ú__main__)rS   rQ   rS   rQ   r   c                 C   sd   t | d ƒ|d | d d  |d | d d   |d | d  | d   |d | d   |d   S )z Objective function r   rQ   r   rR   rS   )r   )r    Úrr$   r$   r%   r0     s    0ÿÿþr0   rQ   gš™™™™™¹?gš™™™™™É?c                 C   s   t | d d | d  | gƒS )z Equality constraint r   rQ   r   ©r   ©r    rb   r$   r$   r%   Úfeqcon  s    r�   c                 C   s   t d| d  dggƒS )z! Jacobian of equality constraint rQ   r   r   r›   rœ   r$   r$   r%   Újeqcon  s    rž   é
   c                 C   s   t | d | d  | gƒS )z Inequality constraint r   r   r›   ©r    r3   r$   r$   r%   Úfieqcon  s    r¡   c                 C   s   t ddggƒS )z# Jacobian of inequality constraint r   r›   r    r$   r$   r%   Újieqcon  s    r¢   r-   )r   r8   r7   )rŸ   z Bounds constraints éH   ú-z * fmin_slsqprP   T)r9   r*   rG   z * _minimize_slsqpr9   r*   z% Equality and inequality constraints )rA   rC   rB   rD   r*   rG   r:   )r   )r   )rŸ   )rŸ   )6Ú__doc__Ú__all__ÚwarningsÚnumpyr   Zscipy.optimize._slsqpr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   Úoptimizer   r   r   r   r   Z_numdiffr   Z_constraintsr   r   Z__docformat__ru   r+   Z_epsilonr   r   r?   r~   r   Ú__name__r0   ÚTr‹   r�   rž   r¡   r¢   rI   r}   Úcenterr    ÚfrJ   r$   r$   r$   r%   Ú<module>   s|   <"ü
 ý
   




ÿÿÿÿýýÿ