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

Compatibility with LinQuadOptInterface solvers (like Gurobi and GLPK) #31

Closed
rdeits opened this issue Jun 26, 2018 · 6 comments
Closed

Comments

@rdeits
Copy link
Collaborator

rdeits commented Jun 26, 2018

Currently, constrained optimization doesn't work with the upcoming Gurobi MOI interface. For example:

optimizer = Gurobi.GurobiOptimizer()
model = Model(optimizer)
x = Variable(model)
lhs = [x]
rhs = [0.]
@constraint model lhs <= rhs
solve!(model)

gives:

ArgumentError: ModelLike of type Gurobi.GurobiOptimizer does not support setting the attribute `ConstraintFunction`

Stacktrace:
 [1] hidden_set!(::Gurobi.GurobiOptimizer, ::MathOptInterface.ConstraintFunction, ::MathOptInterface.ConstraintIndex{MathOptInterface.VectorAffineFunction{Float64},MathOptInterface.Nonpositives}, ::MathOptInterface.VectorAffineFunction{Float64}) at /home/rdeits/locomotion/explorations/simple-qp-miqp/packages/v0.6/MathOptInterface/src/attributes.jl:588
 [2] update!(::SimpleQP.Constraint{Float64,MathOptInterface.Nonpositives}, ::SimpleQP.Model{Float64,Gurobi.GurobiOptimizer}) at /home/rdeits/locomotion/explorations/simple-qp-miqp/packages/v0.6/SimpleQP/src/model.jl:125
 [3] update!(::SimpleQP.Model{Float64,Gurobi.GurobiOptimizer}) at /home/rdeits/locomotion/explorations/simple-qp-miqp/packages/v0.6/SimpleQP/src/model.jl:136
 [4] solve!(::SimpleQP.Model{Float64,Gurobi.GurobiOptimizer}) at /home/rdeits/locomotion/explorations/simple-qp-miqp/packages/v0.6/SimpleQP/src/model.jl:149

(using SimpleQP master, Gurobi.jl with jump-dev/Gurobi.jl#125 , MOI master, and LQOI with JuliaOpt/LinQuadOptInterface.jl#29 ).

It looks like LInQuadOptInterface defines the available canset/canmodify methods, and it only supports things like scalar variable change rather than replacing the constraint function. Gurobi seems to have the relevant low-level APIs to change variable coefficients: https://www.gurobi.com/documentation/8.0/refman/c_grbchgcoeffs.html so I think this should at least be possible. For example, we could probably implement additional canset/set! methods in Gurobi.jl to supplement the ones implemented in LQOI. Or maybe we could update LQOI to allow setting an affine constraint function by using the available modify! methods.

@tkoolen
Copy link
Owner

tkoolen commented Jun 26, 2018

Yeah, the other (non-OSQP) solver interfaces were pretty far behind, and I guess they're still not quite there. I see you already opened an issue with LQOI (JuliaOpt/LinQuadOptInterface.jl#32), which I was going to suggest.

@rdeits
Copy link
Collaborator Author

rdeits commented Jun 26, 2018

Oh, yeah, I was just about to cross-post that here 🙂

@rdeits rdeits changed the title Compatibility with Gurobi.jl Compatibility with LinQuadOptInterface solvers (like Gurobi and GLPK) Jul 17, 2018
@rdeits
Copy link
Collaborator Author

rdeits commented Jul 17, 2018

JuliaOpt/LinQuadOptInterface.jl#37 allows set!(..., ::ConstraintFunction) for LQOI solvers, but I'm still running into issues with SimpleQP:

model = Model(GLPK.GLPKOptimizerLP())
x = Variable(model)
lhs = [x]
rhs = [0.]
@constraint model lhs <= rhs
solve!(model)
MethodError: no method matching passattributes!(::GLPK.GLPKOptimizerLP, ::SimpleQP.SimpleQPMOIModel{Float64}, ::Bool, ::MathOptInterface.Utilities.IndexMap, ::Array{Any,1})
Closest candidates are:
  passattributes!(::MathOptInterface.ModelLike, ::MathOptInterface.ModelLike, ::Bool, ::MathOptInterface.Utilities.IndexMap) at /home/rdeits/locomotion/explorations/linquadopt-gurobi/packages/v0.6/MathOptInterface/src/Utilities/copy.jl:39
  passattributes!(::MathOptInterface.ModelLike, ::MathOptInterface.ModelLike, ::Bool, ::MathOptInterface.Utilities.IndexMap, ::Function) at /home/rdeits/locomotion/explorations/linquadopt-gurobi/packages/v0.6/MathOptInterface/src/Utilities/copy.jl:39
  passattributes!(::MathOptInterface.ModelLike, ::MathOptInterface.ModelLike, ::Bool, ::MathOptInterface.Utilities.IndexMap, ::Function, ::Function) at /home/rdeits/locomotion/explorations/linquadopt-gurobi/packages/v0.6/MathOptInterface/src/Utilities/copy.jl:39
  ...

Stacktrace:
 [1] copyconstraints!(::GLPK.GLPKOptimizerLP, ::SimpleQP.SimpleQPMOIModel{Float64}, ::Bool, ::MathOptInterface.Utilities.IndexMap, ::Type{MathOptInterface.VectorAffineFunction{Float64}}, ::Type{MathOptInterface.Nonpositives}) at /home/rdeits/locomotion/explorations/linquadopt-gurobi/packages/v0.6/MathOptInterface/src/Utilities/copy.jl:90
 [2] defaultcopy!(::GLPK.GLPKOptimizerLP, ::SimpleQP.SimpleQPMOIModel{Float64}, ::Bool) at /home/rdeits/locomotion/explorations/linquadopt-gurobi/packages/v0.6/MathOptInterface/src/Utilities/copy.jl:125
 [3] initialize!(::SimpleQP.Model{Float64,GLPK.GLPKOptimizerLP}) at /home/rdeits/locomotion/explorations/linquadopt-gurobi/packages/v0.6/SimpleQP/src/model.jl:142
 [4] solve!(::SimpleQP.Model{Float64,GLPK.GLPKOptimizerLP}) at /home/rdeits/locomotion/explorations/linquadopt-gurobi/packages/v0.6/SimpleQP/src/model.jl:195

@rdeits
Copy link
Collaborator Author

rdeits commented Jul 18, 2018

That MethodError is jump-dev/MathOptInterface.jl#426

@rdeits
Copy link
Collaborator Author

rdeits commented Jul 18, 2018

Ok, I think I have the constraint issues worked out. Unfortunately, it looks like the way quadratic objectives are handled is different between OSQP and LQOI. Specifically, the doubling of the quadratic coefficients along the diagonal of the cost matrix results in the wrong answer being returned. For example:

using SimpleQP, Gurobi
model = Model(GurobiOptimizer())
x = [Variable(model) for _ in 1:1]
@objective model Minimize (x[1] - 1.5)^2
solve!(model)
SimpleQP.value.(model, x)

gives an optimal solution of 0.75, whereas if we undo the doubling by minimizing (0.5 * x[1] - 1.5)^2 we get 1.5. OSQPOptimizer gives 1.5 for the original model, as expected.

@tkoolen tkoolen mentioned this issue Jul 18, 2018
12 tasks
@tkoolen
Copy link
Owner

tkoolen commented Jul 25, 2018

I think we can close this now. Currently requires master versions of MOI, LQOI, and solver packages.

@tkoolen tkoolen closed this as completed Jul 25, 2018
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

No branches or pull requests

2 participants