B
    &]\
$                 @   sj   d Z ddlmZmZmZ ddlZddlZddlZ	ddl
mZmZmZmZ g ZG dd deZdddZdS )zTrust-region optimization.    )divisionprint_functionabsolute_importN   )_check_unknown_optionswrap_function_status_messageOptimizeResultc               @   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_magZ_cauchy_pointZ_newton_point_fun_jac_hess_hessp)selfxfunjachesshessp r   :lib/python3.7/site-packages/scipy/optimize/_trustregion.py__init__   s    z BaseQuadraticSubproblem.__init__c             C   s*   | j t| j| dt|| |  S )Ng      ?)r   npdotr   r   )r   pr   r   r   __call__%   s    z BaseQuadraticSubproblem.__call__c             C   s   | j dkr| | j| _ | j S )z1Value of objective function at current iteration.N)r   r   r   )r   r   r   r   r   (   s    
zBaseQuadraticSubproblem.func             C   s   | j dkr| | j| _ | j S )z=Value of jacobian of objective function at current iteration.N)r   r   r   )r   r   r   r   r   /   s    
zBaseQuadraticSubproblem.jacc             C   s   | j dkr| | j| _ | j S )z<Value of hessian of objective function at current iteration.N)r   r   r   )r   r   r   r   r   6   s    
zBaseQuadraticSubproblem.hessc             C   s*   | j d k	r|  | j|S t| j|S d S )N)r   r   r   r   r   )r   r   r   r   r   r   =   s    
zBaseQuadraticSubproblem.hesspc             C   s    | j dkrtj| j| _ | j S )zAMagniture of jacobian of objective function at current iteration.N)r   scipylinalgZnormr   )r   r   r   r   jac_magC   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   mathZsqrtZcopysignsorted)r   zdtrust_radiusabcZsqrt_discriminantZauxZtatbr   r   r   get_boundaries_intersectionsJ   s    	z4BaseQuadraticSubproblem.get_boundaries_intersectionsc             C   s   t dd S )Nz9The solve method should be implemented by the child class)NotImplementedError)r   r+   r   r   r   solvea   s    zBaseQuadraticSubproblem.solve)NN)__name__
__module____qualname____doc__r   r    propertyr   r   r   r   r#   r0   r2   r   r   r   r   r
      s   
r
   r         ?     @@333333?-C6?FTc       &      K   s<  t | |dkrtd|dkr0|dkr0td|dkr@tdd|	  krTdk s^n td|dkrntd|dkr~td	||krtd
t| }t| |\}} t||\}}t||\}}t||\}}|dkrt|d }d}|}|}|r|g}||| |||}d}x@|j|
krTy|	|\}}W n0 tj
j
jk
rd } z
d}P W dd}~X Y nX ||}|| }||| |||} |j| j }!|j| }"|"dkrd}P |!|" }#|#dk r|d9 }n|#dkr|rtd| |}|#|	kr|}| }|r|t| |dk	r&|t| |d7 }|j|
k r@d}P ||krd}P qW td td ddf}$|r|dkrt|$|  ntd|$|   td|j  td|  td|d   td|d   td|d   t||dk||j|j|d |d |d ||$| d
}%|dk	r*|j|%d< |r8||%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$   g      ?r   successmaxiterz:A bad approximation caused failure to predict improvement.z3A linalg error occurred, such as a non-psd Hessian.z	Warning: z#         Current function value: %fz         Iterations: %dz!         Function evaluations: %dz!         Gradient evaluations: %dz          Hessian evaluations: %d)
r   r>   Zstatusr   r   ZnfevZnjevZnhevZnitmessager   allvecs)r   
ValueError	Exceptionr   ZasarrayZflattenr   lenr#   r2   r"   ZLinAlgErrorr   minappendcopyr   printr	   r   r   )&r   Zx0argsr   r   r   Z
subproblemZinitial_trust_radiusZmax_trust_radiusZetaZgtolr?   ZdispZ
return_allcallbackZinexactZunknown_optionsZnfunZnjacZnhessZnhesspZwarnflagr+   r   rA   mkr   Zhits_boundaryeZpredicted_valueZ
x_proposedZ
m_proposedZactual_reductionZpredicted_reductionZrhoZstatus_messagesresultr   r   r   _minimize_trust_regionf   s    









rO   )r   NNNNr8   r9   r:   r;   NFFNT)r6   Z
__future__r   r   r   r'   Znumpyr   Zscipy.linalgr!   optimizer   r   r   r	   __all__objectr
   rO   r   r   r   r   <module>   s   X    