ó
î&]\c        
   @` s›  d  Z  d d l m Z m Z m Z d d l m Z d d l m Z d d l	 Z
 d d l m Z d d d	 d
 d d g Z d e f d „  ƒ  YZ d d d d$ d d d d d d „	 Z d d d d d d d d d „ Z e Z d d d d% d d d d d d „	 Z d d d d d d d d d d „	 Z d „  Z d „  Z d „  Z d& d d d „ Z d' d d d „ Z d d d d „ Z d d d  d! „ Z d d d  d" d# „ Z d S((   s·   
Functions
---------
.. autosummary::
   :toctree: generated/

    line_search_armijo
    line_search_wolfe1
    line_search_wolfe2
    scalar_search_wolfe1
    scalar_search_wolfe2

i    (   t   divisiont   print_functiont   absolute_import(   t   warn(   t   minpack2N(   t   xranget   LineSearchWarningt   line_search_wolfe1t   line_search_wolfe2t   scalar_search_wolfe1t   scalar_search_wolfe2t   line_search_armijoc           B` s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyR      s   g-Cëâ6?gÍÌÌÌÌÌì?i2   g:Œ0âŽyE>g›+¡†›„=c         ` s0  | d
 k r ˆ ˆ	 ƒ } n  t ˆ t ƒ rW ˆ d } ˆ d ‰ ˆ | f ˆ  ‰ t ‰ n ˆ  ‰ t ‰ | g ‰ d g ‰ d g ‰ ‡  ‡ ‡ ‡ ‡	 f d †  } ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡	 f d †  } t j | ˆ ƒ } t | | | | | d | d |	 d |
 d | d	 | ƒ\ } } } | ˆ d ˆ d | | ˆ d f S(   sÞ  
    As `scalar_search_wolfe1` but do a line search to direction `pk`

    Parameters
    ----------
    f : callable
        Function `f(x)`
    fprime : callable
        Gradient of `f`
    xk : array_like
        Current point
    pk : array_like
        Search direction

    gfk : array_like, optional
        Gradient of `f` at point `xk`
    old_fval : float, optional
        Value of `f` at point `xk`
    old_old_fval : float, optional
        Value of `f` at point preceding `xk`

    The rest of the parameters are the same as for `scalar_search_wolfe1`.

    Returns
    -------
    stp, f_count, g_count, fval, old_fval
        As in `line_search_wolfe1`
    gval : array
        Gradient of `f` at the final point

    i   i    c         ` s%   ˆ d c d 7<ˆ ˆ |  ˆ ˆ  Œ S(   Ni    i   (    (   t   s(   t   argst   ft   fct   pkt   xk(    s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyt   phiU   s    c         ` sb   ˆ ˆ |  ˆ ˆ Œ ˆ d <ˆ r4 ˆ d c d 7<n ˆ  d c t  ˆ ƒ d 7<t j ˆ d ˆ ƒ S(   Ni    i   (   t   lent   npt   dot(   R   (   R   t   fprimet   gct   gradientt   gvalt   newargsR   R   (    s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyt   derphiY   s
    t   c1t   c2t   amaxt   amint   xtolN(   t   Nonet
   isinstancet   tuplet   Falset   TrueR   R   R	   (   R   R   R   R   t   gfkt   old_fvalt   old_old_fvalR   R   R   R    R!   R"   t   epsR   R   t   derphi0t   stpt   fval(    (
   R   R   R   R   R   R   R   R   R   R   s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyR   "   s&    #

				$*c
         C` s„  | d k r |  d ƒ } n  | d k r6 | d ƒ } n  | d k	 r | d k r t d d | | | ƒ }
 |
 d k  r‡ d }
 q‡ n d }
 | } | } t j d t j ƒ } t j d t ƒ } d } d } x… t | ƒ D]q } t j |
 | | | | |	 | | | | | ƒ \ } } } } | d  d	 k rC| }
 |  | ƒ } | | ƒ } qÓ PqÓ Wd } | d
  d k sn| d  d k rwd } n  | | | f S(   sÙ  
    Scalar function search for alpha that satisfies strong Wolfe conditions

    alpha > 0 is assumed to be a descent direction.

    Parameters
    ----------
    phi : callable phi(alpha)
        Function at point `alpha`
    derphi : callable dphi(alpha)
        Derivative `d phi(alpha)/ds`. Returns a scalar.

    phi0 : float, optional
        Value of `f` at 0
    old_phi0 : float, optional
        Value of `f` at the previous point
    derphi0 : float, optional
        Value `derphi` at 0
    c1, c2 : float, optional
        Wolfe parameters
    amax, amin : float, optional
        Maximum and minimum step size
    xtol : float, optional
        Relative tolerance for an acceptable step.

    Returns
    -------
    alpha : float
        Step size, or None if no suitable step was found
    phi : float
        Value of `phi` at the new point `alpha`
    phi0 : float
        Value of `phi` at `alpha=0`

    Notes
    -----
    Uses routine DCSRCH from MINPACK.

    g        i    g      ð?g)\Âõ(ð?i   i   t   STARTid   t   FGi   t   ERRORi   t   WARNNg)\Âõ( @(   i   (   i   (	   R#   t   minR   t   zerost   intct   floatR   R   t   dcsrch(   R   R   t   phi0t   old_phi0R,   R   R   R    R!   R"   t   alpha1t   phi1t   derphi1t   isavet   dsavet   taskt   maxitert   iR-   (    (    s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyR	   j   s8    + 	i
   c         ` sy  d g ‰ d g ‰ d g ‰ d g ‰ ‡  ‡ ‡ ‡
 ‡ f d †  } t ˆ	 t ƒ ru ‡  ‡ ‡ ‡ ‡ ‡	 ‡
 ‡ f d †  ‰ n' ˆ	 ‰ ‡  ‡ ‡ ‡ ‡ ‡
 ‡ f d †  ‰ | d k rº ˆ ˆ ˆ  Œ } n  t j | ˆ
 ƒ } ˆ d k	 rù ‡ ‡ ‡ ‡ ‡
 ‡ f d †  } n d } t | ˆ | | | | |	 |
 | d | ƒ	\ } } } } | d k rQt d t ƒ n
 ˆ d } | ˆ d ˆ d | | | f S(	   s	  Find alpha that satisfies strong Wolfe conditions.

    Parameters
    ----------
    f : callable f(x,*args)
        Objective function.
    myfprime : callable f'(x,*args)
        Objective function gradient.
    xk : ndarray
        Starting point.
    pk : ndarray
        Search direction.
    gfk : ndarray, optional
        Gradient value for x=xk (xk being the current parameter
        estimate). Will be recomputed if omitted.
    old_fval : float, optional
        Function value for x=xk. Will be recomputed if omitted.
    old_old_fval : float, optional
        Function value for the point preceding x=xk
    args : tuple, optional
        Additional arguments passed to objective function.
    c1 : float, optional
        Parameter for Armijo condition rule.
    c2 : float, optional
        Parameter for curvature condition rule.
    amax : float, optional
        Maximum step size
    extra_condition : callable, optional
        A callable of the form ``extra_condition(alpha, x, f, g)``
        returning a boolean. Arguments are the proposed step ``alpha``
        and the corresponding ``x``, ``f`` and ``g`` values. The line search 
        accepts the value of ``alpha`` only if this 
        callable returns ``True``. If the callable returns ``False`` 
        for the step length, the algorithm will continue with 
        new iterates. The callable is only called for iterates 
        satisfying the strong Wolfe conditions.
    maxiter : int, optional
        Maximum number of iterations to perform

    Returns
    -------
    alpha : float or None
        Alpha for which ``x_new = x0 + alpha * pk``,
        or None if the line search algorithm did not converge.
    fc : int
        Number of function evaluations made.
    gc : int
        Number of gradient evaluations made.
    new_fval : float or None
        New function value ``f(x_new)=f(x0+alpha*pk)``,
        or None if the line search algorithm did not converge.
    old_fval : float
        Old function value ``f(x0)``.
    new_slope : float or None
        The local slope along the search direction at the
        new value ``<myfprime(x_new), pk>``,
        or None if the line search algorithm did not converge.


    Notes
    -----
    Uses the line search algorithm to enforce strong Wolfe
    conditions.  See Wright and Nocedal, 'Numerical Optimization',
    1999, pg. 59-60.

    For the zoom phase it uses an algorithm by [...].

    i    c         ` s%   ˆ d c d 7<ˆ ˆ |  ˆ ˆ  Œ S(   Ni    i   (    (   t   alpha(   R   R   R   R   R   (    s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyR     s    c         ` sw   ˆ d c t  ˆ ƒ d 7<ˆ d } ˆ d } ˆ | f ˆ  } | ˆ |  ˆ | Œ ˆ d <|  ˆ d <t j ˆ d ˆ ƒ S(   Ni    i   (   R   R   R   (   RB   R+   R   R   (   R   R   R   R   t
   gval_alphat   myfprimeR   R   (    s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyR     s    


c         ` sI   ˆ d c d 7<ˆ ˆ |  ˆ ˆ  Œ ˆ d <|  ˆ d <t  j ˆ d ˆ ƒ S(   Ni    i   (   R   R   (   RB   (   R   R   R   R   RC   R   R   (    s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyR     s    
c         ` sB   ˆ d |  k r ˆ  |  ƒ n  ˆ |  ˆ } ˆ |  | | ˆ d ƒ S(   Ni    (    (   RB   R   t   x(   R   t   extra_conditionR   RC   R   R   (    s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyt   extra_condition2,  s    R@   s*   The line search algorithm did not convergeN(   R#   R$   R%   R   R   R
   R   R   (   R   RD   R   R   R(   R)   R*   R   R   R   R    RF   R@   R   R,   RG   t
   alpha_start   phi_start   derphi_star(    (   R   R   RF   R   R   R   R   R   RC   RD   R   R   s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyR   Ã   s,    G				'	!!
c
         C` sÆ  | d k r |  d ƒ } n  | d k rB | d k	 rB | d ƒ } n  d }
 | d k	 r~ | d k r~ t d d | | | ƒ } n d } | d k  r™ d } n  |  | ƒ } | } | } | d k rÉ d „  } n  xêt |	 ƒ D]½} | d k s | d k	 rL|
 | k rLd } | } | } d } | d k r-d } n d d	 | } t | t ƒ Pn  | | | | | k s|| | k r¶| d
 k r¶t |
 | | | | |  | | | | | | ƒ \ } } } Pn  | | ƒ } t | ƒ | | k r| | | ƒ r| } | } | } Pqn  | d k rGt | |
 | | | |  | | | | | | ƒ \ } } } Pn  d | } | d k	 rot | | ƒ } n  | }
 | } | } |  | ƒ } | } qÖ W| } | } d } t d t ƒ | | | | f S(   sÜ  Find alpha that satisfies strong Wolfe conditions.

    alpha > 0 is assumed to be a descent direction.

    Parameters
    ----------
    phi : callable f(x)
        Objective scalar function.
    derphi : callable f'(x), optional
        Objective function derivative (can be None)
    phi0 : float, optional
        Value of phi at s=0
    old_phi0 : float, optional
        Value of phi at previous point
    derphi0 : float, optional
        Value of derphi at s=0
    c1 : float, optional
        Parameter for Armijo condition rule.
    c2 : float, optional
        Parameter for curvature condition rule.
    amax : float, optional
        Maximum step size
    extra_condition : callable, optional
        A callable of the form ``extra_condition(alpha, phi_value)``
        returning a boolean. The line search accepts the value
        of ``alpha`` only if this callable returns ``True``.
        If the callable returns ``False`` for the step length,
        the algorithm will continue with new iterates.
        The callable is only called for iterates satisfying
        the strong Wolfe conditions.
    maxiter : int, optional
        Maximum number of iterations to perform

    Returns
    -------
    alpha_star : float or None
        Best alpha, or None if the line search algorithm did not converge.
    phi_star : float
        phi at alpha_star
    phi0 : float
        phi at 0
    derphi_star : float or None
        derphi at alpha_star, or None if the line search algorithm
        did not converge.

    Notes
    -----
    Uses the line search algorithm to enforce strong Wolfe
    conditions.  See Wright and Nocedal, 'Numerical Optimization',
    1999, pg. 59-60.

    For the zoom phase it uses an algorithm by [...].

    g        i    g      ð?g)\Âõ(ð?i   c         S` s   t  S(   N(   R'   (   RB   R   (    (    s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyt   <lambda>•  s    s7   Rounding errors prevent the line search from convergings4   The line search algorithm could not find a solution s   less than or equal to amax: %si   s*   The line search algorithm did not convergeNg)\Âõ( @(   R#   R3   R   R   R   t   _zoomt   abs(   R   R   R8   R9   R,   R   R   R    RF   R@   t   alpha0R:   t   phi_a1t   phi_a0t	   derphi_a0RA   RH   RI   RJ   t   msgt	   derphi_a1t   alpha2(    (    s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyR
   D  st    ;	$	

c         C` s]  t  j d d d d d d ƒ &y| } | |  } | |  }	 | |	 d | |	 }
 t  j d
 ƒ } |	 d | d <| d | d <|	 d | d <| d | d <t  j | t  j | | | | | | | |	 g ƒ j ƒ  ƒ \ } } | |
 } | |
 } | | d | | } |  | t  j | ƒ d | } Wn t k
 r?d	 SXWd	 QXt  j	 | ƒ sYd	 S| S(   s¼   
    Finds the minimizer for a cubic polynomial that goes through the
    points (a,fa), (b,fb), and (c,fc) with derivative at a of fpa.

    If no minimizer can be found return None

    t   dividet   raiset   overt   invalidi   i    i   i   N(   i   i   (   i    i    (   i    i   (   i   i    (   i   i   (
   R   t   errstatet   emptyR   t   asarrayt   flattent   sqrtt   ArithmeticErrorR#   t   isfinite(   t   at   fat   fpat   bt   fbt   cR   t   Ct   dbt   dct   denomt   d1t   At   Bt   radicalt   xmin(    (    s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyt	   _cubicminÓ  s,    


'

$c   
   	   C` s›   t  j d d d d d d ƒ d yJ | } | } | |  d } | | | | | | } |  | d | }	 Wn t k
 r} d SXWd QXt  j |	 ƒ s— d S|	 S(   s†   
    Finds the minimizer for a quadratic polynomial that goes through
    the points (a,fa), (b,fb) with derivative at a of fpa,

    RU   RV   RW   RX   g      ð?g       @N(   R   RY   R^   R#   R_   (
   R`   Ra   Rb   Rc   Rd   t   DRf   Rg   Rl   Rn   (    (    s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyt   _quadminõ  s    c         C` sN  d } d } d } d } | } d } xt  r@| |  } | d k  rS | |  } } n |  | } } | d k r— | | } t |  | | | | | | ƒ } n  | d k sÏ | d k sÏ | | | k sÏ | | | k  r1| | } t |  | | | | ƒ } | d k s| | | k s| | | k  r1|  d | } q1n  | | ƒ } | | |	 | | k sa| | k r|| } | } | } | } n• | | ƒ } t | ƒ |
 | k rÄ| | | ƒ rÄ| } | } | } Pn  | | |  d k ró| } | } |  } | } n | } |  } | }  | } | } | d 7} | | k r' d } d } d } Pq' q' W| | | f S(   sG   
    Part of the optimization algorithm in `scalar_search_wolfe2`.
    i
   i    gš™™™™™É?gš™™™™™¹?g      à?i   N(   R'   Ro   R#   Rq   RM   (   t   a_lot   a_hit   phi_lot   phi_hit	   derphi_loR   R   R8   R,   R   R   RF   R@   RA   t   delta1t   delta2t   phi_rect   a_rect   dalphaR`   Rc   t   cchkt   a_jt   qchkt   phi_ajt	   derphi_ajt   a_start   val_start   valprime_star(    (    s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyRL   
  sb    	


8
,$	&	
i   c         ` s›   t  j ˆ ƒ ‰ d g ‰ ‡  ‡ ‡ ‡ ‡ f d †  } | d k rN | d ƒ }	 n | }	 t  j | ˆ ƒ }
 t | |	 |
 d | d | ƒ\ } } | ˆ d | f S(   s  Minimize over alpha, the function ``f(xk+alpha pk)``.

    Parameters
    ----------
    f : callable
        Function to be minimized.
    xk : array_like
        Current point.
    pk : array_like
        Search direction.
    gfk : array_like
        Gradient of `f` at point `xk`.
    old_fval : float
        Value of `f` at point `xk`.
    args : tuple, optional
        Optional arguments.
    c1 : float, optional
        Value to control stopping criterion.
    alpha0 : scalar, optional
        Value of `alpha` at start of the optimization.

    Returns
    -------
    alpha
    f_count
    f_val_at_alpha

    Notes
    -----
    Uses the interpolation algorithm (Armijo backtracking) as suggested by
    Wright and Nocedal in 'Numerical Optimization', 1999, pg. 56-57

    i    c         ` s%   ˆ d c d 7<ˆ ˆ |  ˆ ˆ  Œ S(   Ni    i   (    (   R:   (   R   R   R   R   R   (    s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyR   ƒ  s    g        R   RN   N(   R   t
   atleast_1dR#   R   t   scalar_search_armijo(   R   R   R   R(   R)   R   R   RN   R   R8   R,   RB   R;   (    (   R   R   R   R   R   s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyR   ^  s    "	c   	      C` sF   t  |  | | | | d | d | d | ƒ} | d | d d | d f S(   s8   
    Compatibility wrapper for `line_search_armijo`
    R   R   RN   i    i   i   (   R   (	   R   R   R   R(   R)   R   R   RN   t   r(    (    s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyt   line_search_BFGS’  s    !	c         C` sÜ  |  | ƒ } | | | | | k r. | | f S| | d d | | | | } |  | ƒ } | | | | | k r | | f SxP| | k rÑ| d | d | | }	 | d | | | | | d | | | | }
 |
 |	 }
 | d | | | | | d | | | | } | |	 } | t  j t | d d |
 | ƒ ƒ d |
 } |  | ƒ } | | | | | k r| | f S| | | d k s©d | | d k  r¶| d } n  | } | } | } | } q‚ Wd | f S(   s(  Minimize over alpha, the function ``phi(alpha)``.

    Uses the interpolation algorithm (Armijo backtracking) as suggested by
    Wright and Nocedal in 'Numerical Optimization', 1999, pg. 56-57

    alpha > 0 is assumed to be a descent direction.

    Returns
    -------
    alpha
    phi1

    i   g       @i   g      @i   g¸…ëQ¸î?N(   R   R]   RM   R#   (   R   R8   R,   R   RN   R!   RP   R:   RO   t   factorR`   Rc   RT   t   phi_a2(    (    s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyR…   ›  s4    
#


2
(
gš™™™™™¹?g      à?c         C` sX  | d } t  | ƒ }	 d }
 d } d } xt rG| |
 | } |  | ƒ \ } } | |	 | | |
 d | k r{ |
 } Pn  |
 d | | d |
 d | } | | | } |  | ƒ \ } } | |	 | | | d | k rè | } Pn  | d | | d | d | } t j | | |
 | |
 ƒ }
 t j | | | | | ƒ } q+ W| | | | f S(   s>  
    Nonmonotone backtracking line search as described in [1]_

    Parameters
    ----------
    f : callable
        Function returning a tuple ``(f, F)`` where ``f`` is the value
        of a merit function and ``F`` the residual.
    x_k : ndarray
        Initial position
    d : ndarray
        Search direction
    prev_fs : float
        List of previous merit function values. Should have ``len(prev_fs) <= M``
        where ``M`` is the nonmonotonicity window parameter.
    eta : float
        Allowed merit function increase, see [1]_
    gamma, tau_min, tau_max : float, optional
        Search parameters, see [1]_

    Returns
    -------
    alpha : float
        Step length
    xp : ndarray
        Next position
    fp : float
        Merit function value at next position
    Fp : ndarray
        Residual at next position

    References
    ----------
    [1] "Spectral residual method without gradient information for solving
        large-scale nonlinear systems of equations." W. La Cruz,
        J.M. Martinez, M. Raydan. Math. Comp. **75**, 1429 (2006).

    iÿÿÿÿi   i   (   t   maxR'   R   t   clip(   R   t   x_kt   dt   prev_fst   etat   gammat   tau_mint   tau_maxt   f_kt   f_bart   alpha_pt   alpha_mRB   t   xpt   fpt   Fpt   alpha_tpt   alpha_tm(    (    s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyt   _nonmonotone_line_search_cruzÙ  s*    (
	 " "!g333333ë?c         C` sv  d } d } d } xt  r1| | | } |  | ƒ \ } } | | | | | d | k re | } Pn  | d | | d | d | } | | | } |  | ƒ \ } } | | | | | d | k rÒ | } Pn  | d | | d | d | } t j | | | |	 | ƒ } t j | | | |	 | ƒ } q W|
 | d } |
 | | | | | } | } | | | | | | f S(   s‡  
    Nonmonotone line search from [1]

    Parameters
    ----------
    f : callable
        Function returning a tuple ``(f, F)`` where ``f`` is the value
        of a merit function and ``F`` the residual.
    x_k : ndarray
        Initial position
    d : ndarray
        Search direction
    f_k : float
        Initial merit function value
    C, Q : float
        Control parameters. On the first iteration, give values
        Q=1.0, C=f_k
    eta : float
        Allowed merit function increase, see [1]_
    nu, gamma, tau_min, tau_max : float, optional
        Search parameters, see [1]_

    Returns
    -------
    alpha : float
        Step length
    xp : ndarray
        Next position
    fp : float
        Merit function value at next position
    Fp : ndarray
        Residual at next position
    C : float
        New value for the control parameter C
    Q : float
        New value for the control parameter Q

    References
    ----------
    .. [1] W. Cheng & D.-H. Li, ''A derivative-free nonmonotone line
           search and its application to the spectral residual
           method'', IMA J. Numer. Anal. 29, 814 (2009).

    i   i   (   R'   R   R‹   (   R   RŒ   R   R“   Rf   t   QR   R   R‘   R’   t   nuR•   R–   RB   R—   R˜   R™   Rš   R›   t   Q_next(    (    s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyt   _nonmonotone_line_search_cheng!  s,    /	 " "!(    (    (    (    (   t   __doc__t
   __future__R    R   R   t   warningsR   t   scipy.optimizeR   t   numpyR   t   scipy._lib.sixR   t   __all__t   RuntimeWarningR   R#   R   R	   t   line_searchR   R
   Ro   Rq   RL   R   R‡   R…   Rœ   R    (    (    (    s8   lib/python2.7/site-packages/scipy/optimize/linesearch.pyt   <module>   sB   		E	P	Œ	"		T4	?H	