# Hessian with user-defined functions in MathOptNLSModel

I am trying to use user-defined functions with the `MathOptNLSModel` for solving nonlinear least squares problems. I use:

``````using JuMP
using NLPModelsJuMP
using JSOSolvers
model = Model()
g(x...) = 10*length(x) + sum(x[i].^2 .- 10*cos.(2π*x[i]) for i in 1:length(x))
register(model, :g, 5, g, autodiff = true)
x₀ = [1.0, 0.1, 0.2, -0.5, 1.0]
@variable(model, x[i=1:5], start = x₀[i])
@NLexpression(model, res[i in 1:5], g(x...))
nls = MathOptNLSModel(model, res, name = "NL")
``````

(This is a simpler example of a more complicated problem, so I’m not looking to simplify anything down here.) This last line `nls = MathOptNLSModel(model, res, name = "NL")` gives the following stacktrace:

``````julia> nls = MathOptNLSModel(model, res, name = "NL")
Stacktrace:
[1] error(s::String)
@ Base .\error.jl:33
[2] initialize(d::NLPEvaluator, requested_features::Vector{Symbol})
@ JuMP C:\Users\~\.julia\packages\JuMP\lnUbA\src\nlp.jl:427
[3] parser_nonlinear_expression(cmodel::Model, nvar::Int64, F::Vector{NonlinearExpression})
@ NLPModelsJuMP C:\Users\~\.julia\packages\NLPModelsJuMP\j5VnJ\src\utils.jl:371
[4] MathOptNLSModel(cmodel::Model, F::Vector{NonlinearExpression}; name::String)
@ NLPModelsJuMP C:\Users\~\.julia\packages\NLPModelsJuMP\j5VnJ\src\moi_nls_model.jl:29
``````

This shows that the model does not support Hessians. I found similar issues like this online when googling the error message, but none seem to work, e.g. Disable Hessians when not provided by JuMP by spockoyno · Pull Request #12 · jump-dev/Pavito.jl · GitHub and Unsupported feature Hess with user-defined functions using JuMP and Alpine, for this specific function.

How can I work around this issue while still supporting user-defined functions? Is there another interface I could use with my model and NLexpression `res` for solving this problem, if I can’t get around this error?

It seems to be the same error as in one of the links, so the problem is most likely with MathOptNLSModel - open an issue on github with them? They seem to check for hessians and hessians are not available:

As for solving the problem - I can only think about skipping the reformulation provided by NLPModelsJuMP and writing the sum of squares directly in JuMP. Something like:

``````@NLexpression(model, Summ, sum(res[i]*res[i] for i=1:5))
@NLobjective(model,Min,Summ)
set_optimizer(model,Ipopt) ### Add using Ipopt before that line - or any other solver that handles nonlinearities
optimize!(model)
``````
2 Likes

Thanks @blob .
I posted the issue on their GitHub Define MathOptNLSModel when Hessian is not available · Issue #94 · JuliaSmoothOptimizers/NLPModelsJuMP.jl · GitHub and the problem is now fixed.