
    x-jo0                     6    d dl Z ddlmZ d Z	 	 	 	 	 	 	 ddZdS )    N   )_value_and_gradientc                 6   	
 t           j        j                             k     fd fd          \  z   d||z
  z   z
  z  z
  		dz  z  z
  
	
 fd}fd}t           j        j                            
dk    ||          }|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 minimum 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.
    Returns:
        min_pos: the minimum point between the specified points in the cubic curve.
    c                       fS N x1x2s   p/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/incubate/optimizer/functional/line_search.py<lambda>z&cubic_interpolation_.<locals>.<lambda>$   s    2r(     c                       fS r   r   r	   s   r   r   z&cubic_interpolation_.<locals>.<lambda>$   s    RH r         c                                                      	
fd} 	
fd}t          j        	
          }t          j        j                            || |          }t          j        t          j        |                    S )Nc                  >    z
  z    z
  z
  dz  z   z  z  z
  S Nr   r   d1d2g1g2r
   r   s   r   true_fn2z:cubic_interpolation_.<locals>.true_func1.<locals>.true_fn2,   1    bb2glrBwR7G%HIIIr   c                  >    z
  z    z
  z
  dz  z   z  z  z
  S r   r   r   s   r   	false_fn2z;cubic_interpolation_.<locals>.true_func1.<locals>.false_fn2/   r   r   )xy)sqrtpaddle
less_equalstaticnncondminimummaximum)r   r   predmin_posr   r   	d2_squarer   r   r
   r   xmaxxmins       @r   
true_func1z(cubic_interpolation_.<locals>.true_func1)   s    ^^	J 	J 	J 	J 	J 	J 	J 	J 	J 	J	J 	J 	J 	J 	J 	J 	J 	J 	J 	J  2,,,-"''h	BB~fnWd;;TBBBr   c                       z   dz  S )Ng       @r   )r+   r,   s   r   false_func1z)cubic_interpolation_.<locals>.false_func17   s    ts""r           )r!   r#   r$   r%   )r
   f1r   r   f2r   r-   r/   r)   r   r*   r+   r,   s   ` `` `   @@@@r   cubic_interpolation_r3      s    !&&
b"""""$4$4$4$4$4 JD$ 
b1R=BG,	,BARIC C C C C C C C C C C C# # # # # # m##I$4j+NNGNr      :0yE>      ?-C6??
   float32c
                      fdfdt          j        dg|	          t          j        dgd|	          }
t          j        dg||	          } |
          \  }}t          j        |          t          j                  t          j        dgdd          }t          j        dgd|	          t          j        |          t          j        |          t          j        dgdd          }t          j        dgdd	          }fd
}fd}t           j        j                            |||||
||||g           |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:
        f: the objective function to minimize. ``f`` accepts a multivariate input and returns a scalar.
        xk (Tensor): the starting point of the iterates.
        pk (Tensor): search direction.
        max_iters (Scalar): the maximum number of iterations.
        tolerance_grad (Scalar): terminates if the gradient norm is smaller than
            this. Currently gradient norm uses inf norm.
        tolerance_change (Scalar): terminates if the change of function value/position/parameter between
            two iterations is smaller than this value.
        initial_step_length (Scalar): step length used in first iteration.
        c1 (Scalar): parameter for sufficient decrease condition.
        c2 (Scalar): parameter for curvature condition.
        alpha_max (float): max step length.
        dtype ('float32' | 'float64'): the datatype to be used.

    Returns:
        num_func_calls (float): number of objective function called in line search process.
        a_star(Tensor): optimal step length, or 0. if the line search algorithm did not converge.
        phi_star (Tensor): phi at a_star.
        derphi_star (Tensor): derivative of phi at a_star.

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

        - `f` denotes the objective function.
        - `phi` is a function of step size alpha, restricting `f` on a line.

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

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

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

            3. If phi'(a2) >= 0,
                a_star= 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 phi(aj) and derphi(aj).
            1. If phi(aj) > phi(0) + c_1 * aj * phi'(0) or phi(aj) >= phi(a_lo),
                then a_hi <- aj;
            2.
                2.1. If |phi'(aj)| <= -c_2 * phi'(0), then a_star= a2 and stop;

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

                a_lo = aj;
        end(repeat)
    c                 j    t          | z  z             \  }}t          j        |          }|||fS )zCompute function value and derivative of phi at a.
        phi = f(xk + a * pk)
        phi'(a) = f'(xk + a * pk) * pk
        )r   r!   dot)a	phi_valuef_gradphi_gradfpkxks       r   phi_and_derphiz$strong_wolfe.<locals>.phi_and_derphi   s?    
 02B;??	6:fb))&(**r   c	                     t          j        dgdd          }	t          j        dgdd          }
fd}fd}t           j        j                            |||	|
| ||||||g		           |	S )
Nr   r   int64shape
fill_valuedtypeFboolc	                 ~    t          j        ||z
            k     }	t          j        ||	z  |           | 
k     | z  S r   )r!   absassign)j	done_zooma_lophi_lo	derphi_loderf_loa_hiphi_hi	derphi_hir(   max_zoom_iterstolerance_changes             r   	cond_zoomz-strong_wolfe.<locals>.zoom.<locals>.cond_zoom   sG     :dTk**-==DM)d*I666&9*44r   c	                     t                    dt          j        z
            z  }	t          j        t          j        z
            t          j        z
                      |	k     }
t          j        j                            |
fdfd                     \  fd}fdz  z  z   k    k    z  }t          j        j                            ||fd           t          j        j                             fd fd            g	S )	Ng?c                      d z   z  S )Ng      ?r   )rV   rR   s   r   r   z?strong_wolfe.<locals>.zoom.<locals>.body_zoom.<locals>.<lambda>   s    cTD[1 r   c                       S r   r   )ajs   r   r   z?strong_wolfe.<locals>.zoom.<locals>.body_zoom.<locals>.<lambda>   s    2 r   c                      t          j                    t          j                   t          j                   d S r   r!   rO   )rV   r_   rX   derphi_jrW   phi_js   r   true_fnz>strong_wolfe.<locals>.zoom.<locals>.body_zoom.<locals>.true_fn   s?    b$'''eV,,,h	22222r   c                     t          j                   
z  k    }t          j        ||            fd}|  z
  z  dk    z  }t           j        j                            ||d            t          j                    t          j                   t          j                   t          j        	           d S )Nc                      t          j                    t          j                   t          j                   d S r   ra   )rV   rR   rX   rT   rW   rS   s   r   rd   zPstrong_wolfe.<locals>.zoom.<locals>.body_zoom.<locals>.false_fn.<locals>.true_fn   s?    M$---M&&111M)Y77777r   r   )r!   rN   rO   r#   r$   r%   )rR   rQ   pred3rd   pred4rV   r_   c2derf_jrU   derphi_0rX   rb   rT   rW   rc   rS   s   `    r   false_fnz?strong_wolfe.<locals>.zoom.<locals>.body_zoom.<locals>.false_fn   s    
8,,h>eY///8 8 8 8 8 8 8 8 8 8
 #
h$+&>!&CD %%eWd;;;b$'''eV,,,h	222fg.....r   c                                  S r   r   )rR   rQ   rl   s   r   r   z?strong_wolfe.<locals>.zoom.<locals>.body_zoom.<locals>.<lambda>   s    y(A(A r   c                       S r   r   rP   s   r   r   z?strong_wolfe.<locals>.zoom.<locals>.body_zoom.<locals>.<lambda>   s     r   c                       dz   S )Nr   r   ro   s   r   r   z?strong_wolfe.<locals>.zoom.<locals>.body_zoom.<locals>.<lambda>   s    AE r   )r3   r!   rN   r&   r#   r$   r%   )rP   rQ   rR   rS   rT   rU   rV   rW   rX   
min_changer(   rd   pred2r_   rj   rb   rl   rc   c1ri   rk   phi_0rE   s   `````````    @@@@@r   	body_zoomz-strong_wolfe.<locals>.zoom.<locals>.body_zoom   s    &fivy B vz$+666Jvz"t)44fjd6K6KLL  !&&11111:::: B '5nR&8&8#E683 3 3 3 3 3 3 3 3 3/ / / / / / / / / / / / / / / /" UR"Wx%777EVOLEM!!w A A A A A A    %%iMMMMJJA
 
r   r%   body	loop_vars)r!   fullr#   r$   
while_loop)rR   rS   rT   rU   rV   rW   rX   rt   rk   rP   rQ   r[   ru   rY   rs   ri   	max_itersrE   rZ   s          ``    @r   zoomzstrong_wolfe.<locals>.zoom   s     #Kqcaw???Kqce6JJJ		5 	5 	5 	5 	5 	5?	 ?	 ?	 ?	 ?	 ?	 ?	 ?	 ?	B 	##
 	$ 	
 	
 	
  r   r   rH   r0   rG   r   FrL   c                     | k     | z  S r   r   )ils_func_callsa1a2phi_1derf_1doner{   s          r   r%   zstrong_wolfe.<locals>.cond#  s    I$&&r   c                                \  t          j        dz              t          j        |t          j        t          j                            z  |           fd}| z  z  z   k    k     dk    z  z  z  }t          j        ||z  |           t           j        j                            ||d            fd}	| t          j                   z  k    z  }
t          j        ||
z  |           t           j        j                            |
|	d            fd}| dk    z  }t          j        ||z  |           t           j        j                            ||d             fd}t           j        j                            |d |            |gS )Nr   c                       
	  	        } t          j                   t          j                   t          j                   t          j        	| z   	           d S r   ra   )rP   r   r   a_starr   	derf_starrk   derphi_1derphi_2r   rt   r   phi_2phi_starr|   s    r   true_fn1z,strong_wolfe.<locals>.body.<locals>.true_fn1+      
 
A M"f%%%M%***M&),,,M-!+];;;;;r   c                      t          j                    t          j                   t          j                   d S r   ra   )r   r   derf_2r   r   r   s   r   r   z,strong_wolfe.<locals>.body.<locals>.true_fn2B  s?    M"f%%%M%***M&),,,,,r   c                       
	  	        } t          j                   t          j                   t          j                   t          j        	| z   	           d S r   ra   )rP   r   r   r   r   r   rk   r   r   r   rt   r   r   r   r|   s    r   true_fn3z,strong_wolfe.<locals>.body.<locals>.true_fn3K  r   r   r   c                     t          j                    t          j                   t          j                   t          j        t          j        dz                       t          j        dz              d S )Nr   r   )r!   rO   r&   )r   r   	alpha_maxr   r   r~   r   r   s   r   rl   z,strong_wolfe.<locals>.body.<locals>.false_fn`  sw    M"b!!!M%'''M&&)))M&.R;;R@@@M!a%#####r   )r!   rO   anyisinfr#   r$   r%   rN   )r~   r   r   r   r   r   r   r   pred1r   rr   r   rg   rl   r   r   r   r   r   rs   ri   r   rk   r   rt   rE   r   r|   s   ``````        @@@r   rw   zstrong_wolfe.<locals>.body&  s   "0."4"4vxma'777dVZU(;(;<<<dCCC	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<" UR"Wx///Ue^A4NO
 	dUlD)))eXt444	- 	- 	- 	- 	- 	- 	- 	- 	- 	-
 H--"x?@dUlD)))eXt444	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<" Q'dUlD)))eXt444	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	dD(333="b%>>r   rv   )r!   ry   rO   r#   r$   rz   )rB   rD   rC   r{   rZ   initial_step_lengthrs   ri   r   rK   r   r   r   r   r   r~   r   r%   rw   r   r   rk   r   rt   rE   r   r|   s   ````` ```          @@@@@@@@r   strong_wolfer   >   s-   l+ + + + + + +p p p p p p p p pd 1#)5IIII	A33e	<	<	<B	A3+>e	L	L	LB,nR00E68M%  E}X&&HKqcawGGGM [sq>>>F}U##Hf%%I1#!7;;;A;aSU&AAAD' ' ' ' 'B? B? B? B? B? B? B? B? B? B? B? B? B? B? B?H MmRUFDA      8Y55r   )r4   r5   r6   r7   r8   r9   r:   )r!   utilsr   r3   r   r   r   r   <module>r      sm     & & & & & &' ' '\ 

r6 r6 r6 r6 r6 r6r   