Skip to content

Commit

Permalink
add return from callback field in result struct
Browse files Browse the repository at this point in the history
  • Loading branch information
lmiq committed Jul 22, 2023
1 parent 97ad246 commit 3b4b779
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/SPGBoxResult.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ struct SPGBoxResult{T,F,G}
nit::Int64
nfeval::Int64
ierr::Int64
return_from_callback::Bool
end

function Base.show(io::IO, R::SPGBoxResult)
println("")
println(" SPGBOX RESULT: ")
println("")
R.ierr == 0 && println(" Convergence achieved. ")
R.ierr == 0 && println(" Convergence achieved (Return from callback: $return_from_callback). ")
R.ierr == 1 && println(" Maximum number of iterations (nitmax) reached.")
R.ierr == 2 && println(" Maximum number of function evaluations (nfevalmax) reached.")
println("")
Expand Down
12 changes: 6 additions & 6 deletions src/spgbox_main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function spgbox!(
nfeval = 1
fcurrent = fg!(g, x)
gnorm = pr_gradnorm(g, x, lower, upper)
gnorm <= eps && return SPGBoxResult(x, fcurrent, gnorm, nit, nfeval, 0)
gnorm <= eps && return SPGBoxResult(x, fcurrent, gnorm, nit, nfeval, 0, false)

# Do a consertive initial step
small = T(sqrt(Base.eps(T)))
Expand Down Expand Up @@ -236,7 +236,7 @@ function spgbox!(
safequad_ls(xn, gn, x, g, fcurrent, tspg, fref, nfevalmax - nfeval, iprint, func_only, fg!, lower, upper)

if lsfeval < 0
return SPGBoxResult(x, fcurrent, gnorm, nit, nfeval - lsfeval, 2)
return SPGBoxResult(x, fcurrent, gnorm, nit, nfeval - lsfeval, 2, false)
else
nfeval += lsfeval
end
Expand Down Expand Up @@ -268,17 +268,17 @@ function spgbox!(
gnorm = pr_gradnorm(g, x, lower, upper)

# Call callback function
return_from_callback = callback(SPGBoxResult(x, fcurrent, gnorm, nit, nfeval, 0))
return_from_callback = callback(SPGBoxResult(x, fcurrent, gnorm, nit, nfeval, 0, false))
if return_from_callback
return SPGBoxResult(x, fcurrent, gnorm, nit, nfeval, 0)
return SPGBoxResult(x, fcurrent, gnorm, nit, nfeval, 0, true)
end

# Check convergence
gnorm <= eps && return SPGBoxResult(x, fcurrent, gnorm, nit, nfeval, 0)
gnorm <= eps && return SPGBoxResult(x, fcurrent, gnorm, nit, nfeval, 0, false)
end

# Maximum number of iterations achieved
return SPGBoxResult(x, fcurrent, gnorm, nit, nfeval, 1)
return SPGBoxResult(x, fcurrent, gnorm, nit, nfeval, 1, false)
end

"Perform a safeguarded quadratic line search"
Expand Down

0 comments on commit 3b4b779

Please sign in to comment.