From 96fd0528fb73ac893f68792df172710c08fe2dd0 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Wed, 3 Apr 2024 13:12:14 +1300 Subject: [PATCH] Fix getter of MOI.ListOfConstraintIndices (#261) --- src/MOI/MOI_wrapper.jl | 11 ++++++++--- test/test_MOI_wrapper.jl | 11 +++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/MOI/MOI_wrapper.jl b/src/MOI/MOI_wrapper.jl index b088eb15..ac3e6e02 100644 --- a/src/MOI/MOI_wrapper.jl +++ b/src/MOI/MOI_wrapper.jl @@ -3582,21 +3582,26 @@ function MOI.get( return sort!(indices; by = x -> x.value) end +_function_enums(::Type{<:MOI.ScalarAffineFunction}) = (AFFINE,) +_function_enums(::Type{<:MOI.ScalarQuadraticFunction}) = (QUADRATIC,) +_function_enums(::Type{<:MOI.VectorAffineFunction}) = (INDICATOR,) +_function_enums(::Type{<:MOI.VectorOfVariables}) = (SOC, RSOC) + function MOI.get( model::Optimizer, ::MOI.ListOfConstraintIndices{F,S}, ) where { - S, F<:Union{ MOI.ScalarAffineFunction{Float64}, - MOI.VectorAffineFunction{Float64}, MOI.ScalarQuadraticFunction{Float64}, + MOI.VectorAffineFunction{Float64}, MOI.VectorOfVariables, }, + S, } indices = MOI.ConstraintIndex{F,S}[] for (key, info) in model.affine_constraint_info - if typeof(info.set) == S + if info.type in _function_enums(F) && typeof(info.set) == S push!(indices, MOI.ConstraintIndex{F,S}(key)) end end diff --git a/test/test_MOI_wrapper.jl b/test/test_MOI_wrapper.jl index 76670877..2a20a2fa 100644 --- a/test/test_MOI_wrapper.jl +++ b/test/test_MOI_wrapper.jl @@ -2087,6 +2087,17 @@ function test_modify_different_sparsity() return end +function test_ListOfConstraintIndices() + model = Xpress.Optimizer() + x = MOI.add_variable(model) + c = MOI.add_constraint(model, 1.0 * x, MOI.LessThan(0.0)) + F, S = MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64} + G = MOI.ScalarQuadraticFunction{Float64} + @test MOI.get(model, MOI.ListOfConstraintIndices{F,S}()) == [c] + @test isempty(MOI.get(model, MOI.ListOfConstraintIndices{G,S}())) + return +end + function test_ListOfConstraintTypesPresent() model = Xpress.Optimizer() sets = (