Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/Test/test_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 6 additions & 3 deletions src/Utilities/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions test/Utilities/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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