Skip to content

Test InexactError thrown for bad objective function type #502

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

Merged
merged 1 commit into from
Aug 26, 2018
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
25 changes: 25 additions & 0 deletions src/Test/UnitTests/objectives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,31 @@ function feasibility_sense(model::MOI.ModelLike, config::TestConfig)
end
unittests["feasibility_sense"] = feasibility_sense

"""
get_objective_function(model::MOI.ModelLike, config::TestConfig)

Test get objective function.
"""
function get_objective_function(model::MOI.ModelLike, config::TestConfig)
MOI.empty!(model)
@test MOI.isempty(model)
obj_attr = MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}()
@test MOI.supports(model, obj_attr)
MOIU.loadfromstring!(model,"""
variables: x
minobjective: 2.0x + 1.0
""")
x = MOI.get(model, MOI.VariableIndex, "x")
expected_obj_fun = MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(2.0, x)],
1.0)
@test_throws InexactError begin
MOI.get(model, MOI.ObjectiveFunction{MOI.SingleVariable}())
end
obj_fun = MOI.get(model, obj_attr)
@test obj_fun ≈ expected_obj_fun
end
unittests["get_objective_function"] = get_objective_function

"""
solve_constant_obj(model::MOI.ModelLike, config::TestConfig)

Expand Down
6 changes: 5 additions & 1 deletion src/Utilities/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,11 @@ function MOI.get(model::AbstractModel, ::MOI.ObjectiveFunctionType)
end
function MOI.get(model::AbstractModel, ::MOI.ObjectiveFunction{T})::T where T
if typeof(model.objective) != T
throw(InexactError())
if VERSION >= v"0.7-"
throw(InexactError(:get, T, model.objective))
else
throw(InexactError())
end
end
model.objective
end
Expand Down