o
    EbP                     @   st  d Z ddg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r8g 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Z/e0d-1d.d/ e0d0 e#e(ed1dge*dd2d3dd \Z2Z3e0d4 e$e(ed1dgfd5e*id6d2iZ4e0d71d.d/ e0d0 e#e(ed1dge+e,e-e.dd2d8dd \Z2Z3e0d4 e$e(ed1dgfd9e/id6d2iZ4dS d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$   :/usr/lib/python3/dist-packages/scipy/optimize/_slsqp_py.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 }|r9|d|| d	f7 }|rE|d
||	 d	f7 }t| | f|||d|}|rf|d |d |d |d |d f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       | ]	}d | dV  qdS )eqtypefunr   Nr$   .0cr   r$   r%   	<genexpr>       zfmin_slsqp.<locals>.<genexpr>c                 3   r-   )ineqr/   Nr$   r2   r5   r$   r%   r6      r7   r.   r0   r1   r#   r   r8   )r#   boundsconstraintsr    r1   nitstatusmessage)tuple_minimize_slsqp)r!   x0Zeqconsf_eqconsZieqcons	f_ieqconsr:   Zfprimefprime_eqconsfprime_ieqconsr   iteraccr)   r*   full_outputr"   r,   Zoptsconsresr$   r5   r%   r   D   s8   p

"Fc           C         s  t | |d }|}|
 |	sd}t| |du s t|dkr(tj tjfnt|td d t|t	r?|f}ddd}t
|D ]\}}z|d  }W n3 tyg } ztd| |d}~w tyw } ztd|d}~w ty } ztd	|d}~ww |dvrtd
|d  d|vrtd| |d}|du r fdd}||d }||  |d ||dddf7  < qHd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 sft|dkrtj|td%}#tj|td%}$|#tj |$tj n|td&d |D t}%|%jd |krtd'tjd(d) |%dddf |%dddf k}&W d   n	1 sw   Y  |& rt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rt%d/d0  |)}?t&|*d1}@t'|}At(||||||}B	 t)g |||#|$|?|A|@|B||,|+|!|"|.|/|0|1|2|3|4|5|6|7|8|9|:|;|<||=|>R   |+dkr|)}?t'|}A|+d2krt&|*d1}@t(||||||}B|,|-kr2|dur|t* |d$kr2t%d3|,|(j+|?t,-|@f  t.|+dkr:nt$|,}-q|dkrkt%|t$|+ d4 t/|+ d5  t%d6|? t%d7|, t%d8|(j+ t%d9|(j0 t1|?|@dd2 t$|,|(j+|(j0t$|+|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.   r8   r0   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'.r1   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 )N)r   z3-pointcs)r   r   Zrel_stepr:   r   )r   r   r   r:   )r   r   )r    r   )r"   finite_diff_rel_stepr1   r#   
new_boundsr$   r%   cjac%  s   

z3_minimize_slsqp.<locals>.cjac_factory.<locals>.cjacr$   )r1   rN   )r"   rL   r#   rM   )r1   r%   cjac_factory$  s   z%_minimize_slsqp.<locals>.cjac_factoryr   )r1   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                    (   g | ]}t |d   g|d R  qS r1   r   r   r2   r    r$   r%   
<listcomp>G       z#_minimize_slsqp.<locals>.<listcomp>r.   c                    rY   rZ   r[   r2   r\   r$   r%   r]   I  r^   r8   rR   rQ   )Zdtypec                 S   s    g | ]\}}t |t |fqS r$   )r   )r3   lur$   r$   r%   r]   b  s    zDSLSQP Error: the length of bounds is not compatible with that of x0.ignore)Zinvalidz"SLSQP Error: lb > ub in bounds %s.z, c                 s   s    | ]}t |V  qd S )N)str)r3   br$   r$   r%   r6   m  s    z"_minimize_slsqp.<locals>.<genexpr>)r#   r   r"   rL   r:   z%5s %5s %16s %16s)ZNITZFCZOBJFUNZGNORMg        rP   z%5i %5i % 16.6E % 16.6Ez    (Exit mode )z#            Current function value:z            Iterations:z!            Function evaluations:z!            Gradient evaluations:)	r    r1   r#   r<   nfevZnjevr=   r>   Zsuccess)2r   r	   Z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   r1   Zgradintprintr   _eval_constraint_eval_con_normalsr   copyre   r   Znormabsrb   Zngevr   )Cr!   rA   r   r#   r:   r;   r'   r(   r)   r*   r+   r,   rL   Zunknown_optionsrF   rG   rI   ZicconZ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_prevZalphaZf0ZgsZh1Zh2Zh3Zh4tZt0ZtolZiexactZinconsZiresetZitermxlineZn2Zn3Zfxgr4   ar$   )r"   rL   r#   rM   r    r%   r@      s  






>"
"























<








  

r@   c                    sh   |d rt  fdd|d D }ntd}|d r(t  fdd|d D }ntd}t ||f}|S )Nr.   c                    rY   rZ   r[   r3   r   r\   r$   r%   r]     r^   z$_eval_constraint.<locals>.<listcomp>r   r8   c                    rY   rZ   r[   r   r\   r$   r%   r]     r^   )r
   r   )r    rI   Zc_eqZc_ieqr4   r$   r\   r%   r}     s   

r}   c           
         s   |d rt  fdd|d D }nt||f}|d r*t  fdd|d D }nt||f}|dkr;t||f}	nt ||f}	t|	t|dgfd}	|	S )Nr.   c                    $   g | ]}|d   g|d R  qS r#   r   r$   r   r\   r$   r%   r]         z%_eval_con_normals.<locals>.<listcomp>r8   c                    r   r   r$   r   r\   r$   r%   r]     r   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%   r1     s   0r1   rQ   g?g?c                 C   s   t | d d | d  | gS )z Equality constraint r   rQ   r   r   r    rc   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    r4   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   r9   r8   )r   z Bounds constraints H   -z * fmin_slsqprP   T)r:   r*   rH   z * _minimize_slsqpr:   r*   z% Equality and inequality constraints )rB   rD   rC   rE   r*   rH   r;   )5__doc____all__Z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__rt   r+   Z_epsilonr   r   r@   r}   r~   __name__r1   Tr   r   r   r   r   rI   r|   centerr    frJ   r$   r$   r$   r%   <module>   s|    <"
 
 {





