Skip to content

Commit

Permalink
Fix getter of MOI.ListOfConstraintIndices (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Apr 3, 2024
1 parent 5f45fb2 commit 96fd052
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/MOI/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions test/test_MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down

0 comments on commit 96fd052

Please sign in to comment.