From c05544d4de129079f7e3b45f4bf52997ee04beed Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Tue, 25 Jun 2024 16:48:29 +1200 Subject: [PATCH] Fix MOI.get for quadratic constraints (#151) --- src/MOI_wrapper.jl | 38 ++++++++++---------------------------- test/jump_tests.jl | 10 ++++++++++ 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/src/MOI_wrapper.jl b/src/MOI_wrapper.jl index d99cdf8..4915b22 100644 --- a/src/MOI_wrapper.jl +++ b/src/MOI_wrapper.jl @@ -452,9 +452,9 @@ end function MOI.set( model::Optimizer, ::MOI.ConstraintFunction, - c::MOI.ConstraintIndex{F,S}, + c::MOI.ConstraintIndex{F}, f::F, -) where {F,S} +) where {F} MOI.set(model.optimizer, MOI.ConstraintFunction(), c, f) return end @@ -462,8 +462,8 @@ end function MOI.get( model::Optimizer, attr::MOI.ConstraintFunction, - ci::MOI.ConstraintIndex{F,S}, -) where {F,S} + ci::MOI.ConstraintIndex, +) if haskey(model.quadratic_outer_to_inner, ci) inner_ci = model.quadratic_outer_to_inner[ci] return _original_function(model.quadratic_constraint_cache[inner_ci]) @@ -501,8 +501,8 @@ end function MOI.get( model::Optimizer, attr::MOI.ConstraintSet, - ci::MOI.ConstraintIndex{F,S}, -) where {F,S} + ci::MOI.ConstraintIndex, +) if haskey(model.quadratic_outer_to_inner, ci) inner_ci = model.quadratic_outer_to_inner[ci] return model.quadratic_constraint_cache_set[inner_ci] @@ -1084,29 +1084,11 @@ end function MOI.get( model::Optimizer, - attr::T, + attr::MOI.AbstractConstraintAttribute, c::MOI.ConstraintIndex, -) where { - T<:Union{MOI.ConstraintPrimal,MOI.ConstraintDual,MOI.ConstraintBasisStatus}, -} - return MOI.get(model.optimizer, attr, c) -end - -function MOI.get( - model::Optimizer, - attr::AT, - c::MOI.ConstraintIndex{MOI.ScalarAffineFunction{T},S}, -) where { - AT<:Union{ - MOI.ConstraintPrimal, - MOI.ConstraintDual, - MOI.ConstraintBasisStatus, - }, - T, - S<:MOI.AbstractScalarSet, -} - moi_ci = get(model.affine_outer_to_inner, c, c) - return MOI.get(model.optimizer, attr, moi_ci) +) + optimizer_ci = get(model.constraint_outer_to_inner, c, c) + return MOI.get(model.optimizer, attr, optimizer_ci) end # diff --git a/test/jump_tests.jl b/test/jump_tests.jl index 8ee15e2..f167fb9 100644 --- a/test/jump_tests.jl +++ b/test/jump_tests.jl @@ -1079,3 +1079,13 @@ function test_abstract_optimizer_attributes() @test get_attribute(model, "tm_lim") ≈ 60 * 1000 return end + +function test_get_quadratic_constraint() + model = Model(() -> POI.Optimizer(GLPK.Optimizer())) + @variable(model, x) + @variable(model, p in Parameter(2.0)) + @constraint(model, c, p * x <= 10) + optimize!(model) + @test value(c) ≈ 2.0 * value(x) + return +end