From be7d237ac01739301200ad213e8001d1c74dd820 Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 2 Jul 2021 13:18:08 +1200 Subject: [PATCH 1/2] [Utilities] Fix bug in ListOfConstraintAttributesSet --- src/Test/test_model.jl | 38 ++++++++++++++++++++++++++++++++++++++ src/Utilities/model.jl | 9 ++++++--- test/Utilities/model.jl | 31 +++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) 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..8dd63f6e92 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( + src, + MOI.ListOfConstraintAttributesSet{MOI.SingleVariable,MOI.Integer}(), + ) == [] + @test MOI.get( + src, + MOI.ListOfConstraintAttributesSet{ + MOI.ScalarAffineFunction{Float64}, + MOI.EqualTo{Float64}, + }(), + ) == [MOI.ConstraintName()] +end From dd1a9e89355e6237492d8e17c4615887676974bb Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Fri, 2 Jul 2021 13:51:07 +1200 Subject: [PATCH 2/2] Update model.jl --- test/Utilities/model.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Utilities/model.jl b/test/Utilities/model.jl index 8dd63f6e92..be2dcfdb27 100644 --- a/test/Utilities/model.jl +++ b/test/Utilities/model.jl @@ -613,11 +613,11 @@ end """, ) @test MOI.get( - src, + model, MOI.ListOfConstraintAttributesSet{MOI.SingleVariable,MOI.Integer}(), ) == [] @test MOI.get( - src, + model, MOI.ListOfConstraintAttributesSet{ MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64},