o
    fRh1*                     @   s   d Z ddlZddlZddlZddlZddlmZm	Z	m
Z
mZmZ ddlmZ ddlmZ g Zdd ZG d	d
 d
Z					dddZdS )zTrust-region optimization.    N   )_check_unknown_options_status_messageOptimizeResult_prepare_scalar_function_call_callback_maybe_halt)HessianUpdateStrategy)
FD_METHODSc                    s.   dgd u rd fS  fdd}|fS )Nr   c                    s*   d  d7  < t | g|  R  S )Nr   r   )npcopy)xwrapper_argsargsfunctionncalls ]/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/scipy/optimize/_trustregion.pyfunction_wrapper   s   z(_wrap_function.<locals>.function_wrapperr   )r   r   r   r   r   r   _wrap_function   s
   r   c                   @   sj   e Zd ZdZdddZdd Zedd Zed	d
 Zedd Z	dd Z
edd Zdd Zdd ZdS )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                 C   sF   || _ d | _d | _d | _d | _d | _d | _|| _|| _|| _	|| _
d S N)_x_f_g_h_g_mag_cauchy_point_newton_point_fun_jac_hess_hessp)selfr   funjachesshesspr   r   r   __init__(   s   
z BaseQuadraticSubproblem.__init__c                 C   s*   | j t| j| dt|| |  S )Ng      ?)r$   r
   dotr%   r'   r#   pr   r   r   __call__5   s   *z BaseQuadraticSubproblem.__call__c                 C      | j du r| | j| _ | j S )z1Value of objective function at current iteration.N)r   r   r   r#   r   r   r   r$   8      
zBaseQuadraticSubproblem.func                 C   r-   )z=Value of Jacobian of objective function at current iteration.N)r   r    r   r.   r   r   r   r%   ?   r/   zBaseQuadraticSubproblem.jacc                 C   r-   )z<Value of Hessian of objective function at current iteration.N)r   r!   r   r.   r   r   r   r&   F   r/   zBaseQuadraticSubproblem.hessc                 C   s&   | j d ur|  | j|S t| j|S r   )r"   r   r
   r)   r&   r*   r   r   r   r'   M   s   
zBaseQuadraticSubproblem.hesspc                 C   s    | j du rtj| j| _ | j S )zAMagnitude of jacobian of objective function at current iteration.N)r   scipylinalgnormr%   r.   r   r   r   jac_magS   s   
zBaseQuadraticSubproblem.jac_magc                 C   s   t ||}dt || }t |||d  }t|| d| |  }|t|| }| d|  }	d| | }
t|	|
gS )z
        Solve the scalar quadratic equation ``||z + t d|| == trust_radius``.
        This is like a line-sphere intersection.
        Return the two values of t, sorted from low to high.
              )r
   r)   mathsqrtcopysignsorted)r#   zdtrust_radiusabcsqrt_discriminantauxtatbr   r   r   get_boundaries_intersectionsZ   s   	z4BaseQuadraticSubproblem.get_boundaries_intersectionsc                 C   s   t d)Nz9The solve method should be implemented by the child class)NotImplementedError)r#   r=   r   r   r   solveq   s   zBaseQuadraticSubproblem.solve)NN)__name__
__module____qualname____doc__r(   r,   propertyr$   r%   r&   r'   r3   rE   rG   r   r   r   r   r      s    
	



r   r         ?     @@333333?-C6?FTc           #         sr  t | |du rtd|du r|du rtd|du r tdd|	  kr-dk s2td td|dkr:td|dkrBtd	||krJtd
t| }t| ||||d  j}  j}t	|rh j
}nt	|rmn|tv svt|trd} fdd}ntdt||\}}|du rt|d }d}|}|}|r|g}||| |||}d}|j|
kr@z	||\}}W n tjjy   d}Y n{w ||}|| }||| |||}|j|j }|j| }|dkrd}nX|| }|dk r|d9 }n|dkr|rtd| |}||	kr|}|}|r|t| |d7 }t||jd} t|| r*n|j|
k r3d}n||kr;d}n|j|
kstd td ddf}!|r|dkrYt|!|  n
tj|!| tdd td|jd td|  td j  td j  td j |d    t||dk||j|j! j j j |d  ||!| d
}"|dur|j
|"d < |r||"d!< |"S )"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.

    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 methodsr   g      ?zinvalid 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&   r   c                    s     | |S r   )r&   r)   )r   r+   r   sfr   r   r'      s   z%_minimize_trust_region.<locals>.hessp      r4   g      ?r   )r   r$   successmaxiterz:A bad approximation caused failure to predict improvement.z3A linalg error occurred, such as a non-psd Hessian.)
stacklevelz!         Current function value: fz         Iterations: %dz!         Function evaluations: %dz!         Gradient evaluations: %dz          Hessian evaluations: %d)
r   rU   statusr$   r%   nfevnjevnhevnitmessager&   allvecs)"r   
ValueError	Exceptionr
   asarrayflattenr   r$   gradcallabler&   r	   
isinstancer   r   lenr3   rG   r1   LinAlgErrorminappendr   r   r   r   printwarningswarnRuntimeWarningrZ   ngevr\   r%   )#r$   x0r   r%   r&   r'   
subprobleminitial_trust_radiusmax_trust_radiusetagtolrV   disp
return_allcallbackinexactunknown_optionsnhesspwarnflagr=   r   r_   mkr+   hits_boundarypredicted_value
x_proposed
m_proposedactual_reductionpredicted_reductionrhointermediate_resultstatus_messagesresultr   rQ   r   _minimize_trust_regionv   s   




;


r   )r   NNNNrM   rN   rO   rP   NFFNT)rK   r7   rl   numpyr
   scipy.linalgr0   	_optimizer   r   r   r   r   'scipy.optimize._hessian_update_strategyr   (scipy.optimize._differentiable_functionsr	   __all__r   r   r   r   r   r   r   <module>   s"    X