# Hessian with user-defined functions in MathOptNLSModel

**URL:** <https://discourse.julialang.org/t/hessian-with-user-defined-functions-in-mathoptnlsmodel/74672>\
**Category:** Optimization (Mathematical)\
**Tags:** package, jump, optimization\
**Created:** [January 15, 2022, 3:41pm UTC](https://discourse.julialang.org/t/hessian-with-user-defined-functions-in-mathoptnlsmodel/74672 "2022-01-15T15:41:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [January 15, 2022, 3:41pm UTC](https://discourse.julialang.org/t/hessian-with-user-defined-functions-in-mathoptnlsmodel/74672/1 "2022-01-15T15:41:56Z")

</div>

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

```julia
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
julia> nls = MathOptNLSModel(model, res, name = "NL")
ERROR: LoadError: Unsupported feature Hess
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. [https://github.com/jump-dev/Pavito.jl/pull/12](https://github.com/jump-dev/Pavito.jl/pull/12) and [Unsupported feature Hess with user-defined functions using JuMP and Alpine](https://discourse.julialang.org/t/unsupported-feature-hess-with-user-defined-functions-using-jump-and-alpine/70204), 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?

---

<div class="post-metadata">

**Author:** ![blob](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@blob](https://discourse.julialang.org/u/blob)\
**Post date:** [January 17, 2022, 8:41am UTC](https://discourse.julialang.org/t/hessian-with-user-defined-functions-in-mathoptnlsmodel/74672/2 "2022-01-17T08:41:32Z")

</div>

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:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/0/90d272e9dcfc837f84d9f86c8911737c0e316a17.png)

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:

```julia
@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)

```

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [January 20, 2022, 10:04pm UTC](https://discourse.julialang.org/t/hessian-with-user-defined-functions-in-mathoptnlsmodel/74672/3 "2022-01-20T22:04:04Z")

</div>

Thanks @blob .  
I posted the issue on their GitHub [https://github.com/JuliaSmoothOptimizers/NLPModelsJuMP.jl/issues/94](https://github.com/JuliaSmoothOptimizers/NLPModelsJuMP.jl/issues/94) and the problem is now fixed.
