Skip to content

Add tests for ObjectiveBound #446

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 2 commits into from
Aug 4, 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
81 changes: 81 additions & 0 deletions src/Test/UnitTests/solve.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""
solve_objbound_edge_cases(model::MOI.ModelLike, config::TestConfig)
Test a variety of edge cases related to the ObjectiveBound attribute.
"""
function solve_objbound_edge_cases(model::MOI.ModelLike, config::TestConfig)
@testset "Min IP with constant" begin
MOI.empty!(model)
@test MOI.isempty(model)
MOIU.loadfromstring!(model, """
variables: x
minobjective: 2.0x + -1.0
c1: x >= 1.5
c2: x in Integer()
""")
x = MOI.get(model, MOI.VariableIndex, "x")
test_model_solution(model, config;
objective_value = 3.0,
variable_primal = [(x, 2.0)]
)
if config.solve
@test MOI.get(model, MOI.ObjectiveBound()) <= 3.0
end
end

@testset "Max IP with constant" begin
MOI.empty!(model)
@test MOI.isempty(model)
MOIU.loadfromstring!(model, """
variables: x
maxobjective: 2.0x + 1.0
c1: x <= 1.5
c2: x in Integer()
""")
x = MOI.get(model, MOI.VariableIndex, "x")
test_model_solution(model, config;
objective_value = 3.0,
variable_primal = [(x, 1.0)]
)
if config.solve
@test MOI.get(model, MOI.ObjectiveBound()) >= 3.0
end
end

@testset "Min LP with constant" begin
MOI.empty!(model)
@test MOI.isempty(model)
MOIU.loadfromstring!(model, """
variables: x
minobjective: 2.0x + -1.0
c1: x >= 1.5
""")
x = MOI.get(model, MOI.VariableIndex, "x")
test_model_solution(model, config;
objective_value = 2.0,
variable_primal = [(x, 1.5)]
)
if config.solve
@test MOI.get(model, MOI.ObjectiveBound()) <= 2.0
end
end

@testset "Max LP with constant" begin
MOI.empty!(model)
@test MOI.isempty(model)
MOIU.loadfromstring!(model, """
variables: x
maxobjective: 2.0x + 1.0
c1: x <= 1.5
""")
x = MOI.get(model, MOI.VariableIndex, "x")
test_model_solution(model, config;
objective_value = 4.0,
variable_primal = [(x, 1.5)]
)
if config.solve
@test MOI.get(model, MOI.ObjectiveBound()) >= 4.0
end
end
end
unittests["solve_objbound_edge_cases"] = solve_objbound_edge_cases
1 change: 1 addition & 0 deletions src/Test/UnitTests/unit_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,6 @@ include("objectives.jl")
include("constraints.jl")
include("basic_constraint_tests.jl")
include("modifications.jl")
include("solve.jl")

@moitestset unit
11 changes: 11 additions & 0 deletions src/Utilities/mockoptimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mutable struct MockOptimizer{MT<:MOI.ModelLike} <: MOI.AbstractOptimizer
resultcount::Int
evalobjective::Bool # Computes ObjectiveValue by evaluation ObjectiveFunction with VariablePrimal
objectivevalue::Float64
objectivebound::Float64 # set this using MOI.set!(model, MOI.ObjectiveBound(), value)
primalstatus::MOI.ResultStatusCode
dualstatus::MOI.ResultStatusCode
varprimal::Dict{MOI.VariableIndex,Float64}
Expand Down Expand Up @@ -57,6 +58,7 @@ MockOptimizer(inner_model::MOI.ModelLike; needsallocateload=false, evalobjective
0,
evalobjective,
NaN,
NaN,
MOI.UnknownResultStatus,
MOI.UnknownResultStatus,
Dict{MOI.VariableIndex,Float64}(),
Expand Down Expand Up @@ -180,6 +182,13 @@ MOI.get(mock::MockOptimizer, attr::MOI.AbstractConstraintAttribute, idx::MOI.Con
MOI.get(mock::MockOptimizer, ::MOI.ConstraintDual, idx::MOI.ConstraintIndex) = mock.condual[xor_index(idx)]
MOI.get(mock::MockOptimizer, ::MockConstraintAttribute, idx::MOI.ConstraintIndex) = mock.conattribute[xor_index(idx)]

MOI.supports(mock::MockOptimizer, ::MOI.ObjectiveBound) = true
MOI.canget(mock::MockOptimizer, ::MOI.ObjectiveBound) = !isnan(mock.objectivebound)
MOI.get(mock::MockOptimizer, ::MOI.ObjectiveBound) = mock.objectivebound
function MOI.set!(mock::MockOptimizer, ::MOI.ObjectiveBound, value::Float64)
mock.objectivebound = value
end

function MOI.empty!(mock::MockOptimizer)
MOI.empty!(mock.inner_model)
mock.attribute = 0
Expand All @@ -191,6 +200,7 @@ function MOI.empty!(mock::MockOptimizer)
mock.terminationstatus = MOI.Success
mock.resultcount = 0
mock.objectivevalue = NaN
mock.objectivebound = NaN
mock.primalstatus = MOI.UnknownResultStatus
mock.dualstatus = MOI.UnknownResultStatus
mock.varprimal = Dict{MOI.VariableIndex,Float64}()
Expand All @@ -206,6 +216,7 @@ function MOI.isempty(mock::MockOptimizer)
!mock.solved && !mock.hasprimal && !mock.hasdual &&
mock.terminationstatus == MOI.Success &&
mock.resultcount == 0 && isnan(mock.objectivevalue) &&
isnan(mock.objectivebound) &&
mock.primalstatus == MOI.UnknownResultStatus &&
mock.dualstatus == MOI.UnknownResultStatus
end
Expand Down
25 changes: 24 additions & 1 deletion test/Test/unit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ end
"solve_qcp_edge_cases",
"solve_affine_deletion_edge_cases",
"solve_duplicate_terms_obj",
"solve_integer_edge_cases"
"solve_integer_edge_cases",
"solve_objbound_edge_cases"
])

@testset "solve_blank_obj" begin
Expand Down Expand Up @@ -209,6 +210,28 @@ end
)
MOIT.solve_integer_edge_cases(mock, config)
end
@testset "solve_objbound_edge_cases" begin
MOIU.set_mock_optimize!(mock,
(mock::MOIU.MockOptimizer) -> begin
MOI.set!(mock, MOI.ObjectiveBound(), 3.0)
MOIU.mock_optimize!(mock, MOI.Success, (MOI.FeasiblePoint, [2.0]))
end,
(mock::MOIU.MockOptimizer) -> begin
MOI.set!(mock, MOI.ObjectiveBound(), 3.0)
MOIU.mock_optimize!(mock, MOI.Success, (MOI.FeasiblePoint, [1.0]))
end,
(mock::MOIU.MockOptimizer) -> begin
MOI.set!(mock, MOI.ObjectiveBound(), 2.0)
MOIU.mock_optimize!(mock, MOI.Success, (MOI.FeasiblePoint, [1.5]))
end,
(mock::MOIU.MockOptimizer) -> begin
MOI.set!(mock, MOI.ObjectiveBound(), 4.0)
MOIU.mock_optimize!(mock, MOI.Success, (MOI.FeasiblePoint, [1.5]))
end
)
MOIT.solve_objbound_edge_cases(mock, config)
end

end

@testset "modifications" begin
Expand Down