# How to print solver status and reason for termination for NLopt and Optimization.jl

**URL:** <https://discourse.julialang.org/t/how-to-print-solver-status-and-reason-for-termination-for-nlopt-and-optimization-jl/111308>\
**Category:** Optimization (Mathematical)\
**Tags:** optim, optimization, nlopt\
**Created:** [March 7, 2024, 4:07pm UTC](https://discourse.julialang.org/t/how-to-print-solver-status-and-reason-for-termination-for-nlopt-and-optimization-jl/111308 "2024-03-07T16:07:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![structural](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@structural](https://discourse.julialang.org/u/structural)\
**Post date:** [March 7, 2024, 4:07pm UTC](https://discourse.julialang.org/t/how-to-print-solver-status-and-reason-for-termination-for-nlopt-and-optimization-jl/111308/1 "2024-03-07T16:07:04Z")

</div>

I am searching for a minimum using `NLopt.LN_SBPLX`. The optimizer stops prematurely after ~100 iterations without throwing any errors or printing any messages. `display(sol)` simply returns the last parameter vector that was used by the solver. I know the termination is premature because the value of the objective function was decreasing immediately before the termination (I can tell because the function `moment_for_optimizer` prints the value of the objective function).

How can I display the solver status and reason for termination? Usually solvers give you messages such as `xtol was reached` etc. I am looking for something like that so I know which tolerance to increase if I want the solver to continue running.

Below is how I call the optimizer

```julia
# moment_for_optimizer(x) = somefunction(x)

g(p_vec, pp) = moment_for_optimizer(p_vec)
f = OptimizationFunction(g)
prob = Optimization.OptimizationProblem(f, param_vec_guess, 1)
sol = solve(prob, NLopt.LN_SBPLX())

display(sol)

```

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [March 7, 2024, 7:48pm UTC](https://discourse.julialang.org/t/how-to-print-solver-status-and-reason-for-termination-for-nlopt-and-optimization-jl/111308/2 "2024-03-07T19:48:18Z")

</div>

Here’s a code snippet that shows how you can do this.

```julia
    (objvalue, xopt, flag) = NLopt.optimize(opt, startval)
    return xopt, objvalue, flag
end

function fmincon(silent=false)
    if !silent
        println("with no arguments, fmincon() runs a simple example")
        println("type edit(fmincon, ()) to see the example code")
    end
    # return of objective should be real valued, thus the [1] to pull value out of 1-dim array
    obj = x -> x'x
    x = [2.0, 2.0]
    # sum of params should be 1
    R = [1.0 1.0]
    r = 1.0
    results = fmincon(obj, x, R, r)
    # check convergence
    results[3] == :FTOL_REACHED
end

```

NLopt.optimize returns a triplet, the last item of which is the convergence message. The complete function checks for convergence to the function tolerance, at the end. The NLopt documentation contains information on the other possible values of the flag.

fmincon is just a wrapper to NLopt’s LNCOBYLA, for student’s who are used to MATLAB.
