# Is it possible that passing user defined gradient slows down the solver

**URL:** https://discourse.julialang.org/t/is-it-possible-that-passing-user-defined-gradient-slows-down-the-solver/10311
**Category:** Optimization (Mathematical)
**Created:** [April 12, 2018, 10:34pm UTC](https://discourse.julialang.org/t/is-it-possible-that-passing-user-defined-gradient-slows-down-the-solver/10311 "2018-04-12T22:34:17Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Ksun46](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksun46/32/9437_2.png) [@Ksun46](https://discourse.julialang.org/u/Ksun46)
#### Post date: [April 12, 2018, 10:34pm UTC](https://discourse.julialang.org/t/is-it-possible-that-passing-user-defined-gradient-slows-down-the-solver/10311/1 "2018-04-12T22:34:17Z")

</div>

Hi all,

I am using JuMP with Ipopt and encounter this problem. For example, I have a constraint looks like x^2 + y^2 \<= 1. Originally I just add this constraints to the model. But when I try to define a function f(x, y) = x^2 + y^2, register this function and its gradient to the model (I tried both autodiffer and user-defined gradient), and then run the code, Ipopt actually takes more time and iterations to converge. I am wondering is this possible, or I am not coding in the correct way?

In addition, just to make sure, currently we cannot give hessian of multivariate functions, right?

Thanks in advanced.

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [April 12, 2018, 11:10pm UTC](https://discourse.julialang.org/t/is-it-possible-that-passing-user-defined-gradient-slows-down-the-solver/10311/2 "2018-04-12T23:10:23Z")

</div>

Since Ipopt is actually taking more iterations (rather than just more time), the gradients you’re supplying might be wrong. Ipopt has a built-in derivative checking function (enable using `derivative_test` keyword), which is pretty handy. Specifically, I’ve used the following in tests:

[https://github.com/JuliaRobotics/MotionCaptureJointCalibration.jl/blob/efd5f040ba7a856f9cc96f16aa5a76f3352d228c/test/runtests.jl#L83](https://github.com/JuliaRobotics/MotionCaptureJointCalibration.jl/blob/efd5f040ba7a856f9cc96f16aa5a76f3352d228c/test/runtests.jl#L83)

---

<div class="post-metadata">

### Author: ![Ksun46](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksun46/32/9437_2.png) [@Ksun46](https://discourse.julialang.org/u/Ksun46)
#### Post date: [April 12, 2018, 11:34pm UTC](https://discourse.julialang.org/t/is-it-possible-that-passing-user-defined-gradient-slows-down-the-solver/10311/3 "2018-04-12T23:34:25Z")

</div>

Thank you for the fast responses. I just tried and the solver prints no errors detected by derivative checker. It looks like with user defined function and gradient(i.e., JuMP.register()), Ipopt is not evaluating the Lagrangian Hessian.

---

<div class="post-metadata">

### Author: ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)
#### Post date: [April 13, 2018, 1:36pm UTC](https://discourse.julialang.org/t/is-it-possible-that-passing-user-defined-gradient-slows-down-the-solver/10311/4 "2018-04-13T13:36:23Z")

</div>

That is correct. JuMP does not provide Hessian information to Ipopt if there are non-univariate user-defined functions present. This can, of course, affect the algorithmic performance of the solver.

---

<div class="post-metadata">

### Author: ![Ksun46](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksun46/32/9437_2.png) [@Ksun46](https://discourse.julialang.org/u/Ksun46)
#### Post date: [April 13, 2018, 1:45pm UTC](https://discourse.julialang.org/t/is-it-possible-that-passing-user-defined-gradient-slows-down-the-solver/10311/5 "2018-04-13T13:45:55Z")

</div>

Thank you for the response. So if I use juMP.register(), Ipopt will not use second order information? But if I don’t user defined function, i.e., use a loop to add variables to constraints/variable, then Ipopt will automatically calculate both gradient and hessian? Am I understanding correctly? Thanks.

---

<div class="post-metadata">

### Author: ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)
#### Post date: [April 18, 2018, 2:34am UTC](https://discourse.julialang.org/t/is-it-possible-that-passing-user-defined-gradient-slows-down-the-solver/10311/6 "2018-04-18T02:34:31Z")

</div>

If you register a non-univariate user-defined function, then JuMP will report to Ipopt that hessians are not available to be queried. Ipopt will switch to “limited-memory hessian approximation” in this case (see [https://github.com/JuliaOpt/Ipopt.jl/blob/8319f091254f5ad2974f8812280fcb9b29df003b/src/IpoptSolverInterface.jl#L38](https://github.com/JuliaOpt/Ipopt.jl/blob/8319f091254f5ad2974f8812280fcb9b29df003b/src/IpoptSolverInterface.jl#L38)). Otherwise, JuMP provides derivatives and by default Ipopt will use both gradients and hessians.
