Convex.jl icon indicating copy to clipboard operation
Convex.jl copied to clipboard

Add support for quadratic objective functions

Open tpunnoose opened this issue 5 years ago • 6 comments

I am trying to use Convex.jl to interface with the OSQP solver. However, for some reason I am unable to use quadform in the objective and still have it link to the OSQP optimizer.

using Convex
using LinearAlgebra
using OSQP

x = Variable(5)
P = Diagonal(ones(5))

problem = minimize(quadform(x, P))

solve!(problem, OSQP.Optimizer)

When this code runs I get the following error:

LoadError: MathOptInterface.UnsupportedConstraint{MathOptInterface.VectorAffineFunction{Float64},MathOptInterface.SecondOrderCone}("")

I don't understand why it throws an unsupported constraint error as there are no constraints.

tpunnoose avatar Apr 09 '20 04:04 tpunnoose

Thanks for bringing this to my attention. This currently doesn’t work in Convex because Convex rewrites the problem to have a linear objective function subject to “second order cone constraints”. This transformation makes solving such a problem easy for solvers that support such constraints but not quadratic objectives (ECOS, SCS, etc), but makes it unable to work with OSQP that does want this specific form. The long term fix is to only do this transformation as necessary, instead of always doing it as Convex does now. I hope to move to that approach (by delegating these transformations to MathOptInterface which only applies them when necessary) at some point but it might take some time.

In the meantime, I’d suggest using JuMP.jl or maybe Parametron.jl, depending on your use case.

I’ll mark this as a feature request to be closed whenever the right behavior is implemented.

ericphanson avatar Apr 09 '20 12:04 ericphanson

You should be able to do (I haven't tested though)

using Convex
using LinearAlgebra
using OSQP
using MathOptInterface

x = Variable(5)
P = Diagonal(ones(5))

problem = minimize(quadform(x, P))

solve!(problem, () -> MOI.Bridges.full_bridge_optimizer(OSQP.Optimizer(), Float64))

odow avatar Apr 10 '20 13:04 odow

@odow Convex transmit the problem as a SOC constraint and we don't have any bridge to transform this SOC constraint back to a quadratic objective as OSQP requires (and it does not seem to be something feasible via bridges) so I don't think that this will work.

The long term fix is to only do this transformation was necessary, instead of always doing it as Convex does now. I hope to move to that approach (by delegating these transformations to MathOptInterface which only applies them when necessary) at some point but it might take some time.

I agree that this would be the ideal long term fix.

blegat avatar Apr 10 '20 14:04 blegat

I just noticed that for other solvers like COSMO that allows quadrative objectives, Convex also uses this trick and does not pass along a quadratic objective.

What is the current status on this ? Will it be possible soeday to pass along quadratic objectives to solvers ?

lrnv avatar Sep 19 '21 18:09 lrnv

What is the current status on this ?

same as before- Convex reformulates all problems with extended formulations (always) and passes a single variable objective.

Will it be possible soeday to pass along quadratic objectives to solvers ?

Just depends if someone implements it or not :). AFAIK no one is actively developing Convex.jl right now, but anyone who wants to can!

I used Convex a little bit in my PhD so I tried to help develop it further for a year or two (and mostly just fixed bugs)- happy to try to help and provide code review for any PRs (but I don’t really see myself doing much development myself any time soon- I don’t use Convex anymore, so it’s hard to be motivated for it).

ericphanson avatar Sep 19 '21 18:09 ericphanson

So there's a path forward here:

If the objective is a quadratic atom, then instead of calling conic_form! on it, we could convert into a ScalarQuadraticFunction if supported.

Here's the bits that would need changed:

https://github.com/jump-dev/Convex.jl/blob/946d18d131a3404c984725fbd05cae1bf38d0bc4/src/problems.jl#L136-L171

odow avatar Jan 22 '24 03:01 odow