๓
ฺฦ๗Xc           @` sธ   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 d e f d     YZ d d d d d d d	 d
 d d e e d d  Z d S(   s   Trust-region optimization.i    (   t   divisiont   print_functiont   absolute_importNi   (   t   _check_unknown_optionst   wrap_functiont   _status_messaget   OptimizeResultt   BaseQuadraticSubproblemc           B` s}   e  Z d  Z d
 d
 d  Z d   Z e d    Z e d    Z e d    Z	 d   Z
 e d    Z d   Z d	   Z RS(   sQ  
    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``.
    c         C` sg   | |  _  d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _ | |  _ | |  _	 | |  _
 | |  _ d  S(   N(   t   _xt   Nonet   _ft   _gt   _ht   _g_magt   _cauchy_pointt   _newton_pointt   _funt   _jact   _hesst   _hessp(   t   selft   xt   funt   jact   hesst   hessp(    (    s:   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_trustregion.pyt   __init__   s    										c         C` s7   |  j  t j |  j |  d t j | |  j |   S(   Ng      เ?(   R   t   npt   dotR   R   (   R   t   p(    (    s:   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_trustregion.pyt   __call__%   s    c         C` s.   |  j  d k r' |  j |  j  |  _  n  |  j  S(   s1   Value of objective function at current iteration.N(   R
   R	   R   R   (   R   (    (    s:   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_trustregion.pyR   (   s    c         C` s.   |  j  d k r' |  j |  j  |  _  n  |  j  S(   s=   Value of jacobian of objective function at current iteration.N(   R   R	   R   R   (   R   (    (    s:   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_trustregion.pyR   /   s    c         C` s.   |  j  d k r' |  j |  j  |  _  n  |  j  S(   s<   Value of hessian of objective function at current iteration.N(   R   R	   R   R   (   R   (    (    s:   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_trustregion.pyR   6   s    c         C` s9   |  j  d  k	 r" |  j  |  j |  St j |  j |  Sd  S(   N(   R   R	   R   R   R   R   (   R   R   (    (    s:   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_trustregion.pyR   =   s    c         C` s1   |  j  d k r* t j j |  j  |  _  n  |  j  S(   sA   Magniture of jacobian of objective function at current iteration.N(   R   R	   t   scipyt   linalgt   normR   (   R   (    (    s:   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_trustregion.pyt   jac_magC   s    c   
      C` s   t  j | |  } d t  j | |  } t  j | |  | d } t j | | d | |  } | | d | } | | d | }	 | |	 f S(   sภ   
        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.
        i   i   (   R   R   t   matht   sqrt(
   R   t   zt   dt   trust_radiust   at   bt   ct   sqrt_discriminantt   tat   tb(    (    s:   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_trustregion.pyt   get_boundaries_intersectionsJ   s    c         C` s   t  d   d  S(   Ns9   The solve method should be implemented by the child class(   t   NotImplementedError(   R   R'   (    (    s:   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_trustregion.pyt   solveX   s    N(   t   __name__t
   __module__t   __doc__R	   R   R   t   propertyR   R   R   R   R"   R.   R0   (    (    (    s:   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_trustregion.pyR      s   			g      ๐?g     @@g333333ร?g-C๋โ6?c   %      K` sc  t  |  | d$ k r% t d   n  | d$ k rL | d$ k rL t d   n  | d$ k rg t d   n  d |	 k o~ d k  n s t d   n  | d k rญ t d   n  | d k rศ t d   n  | | k rใ t d	   n  t j |  j   } t |  |  \ } }  t | |  \ } } t | |  \ } } t | |  \ } } | d$ k rkt |  d
 } n  d } | } | } | r| g } n  | | |  | | |  } d } xxt	 r'y | j
 |  \ } } Wn# t j j j k
 r๔} d } Pn X| |  } | | } | | |  | | |  } | j | j }  | j | }! |! d k rVd } Pn  |  |! }" |" d k  ry| d 9} n( |" d k rก| rกt d | |  } n  |" |	 k rผ| } | } n  | rา| j |  n  | d$ k	 r๋| |  n  | d 7} | j |
 k  rd } Pn  | | k rฐd } PqฐqฐWt d t d d d f }# | rฯ| d k ret |# |  n t d |# |  t d | j  t d |  t d | d  t d | d  t d | d  n  t d | d | d k d | d | j d | j d | d d | d d | d d  | d! |# |  
}$ | d$ k	 rL| j |$ d" <n  | r_| |$ d# <n  |$ S(%   s  
    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.

    This function is called by the `minimize` function.
    It is not supposed to be called directly.
    s7   Jacobian is currently required for trust-region methodss_   Either the Hessian or the Hessian-vector product is currently required for trust-region methodssB   A subproblem solving strategy is required for trust-region methodsi    g      ะ?s   invalid acceptance stringencys%   the max trust radius must be positives)   the initial trust radius must be positives?   the initial trust radius must be less than the max trust radiusiศ   i   i   g      ่?i   t   successt   maxiters:   A bad approximation caused failure to predict improvement.s3   A linalg error occurred, such as a non-psd Hessian.s	   Warning: s#            Current function value: %fs            Iterations: %ds!            Function evaluations: %ds!            Gradient evaluations: %ds             Hessian evaluations: %dR   t   statusR   R   t   nfevt   njevt   nhevt   nitt   messageR   t   allvecsN(   R   R	   t
   ValueErrort	   ExceptionR   t   asarrayt   flattenR   t   lent   TrueR0   R    t   LinAlgErrorR   t   mint   appendR"   R   t   printR   R   R   (%   R   t   x0t   argsR   R   R   t
   subproblemt   initial_trust_radiust   max_trust_radiust   etat   gtolR6   t   dispt
   return_allt   callbackt   unknown_optionst   nfunt   njact   nhesst   nhesspt   warnflagR'   R   R=   t   mt   kR   t   hits_boundaryt   et   predicted_valuet
   x_proposedt
   m_proposedt   actual_reductiont   predicted_reductiont   rhot   status_messagest   result(    (    s:   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_trustregion.pyt   _minimize_trust_region]   sจ    
	

	
	&(    (   R3   t
   __future__R    R   R   R#   t   numpyR   t   scipy.linalgR   t   optimizeR   R   R   R   t   __all__t   objectR   R	   t   FalseRd   (    (    (    s:   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_trustregion.pyt   <module>   s   "O		