Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
abelsiqueira opened this issue Nov 24, 2023 · 9 comments · Fixed by #190
Closed

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

abelsiqueira opened this issue Nov 24, 2023 · 9 comments · Fixed by #190

Comments

@abelsiqueira
Copy link
Member

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
Copy link
Contributor

blegat commented Nov 24, 2023

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
Copy link
Contributor

blegat commented Dec 4, 2023

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
Copy link
Member

tmigot commented Dec 16, 2023

I think we started some work on this with @amontoison a while ago #102 (connected to #50 )

@abelsiqueira
Copy link
Member Author

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

@abelsiqueira
Copy link
Member Author

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
Copy link
Member

amontoison commented Jun 22, 2024

@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
Copy link
Contributor

blegat commented Jul 1, 2024

You allow nonlinear functions thanks to

F <: QF ||
F == MOI.ScalarNonlinearFunction ||
F == VI ||

but you don't do anything with it.
You are missing a if typeof(fun) == MOI.ScalarNonlinearFunction here

if typeof(fun) <: SAF
parser_SAF(fun, set, linrows, lincols, linvals, nlin, lin_lcon, lin_ucon, index_map)
nlin += 1
end
if typeof(fun) <: VAF
parser_VAF(fun, set, linrows, lincols, linvals, nlin, lin_lcon, lin_ucon, index_map)
nlin += set.dimension
end
if typeof(fun) <: SQF
parser_SQF(fun, set, nvar, qcons, quad_lcon, quad_ucon, index_map)
nquad += 1
end
if typeof(fun) <: VQF
parser_VQF(fun, set, nvar, qcons, quad_lcon, quad_ucon, index_map)
nquad += set.dimension
end

@amontoison
Copy link
Member

amontoison commented Jul 2, 2024

@blegat It's you that allowed nonlinear functions 😉
-> PR #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
Copy link
Contributor

blegat commented Jul 3, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants