JuliaSmoothOptimizers / NLPModelsJuMP.jl

Create NLPModels with JuMP
Other
33 stars 8 forks source link

Quadratic functions in the constraints are not picked up by MathOptNLPModel #173

Closed abelsiqueira closed 3 days ago

abelsiqueira commented 8 months ago

More specifically,

model = Model()
@variable(model, x[1:2])
@objective(model, Min, sum(x .^ 4))
@constraint(model, sum(x .^ 2) == 1)
nlp = MathOptNLPModel(model)

warns with

┌ Warning: Function MathOptInterface.ScalarQuadraticFunction{Float64} is not supported.
└ @ NLPModelsJuMP ~/.julia/packages/NLPModelsJuMP/1KSlI/src/utils.jl:219

And indeed, there are no constraints in the model. Solving the problem via JuMP using NLPModels.Optimizer works.

cc. @blegat, if you can help us again.

blegat commented 8 months ago

The Optimizer declares that quadratic functions are not supported through supports_constraint so JuMP bridges them to ScalarNonlinearFunction using https://jump.dev/MathOptInterface.jl/dev/submodules/Bridges/list_of_bridges/#MathOptInterface.Bridges.Constraint.ToScalarNonlinearBridge and these nonlinear functions are then handled correctly by MathOptNLPModel.

blegat commented 7 months ago

If you want to get the NLPModel of a JuMP model, I would query the internal field of the Optimizer. We could add an MOI attribute to make it easy to access without the need to query an internal field of JuMP.unsafe_backend

tmigot commented 7 months ago

I think we started some work on this with @amontoison a while ago https://github.com/JuliaSmoothOptimizers/NLPModelsJuMP.jl/pull/102 (connected to #50 )

abelsiqueira commented 6 months ago

Just an update, related to #174, more general nonlinear constraints are also not supported

abelsiqueira commented 6 months ago

MWE:

using JuMP, NLPModelsJuMP, Percival
model = Model()
@variable(model, x[1:2])
@objective(model, Min, (x[1] - 1)^2 + 4 * (x[2] - x[1]^2)^2)
@constraint(model, x[1]^2 + x[2]^2 == 1)
@constraint(model, x[1]^4 + x[2]^4 == 1)
@constraint(model, x[1] * exp(x[2]) == 1)
nlp = MathOptNLPModel(model) # Warns Function MathOptInterface.ScalarQuadraticFunction{Float64} is not supported.
                             # And shows no constraints
output = percival(nlp) # Warns Problem does not have general constraints; calling tron
output.solution # Shows close to [1;1]
amontoison commented 1 month ago

@blegat @abelsiqueira Quadratic constraints with @constraint are now supported with the release 0.13.0 but general nonlinear constraints with @constraint are still not working when we use MathOptNLPModel or MathOptNLSModel.

I don't understand how they are stored because I don't have any error when I create the MathOptNLPModel.

Don't we need to do something in this function here for ScalarNonLinearFunction ?

blegat commented 3 weeks ago

You allow nonlinear functions thanks to https://github.com/JuliaSmoothOptimizers/NLPModelsJuMP.jl/blob/3dd5054c3c774a4cbc887691964ab82632165ec4/src/utils.jl#L379-L381

but you don't do anything with it. You are missing a if typeof(fun) == MOI.ScalarNonlinearFunction here https://github.com/JuliaSmoothOptimizers/NLPModelsJuMP.jl/blob/3dd5054c3c774a4cbc887691964ab82632165ec4/src/utils.jl#L395-L410

amontoison commented 3 weeks ago

@blegat It's you that allowed nonlinear functions :wink: -> PR https://github.com/JuliaSmoothOptimizers/NLPModelsJuMP.jl/pull/171

You modified this line: https://github.com/JuliaSmoothOptimizers/NLPModelsJuMP.jl/pull/171/files#diff-47c27891e951c8cd946b850dc2df31082624afdf57446c21cb6992f5f4b74aa2R219 Should we remove it and add it back when it's fully supported?

blegat commented 3 weeks ago

Yes, indeed, I remember now. The current strategy is to put all the nonlinear function in the NLPBlock (see _nlp_model) since the NLPBlock is supported. So now throwing when we see these function is expected since we handle them later by adding them to the NLPBlock. If you add support for it natively, you can then stop adding them in the NLPBlock