Skip to content

Add ability to silence warnings for failure to converge #957

@miles-1

Description

@miles-1

Is your feature request related to a problem? Please describe.

The default behavior for solving an optimization problem (to give a warning if the solver fails to converge) is appropriate. However, in cases where this failure can be handled after solving, such a warning takes away control of terminal appearance (e.g., progress bars). I've run into this warning in OptimizationNLopt.jl:

# lines 298-300
if retcode == ReturnCode.Failure
    @warn "NLopt failed to converge: $(ret)"
end

Describe the solution you’d like

There should be a way to specifically silence the warning that appears when the optimization algorithm does not converge. Example implementation as a new argument to solve (in the context of differential equation solutions):

f((t,), _=nothing) = soln(t, idxs=1)
optf = OptimizationFunction(f, AutoForwardDiff())
optprob = OptimizationProblem(optf, [initial_guess], lb=[first(t_span)], ub=[last(t_span)])
opt = solve(optprob, NLopt.LD_LBFGS(), silence_failure_warning = true) # new keyword argument here
if opt.retcode != ReturnCode.Success
    # handle failure here
end

Describe alternatives you’ve considered

So far, I've found two solutions that work, but both silence all warnings instead of just the relevant one:

opt = redirect_stderr(devnull) do
    solve(optprob, NLopt.LD_LBFGS())
end
# this redirects everything going to stderr & can cause problems if used within a thread
using Logging

const quiet_logger = SimpleLogger(devnull, Logging.Error)

opt = with_logger(QuietLogger(Logging.Info)) do
    solve(optprob, NLopt.LD_LBFGS())
end
# silences all warnings, and also requires import of another package

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions