Skip to content

Commit f5d7b81

Browse files
authored
Disallow querying is_set_by_optimize attributes unless explicitly supported (#88)
1 parent b00863e commit f5d7b81

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/MultiObjectiveAlgorithms.jl

Lines changed: 16 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

@@ -569,6 +577,14 @@ function MOI.get(
569577
return sol.x[x]
570578
end
571579

580+
function MOI.get(
581+
model::Optimizer,
582+
attr::MOI.VariablePrimal,
583+
x::Vector{MOI.VariableIndex},
584+
)
585+
return MOI.get.(model, attr, x)
586+
end
587+
572588
function MOI.get(model::Optimizer, attr::MOI.ObjectiveValue)
573589
return model.solutions[attr.result_index].y
574590
end

test/test_model.jl

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

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

104128
TestModel.run_tests()

0 commit comments

Comments
 (0)