diff --git a/src/Test/test_model.jl b/src/Test/test_model.jl index 18eb8d123e..2bd53d4229 100644 --- a/src/Test/test_model.jl +++ b/src/Test/test_model.jl @@ -1013,3 +1013,41 @@ function test_model_delete(model::MOI.ModelLike, config::Config) ), ) end + +""" + test_ListOfConstraintAttributesSet( + model::MOI.ModelLike, + config::Config{T}, + ) where {T} + +Test issue #1429: ConstraintName should only be included in +`ListOfConstraintAttributesSet` if it actually is set. +""" +function test_ListOfConstraintAttributesSet( + model::MOI.ModelLike, + ::Config{T}, +) where {T} + x = MOI.add_variable(model) + MOI.add_constraint(model, MOI.SingleVariable(x), MOI.GreaterThan(zero(T))) + c = MOI.add_constraint( + model, + MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(one(T), x)], zero(T)), + MOI.EqualTo(one(T)), + ) + MOI.set(model, MOI.ConstraintName(), c, "c") + @test MOI.get( + model, + MOI.ListOfConstraintAttributesSet{ + MOI.ScalarAffineFunction{T}, + MOI.EqualTo{T}, + }(), + ) == [MOI.ConstraintName()] + @test MOI.get( + model, + MOI.ListOfConstraintAttributesSet{ + MOI.SingleVariable, + MOI.GreaterThan{T}, + }(), + ) == [] + return +end diff --git a/src/Utilities/model.jl b/src/Utilities/model.jl index 30209366f5..1fad5751e1 100644 --- a/src/Utilities/model.jl +++ b/src/Utilities/model.jl @@ -322,9 +322,12 @@ end function MOI.get( model::AbstractModel, - ::MOI.ListOfConstraintAttributesSet, -)::Vector{MOI.AbstractConstraintAttribute} - return isempty(model.con_to_name) ? [] : [MOI.ConstraintName()] + ::MOI.ListOfConstraintAttributesSet{F,S}, +) where {F,S} + if any(k -> k isa MOI.ConstraintIndex{F,S}, keys(model.con_to_name)) + return MOI.AbstractConstraintAttribute[MOI.ConstraintName()] + end + return MOI.AbstractConstraintAttribute[] end # Objective diff --git a/test/Utilities/model.jl b/test/Utilities/model.jl index 196c9cb4d8..be2dcfdb27 100644 --- a/test/Utilities/model.jl +++ b/test/Utilities/model.jl @@ -593,3 +593,34 @@ end bound_vectors_test(Int, 0, 0) bound_vectors_test(Float64, -Inf, Inf) end + +@testset "ListOfConstraintAttributesSet" begin + model = MOI.Utilities.Model{Float64}() + MOI.Utilities.loadfromstring!( + model, + """ + variables: a, b, c, d + minobjective: a + b + c + d + a >= 1.0 + b <= 2.0 + c == 3.0 + d in Interval(-4.0, 4.0) + a in Integer() + b in ZeroOne() + c7: a + b >= -1.1 + c8: a + b <= 2.2 + c8: c + d == 2.2 + """, + ) + @test MOI.get( + model, + MOI.ListOfConstraintAttributesSet{MOI.SingleVariable,MOI.Integer}(), + ) == [] + @test MOI.get( + model, + MOI.ListOfConstraintAttributesSet{ + MOI.ScalarAffineFunction{Float64}, + MOI.EqualTo{Float64}, + }(), + ) == [MOI.ConstraintName()] +end