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

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?

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.

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

1 Like

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

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}())

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.