Skip to content

Commit a7cbccd

Browse files
committed
Disallow querying is_set_by_optimize attributes unless explicitly supported
1 parent 084ca30 commit a7cbccd

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/MultiObjectiveAlgorithms.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,18 @@ function MOI.set(model::Optimizer, attr::_ATTRIBUTES, args...)
446446
end
447447

448448
function MOI.get(model::Optimizer, attr::_ATTRIBUTES, args...)
449+
if MOI.is_set_by_optimize(attr)
450+
msg = "MOA does not support querying this attribute."
451+
throw(MOI.GetAttributeNotAllowed(attr, msg))
452+
end
449453
return MOI.get(model.inner, attr, args...)
450454
end
451455

452456
function MOI.get(model::Optimizer, attr::_ATTRIBUTES, arg::Vector{T}) where {T}
457+
if MOI.is_set_by_optimize(attr)
458+
msg = "MOA does not support querying this attribute."
459+
throw(MOI.GetAttributeNotAllowed(attr, msg))
460+
end
453461
return MOI.get.(model, attr, arg)
454462
end
455463

test/test_model.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,30 @@ function test_solve_time()
100100
return
101101
end
102102

103+
function test_unnsupported_attributes()
104+
model = MOA.Optimizer(HiGHS.Optimizer)
105+
MOI.set(model, MOI.Silent(), true)
106+
x = MOI.add_variables(model, 2)
107+
c = MOI.add_constraint.(model, x, MOI.GreaterThan(0.0))
108+
f = MOI.Utilities.operate(vcat, Float64, 1.0 .* x...)
109+
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
110+
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
111+
MOI.optimize!(model)
112+
@test_throws(
113+
MOI.GetAttributeNotAllowed{MOI.RelativeGap},
114+
MOI.get(model, MOI.RelativeGap()),
115+
)
116+
@test_throws(
117+
MOI.GetAttributeNotAllowed{MOI.DualObjectiveValue},
118+
MOI.get(model, MOI.DualObjectiveValue()),
119+
)
120+
@test_throws(
121+
MOI.GetAttributeNotAllowed{MOI.ConstraintDual},
122+
MOI.get(model, MOI.ConstraintDual(), c),
123+
)
124+
return
125+
end
126+
103127
end
104128

105129
TestModel.run_tests()

0 commit comments

Comments
 (0)