# Querying \`JuMP.NLPEvaluator\` for objective value and constraints with MathProgBase

**URL:** <https://discourse.julialang.org/t/querying-jump-nlpevaluator-for-objective-value-and-constraints-with-mathprogbase/16427>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [October 16, 2018, 11:34pm UTC](https://discourse.julialang.org/t/querying-jump-nlpevaluator-for-objective-value-and-constraints-with-mathprogbase/16427 "2018-10-16T23:34:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [October 16, 2018, 11:34pm UTC](https://discourse.julialang.org/t/querying-jump-nlpevaluator-for-objective-value-and-constraints-with-mathprogbase/16427/1 "2018-10-16T23:34:03Z")

</div>

I have an NLP model `nlp_model`, and I want to evaluate its objective function / constraints at a point `x`. I first create an `NLPEvaluator` as `mpb_model = JuMP.NLPEvaluator(nlp_model)` and then try to query it with `MathProgBase.eval_f(mpb_model, x)`, but I get an Undefined Reference error.

I can first initialize the interface with `MathProgBase.initialize(mpb_model, [:Grad])` and then call `eval_f` fine, but this calls the JIT warmup on computing the gradient. To avoid this, I added a `:Func` option to the `features_available` function so that `initialize` performs the initialization without a gradient call.

Is there a better way to ask for the objective function / constraints?

---

<div class="post-metadata">

**Author:** ![pjssilva](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pjssilva/32/4238_2.png) [@pjssilva](https://discourse.julialang.org/u/pjssilva)\
**Post date:** [October 17, 2018, 2:32pm UTC](https://discourse.julialang.org/t/querying-jump-nlpevaluator-for-objective-value-and-constraints-with-mathprogbase/16427/2 "2018-10-17T14:32:39Z")

</div>

Why to you need to use MathProgBase? NLPModels have their own functions to compute values. To compute the objective you should use `obj(nlp_model, x)`. To compute the constraints use `cons(nlp_model, x)`.

If you need, for some reason, to convert the NLPModel to a MathProgBase one, you should use `mpb_model = NLPtoMPB(nlp_model, solver)`. I am not sure if the `solver` parameter is required. I believe not.

---

<div class="post-metadata">

**Author:** ![dpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpo/32/3335_2.png) [@dpo](https://discourse.julialang.org/u/dpo)\
**Post date:** [October 17, 2018, 8:31pm UTC](https://discourse.julialang.org/t/querying-jump-nlpevaluator-for-objective-value-and-constraints-with-mathprogbase/16427/3 "2018-10-17T20:31:32Z")

</div>

I don’t think the OP means that he’s using NLPModels.jl here.

---

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [October 17, 2018, 8:40pm UTC](https://discourse.julialang.org/t/querying-jump-nlpevaluator-for-objective-value-and-constraints-with-mathprogbase/16427/4 "2018-10-17T20:40:19Z")

</div>

Ah, thanks @pjssilva and @dpo. Yes, I wasn’t familiar with `NLPModels.jl` and had just been interfacing with MPB.

---

<div class="post-metadata">

**Author:** ![pjssilva](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pjssilva/32/4238_2.png) [@pjssilva](https://discourse.julialang.org/u/pjssilva)\
**Post date:** [October 17, 2018, 9:09pm UTC](https://discourse.julialang.org/t/querying-jump-nlpevaluator-for-objective-value-and-constraints-with-mathprogbase/16427/5 "2018-10-17T21:09:43Z")

</div>

Sure, sorry.

You can pass an empty Symbol vector to initialize if you don’t need the gradients. It seems to work here. I hope it avoids the JIT for the gradient as you did not ask for it. Initialize with something like:

`MathProgBase.initialize(mpb_mode, Vector{Symbol}())`

---

<div class="post-metadata">

**Author:** ![dpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpo/32/3335_2.png) [@dpo](https://discourse.julialang.org/u/dpo)\
**Post date:** [October 17, 2018, 9:51pm UTC](https://discourse.julialang.org/t/querying-jump-nlpevaluator-for-objective-value-and-constraints-with-mathprogbase/16427/6 "2018-10-17T21:51:56Z")

</div>

That said, you may have luck defining an NLPModel from NLPModels.jl and evaluating it as @pjssilva indicated. If you need to pass it to a NLP solver later, you can convert it to a MathProgBase model. We’re taking care of the evaluator under the hood.
