Skip to content
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
47 changes: 47 additions & 0 deletions src/Test/test_objective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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_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_objective_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
116 changes: 0 additions & 116 deletions test/Utilities/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}())
Expand Down Expand Up @@ -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
Expand All @@ -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()
76 changes: 76 additions & 0 deletions test/Utilities/vector_bounds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()