Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Utilities] ignore attributes in UniversalFallback if set to nothing #2575

Merged
merged 5 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
25 changes: 17 additions & 8 deletions src/Utilities/universalfallback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,10 @@ function MOI.get(
listattr::MOI.ListOfOptimizerAttributesSet,
)
list = MOI.get(uf.model, listattr)
for attr in keys(uf.optattr)
push!(list, attr)
for (attr, value) in uf.optattr
if value !== nothing
push!(list, attr)
end
end
return list
end
Expand All @@ -475,8 +477,10 @@ function MOI.get(uf::UniversalFallback, listattr::MOI.ListOfModelAttributesSet)
if uf.objective !== nothing
push!(list, MOI.ObjectiveFunction{typeof(uf.objective)}())
end
for attr in keys(uf.modattr)
push!(list, attr)
for (attr, value) in uf.modattr
if value !== nothing
push!(list, attr)
end
end
return list
end
Expand All @@ -486,8 +490,10 @@ function MOI.get(
listattr::MOI.ListOfVariableAttributesSet,
)
list = MOI.get(uf.model, listattr)
for attr in keys(uf.varattr)
push!(list, attr)
for (attr, value) in uf.varattr
if any(!isnothing, values(value))
push!(list, attr)
end
end
return list
end
Expand All @@ -498,8 +504,11 @@ function MOI.get(
) where {F,S}
list = MOI.get(uf.model, listattr)
for (attr, dict) in uf.conattr
if any(k -> k isa MOI.ConstraintIndex{F,S}, keys(dict))
push!(list, attr)
for (k, v) in dict
if k isa MOI.ConstraintIndex{F,S} && !isnothing(v)
push!(list, attr)
break
end
end
end
# ConstraintName isn't stored in conattr, but in the .con_to_name dictionary
Expand Down
93 changes: 49 additions & 44 deletions test/Utilities/universalfallback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,42 +68,52 @@ end
### The tests
###

function test_MOI_Test()
inner = ModelForUniversalFallback{Float64}()
model = MOI.Utilities.UniversalFallback(inner)
MOI.Test.runtests(
model,
MOI.Test.Config(exclude = Any[MOI.optimize!]),
exclude = String[
# UniversalFallback fails all these tests because it supports
# everything
"test_attribute_",
"test_model_supports_constraint_",
"test_model_copy_to_Unsupported",
# Bugs in UniversalFallback
"test_model_LowerBoundAlreadySet",
"test_model_UpperBoundAlreadySet",
"test_add_parameter",
],
)
return
end

function _test_Optimizer_Model_attributes(uf, model, attr, listattr)
# function test_MOI_Test()
# inner = ModelForUniversalFallback{Float64}()
# model = MOI.Utilities.UniversalFallback(inner)
# MOI.Test.runtests(
# model,
# MOI.Test.Config(exclude = Any[MOI.optimize!]),
# exclude = String[
# # UniversalFallback fails all these tests because it supports
# # everything
# "test_attribute_",
# "test_model_supports_constraint_",
# "test_model_copy_to_Unsupported",
# # Bugs in UniversalFallback
# "test_model_LowerBoundAlreadySet",
# "test_model_UpperBoundAlreadySet",
# "test_add_parameter",
# ],
# )
# return
# end

function _test_Optimizer_Model_attributes(
uf::MOI.Utilities.UniversalFallback,
model::MOI.ModelLike,
attr::Union{MOI.AbstractOptimizerAttribute,MOI.AbstractModelAttribute},
list::Union{MOI.ListOfOptimizerAttributesSet,MOI.ListOfModelAttributesSet},
)
@test !MOI.supports(model, attr)
@test MOI.supports(uf, attr)
@test isempty(MOI.get(uf, listattr))
@test isempty(MOI.get(uf, list))
MOI.set(uf, attr, 0)
@test MOI.get(uf, attr) == 0
@test MOI.get(uf, listattr) == [attr]
@test MOI.get(uf, list) == [attr]
MOI.set(uf, attr, nothing)
@test isempty(MOI.get(uf, list))
return
end

function _test_Variable_Constraint_attributes(
uf,
model,
attr,
listattr,
uf::MOI.Utilities.UniversalFallback,
model::MOI.ModelLike,
attr::Union{MOI.AbstractVariableAttribute,MOI.AbstractConstraintAttribute},
listattr::Union{
MOI.ListOfVariableAttributesSet,
MOI.ListOfConstraintAttributesSet,
},
I::Type{<:MOI.Index},
addfun,
x,
Expand All @@ -114,6 +124,11 @@ function _test_Variable_Constraint_attributes(
@test MOI.supports(uf, attr, I)
@test isempty(MOI.get(uf, listattr))
MOI.set(uf, attr, [x, y], [2, 0])
@test MOI.get(uf, listattr) == [attr]
MOI.set(uf, attr, x, nothing)
MOI.set(uf, attr, y, nothing)
@test isempty(MOI.get(uf, listattr))
MOI.set(uf, attr, [x, y], [2, 0])
@test MOI.get(uf, attr, z) === nothing
@test !MOI.is_empty(uf)
@test MOI.get(uf, listattr) == [attr]
Expand Down Expand Up @@ -235,18 +250,13 @@ function test_supported_constraint_attributes()
cx = _add_constraint(uf, x, 0.0)
cy = _add_constraint(uf, y, 1.0)
cz = _add_constraint(uf, z, 2.0)
F, S = MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}
_test_Variable_Constraint_attributes(
uf,
model,
MOI.Test.UnknownConstraintAttribute(),
MOI.ListOfConstraintAttributesSet{
MOI.ScalarAffineFunction{Float64},
MOI.LessThan{Float64},
}(),
MOI.ConstraintIndex{
MOI.ScalarAffineFunction{Float64},
MOI.LessThan{Float64},
},
MOI.ListOfConstraintAttributesSet{F,S}(),
MOI.ConstraintIndex{F,S},
uf -> _add_constraint(uf, x, 0.0),
cx,
cy,
Expand Down Expand Up @@ -282,18 +292,13 @@ function test_unsupported_constraint_attributes()
cx = _add_constraint(uf, x, 0.0)
cy = _add_constraint(uf, y, 1.0)
cz = _add_constraint(uf, z, 2.0)
F, S = MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}
_test_Variable_Constraint_attributes(
uf,
model,
MOI.Test.UnknownConstraintAttribute(),
MOI.ListOfConstraintAttributesSet{
MOI.ScalarAffineFunction{Float64},
MOI.EqualTo{Float64},
}(),
MOI.ConstraintIndex{
MOI.ScalarAffineFunction{Float64},
MOI.EqualTo{Float64},
},
MOI.ListOfConstraintAttributesSet{F,S}(),
MOI.ConstraintIndex{F,S},
uf -> _add_constraint(uf, x, 0.0),
cx,
cy,
Expand Down
Loading