Skip to content

Commit

Permalink
Disallow querying is_set_by_optimize attributes unless explicitly sup…
Browse files Browse the repository at this point in the history
…ported (#88)
  • Loading branch information
odow authored Oct 22, 2024
1 parent b00863e commit f5d7b81
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/MultiObjectiveAlgorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,18 @@ function MOI.set(model::Optimizer, attr::_ATTRIBUTES, args...)
end

function MOI.get(model::Optimizer, attr::_ATTRIBUTES, args...)
if MOI.is_set_by_optimize(attr)
msg = "MOA does not support querying this attribute."
throw(MOI.GetAttributeNotAllowed(attr, msg))
end
return MOI.get(model.inner, attr, args...)
end

function MOI.get(model::Optimizer, attr::_ATTRIBUTES, arg::Vector{T}) where {T}
if MOI.is_set_by_optimize(attr)
msg = "MOA does not support querying this attribute."
throw(MOI.GetAttributeNotAllowed(attr, msg))
end
return MOI.get.(model, attr, arg)
end

Expand Down Expand Up @@ -569,6 +577,14 @@ function MOI.get(
return sol.x[x]
end

function MOI.get(
model::Optimizer,
attr::MOI.VariablePrimal,
x::Vector{MOI.VariableIndex},
)
return MOI.get.(model, attr, x)
end

function MOI.get(model::Optimizer, attr::MOI.ObjectiveValue)
return model.solutions[attr.result_index].y
end
Expand Down
24 changes: 24 additions & 0 deletions test/test_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,30 @@ function test_solve_time()
return
end

function test_unnsupported_attributes()
model = MOA.Optimizer(HiGHS.Optimizer)
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variables(model, 2)
c = MOI.add_constraint.(model, x, MOI.GreaterThan(0.0))
f = MOI.Utilities.operate(vcat, Float64, 1.0 .* x...)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
MOI.optimize!(model)
@test_throws(
MOI.GetAttributeNotAllowed{MOI.RelativeGap},
MOI.get(model, MOI.RelativeGap()),
)
@test_throws(
MOI.GetAttributeNotAllowed{MOI.DualObjectiveValue},
MOI.get(model, MOI.DualObjectiveValue()),
)
@test_throws(
MOI.GetAttributeNotAllowed{MOI.ConstraintDual},
MOI.get(model, MOI.ConstraintDual(), c),
)
return
end

end

TestModel.run_tests()

0 comments on commit f5d7b81

Please sign in to comment.