From 591b54457423b51665ddda0fb292372ce32c191c Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 22 Jul 2021 11:58:06 +1200 Subject: [PATCH 1/2] Move tests out of Utilities/model.jl --- src/Test/test_objective.jl | 47 +++++++++++++ test/Utilities/model.jl | 116 -------------------------------- test/Utilities/vector_bounds.jl | 76 +++++++++++++++++++++ 3 files changed, 123 insertions(+), 116 deletions(-) diff --git a/src/Test/test_objective.jl b/src/Test/test_objective.jl index 6027ed0655..bc07566fe2 100644 --- a/src/Test/test_objective.jl +++ b/src/Test/test_objective.jl @@ -473,3 +473,50 @@ function setup_test( ) return end + +""" + test_objective_set_via_modify(model::MOI.ModelLike, config::Config) + +Test that a SclaarAffineFunction can be set via modification without setting an +objective prior. +""" +function test_objective_set_via_modify(model::MOI.ModelLike, config::Config) + attr = MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}() + @requires MOI.supports(model, attr) + @requires _supports(config, MOI.modify) + @requires _supports(config, MOI.ScalarCoefficientChange) + @test MOI.get(model, MOI.ListOfModelAttributesSet()) == [] + x = MOI.add_variable(model) + MOI.modify(model, attr, MOI.ScalarCoefficientChange(x, 1.0)) + @test MOI.get(model, MOI.ListOfModelAttributesSet()) == [attr] + return +end + +""" + test_incorrect_modifications(model::MOI.ModelLike, config::Config) + +Test that constraint sets cannot be set for the wrong set type, and that +SingleVariable functions cannot be modified. +""" +function test_incorrect_modifications(model::MOI.ModelLike, ::Config) + @requires MOI.supports_constraint( + model, + MOI.ScalarAffineFunction{Float64}, + MOI.EqualTo{Float64}, + ) + x = MOI.add_variable(model) + c = MOI.add_constraint( + model, + MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 0.0), + MOI.EqualTo(1.0), + ) + @test_throws( + ArgumentError, + MOI.set(model, MOI.ConstraintSet(), c, MOI.LessThan(1.0)), + ) + @test_throws( + ArgumentError, + MOI.set(model, MOI.ConstraintFunction(), c, MOI.SingleVariable(x)), + ) + return +end diff --git a/test/Utilities/model.jl b/test/Utilities/model.jl index cded17397b..0829d5d258 100644 --- a/test/Utilities/model.jl +++ b/test/Utilities/model.jl @@ -154,15 +154,6 @@ function test_TestExternalModel() return end -function test_bound_twice() - for T in [Int, Float64] - model = MOI.Utilities.Model{T}() - MOI.Test.test_model_LowerBoundAlreadySet(model, MOI.Test.Config(T)) - MOI.Test.test_model_UpperBoundAlreadySet(model, MOI.Test.Config(T)) - end - return -end - function _test_ObjectiveFunction(T) model = MOI.Utilities.Model{T}() @test !MOI.supports(model, MOI.ObjectiveFunction{DummyFunction}()) @@ -479,26 +470,6 @@ function test_default_fallbacks() ) end -function test_ListOfConstraintTypesPresent() - @testset "$set" for set in ( - MOI.EqualTo(1.0), - MOI.GreaterThan(1.0), - MOI.LessThan(1.0), - MOI.Interval(1.0, 2.0), - MOI.Semicontinuous(1.0, 2.0), - MOI.Semiinteger(1.0, 2.0), - MOI.Integer(), - MOI.ZeroOne(), - ) - model = MOI.Utilities.Model{Float64}() - x = MOI.add_variable(model) - MOI.add_constraint(model, MOI.SingleVariable(x), set) - @test MOI.get(model, MOI.ListOfConstraintTypesPresent()) == - [(MOI.SingleVariable, typeof(set))] - end - return -end - function test_extension_dictionary() model = MOI.Utilities.Model{Float64}() model.ext[:my_store] = 1 @@ -513,93 +484,6 @@ function test_extension_dictionary() return end -""" -TODO(odow): consider moving this to MOI.Test -""" -function test_objective_set_via_modify() - model = MOI.Utilities.Model{Float64}() - x = MOI.add_variable(model) - attr = MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}() - MOI.modify(model, attr, MOI.ScalarCoefficientChange(x, 1.0)) - @test attr in MOI.get(model, MOI.ListOfModelAttributesSet()) - return -end - -function test_incorrect_modifications() - model = MOI.Utilities.Model{Float64}() - x = MOI.add_variable(model) - c = MOI.add_constraint( - model, - MOI.ScalarAffineFunction([MOI.ScalarAffineTerm(1.0, x)], 0.0), - MOI.EqualTo(1.0), - ) - @test_throws( - ArgumentError, - MOI.set(model, MOI.ConstraintSet(), c, MOI.LessThan(1.0)), - ) - @test_throws( - ArgumentError, - MOI.set(model, MOI.ConstraintFunction(), c, MOI.SingleVariable(x)), - ) - return -end - -function _test_bound_vectors(::Type{T}, nolb, noub) where {T} - model = MOI.Utilities.Model{T}() - @test model.variable_bounds.lower == T[] - @test model.variable_bounds.upper == T[] - x = MOI.add_variable(model) - fx = MOI.SingleVariable(x) - @test model.variable_bounds.lower == [nolb] - @test model.variable_bounds.upper == [noub] - ux = MOI.add_constraint(model, fx, MOI.LessThan(T(1))) - @test model.variable_bounds.lower == [nolb] - @test model.variable_bounds.upper == [T(1)] - y = MOI.add_variable(model) - fy = MOI.SingleVariable(y) - @test model.variable_bounds.lower == [nolb, nolb] - @test model.variable_bounds.upper == [T(1), noub] - cy = MOI.add_constraint(model, fy, MOI.Interval(T(2), T(3))) - @test model.variable_bounds.lower == [nolb, T(2)] - @test model.variable_bounds.upper == [T(1), T(3)] - lx = MOI.add_constraint(model, fx, MOI.GreaterThan(T(0))) - @test model.variable_bounds.lower == [T(0), T(2)] - @test model.variable_bounds.upper == [T(1), T(3)] - MOI.delete(model, lx) - @test model.variable_bounds.lower == [nolb, T(2)] - @test model.variable_bounds.upper == [T(1), T(3)] - MOI.delete(model, ux) - @test model.variable_bounds.lower == [nolb, T(2)] - @test model.variable_bounds.upper == [noub, T(3)] - cx = MOI.add_constraint(model, fx, MOI.Semicontinuous(T(3), T(4))) - @test model.variable_bounds.lower == [T(3), T(2)] - @test model.variable_bounds.upper == [T(4), T(3)] - MOI.delete(model, cy) - @test model.variable_bounds.lower == [T(3), nolb] - @test model.variable_bounds.upper == [T(4), noub] - sy = MOI.add_constraint(model, fy, MOI.Semiinteger(T(-2), T(-1))) - @test model.variable_bounds.lower == [T(3), T(-2)] - @test model.variable_bounds.upper == [T(4), T(-1)] - MOI.delete(model, sy) - @test model.variable_bounds.lower == [T(3), nolb] - @test model.variable_bounds.upper == [T(4), noub] - ey = MOI.add_constraint(model, fy, MOI.EqualTo(T(-3))) - @test model.variable_bounds.lower == [T(3), T(-3)] - @test model.variable_bounds.upper == [T(4), T(-3)] - MOI.delete(model, ey) - @test model.variable_bounds.lower == [T(3), nolb] - @test model.variable_bounds.upper == [T(4), noub] - MOI.delete(model, cx) - @test model.variable_bounds.lower == [nolb, nolb] - @test model.variable_bounds.upper == [noub, noub] -end - -function test_bound_vectors() - _test_bound_vectors(Int, 0, 0) - _test_bound_vectors(Float64, -Inf, Inf) - return -end - end # module TestModel.runtests() diff --git a/test/Utilities/vector_bounds.jl b/test/Utilities/vector_bounds.jl index 5c2633274a..ceea967305 100644 --- a/test/Utilities/vector_bounds.jl +++ b/test/Utilities/vector_bounds.jl @@ -259,6 +259,82 @@ function test_set_from_constants() return end +function _test_bound_vectors(::Type{T}, nolb, noub) where {T} + variable_bounds = MOI.Utilities.SingleVariableConstraints{T}() + @test variable_bounds.lower == T[] + @test variable_bounds.upper == T[] + x = MOI.add_variable(variable_bounds) + fx = MOI.SingleVariable(x) + @test variable_bounds.lower == [nolb] + @test variable_bounds.upper == [noub] + ux = MOI.add_constraint(variable_bounds, fx, MOI.LessThan(T(1))) + @test variable_bounds.lower == [nolb] + @test variable_bounds.upper == [T(1)] + y = MOI.add_variable(variable_bounds) + fy = MOI.SingleVariable(y) + @test variable_bounds.lower == [nolb, nolb] + @test variable_bounds.upper == [T(1), noub] + cy = MOI.add_constraint(variable_bounds, fy, MOI.Interval(T(2), T(3))) + @test variable_bounds.lower == [nolb, T(2)] + @test variable_bounds.upper == [T(1), T(3)] + lx = MOI.add_constraint(variable_bounds, fx, MOI.GreaterThan(T(0))) + @test variable_bounds.lower == [T(0), T(2)] + @test variable_bounds.upper == [T(1), T(3)] + MOI.delete(variable_bounds, lx) + @test variable_bounds.lower == [nolb, T(2)] + @test variable_bounds.upper == [T(1), T(3)] + MOI.delete(variable_bounds, ux) + @test variable_bounds.lower == [nolb, T(2)] + @test variable_bounds.upper == [noub, T(3)] + cx = MOI.add_constraint(variable_bounds, fx, MOI.Semicontinuous(T(3), T(4))) + @test variable_bounds.lower == [T(3), T(2)] + @test variable_bounds.upper == [T(4), T(3)] + MOI.delete(variable_bounds, cy) + @test variable_bounds.lower == [T(3), nolb] + @test variable_bounds.upper == [T(4), noub] + sy = MOI.add_constraint(variable_bounds, fy, MOI.Semiinteger(T(-2), T(-1))) + @test variable_bounds.lower == [T(3), T(-2)] + @test variable_bounds.upper == [T(4), T(-1)] + MOI.delete(variable_bounds, sy) + @test variable_bounds.lower == [T(3), nolb] + @test variable_bounds.upper == [T(4), noub] + ey = MOI.add_constraint(variable_bounds, fy, MOI.EqualTo(T(-3))) + @test variable_bounds.lower == [T(3), T(-3)] + @test variable_bounds.upper == [T(4), T(-3)] + MOI.delete(variable_bounds, ey) + @test variable_bounds.lower == [T(3), nolb] + @test variable_bounds.upper == [T(4), noub] + MOI.delete(variable_bounds, cx) + @test variable_bounds.lower == [nolb, nolb] + @test variable_bounds.upper == [noub, noub] +end + +function test_bound_vectors() + _test_bound_vectors(Int, 0, 0) + _test_bound_vectors(Float64, -Inf, Inf) + return +end + +function test_ListOfConstraintTypesPresent_2() + for set in ( + MOI.EqualTo(1.0), + MOI.GreaterThan(1.0), + MOI.LessThan(1.0), + MOI.Interval(1.0, 2.0), + MOI.Semicontinuous(1.0, 2.0), + MOI.Semiinteger(1.0, 2.0), + MOI.Integer(), + MOI.ZeroOne(), + ) + model = MOI.Utilities.SingleVariableConstraints{Float64}() + x = MOI.add_variable(model) + MOI.add_constraint(model, MOI.SingleVariable(x), set) + @test MOI.get(model, MOI.ListOfConstraintTypesPresent()) == + [(MOI.SingleVariable, typeof(set))] + end + return +end + end # module TestBox.runtests() From 42626b89b768c6f32ca1e9c94bd576f2ceb3bbae Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 22 Jul 2021 12:06:51 +1200 Subject: [PATCH 2/2] Fix typo --- src/Test/test_objective.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Test/test_objective.jl b/src/Test/test_objective.jl index bc07566fe2..ff0950152e 100644 --- a/src/Test/test_objective.jl +++ b/src/Test/test_objective.jl @@ -493,12 +493,12 @@ function test_objective_set_via_modify(model::MOI.ModelLike, config::Config) end """ - test_incorrect_modifications(model::MOI.ModelLike, config::Config) + test_objective_incorrect_modifications(model::MOI.ModelLike, config::Config) Test that constraint sets cannot be set for the wrong set type, and that SingleVariable functions cannot be modified. """ -function test_incorrect_modifications(model::MOI.ModelLike, ::Config) +function test_objective_incorrect_modifications(model::MOI.ModelLike, ::Config) @requires MOI.supports_constraint( model, MOI.ScalarAffineFunction{Float64},