L i^]dZddgZddlZddlmZddlmZddl m Z m Z m Z m Z mZdd lmZdd lmZmZdd lmZdd lmZdd lmZdZej8ej:ej<j>Z dZ!dddddddddddddde dfdZ"dddddddde dddf dZ#dedede$de%de%f dZ&dedede$de%de%f dZ'y)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_slsqpN)slsqp)norm)OptimizeResult_check_unknown_options_prepare_scalar_function_clip_x_for_func _check_clip_x)approx_derivative)old_bound_to_new_arr_to_scalar)array_namespace)array_api_extra)NDArrayzrestructuredtext encLt||d||}tj|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)methodabs_stepargs)r np atleast_2d)xfuncepsilonrjacs ^/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/optimize/_slsqp_py.pyrr#s*6 D!I!% 'C == dgư>c ||} | | | | dk7||d}d}|t fd|Dz }|t fd|Dz }|r |d|| dfz }|r |d|| dfz }t|| f|||d |}|r|d |d |d |d |dfS|d S)aC 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 `. r)maxiterftoliprintdispepscallbackr c3*K|] }d|d yw)eqtypefunrNr .0crs r zfmin_slsqp..sIQ448Ic3*K|] }d|d yw)ineqr+Nr r.s rr1zfmin_slsqp..sLq6!T:Lr2r*)r,r-rrr4)rbounds constraintsrr-nitstatusmessage)tuple_minimize_slsqp)rx0eqconsf_eqconsieqcons f_ieqconsr5fprime fprime_eqconsfprime_ieqconsriteraccr%r& full_outputrr(optsconsress ` rrrEs` aK  "D D EI&I IIDELGL LLD $x  # # &> # # $D 4fV&* 4.2 4C3xUSZXINN3xrFc t 45t||}| 4| sd}t|}tj|j |d|}|j }|j |jdr |j}|j|j||d}|t|dk(r"tj tjf5n t|5tj|5d5d}t|t r|f}ddd}t#|D]\}} |d j%}|dvrt'd |d d d|vrt'd |d|j/d}|4 5fd}||d}||xx|d||j/dddfz cc<ddddddddddd d! }t1t3t|d"Dcgc]$}tj4|d|g|d&c}}t1t3t|d#Dcgc]$}tj4|d|g|d&c}}||z}t|}|t|dk(rvtj6|t8$} tj6|t8$}!| j;tj<|!j;tj<n=tj>|D"#cgc]\}"}#tA|"tA|#fc}#}"t8}$|$jBd|k7r tEd%tjFd&'5|$dddf|$dddfkD}%ddd%jIr%t'd(d)jKd*|%Dd+|$dddfjM|$dddfjM}!} tjN|$}&tj<| |&dddf<tj<|!|&dddf<tQ|||| 5| ,}'tS|'jT5}(tS|'jV5})id-|d.d/d0d/d1d/d2d/d3d/d4d/d5d/d6d/d7d/d8d9|zd:dd;ddtY|d?d||d|d@}*|dAk\rt[dBdCdDdEdCdDdFdGdDdHdGtj\t_|dA|zzdAzdgtj`$}+||dzzdAzdI|z|zz|dJ|zzdKz|zz dL|zzdM|z|zzdN|zz||zzdOz},|dk(r|,dA|z|dzzz },tj\t_|,dtj $}-|(|}.|)|}/tj\t_d|dA|zzdAzgtj $}0tj\t_d||gtj dPQ}1tj\t_d|gtj $}2tc|1||||te|2||||d}3 tg|*|.|/|1|2||0| |!|-|+ |*dRdk(r |'jU|}.te|2|||||*dRdk(r |'jW|}/tc|1|||||*d=|3kDrR| | tjL||dAk\r0t[|*d=dSdD|'jhdSdD|.dTdDtk|/dTtm|*dRdk7rn|*d=}3|dk\rbt[||*dRdU|*dRdVzt[dW|.t[dX|*d=t[dY|'jht[dZ|'jntq||.|/|*d=|'jh|'jn|*dR||*dR|*dRdk(|0d|[ S#t($r}t)d |d |d}~wt*$r}t+d|d}~wt,$r}t+d|d}~wwxYwcc}wcc}wcc}#}"w#1swYxYw)\a Minimize a scalar function of one or more variables using Sequential Least Squares Programming (SLSQP). Parameters ---------- ftol : float Precision target for the value of f in the stopping criterion. This value controls the final accuracy for checking various optimality conditions; gradient of the lagrangian and absolute sum of the constraint violations should be lower than ``ftol``. Similarly, computed step size and the objective function changes are checked against this value. Default is 1e-6. 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, optional Maximum number of iterations. Default value is 100. 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(x) * max(1, abs(x))``, 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. workers : int, map-like callable, optional A map-like callable, such as `multiprocessing.Pool.map` for evaluating any numerical differentiation in parallel. This evaluation is carried out as ``workers(fun, iterable)``. .. versionadded:: 1.16.0 Returns ------- res : OptimizeResult The optimization result represented as an `OptimizeResult` object. In this dict-like object the following fields are of particular importance: ``x`` the solution array, ``success`` a Boolean flag indicating if the optimizer exited successfully, ``message`` which describes the reason for termination, and ``multipliers`` which contains the Karush-Kuhn-Tucker (KKT) multipliers for the QP approximation used in solving the original nonlinear problem. See ``Notes`` below. See also `OptimizeResult` for a description of other attributes. Notes ----- The KKT multipliers are returned in the ``OptimizeResult.multipliers`` attribute as a NumPy array. Denoting the dimension of the equality constraints with ``meq``, and of inequality constraints with ``mineq``, then the returned array slice ``m[:meq]`` contains the multipliers for the equality constraints, and the remaining ``m[meq:meq + mineq]`` contains the multipliers for the inequality constraints. The multipliers corresponding to bound inequalities are not returned. See [1]_ pp. 321 or [2]_ for an explanation of how to interpret these multipliers. The internal QP problem is solved using the methods given in [3]_ Chapter 25. Note that if new-style `NonlinearConstraint` or `LinearConstraint` were used, then ``minimize`` converts them first to old-style constraint dicts. It is possible for a single new-style constraint to simultaneously contain both inequality and equality constraints. This means that if there is mixing within a single constraint, then the returned list of multipliers will have a different length than the original new-style constraints. References ---------- .. [1] Nocedal, J., and S J Wright, 2006, "Numerical Optimization", Springer, New York. .. [2] Kraft, D., "A software package for sequential quadratic programming", 1988, Tech. Rep. DFVLR-FB 88-28, DLR German Aerospace Center, Germany. .. [3] Lawson, C. L., and R. J. Hanson, 1995, "Solving Least Squares Problems", SIAM, Philadelphia, PA. rr)ndimxpz real floatingNr )r*r4r,zUnknown constraint type 'z'.z Constraint z has no type defined.z/Constraints must be defined using a dictionary.z#Constraint's type must be a string.r-z has no function defined.rcfd}|S)Ncht|}dvrt||St|d|S)N)rz3-pointcs)rrrel_stepr5r)rrrr5)r r )rrrfinite_diff_rel_stepr-r new_boundss rcjacz3_minimize_slsqp..cjac_factory..cjac]sT%a4A::0a$:N8B DD 1a :A8B DDrr )r-rTrrRrrSs` r cjac_factoryz%_minimize_slsqp..cjac_factory\s D D rr)r-rrz$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 linesearchzIteration limit reached) rMrr r*r4)dtypezDSLSQP Error: the length of bounds is not compatible with that of x0.ignore)invalidzSLSQP Error: lb > ub in bounds z, c32K|]}t|yw)N)str)r/bs rr1z"_minimize_slsqp..s)AQ#a&)As.)rrrrRr5workersrEalphagf0gsh1h2h3h4tt0tolg$@exact inconsistentresetrDitermaxline)mmeqmodenrVNITz>5 FCOBJFUNz>16GNORMrWrYr[r]r\#F)r^orderrw5dz16.6Ez (Exit mode )z# Current function value:z Iterations:z! Function evaluations:z! Gradient evaluations:) rr-rr7nfevnjevr8r9success multipliers)9r rxpx atleast_ndasarrayfloat64isdtyper^reshapeastypelenrinfrclip isinstancedict enumeratelower ValueErrorKeyError TypeErrorAttributeErrorgetsummap atleast_1demptyfloatfillnanarrayrshape IndexErrorerrstateanyjoincopyisfiniter r r-gradintprintzerosmaxint32_eval_con_normals_eval_constraintrrlanormabsngevr)6rr<rrr5r6r#r$r%r&r'r(rRreunknown_optionsrErLr^rrHicconctypeerTrU exit_modesr0rvmieqrurxxlxuloupbndsbnderrinfbndsf wrapped_fun wrapped_grad state_dictindices buffer_sizebufferfxgmultCd iter_prevrrSs6 ` ` @@rr;r;s ^?+ CG   B  2Q2 6B JJE zz"((O, 299R',A~V)vvgrvv& %f-  :a=*Q-0A+t$"o b !D[)+9C NK%%'EN* ##$ %C s3V &xqx'>AfI'>?&' (D d A AA~V) XXau % XXau %  xx&,."2r),nR.@A./46 ::a=A ;< <[[ * -!Q$Z$q!t*,F - ::<> $ )A&)A AB!EF Fad"DAJOO$5B++d##666!Q$<666!Q$< "$ss7K)3W FB #266:6K#BGGZ8L. s c c  c  c  c c S c tCx     3wRRS-T T  3R8 'F);< 1277; 1277;   6 2rww&!:j6H+IF#q(tBQx C K[,ABC J 2*+01 2 JAB I Jd#&.  - -sN$_ )`)`"9 `' `- `_%% `1 _== ` ``-`7rrrHrurvc|dk(ry|dkDr[d}|dD]Q}tj|d|g|dj}||||t|z|t|z }S||kDr[|}|dD]Q}tj|d|g|dj}||||t|z|t|z }Sy)Nrr*r-rr4)rrravelr)rrrHrurvrowrtemps rrr0sAv  Qw: C==UA!<F !<=CCED%)Ac#D / " 3t9 C   3w< C==UA!<F !<=CCED%)Ac#D / " 3t9 C   rrc|dk(ry|dkDr[d}|dD]Q}tj|d|g|d}|||||jdzddf<||jdz }S||kDr[|}|dD]Q}tj|d|g|d}|||||jdzddf<||jdz }Sy)Nrr*rrr4)rrr)rrrHrurvrrrs rrrIsAv Qw: !C==UA!<F !<=D,0Ac# 1 %%q( ) 4::a= C !  3w< !C==UA!<F !<=D,0Ac# 1 %%q( ) 4::a= C !  r)(__doc____all__numpyr _slsqplibr scipy.linalgrr _optimizerr r r r _numdiffr _constraintsrrscipy._lib._array_apir scipy._librr numpy.typingr __docformat__sqrtfinforr'_epsilonrrr;rrrrr rrrs  l +'''(:1- % 2778288BJJ'++ ,D!#T2T"#6d8 Od$&4 "fQU 4d Vr   G 4 C c 2  W D S s r