L i,dZddlZddlZddlZddlZddlmZm Z m Z m Z m Z ddl mZddlmZgZdZGdd Z d d Zy) zTrust-region optimization.N)_check_unknown_options_status_messageOptimizeResult_prepare_scalar_function_call_callback_maybe_halt)HessianUpdateStrategy) FD_METHODSc0dgdfSfd}|fS)Nrc\dxxdz cc<tj|g|zS)Nrr)npcopy)x wrapper_argsargsfunctionncallss a/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/optimize/_trustregion.pyfunction_wrapperz(_wrap_function..function_wrappers-q Q  ;lT&9;;)rrrrs`` @r_wrap_functionrs/SFt|< # ##rcpeZdZdZd dZdZedZedZedZ dZ ed Z d Z d Z y) BaseQuadraticSubproblemaQ Base/abstract class defining the quadratic model for trust-region minimization. Child classes must implement the ``solve`` method. Values of the objective function, Jacobian and Hessian (if provided) at the current iterate ``x`` are evaluated on demand and then stored as attributes ``fun``, ``jac``, ``hess``. Nc||_d|_d|_d|_d|_d|_d|_||_||_||_ ||_ yN) _x_f_g_h_g_mag _cauchy_point _newton_point_fun_jac_hess_hessp)selfrfunjachesshessps r__init__z BaseQuadraticSubproblem.__init__(sQ !!    rc|jtj|j|zdtj||j |zzS)Ng?)r)r dotr*r,r(ps r__call__z BaseQuadraticSubproblem.__call__5s=xx"&&1--bffQ 1 6N0NNNrcr|j |j|j|_|jS)z1Value of objective function at current iteration.)rr$rr(s rr)zBaseQuadraticSubproblem.fun8* 77?ii(DGwwrcr|j |j|j|_|jS)z=Value of Jacobian of objective function at current iteration.)rr%rr4s rr*zBaseQuadraticSubproblem.jac?r5rcr|j |j|j|_|jS)zdt5d$j@|dzdt/||dk(||j|jB$j<$j>$j@|dz||"| }#||j|#d <| r|#d!<|#S#tj$j&$rd}YQwxYw)"a/ Minimization of scalar function of one or more variables using a trust-region algorithm. Options for the trust-region algorithm are: initial_trust_radius : float Initial trust radius. max_trust_radius : float Never propose steps that are longer than this value. eta : float Trust region related acceptance stringency for proposed steps. gtol : float Gradient norm must be less than `gtol` before successful termination. maxiter : int Maximum number of iterations to perform. disp : bool If True, print convergence message. inexact : bool Accuracy to solve subproblems. If True requires less nonlinear iterations, but more vector products. Only effective for method trust-krylov. 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)``. Only for 'trust-krylov', 'trust-ncg'. .. versionadded:: 1.16.0 This function is called by the `minimize` function. It is not supposed to be called directly. Nz7Jacobian is currently required for trust-region methodsz_Either the Hessian or the Hessian-vector product is currently required for trust-region methodszBA subproblem solving strategy is required for trust-region methodsrg?zinvalid acceptance stringencyz%the max trust radius must be positivez)the initial trust radius must be positivez?the initial trust radius must be less than the max trust radius)r*r+rworkerscDj|j|Sr)r+r/)rr1rsfs rr,z%_minimize_trust_region..hessps771:>>!$ $rr?g?r)rr)successmaxiterz:A bad approximation caused failure to predict improvement.z3A linalg error occurred, such as a non-psd Hessian.) stacklevelz! Current function value: fz Iterations: rGz Function evaluations: z Gradient evaluations: z Hessian evaluations: ) rr_statusr)r*nfevnjevnhevnitmessager+allvecs)"r ValueError Exceptionr asarrayflattenrr)gradcallabler+r isinstancer rlenr=rSr; LinAlgErrorminappendrrrrprintwarningswarnRuntimeWarningrdngevrfr*)%r)x0rr*r+r, subprobleminitial_trust_radiusmax_trust_radiusetagtolr`disp return_allcallbackinexactrZunknown_optionsnhesspwarnflagrHrrimkr1 hits_boundarypredicted_value x_proposed m_proposedactual_reductionpredicted_reductionrhointermediate_resultstatus_messagesresultr\s% @r_minimize_trust_regionrvsON?+ {#$ $ | JK K01 1 OtO788 7881?@@q DEE//,- - B   !B " RSt$ B &&C ''C~ww % *  41F G  %JK K#5$/MFEb'#+H(L A#1c3e,A A ))t    ww|4 A} A$U  CdEB 55:>>1eeo5 ! #H !44 : D L 4ZMq~/?@L 9AA  NN2771: & Q,qaee< $X/B C  99t H  <H o ))t v I & I & H A O  q= /(+ , MM/(3^PQ R 1!%%;< %aU+, /{;< /{;< .rww/B1.EFG a(a- !155rwwRWW!#6!9!4!$3H$=?F  v#y M]yy$$ H  s,N!!OO)rNNNNg?g@@g333333?g-C6?NFFNTN)rWrBrvnumpyr scipy.linalgr: _optimizerrrrr'scipy.optimize._hessian_update_strategyr (scipy.optimize._differentiable_functionsr __all__rrrrrrrsW 33J?  $U5U5pIMADCG@E@D Hr