exact_linesearch Subroutine

private subroutine exact_linesearch(me, xold, p, x, f, fvec, fjac, fjac_sparse)

An exact linesearch that uses a derivative-free minimizer to find the minimum value of f(x) between x = xold + p * alpha_min and x = xold + p * alpha_max.

Usually this is overkill and not necessary, but is here as an option for testing.

Arguments

Type IntentOptional Attributes Name
class(nlesolver_type), intent(inout) :: me
real(kind=wp), intent(in), dimension(me%n) :: xold

previous value of x

real(kind=wp), intent(in), dimension(me%n) :: p

search direction

real(kind=wp), intent(out), dimension(me%n) :: x

new x

real(kind=wp), intent(inout) :: f

magnitude of fvec

real(kind=wp), intent(inout), dimension(me%m) :: fvec

function vector

real(kind=wp), intent(in), optional, dimension(:,:) :: fjac

jacobian matrix [dense]

real(kind=wp), intent(in), optional, dimension(:) :: fjac_sparse

jacobian matrix [sparse]


Calls

proc~~exact_linesearch~~CallsGraph proc~exact_linesearch exact_linesearch fmin fmin proc~exact_linesearch->fmin proc~adjust_search_direction nlesolver_type%adjust_search_direction proc~exact_linesearch->proc~adjust_search_direction proc~compute_next_step nlesolver_type%compute_next_step proc~exact_linesearch->proc~compute_next_step proc~int2str int2str proc~adjust_search_direction->proc~int2str proc~set_status nlesolver_type%set_status proc~adjust_search_direction->proc~set_status proc~set_status->proc~int2str proc~real2str real2str proc~set_status->proc~real2str

Source Code

    subroutine exact_linesearch(me,xold,p,x,f,fvec,fjac,fjac_sparse)

    implicit none

    class(nlesolver_type),intent(inout) :: me
    real(wp),dimension(me%n),intent(in) :: xold      !! previous value of `x`
    real(wp),dimension(me%n),intent(in) :: p         !! search direction
    real(wp),dimension(me%n),intent(out) :: x        !! new `x`
    real(wp),intent(inout) :: f                      !! magnitude of `fvec`
    real(wp),dimension(me%m),intent(inout) :: fvec   !! function vector
    real(wp),dimension(:,:),intent(in),optional :: fjac !! jacobian matrix [dense]
    real(wp),dimension(:),intent(in),optional :: fjac_sparse !! jacobian matrix [sparse]

    real(wp),dimension(:),allocatable :: xnew      !! trial `x` (used in [[func_for_fmin]])
    real(wp),dimension(:),allocatable :: fvecnew   !! trial `fvec` (used in [[func_for_fmin]])
    real(wp),dimension(:),allocatable :: search_direction !! search direction to use (may be modified from `p` if bounds are violated)
    logical,dimension(:),allocatable :: modified  !! indicates the elements of p that were modified
    real(wp) :: alpha      !! step length returned by [[fmin]]
    real(wp) :: phi        !! value returned by [[func_for_fmin]]
    real(wp) :: phi_best   !! best (smallest) value of `norm(fvec)**2` found so far
    real(wp) :: alpha_best !! `alpha` corresponding to `phi_best`

    allocate(xnew(me%n))
    allocate(fvecnew(me%m))
    allocate(search_direction(me%n))
    allocate(modified(me%n))

    ! set the search direction (this must be done before the line search):
    call me%adjust_search_direction(xold,p,me%alpha_max,search_direction,modified)

    ! the full step is evaluated explicitly, since fmin never evaluates
    ! the endpoints of the interval, and near the solution the full
    ! Newton step is usually the minimizer.
    phi_best = huge(one)
    phi = func_for_fmin(me%alpha_max)

    ! find the minimum value of f in the range of alphas.
    ! Note: the function minimized is norm(fvec)**2, which has the same minimizer as
    ! norm(fvec), but (for the 2-norm) is smooth at a zero, which is much better for the
    ! parabolic interpolation used by fmin. The best point is saved in func_for_fmin.
    alpha = fmin(func_for_fmin,me%alpha_min,me%alpha_max,me%fmin_tol)

    if (me%verbose) write(me%iunit,'(1P,*(A,1X,E16.6))') '        alpha_min = ', alpha_best

    ! x, fvec, and f were saved for the best point evaluated:
    f = sqrt(phi_best)

contains

    real(wp) function func_for_fmin(alpha)
    !! function for [[fmin]]: `norm(fvec)**2` at `xold + alpha*search_direction`.
    !! Also saves the best point evaluated so far in `x` and `fvec`.
    implicit none
    real(wp),intent(in) :: alpha !! indep variable

    call me%compute_next_step(xold, search_direction, alpha, me%alpha_max, modified, xnew)
    call me%func(xnew,fvecnew)
    func_for_fmin = me%norm(fvecnew)**2

    if (func_for_fmin < phi_best .or. phi_best == huge(one)) then  ! (always save the first one)
        phi_best   = func_for_fmin
        alpha_best = alpha
        x          = xnew
        fvec       = fvecnew
    end if

    end function func_for_fmin

    end subroutine exact_linesearch