# Using Knitro with ForwardDiff and JuMP

**URL:** <https://discourse.julialang.org/t/using-knitro-with-forwarddiff-and-jump/124396>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, forwarddiff\
**Created:** [January 3, 2025, 5:38am UTC](https://discourse.julialang.org/t/using-knitro-with-forwarddiff-and-jump/124396 "2025-01-03T05:38:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rick](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@Rick](https://discourse.julialang.org/u/Rick)\
**Post date:** [January 3, 2025, 5:38am UTC](https://discourse.julialang.org/t/using-knitro-with-forwarddiff-and-jump/124396/1 "2025-01-03T05:38:38Z")

</div>

Hi,

I am trying to use Knitro with `JuMP` which contains a constraint that imposes the gradient of a function to be 0. I want to compute the gradient using `ForwardDiff`.

I found this can be done using `Ipopt`, but I cannot get it to work using Knitro. For instance, I tried to use the code in this example ([@constraint with ForwardDiff.gradient - #2 by odow](https://discourse.julialang.org/t/constraint-with-forwarddiff-gradient/104949/2)) using the Knitro optimizer but I got an error message that it is not supported.

Here is the MWE below:

```julia
using ForwardDiff, JuMP, KNITRO

Q = [1.65539 2.89376; 2.89376 6.51521];
q = [2; -3]
f(x) = 0.5*x'*Q*x + q'*x + exp(-1.3*x[1] + 0.3*x[2]^2)
kkt_conditions(x) = ForwardDiff.gradient(f,x)

lm = KNITRO.LMcontext()
model = Model(() -> KNITRO.Optimizer(license_manager = lm,
                                     outlev = 1))
JuMP.@variable(model, x[i=1:2])
kkt_conditions(x) = ForwardDiff.gradient(f,x)
residual_fx(_x) = kkt_conditions(_x)

JuMP.@operator(model, op_f1, 2, (x...) -> residual_fx(collect(x))[1])
JuMP.@constraint(model, op_f1(x...) == 0)
JuMP.optimize!(model)

```

The error I get is:

```julia
ERROR: Constraints of type MathOptInterface.ScalarNonlinearFunction-in-MathOptInterface.EqualTo{Float64} are not supported by the solver.

If you expected the solver to support your problem, you may have an error in your formulation. Otherwise, consider using a different solver.

The list of available solvers, along with the problem types they support, is available at https://jump.dev/JuMP.jl/stable/installation/#Supported-solvers.

```

I checked the link in the error message, but it says Knitro supports NLP too, so I am not sure how it can be adapted to work with ForwardDiff

Thanks for the help in advance!!

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [January 3, 2025, 6:00am UTC](https://discourse.julialang.org/t/using-knitro-with-forwarddiff-and-jump/124396/2 "2025-01-03T06:00:48Z")

</div>

I expect this to work.

What is `import Pkg; Pkg.status()`? You might have an old version of KNITRO.jl and it just needs updating.

---

<div class="post-metadata">

**Author:** ![Rick](https://avatars.discourse-cdn.com/v4/letter/r/9f8e36/32.png) [@Rick](https://discourse.julialang.org/u/Rick)\
**Post date:** [January 3, 2025, 6:22am UTC](https://discourse.julialang.org/t/using-knitro-with-forwarddiff-and-jump/124396/3 "2025-01-03T06:22:22Z")

</div>

Amazing – it works now. I had v0.13.0. The code works by updating to v0.14.4. Thanks!
