# NLopt for ODE parameter estimation: how to get verbose printed while using build\_loss\_objective

**URL:** <https://discourse.julialang.org/t/nlopt-for-ode-parameter-estimation-how-to-get-verbose-printed-while-using-build-loss-objective/114491>\
**Category:** Optimization (Mathematical)\
**Created:** [May 21, 2024, 5:59am UTC](https://discourse.julialang.org/t/nlopt-for-ode-parameter-estimation-how-to-get-verbose-printed-while-using-build-loss-objective/114491 "2024-05-21T05:59:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Piyush](https://avatars.discourse-cdn.com/v4/letter/p/ecb155/32.png) [@Piyush](https://discourse.julialang.org/u/Piyush)\
**Post date:** [May 21, 2024, 5:59am UTC](https://discourse.julialang.org/t/nlopt-for-ode-parameter-estimation-how-to-get-verbose-printed-while-using-build-loss-objective/114491/1 "2024-05-21T05:59:46Z")

</div>

Hi,

Is there a way to print the function value while using any NLopt algorithm for parameter estimation while using build\_loss\_objective function to build the cost function?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [May 21, 2024, 6:58am UTC](https://discourse.julialang.org/t/nlopt-for-ode-parameter-estimation-how-to-get-verbose-printed-while-using-build-loss-objective/114491/2 "2024-05-21T06:58:17Z")

</div>

Please consider adding more context to your post, such as

- What packages are you making use of?
- Where does `build_loss_objective` come from?
- Are you using NLopt directly or through an interface package?

---

<div class="post-metadata">

**Author:** ![Piyush](https://avatars.discourse-cdn.com/v4/letter/p/ecb155/32.png) [@Piyush](https://discourse.julialang.org/u/Piyush)\
**Post date:** [May 21, 2024, 8:19am UTC](https://discourse.julialang.org/t/nlopt-for-ode-parameter-estimation-how-to-get-verbose-printed-while-using-build-loss-objective/114491/3 "2024-05-21T08:19:51Z")

</div>

```julia
using DifferentialEquations, Plots, DiffEqParamEstim, Optimization, OptimizationMOI,
      OptimizationNLopt, NLopt

function f(du, u, p, t)
    du[1] = p[1] * u[1] - u[1] * u[2]
    du[2] = -3 * u[2] + u[1] * u[2]
end

u0 = [1.0; 1.0]
tspan = (0.0, 10.0)
p = [1.5]
prob = ODEProblem(f, u0, tspan, p)
sol = solve(prob, Tsit5())

t = collect(range(0, stop = 10, length = 200))
randomized = VectorOfArray([(sol(t[i]) + 0.01randn(2)) for i in 1:length(t)])
data = convert(Array, randomized)

obj = build_loss_objective(prob, Tsit5(), L2Loss(t, data), Optimization.AutoForwardDiff())

opt = Opt(:LN_COBYLA, 1)
optprob = Optimization.OptimizationProblem(obj, [1.3])
res = solve(optprob, opt)

```

Thank you for your response @baggepinnen. I am following the example given here: [Global Optimization via NLopt · DiffEqParamEstim.jl](https://docs.sciml.ai/DiffEqParamEstim/stable/tutorials/global_optimization/)

I would like to see the verbose for the optimizer that is value of gradient and the objective function value

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 21, 2024, 12:18pm UTC](https://discourse.julialang.org/t/nlopt-for-ode-parameter-estimation-how-to-get-verbose-printed-while-using-build-loss-objective/114491/4 "2024-05-21T12:18:21Z")

</div>

Add a callback to the optimization. They are defined in the docstring:

> **[Common Solver Options (Solve Keyword Arguments) · Optimization.jl](https://docs.sciml.ai/Optimization/stable/API/solve/)**
>
> Documentation for Optimization.jl.

For example:

```julia
callback = function (state, l) #callback function to observe training
    display(l)
    return false
end

```

and then add `callback = callback` to the kwargs of solve.

---

<div class="post-metadata">

**Author:** ![Piyush](https://avatars.discourse-cdn.com/v4/letter/p/ecb155/32.png) [@Piyush](https://discourse.julialang.org/u/Piyush)\
**Post date:** [May 22, 2024, 5:16am UTC](https://discourse.julialang.org/t/nlopt-for-ode-parameter-estimation-how-to-get-verbose-printed-while-using-build-loss-objective/114491/5 "2024-05-22T05:16:15Z")

</div>

Thank you so much for helping me out @ChrisRackauckas
