a
    ˜¥»a;j  ã                	   @   s¶   d Z ddlmZ ddlmZ ddlZg d¢ZG dd„ deƒZ	d+dd„Z
d,dd„Ze
Zd-dd„Zd.dd„Zdd„ Zdd„ Zdd„ Zd/dd„Zd0d d!„Zd1d"d#„Zd2d&d'„Zd3d)d*„ZdS )4z·
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                   @   s   e Zd ZdS )r   N)Ú__name__Ú
__module__Ú__qualname__© r   r   úS/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/scipy/optimize/linesearch.pyr      s   r   r   ç-Cëâ6?çÍÌÌÌÌÌì?é2   ç:Œ0âŽyE>ç›+¡†›„=c                    sÎ   |du rˆˆ	ƒ}t ˆtƒr<ˆd }ˆd ‰ˆ|fˆ  ‰d‰nˆ ‰d‰|g‰dg‰dg‰‡ ‡‡‡‡	fdd„}‡‡‡‡‡‡‡‡	fdd	„}t |ˆ¡}t|||||||	|
||d
�
\}}}|ˆ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

    Né   r   FTc                    s(   ˆd  d7  < ˆˆ| ˆ  gˆ ¢R Ž S ©Nr   r   r   ©Ús©ÚargsÚfÚfcÚpkÚxkr   r   ÚphiR   s    zline_search_wolfe1.<locals>.phic                    sZ   ˆˆ| ˆ  gˆ¢R Ž ˆd< ˆr2ˆd  d7  < nˆ d  t ˆƒd 7  < t ˆd ˆ¡S r   ©ÚlenÚnpÚdotr   )r   ÚfprimeÚgcÚgradientÚgvalÚnewargsr   r   r   r   ÚderphiV   s
    z"line_search_wolfe1.<locals>.derphi)Úc1Úc2ÚamaxÚaminÚxtol)Ú
isinstanceÚtupler!   r"   r   )r   r#   r   r   ÚgfkÚold_fvalÚold_old_fvalr   r)   r*   r+   r,   r-   Úepsr   r(   Úderphi0ÚstpZfvalr   )
r   r   r   r#   r$   r%   r&   r'   r   r   r   r      s(    #


þr   c
                 C   s  |du r| dƒ}|du r |dƒ}|durT|dkrTt dd||  | ƒ}
|
dk rXd}
nd}
|}|}t dtj¡}t dt¡}d}d	}t|ƒD ]T}t |
|||||	|||||¡\}}}}|dd
… dkrÚ|}
| |ƒ}||ƒ}qŠ qäqŠ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   STARTéd   r9   s   FGé   s   ERRORé   s   WARN)Úminr!   ÚzerosZintcÚfloatÚranger   Zdcsrch)r   r(   Úphi0Úold_phi0r4   r)   r*   r+   r,   r-   Úalpha1Úphi1Zderphi1ZisaveZdsaveZtaskÚmaxiterÚir5   r   r   r   r   g   s:    ,
þ
$r   é
   c                    s  dg‰dg‰dg‰dg‰‡ ‡‡‡
‡fdd„}t ˆ	tƒrR‡ ‡‡‡‡‡	‡
‡fdd„‰nˆ	‰‡ ‡‡‡‡‡
‡fdd„‰|du r†ˆˆgˆ ¢R Ž }t |ˆ
¡}ˆdur²‡‡‡‡‡
‡fdd	„}nd}t|ˆ|||||	|
||d
�
\}}}}|du rîtdtƒ nˆd }|ˆd ˆd |||fS )al  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, pp. 59-61.

    Examples
    --------
    >>> 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                    s(   ˆd  d7  < ˆˆ| ˆ  gˆ ¢R Ž S r   r   ©Úalphar   r   r   r     s    zline_search_wolfe2.<locals>.phic                    sh   ˆd  t ˆƒd 7  < ˆd }ˆd }ˆ|fˆ  }|ˆ| ˆ  g|¢R Ž ˆd< | ˆd< t ˆd ˆ¡S r   r   )rJ   r3   r#   r'   )r   r   r   r&   Ú
gval_alphaÚmyfprimer   r   r   r   r(   "  s    z"line_search_wolfe2.<locals>.derphic                    sD   ˆd  d7  < ˆˆ| ˆ  gˆ ¢R Ž ˆd< | ˆd< t  ˆd ˆ¡S r   )r!   r"   rI   )r   r#   r$   r&   rK   r   r   r   r   r(   -  s    c                    s2   ˆd | krˆ | ƒ ˆ| ˆ  }ˆ| ||ˆd ƒS )Nr   r   )rJ   r   Úx)r(   Úextra_conditionr&   rK   r   r   r   r   Úextra_condition2:  s    z,line_search_wolfe2.<locals>.extra_condition2)rF   ú*The line search algorithm did not converge)r.   r/   r!   r"   r   r   r   )r   rL   r   r   r0   r1   r2   r   r)   r*   r+   rN   rF   r   r4   rO   Ú
alpha_starÚphi_starÚderphi_starr   )r   r(   rN   r   r   r#   r$   r&   rK   rL   r   r   r   r   Á   s.    W
	þr   c
                 C   s  |du r| dƒ}|du r |dƒ}d}
|durL|dkrLt dd||  | ƒ}nd}|dk r\d}|durnt ||ƒ}| |ƒ}|}|}|du rŽdd„ }t|	ƒD �]P}|dks´|durî|
|krîd}|}|}d}|dkrÒd}nd	d
|  }t|tƒ  �q |dk}|||| |  k�s||k�rF|�rFt|
||||| ||||||ƒ\}}} �q ||ƒ}t|ƒ| | k�r€|||ƒ�r€|}|}|} �q |dk�r´t||
|||| ||||||ƒ\}}} �q d| }|du�rÐt ||ƒ}|}
|}|}| |ƒ}|}q–|}|}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.

    Nr6   r   r7   r8   c                 S   s   dS )NTr   )rJ   r   r   r   r   Ú<lambda>¤  ó    z&scalar_search_wolfe2.<locals>.<lambda>z7Rounding errors prevent the line search from convergingz4The line search algorithm could not find a solution zless than or equal to amax: %sr9   rP   )r>   rA   r   r   Ú_zoomÚabs)r   r(   rB   rC   r4   r)   r*   r+   rN   rF   Úalpha0rD   Úphi_a1Úphi_a0Z	derphi_a0rG   rQ   rR   rS   ÚmsgZnot_first_iterationZ	derphi_a1Úalpha2r   r   r   r   R  sŠ    9
ÿ
ÿÿ
þÿ

þÿ


r   c              
   C   sL  t jdddd��� zà|}||  }||  }	||	 d ||	  }
t  d¡}|	d |d< |d  |d< |	d  |d< |d |d	< t  |t  || ||  || ||	  g¡ ¡ ¡\}}||
 }||
 }|| d| |  }| | t  |¡ d|   }W n" t�y   Y W d
  ƒ d
S 0 W d
  ƒ n1 �s.0    Y  t  |¡�sHd
S |S )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©ÚdivideZoverÚinvalidr9   )r9   r9   )r   r   )r   r   é   )r   r   )r   r   N)	r!   ÚerrstateÚemptyr"   ZasarrayÚflattenÚsqrtÚArithmeticErrorÚisfinite)ÚaÚfaÚfpaÚbÚfbÚcr   ÚCÚdbÚdcZdenomÚd1ÚAÚBÚradicalÚxminr   r   r   Ú	_cubicminã  s.    

ÿ 4rv   c           
   	   C   s¤   t jdddd��r z@|}|}|| d  }|| ||  ||  }| |d|   }	W n  tyr   Y W d  ƒ dS 0 W d  ƒ n1 sˆ0    Y  t  |	¡s dS |	S )z†
    Finds the minimizer for a quadratic polynomial that goes through
    the points (a,fa), (b,fb) with derivative at a of fpa.

    r]   r^   r7   ç       @N)r!   rb   rf   rg   )
rh   ri   rj   rk   rl   ÚDrn   ro   rs   ru   r   r   r   Ú_quadmin  s    2
ry   c                 C   s¢  d}d}d}d}|}d}||  }|dk r4||  }}n
| | }}|dkrb|| }t | ||||||ƒ}|dksŠ|du sŠ||| ksŠ||| k rÎ|| }t| ||||ƒ}|du sÂ||| ksÂ||| k rÎ| d|  }||ƒ}|||	| |  ksô||k�r|}|}|}|}np||ƒ}t|ƒ|
 | k�r>|||ƒ�r>|}|}|}�q˜|||   dk�rb|}|}| }|}n|}| }|} |}|}|d7 }||krd}d}d}�q˜q|||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.

    rH   r   gš™™™™™É?çš™™™™™¹?Nç      à?r   )rv   ry   rW   )Za_loZa_hiZphi_loZphi_hiZ	derphi_lor   r(   rB   r4   r)   r*   rN   rF   rG   Zdelta1Zdelta2Zphi_recZa_recZdalpharh   rk   ZcchkZa_jZqchkZphi_ajZ	derphi_ajZa_starZval_starZvalprime_starr   r   r   rV     sb    	

ÿ(  rV   r   c                    sj   t  ˆ¡‰dg‰‡ ‡‡‡‡fdd„}|du r6|dƒ}	n|}	t  |ˆ¡}
t||	|
||d�\}}|ˆ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                    s(   ˆd  d7  < ˆˆ| ˆ  gˆ ¢R Ž S r   r   )rD   r   r   r   r   š  s    zline_search_armijo.<locals>.phiNr6   )r)   rX   )r!   Z
atleast_1dr"   Úscalar_search_armijo)r   r   r   r0   r1   r   r)   rX   r   rB   r4   rJ   rE   r   r   r   r	   u  s    "


ÿ
r	   c           	   
   C   s0   t | |||||||d�}|d |d d|d fS )z8
    Compatibility wrapper for `line_search_armijo`
    )r   r)   rX   r   r   r9   )r	   )	r   r   r   r0   r1   r   r)   rX   Úrr   r   r   Úline_search_BFGS©  s    ÿr~   c                 C   s–  | |ƒ}|||| |  kr$||fS | |d  d || ||   }| |ƒ}|||| |  krj||fS ||k�rŽ|d |d  ||  }	|d || ||   |d || ||    }
|
|	 }
|d  || ||   |d || ||    }||	 }| t  t|d d|
 |  ƒ¡ d|
  }| |ƒ}|||| |  k�rP||fS || |d k�std||  dk �r||d }|}|}|}|}qjd|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

    r9   rw   ra   g      @r   g¸…ëQ¸î?N)r!   re   rW   )r   rB   r4   r)   rX   r,   rZ   rD   rY   Zfactorrh   rk   r\   Zphi_a2r   r   r   r|   ²  s8    "
ÿÿ,$r|   rz   r{   c                 C   s  |d }t |ƒ}	d}
d}d}||
|  }| |ƒ\}}||	| ||
d  |  krX|
}�q|
d | |d|
 d |   }|||  }| |ƒ\}}||	| ||d  |  kr¶| }�q|d | |d| d |   }t |||
 ||
 ¡}
t ||| || ¡}q||||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   r9   )Úmaxr!   Úclip)r   Úx_kÚdZprev_fsÚetaÚgammaÚtau_minÚtau_maxÚf_kZf_barÚalpha_pÚalpha_mrJ   ÚxpÚfpÚFpÚalpha_tpÚalpha_tmr   r   r   Ú_nonmonotone_line_search_cruzð  s(    (  r�   ç333333ë?c                 C   s(  d}d}d}|||  }| |ƒ\}}||| ||d  |  krF|}qð|d | |d| d |   }|||  }| |ƒ\}}||| ||d  |  kr¢| }qð|d | |d| d |   }t  ||| |	| ¡}t  ||| |	| ¡}q|
| d }|
| ||  | | }|}||||||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   r9   )r!   r�   )r   r‚   rƒ   rˆ   rn   ÚQr„   r…   r†   r‡   Únur‰   rŠ   rJ   r‹   rŒ   r�   rŽ   r�   ZQ_nextr   r   r   Ú_nonmonotone_line_search_cheng8  s*    /  r”   )	NNNr   r   r   r   r   r   )NNNr   r   r   r   r   )	NNNr   r   r   NNrH   )NNNr   r   NNrH   )r   r   r   )r   r   r   )r   r   r   )r   rz   r{   )r   rz   r{   r‘   )Ú__doc__Úwarningsr   Zscipy.optimizer   Únumpyr!   Ú__all__ÚRuntimeWarningr   r   r   Zline_searchr   r   rv   ry   rV   r	   r~   r|   r�   r”   r   r   r   r   Ú<module>   sJ       ý
H   þ
S   þ
     ý
 "[
4
	
? ÿ
I  þ