
    xfi                         d Z ddlmZ ddlmZ ddlZg dZ G d de	      Z
	 	 	 	 ddZ	 	 	 dd	ZeZ	 	 	 dd
Z	 	 	 	 ddZd Zd Zd ZddZddZddZ	 ddZ	 	 ddZy)z
Functions
---------
.. autosummary::
   :toctree: generated/

    line_search_armijo
    line_search_wolfe1
    line_search_wolfe2
    scalar_search_wolfe1
    scalar_search_wolfe2

    )warn)	_minpack2N)LineSearchWarningline_search_wolfe1line_search_wolfe2scalar_search_wolfe1scalar_search_wolfe2line_search_armijoc                       e Zd Zy)r   N)__name__
__module____qualname__     </usr/lib/python3/dist-packages/scipy/optimize/_linesearch.pyr   r      s    r   r   c                      |	 g }|gdgdg fd}fd}t        j                  |      }t        |||||||	|
||
      \  }}}|d   d   ||d   fS )a  
    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

    r   c                 <    dxx   dz  cc<    | z  z   g S Nr      r   )sargsffcpkxks    r   phizline_search_wolfe1.<locals>.phiI   s(    
1
ad"T""r   c                 t     | z  z   g d<   dxx   dz  cc<   t        j                  d         S r   npdot)r   r   fprimegcgvalr   r   s    r   derphiz"line_search_wolfe1.<locals>.derphiM   s@    ad*T*Q
1
vvd1gr""r   )c1c2amaxaminxtol)r   r    r   )r   r!   r   r   gfkold_fvalold_old_fvalr   r%   r&   r'   r(   r)   r   r$   derphi0stpfvalr   r"   r#   s   ````   `          @@@r   r   r      s    F {R$5D
B
B# ## #
 ffS"oG.<bt$T;Cx 1r!udHd1g55r   c
                    | | d      }| |d      }|"|dk7  rt        dd||z
  z  |z        }
|
dk  rd}
nd}
|}|}t        j                  dt        j                        }t        j                  dt              }d}d	}t        |      D ]B  }t        j                  |
|||||	|||||      \  }}}}|dd
 dk(  r|}
 | |      } ||      }B n d}|dd dk(  s|dd dk(  rd}|||fS )a,  
    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 phi'(alpha)
        Objective function derivative. Returns a scalar.
    phi0 : float, optional
        Value of phi at 0
    old_phi0 : float, optional
        Value of phi at previous point
    derphi0 : float, optional
        Value derphi at 0
    c1 : float, optional
        Parameter for Armijo condition rule.
    c2 : float, optional
        Parameter for curvature condition rule.
    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.

    N        r         ?)\( @)   )   s   STARTd   r4   s   FG   s   ERROR   s   WARN)minr   zerosintcfloatrangeminpack2dcsrch)r   r$   phi0old_phi0r-   r%   r&   r'   r(   r)   alpha1phi1derphi1isavedsavetaskmaxiterir.   s                      r   r   r   [   s0   X |2w*1S&$/27:;A:FDGHHT277#EHHUE"EDG7^ #+??6435r437ue$M T7D 8uFs8DSkG BQx8tBQx72d?r   c                 <    dgdgdgdg fd}|fd|	 g }t        j                  |      }fd}nd}t        ||||||	|
||
      \  }}}}|t        dt               nd   }|d   d   |||fS )a  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. The search direction must be a descent direction
        for the algorithm to converge.
    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, pp. 59-61.

    The search direction `pk` must be a descent direction (e.g.
    ``-myfprime(xk)``) to find a step length that satisfies the strong Wolfe
    conditions. If the search direction is not a descent direction (e.g.
    ``myfprime(xk)``), then `alpha`, `new_fval`, and `new_slope` will be None.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.optimize import line_search

    A objective function and its gradient are defined.

    >>> def obj_func(x):
    ...     return (x[0])**2+(x[1])**2
    >>> def obj_grad(x):
    ...     return [2*x[0], 2*x[1]]

    We can find alpha that satisfies strong Wolfe conditions.

    >>> start_point = np.array([1.8, 1.7])
    >>> search_gradient = np.array([-1.0, -1.0])
    >>> line_search(obj_func, obj_grad, start_point, search_gradient)
    (1.0, 2, 1, 1.1300000000000001, 6.13, [1.6, 1.4])

    r   Nc                 <    dxx   dz  cc<    | z  z   g S r   r   )alphar   r   r   r   r   s    r   r   zline_search_wolfe2.<locals>.phi  s(    
1
ebj(4((r   c                 ~    dxx   dz  cc<    | z  z   g d<   | d<   t        j                  d         S r   r   )rL   r   r!   r"   r#   
gval_alphar   r   s    r   r$   z"line_search_wolfe2.<locals>.derphi   sI    
1
ebj040Q
1vvd1gr""r   c                 P    d   | k7  r |        | z  z   } | ||d         S )Nr   r   )	rL   r   xr$   extra_conditionr#   rN   r   r   s	      r   extra_condition2z,line_search_wolfe2.<locals>.extra_condition2-  s8    !}%uURZA"5!S$q'::r   )rH   *The line search algorithm did not converge)r   r    r	   r   r   )r   myfprimer   r   r*   r+   r,   r   r%   r&   r'   rQ   rH   r   r-   rR   
alpha_starphi_starderphi_starr$   r   r!   r"   r#   rN   s   ` ``   `   `       @@@@@@r   r   r      s    | B
B6DJ) ) F# # {R$ffS"oG"	; 	;  2F<"b$g3//J(K 9;LM 1gr!ubeXxDDr   c
                    | | d      }| |d      }d}
||dk7  rt        dd||z
  z  |z        }nd}|dk  rd}|t        ||      } | |      }|}|}|d }t        |	      D ]  }|dk(  s|/|
|k(  r*d}|}|}d}|dk(  rd}ndd	|z  z   }t        |t                n|dkD  }||||z  |z  z   kD  s||k\  r|rt	        |
||||| ||||||      \  }}} n ||      }t        |      | |z  k  r |||      r|}|}|} n\|dk\  rt	        ||
|||| ||||||      \  }}} n;d
|z  }|t        ||      }|}
|}|} | |      }|} |}|}d}t        dt               ||||fS )a  Find alpha that satisfies strong Wolfe conditions.

    alpha > 0 is assumed to be a descent direction.

    Parameters
    ----------
    phi : callable phi(alpha)
        Objective scalar function.
    derphi : callable phi'(alpha)
        Objective function derivative. Returns a scalar.
    phi0 : float, optional
        Value of phi at 0.
    old_phi0 : float, optional
        Value of phi at previous point.
    derphi0 : float, optional
        Value of derphi at 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, pp. 59-61.

    Nr1   r   r2   r3   c                      y)NTr   )rL   r   s     r   rQ   z-scalar_search_wolfe2.<locals>.extra_condition  s    r   z7Rounding errors prevent the line search from convergingz4The line search algorithm could not find a solution zless than or equal to amax: %sr4   rS   )r9   r=   r   r   _zoomabs)r   r$   r@   rA   r-   r%   r&   r'   rQ   rH   alpha0rB   phi_a1phi_a0	derphi_a0rI   rU   rV   rW   msgnot_first_iteration	derphi_a1alpha2s                          r   r	   r	   E  s1   r |2w*F1S&$/27:;zVT"[F FI	 7^ 8NQ;4+$ JHDK{OL6=> '(!eTBK'111v#6fff$if"GR_F .J+ 6N		Nrc'k)vv.#
!'Nfff$if"GR_F .J+ V&FV	c8Nj 
9;LMx{22r   c           
      p   t        j                  ddd      5  	 |}|| z
  }|| z
  }	||	z  dz  ||	z
  z  }
t        j                  d      }|	dz  |d<   |dz   |d<   |	dz   |d<   |dz  |d	<   t        j                  |t        j                  ||z
  ||z  z
  ||z
  ||	z  z
  g      j                               \  }}||
z  }||
z  }||z  d|z  |z  z
  }| | t        j                  |      z   d|z  z  z   }	 d
d
d
       t        j                        sy
|S # t        $ r Y d
d
d
       y
w xY w# 1 sw Y   8xY w)z
    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.

    raisedivideoverinvalidr4   )r4   r4   )r   r   )r   r      )r   r   )r   r   N)	r   errstateemptyr    asarrayflattensqrtArithmeticErrorisfinite)afafpabfbcr   Cdbdcdenomd1ABradicalxmins                   r   	_cubicminr     sk    
G'7	C 	AQBQB"WNb2g.E&!BQwBtHaxBtHaxBtHQwBtHVVB

BGa"f,<,.Ga"f,<,> !??FwyJFQJAJA!ea!eai'GRWWW--!a%88D!& ;;tK	  	% "	# s)   D,CD	D)D,(D))D,,D5c                    t        j                  ddd      5  	 |}|}|| dz  z
  }||z
  ||z  z
  ||z  z  }| |d|z  z  z
  }		 ddd       t        j                  	      sy|	S # t        $ r Y ddd       yw xY w# 1 sw Y   8xY w)z
    Finds the minimizer for a quadratic polynomial that goes through
    the points (a,fa), (b,fb) with derivative at a of fpa.

    re   rf   r2          @N)r   rk   rp   rq   )
rr   rs   rt   ru   rv   Drx   ry   r~   r   s
             r   _quadminr     s     
G'7	C 	AAQWBa!b&R"W-AqC!G}$D ;;tK	  	 	 s(   A;(A$$	A8-A;7A88A;;Bc           	         d}d}d}d}|}d}	 || z
  }|dk  r|| }}n| |}}|dkD  r||z  }t        | ||||||      }|dk(  s||z
  kD  s|||z   k  r.||z  }t        | ||||      }||||z
  kD  s|||z   k  r| d|z  z   } ||      }|||	|z  |z  z   kD  s||k\  r	|}|}|}|}nH ||      }t        |      |
 |z  k  r |||      r|}|}|}n0||| z
  z  dk\  r	|}|}| }|}n|}| }|} |}|}|dz  }||kD  rd}d}d}n|||fS )a  Zoom stage of approximate linesearch satisfying strong Wolfe conditions.

    Part of the optimization algorithm in `scalar_search_wolfe2`.

    Notes
    -----
    Implements Algorithm 3.6 (zoom) in Wright and Nocedal,
    'Numerical Optimization', 1999, pp. 61.

    
   r   g?皙?N      ?r   )r   r   r[   )a_loa_hiphi_lophi_hi	derphi_lor   r$   r@   r-   r%   r&   rQ   rH   rI   delta1delta2phi_reca_recdalpharr   ru   cchka_jqchkphi_aj	derphi_aja_starval_starvalprime_stars                                r   rZ   rZ     s    G	AFFGE
 A:qAqA EF?DD&)T6!7,CFq4xS1t8^F?D4D&ACqv34<SZ' STBsF7N**&0@GEDFsI9~"W,f1M! )$+&!+  DF!I	QKFH MA B 8]**r   c                      t        j                        dg fd}|	 |d      }	n|}	t        j                  |      }
t        ||	|
||      \  }}|d   |fS )a  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, pp. 56-57

    r   c                 <    dxx   dz  cc<    | z  z   g S r   r   )rB   r   r   r   r   r   s    r   r   zline_search_armijo.<locals>.phi  s(    
1
fRi'$''r   r1   )r%   r\   )r   
atleast_1dr    scalar_search_armijo)r   r   r   r*   r+   r   r%   r\   r   r@   r-   rL   rC   r   s   ```  `       @r   r
   r
   i  st    D 
r	B
B( ( 2wffS"oG&sD'b.46KE4"Q%r   c           
      F    t        | |||||||      }|d   |d   d|d   fS )z8
    Compatibility wrapper for `line_search_armijo`
    )r   r%   r\   r   r   r4   )r
   )	r   r   r   r*   r+   r   r%   r\   rs	            r   line_search_BFGSr     s:     	1b"c8$2"(	*AQ41q!A$r   c                 ^    | |      }||||z  |z  z   k  r||fS | |dz  z  dz  ||z
  ||z  z
  z  } | |      }||||z  |z  z   k  r||fS ||kD  r|dz  |dz  z  ||z
  z  }	|dz  ||z
  ||z  z
  z  |dz  ||z
  ||z  z
  z  z
  }
|
|	z  }
|dz   ||z
  ||z  z
  z  |dz  ||z
  ||z  z
  z  z   }||	z  }| t        j                  t        |dz  d|
z  |z  z
              z   d|
z  z  } | |      }||||z  |z  z   k  r||fS ||z
  |dz  kD  sd||z  z
  dk  r|dz  }|}|}|}|}||kD  rd|fS )a(  Minimize over alpha, the function ``phi(alpha)``.

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

    alpha > 0 is assumed to be a descent direction.

    Returns
    -------
    alpha
    phi1

    r4   r   rj   g      @r   gQ?N)r   ro   r[   )r   r@   r-   r%   r\   r(   r^   rB   r]   factorrr   ru   rc   phi_a2s                 r   r   r     s    [F6	')))v~ Z&!)#c)Vd]Wv=M-MNF[F$F7***v~ 4-VQY&&-8AI$78AI$789JQYJ&4-'&.89AI$789J"rwws1a4!a%'/#9:;;AFVdRYw...6>!VOv|+F6M0AT/Ic\F+ 4-0 <r   c                    |d   }t        |      }	d}
d}d}	 ||
|z  z   } | |      \  }}||	|z   ||
dz  z  |z  z
  k  r|
}n|
dz  |z  |d|
z  dz
  |z  z   z  }|||z  z
  } | |      \  }}||	|z   ||dz  z  |z  z
  k  r| }nR|dz  |z  |d|z  dz
  |z  z   z  }t        j                  |||
z  ||
z        }
t        j                  |||z  ||z        }||||fS )a@  
    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).

    r   r4   )maxr   clip)r   x_kdprev_fsetagammatau_mintau_maxf_kf_baralpha_palpha_mrL   xpfpFpalpha_tpalpha_tms                     r   _nonmonotone_line_search_cruzr     sG   P "+CLEGGE
7Q;2Buwz1C777EA:#rQwY]C,?'?@7Q;2Buwz1C777HEA:#rQwY]C,?'?@''(Gg$5w7HI''(Gg$5w7HI) , "b"r   c                    d}d}d}	 |||z  z   } | |      \  }}|||z   ||dz  z  |z  z
  k  r|}n|dz  |z  |d|z  dz
  |z  z   z  }|||z  z
  } | |      \  }}|||z   ||dz  z  |z  z
  k  r| }nR|dz  |z  |d|z  dz
  |z  z   z  }t        j                  |||z  |	|z        }t        j                  |||z  |	|z        }|
|z  dz   }|
|z  ||z   z  |z   |z  }|}||||||fS )a  
    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).

    r   r4   )r   r   )r   r   r   r   rx   Qr   r   r   r   nur   r   rL   r   r   r   r   r   Q_nexts                       r   _nonmonotone_line_search_chengr   ,  sg   ^ GGE
7Q;2BS57A:-333EA:#rQwY]C,?'?@7Q;2BS57A:-333HEA:#rQwY]C,?'?@''(Gg$5w7HI''(Gg$5w7HI) . !VaZF	a1s7	b	 F*AA"b"a""r   )	NNNr   -C6??2   :0yE>+=)NNNr   r   r   r   r   )	NNNr   r   r   NNr   )NNNr   r   NNr   )r   r   r   )r   r   r   )r   r   r   )r   r   r   g333333?)__doc__warningsr   scipy.optimizer   r>   numpyr   __all__RuntimeWarningr   r   r   line_searchr   r	   r   r   rZ   r
   r   r   r   r   r   r   r   <module>r      s     0 !	 	 /337?C!96x IM%(27Pf ! @DIM57KE\ ,004/379O3dD*T+v1h7~ DGER EH&*N#r   