o
    "j)                     @   s(   d dl Z d
ddZ				ddd	ZdS )    Nc                 C   s   |dur	|\}}n| |kr| |fn|| f\}}|| d||  | |   }	|	d ||  }
|
dkrj|
  }| |krN|||  || |	 || d|     }n| | | || |	 || d|     }tt|||S || d S )a]  Cubic interpolation between (x1, f1, g1) and (x2, f2, g2).
        Use two points and their gradient to determine a cubic function and get the minimun point
        between them in the cubic curve.

    Reference:
        Jorge Nocedal, Stephen J. Wright, Numerical Optimization, Second Edition, 2006.
        pp59: formula 3.59

    Args:
        x1, f1, g1: point1's position, value and gradient.
        x2, f2, g2: point2's position, value and gradient.
        bounds: bounds of interpolation area

    Returns:
        min_pos: the minimun point between the specified points in the cubic curve.
    N      r   g       @)sqrtminmax)x1f1g1Zx2f2g2boundsZ
xmin_boundZ
xmax_boundZd1Z	d2_squareZd2Zmin_pos r   n/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/incubate/optimizer/line_search_dygraph.py_cubic_interpolate   s   
*(r   -C6??&.>   c           !   	   C   s  |   }| }| |||\}}d}t||}tjd|jd|||f\}}}}d}d}||
k r|||| |  ksD|dkrW||krW||g}||g}|| g}||g}nkt || | krm|g}|g}|g}d}nU|dkr||g}||g}|| g}||g}n>|d||   }|d }|}t||||||||fd}|}|}| }|}| |||\}}|d7 }||}|d7 }||
k s2||
krd|g}||g}||g}d}|d |d	 krd
nd\}}|s||
k rt |d |d  | |	k rnt|d |d |d |d |d |d }dt|t|  } tt|| |t| | k r`|s:|t|ks:|t|kr]t |t| t |t| k rTt||  }nt||  }d}nd}nd}| |||\}}|d7 }||}|d7 }|||| |  ks||| kr|||< |||< | ||< |||< |d |d krd
nd\}}nEt || | krd}n%||| ||   dkr|| ||< || ||< || ||< || ||< |||< |||< | ||< |||< |s||
k s|| }|| }|| }||||fS )a4  Implements of line search algorithm that satisfies the strong Wolfe conditions using double zoom.

    Reference:
        Jorge Nocedal, Stephen J. Wright, Numerical Optimization, Second Edition, 2006.
        pp60: Algorithm 3.5 (Line Search Algorithm).

    Args:
        obj_func: the objective function to minimize. ```` accepts a multivariate input and returns a scalar.
        xk (Tensor): the starting point of the iterates.
        alpha (Scalar): the initial step size.
        d (Tensor): search direction.
        loss (scalar): the initial loss
        grad (Tensor): the initial grad
        c1 (Scalar): parameter for sufficient decrease condition.
        c2 (Scalar): parameter for curvature condition.
        tolerance_change (Scalar): terminates if the change of function value/position/parameter between
            two iterations is smaller than this value.
        max_ls(int): max iteration of line search.
        alpha_max (float): max step length.

    Returns:
        loss_new (Scaler): loss of obj_func at final alpha.
        grad_new, (Tensor): derivative of obj_func at final alpha.
        alpha(Tensor): optimal step length, or 0. if the line search algorithm did not converge.
        ls_func_evals (Scaler): number of objective function called in line search process.

    Following summarizes the essentials of the strong Wolfe line search algorithm.
    Some notations used in the description:

        - `func` denotes the objective function.
        - `obi_func` is a function of step size alpha, restricting `obj_func` on a line.

            obi_func = func(xk + alpha * d),
            where xk is the position of k'th iterate, d is the line search direction(decent direction),
            and a is the step size.
        - alpha : substitute of alpha
        - a1 is alpha of last iteration, which is alpha_(i-1).
        - a2 is alpha of current iteration, which is alpha_i.
        - a_lo is alpha in left position when calls zoom, which is alpha_low.
        - a_hi is alpha in right position when calls zoom, which is alpha_high.

    Line Search Algorithm:
        repeat
            Compute obi_func(a2) and derphi(a2).
            1. If obi_func(a2) > obi_func(0) + c_1 * a2 * obi_func'(0) or [obi_func(a2) >= obi_func(a1) and i > 1],
                alpha= zoom(a1, a2) and stop;

            2. If |obi_func'(a2)| <= -c_2 * obi_func'(0),
                alpha= a2 and stop;

            3. If obi_func'(a2) >= 0,
                alpha= zoom(a2, a1) and stop;

            a1 = a2
            a2 = min(2 * a2, a2)
            i = i + 1
        end(repeat)

    zoom(a_lo, a_hi) Algorithm:
        repeat
            aj = cubic_interpolation(a_lo, a_hi)
            Compute obi_func(aj) and derphi(aj).
            1. If obi_func(aj) > obi_func(0) + c_1 * aj * obi_func'(0) or obi_func(aj) >= obi_func(a_lo),
                then a_hi <- aj;
            2.
                2.1. If |obi_func'(aj)| <= -c_2 * obi_func'(0), then alpha= a2 and stop;

                2.2. If obi_func'(aj) * (a2 - a1) >= 0, then a_hi = a_lo

                a_lo = aj;
        end(repeat)
       r   )dtypeFTg{Gz?
   )r   )r   r   )r   r   g?)	absr   clonepaddledotZ	to_tensorr   r   r   )!Zobj_funcZxkalphadZlossZgradZgtdc1c2Ztolerance_changeZmax_lsZd_normZloss_newZgrad_newZls_func_evalsZgtd_newZt_prevZf_prevZg_prevZgtd_prevdoneZls_iterbracketZ	bracket_fZ	bracket_gZbracket_gtdZmin_stepZmax_steptmpZinsuf_progressZlow_posZhigh_posepsr   r   r   _strong_wolfe6   s   V
3 "

Lr$   )N)r   r   r   r   )r   r   r$   r   r   r   r   <module>   s   
,