Skip to content

Should the state be updated in solver_step! or solve! directly? #129

@benedict-96

Description

@benedict-96

The NonlinearSolverStatus was made immutable in #109.

With commit 85d665a we now have:

function solve!(x::AbstractVector, state::OptimizerState, opt::Optimizer)
initialize_state!(state)
while true
increase_iteration_number!(state)
solver_step!(x, state, opt)
status = OptimizerStatus(state, cache(opt), value(problem(opt), x); config = config(opt))
meets_stopping_criteria(status, opt, state) && break
update!(state, gradient(opt), x)
end
warn_iteration_number(state, config(opt))
status = OptimizerStatus(state, cache(opt), value(problem(opt), x); config = config(opt))
OptimizerResult(status, x, value(problem(opt), x))
end

for the optimizer and
function solve!(x::AbstractArray, s::NonlinearSolver, state::NonlinearSolverState, params=NullParameters())
initialize!(s, x)
while true
increase_iteration_number!(state)
solver_step!(x, s, state, params)
# update!(cache(s), state, x, nonlinearproblem(s), params) # this should not be necessary!
status = NonlinearSolverStatus(state, cache(s), config(s))
meets_stopping_criteria(status, iteration_number(state), config(s)) && break
end
status = NonlinearSolverStatus(state, cache(s), config(s))
config(s).verbosity > 1 && print_status(status, iteration_number(state), config(s))
warn_iteration_number(iteration_number(state), config(s))
x
end

for the nonlinear solver. This seems to be largely uniform, but the NonlinearSolverState is updated in solver_step!, whereas the OptimizerState is updated in solve! directly (see above).

The solver step in 85d665a:

function solver_step!(x::AbstractVector{T}, s::NonlinearSolver{T}, state::NonlinearSolverState{T}, params) where {T}
direction!(s, x, params; state = state)
# The following loop checks if the RHS contains any NaNs.
# If so, the direction vector is reduced by a factor of LINESEARCH_NAN_FACTOR.
update!(state, x, value!(value(state), nonlinearproblem(s), x, params), iteration_number(state))
for _ in 1:LINESEARCH_NAN_MAX_ITERATIONS
solution(cache(s)) .= solution(state) .+ direction(cache(s))
value!(value(cache(s)), nonlinearproblem(s), solution(cache(s)), params)
if any(isnan, value(cache(s)))
(s.config.verbosity 2 && @warn "NaN detected in nonlinear solver. Reducing length of direction vector.")
direction(cache(s)) .*= T(LINESEARCH_NAN_FACTOR)
else
break
end
end
α = solve(linesearch_problem(s, state, params), linesearch(s))
compute_new_iterate!(x, α, direction(cache(s)))
x
end

Originally posted by @benedict-96 in #61

Metadata

Metadata

Assignees

No one assigned

    Labels

    optimizersLabeled issues concern the `Optimizer`; this will eventually be moved to `GeometricOptimizers`.solversThis issue concerns solvers (both linear and nonlinear).

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions