Skip to content
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

Test that added_... is implemented for concrete types and warn if typ… #870

Merged
merged 3 commits into from
Sep 4, 2019
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
4 changes: 2 additions & 2 deletions src/Bridges/Constraint/det.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ end

MOI.supports_constraint(::Type{LogDetBridge{T}}, ::Type{<:Union{MOI.VectorOfVariables, MOI.VectorAffineFunction{T}}}, ::Type{MOI.LogDetConeTriangle}) where T = true
MOIB.added_constrained_variable_types(::Type{<:LogDetBridge}) = Tuple{DataType}[]
MOIB.added_constraint_types(::Type{LogDetBridge{T}}, ::Type{<:Union{MOI.VectorOfVariables, MOI.VectorAffineFunction{T}}}, ::Type{MOI.LogDetConeTriangle}) where T = [(MOI.VectorAffineFunction{T}, MOI.PositiveSemidefiniteConeTriangle), (MOI.VectorAffineFunction{T}, MOI.ExponentialCone), (MOI.ScalarAffineFunction{T}, MOI.LessThan{T})]
MOIB.added_constraint_types(::Type{LogDetBridge{T}}) where T = [(MOI.VectorAffineFunction{T}, MOI.PositiveSemidefiniteConeTriangle), (MOI.VectorAffineFunction{T}, MOI.ExponentialCone), (MOI.ScalarAffineFunction{T}, MOI.LessThan{T})]

"""
sublog(model, x::MOI.VariableIndex, y::MOI.VariableIndex, z::MOI.VariableIndex, ::Type{T}) where T
Expand Down Expand Up @@ -197,7 +197,7 @@ end

MOI.supports_constraint(::Type{RootDetBridge{T}}, ::Type{<:Union{MOI.VectorOfVariables, MOI.VectorAffineFunction{T}}}, ::Type{MOI.RootDetConeTriangle}) where T = true
MOIB.added_constrained_variable_types(::Type{<:RootDetBridge}) = Tuple{DataType}[]
MOIB.added_constraint_types(::Type{RootDetBridge{T}}, ::Type{<:Union{MOI.VectorOfVariables, MOI.VectorAffineFunction{T}}}, ::Type{MOI.RootDetConeTriangle}) where T = [(MOI.VectorAffineFunction{T}, MOI.PositiveSemidefiniteConeTriangle), (MOI.VectorAffineFunction{T}, MOI.GeometricMeanCone)]
MOIB.added_constraint_types(::Type{RootDetBridge{T}}) where T = [(MOI.VectorAffineFunction{T}, MOI.PositiveSemidefiniteConeTriangle), (MOI.VectorAffineFunction{T}, MOI.GeometricMeanCone)]

# Attributes, Bridge acting as a model
MOI.get(b::RootDetBridge, ::MOI.NumberOfVariables) = length(b.Δ)
Expand Down
14 changes: 10 additions & 4 deletions test/Bridges/Constraint/det.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ config = MOIT.TestConfig()
MOIT.logdett1ftest(bridged_mock, config)
# Dual is not yet implemented for LogDet bridge
ci = first(MOI.get(bridged_mock, MOI.ListOfConstraintIndices{MOI.VectorAffineFunction{Float64}, MOI.LogDetConeTriangle}()))
test_delete_bridge(bridged_mock, ci, 5, ((MOI.VectorAffineFunction{Float64}, MOI.ExponentialCone, 0), (MOI.VectorAffineFunction{Float64}, MOI.PositiveSemidefiniteConeTriangle, 0)))
test_delete_bridge(bridged_mock, ci, 5, (
(MOI.VectorAffineFunction{Float64}, MOI.ExponentialCone, 0),
(MOI.VectorAffineFunction{Float64}, MOI.PositiveSemidefiniteConeTriangle, 0),
(MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}, 0)
))
end

@testset "RootDet" begin
Expand All @@ -28,7 +32,9 @@ end
MOIT.rootdett1ftest(bridged_mock, config)
# Dual is not yet implemented for RootDet bridge
ci = first(MOI.get(bridged_mock, MOI.ListOfConstraintIndices{MOI.VectorAffineFunction{Float64}, MOI.RootDetConeTriangle}()))
test_delete_bridge(bridged_mock, ci, 4, ((MOI.VectorAffineFunction{Float64}, MOI.RotatedSecondOrderCone, 0),
(MOI.VectorAffineFunction{Float64}, MOI.GeometricMeanCone, 0),
(MOI.VectorAffineFunction{Float64}, MOI.PositiveSemidefiniteConeTriangle, 0)))
test_delete_bridge(bridged_mock, ci, 4, (
(MOI.VectorAffineFunction{Float64}, MOI.RotatedSecondOrderCone, 0),
(MOI.VectorAffineFunction{Float64}, MOI.GeometricMeanCone, 0),
(MOI.VectorAffineFunction{Float64}, MOI.PositiveSemidefiniteConeTriangle, 0)
))
end
4 changes: 2 additions & 2 deletions test/Bridges/Constraint/functionize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ config_with_basis = MOIT.TestConfig(basis = true)
@test MOI.get(bridged_mock, MOI.ConstraintSet(), ci) == MOI.GreaterThan(0.0)
MOI.set(bridged_mock, MOI.ConstraintSet(), ci, MOI.GreaterThan(1.0))
@test MOI.get(bridged_mock, MOI.ConstraintSet(), ci) == MOI.GreaterThan(1.0)
test_delete_bridge(bridged_mock, ci, 2, ((MOI.ScalarAffineFunction{Float64}, MOI.Interval{Float64}, 0),))
test_delete_bridge(bridged_mock, ci, 2, ((MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}, 0),))

MOIT.basic_constraint_tests(bridged_mock, config,
include=[(F, S) for
Expand Down Expand Up @@ -79,7 +79,7 @@ end
@test MOI.get(bridged_mock, MOI.ConstraintSet(), ci) == MOI.PowerCone(0.1)
MOI.set(bridged_mock, MOI.ConstraintSet(), ci, MOI.PowerCone(0.2))
@test MOI.get(bridged_mock, MOI.ConstraintSet(), ci) == MOI.PowerCone(0.2)
test_delete_bridge(bridged_mock, ci, 3, ((MOI.VectorAffineFunction{Float64}, MOI.Nonnegatives, 0),))
test_delete_bridge(bridged_mock, ci, 3, ((MOI.VectorAffineFunction{Float64}, MOI.PowerCone{Float64}, 0),))

MOIT.basic_constraint_tests(bridged_mock, config,
include=[(F, S) for
Expand Down
10 changes: 8 additions & 2 deletions test/Bridges/Constraint/slack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ config = MOIT.TestConfig()
MOI.modify(bridged_mock, ci, MOI.ScalarConstantChange{Float64}(1.0))
@test MOI.get(bridged_mock, MOI.ConstraintFunction(), ci) ≈
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm{Float64}.([2.0, 1.0], [x, y]), 1.0)
test_delete_bridge(bridged_mock, ci, 2, ((MOI.ScalarAffineFunction{Float64}, MOI.Interval{Float64}, 0),))
test_delete_bridge(bridged_mock, ci, 2, (
(MOI.SingleVariable, MOI.GreaterThan{Float64}, 0),
(MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}, 0)
))

MOIT.basic_constraint_tests(bridged_mock, config,
include=[(F, S) for
Expand Down Expand Up @@ -99,7 +102,10 @@ end
MOI.modify(bridged_mock, ci, MOI.VectorConstantChange([1.0]))
@test MOI.get(bridged_mock, MOI.ConstraintFunction(), ci) ≈
MOI.VectorAffineFunction(MOI.VectorAffineTerm.(1, MOI.ScalarAffineTerm.([2.0, 1.0], [x, y])), [1.0])
test_delete_bridge(bridged_mock, ci, 2, ((MOI.VectorAffineFunction{Float64}, MOI.Zeros, 0),))
test_delete_bridge(bridged_mock, ci, 2, (
(MOI.VectorOfVariables, MOI.Nonpositives, 0),
(MOI.VectorAffineFunction{Float64}, MOI.Zeros, 0)
))

fp = MOI.VectorAffineFunction(MOI.VectorAffineTerm.([1,2,3], MOI.ScalarAffineTerm.([1.0, 2.0, 3.0], [x, y, y])), [0.0, 0.0, 0.0])
cp = MOI.add_constraint(bridged_mock, fp, MOI.PowerCone(0.1))
Expand Down
6 changes: 5 additions & 1 deletion test/Bridges/Variable/rsoc_to_psd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ bridged_mock = MOIB.Variable.RSOCtoPSD{Float64}(mock)

test_delete_bridged_variables(bridged_mock, xy, MOI.RotatedSecondOrderCone, 2, (
(MOI.VectorOfVariables, MOI.Nonnegatives, 0),
(MOI.VectorOfVariables, MOI.PositiveSemidefiniteConeTriangle, 0),
(MOI.SingleVariable, MOI.EqualTo{Float64}, 0),
(MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}, 0),
(MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}, 1)
))
end
Expand Down Expand Up @@ -164,9 +167,10 @@ end
end

test_delete_bridged_variables(bridged_mock, v, MOI.RotatedSecondOrderCone, 4, (
(MOI.VectorOfVariables, MOI.Nonnegatives, 0),
(MOI.VectorOfVariables, MOI.PositiveSemidefiniteConeTriangle, 0),
(MOI.SingleVariable, MOI.EqualTo{Float64}, 0),
(MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}, 0),
(MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}, 0)
))
end
end
17 changes: 17 additions & 0 deletions test/Bridges/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@ function test_num_constraints(bridged_mock, F, S, n)
@test ((F, S) in MOI.get(bridged_mock, MOI.ListOfConstraints())) == !iszero(n)
end

function warn_incomplete_list_num_constraints(BT, list_num_constraints)
for (S,) in MOIB.added_constrained_variable_types(BT)
F = MOIU.variable_function_type(S)
if !any(c -> c[1] == F && c[2] == S, list_num_constraints)
error("Bridges of type $BT add constrained variable in $S but their number is not tested.")
end
end
for (F, S) in MOIB.added_constraint_types(BT)
if !any(c -> c[1] == F && c[2] == S, list_num_constraints)
error("Bridges of type $BT add $F-in-$S constraints but their number is not tested.")
end
end
end

# Test deletion of constraint bridge used for constraint `ci`
function test_delete_bridge(
m::MOIB.AbstractBridgeOptimizer, ci::MOI.ConstraintIndex{F, S}, nvars::Int,
list_num_constraints::Tuple; used_bridges = 1, num_bridged = 1) where {F, S}
warn_incomplete_list_num_constraints(typeof(MOIB.bridge(m, ci)), list_num_constraints)
function num_bridges()
return count(bridge -> true, values(MOIB.Constraint.bridges(m)))
end
Expand Down Expand Up @@ -35,6 +50,7 @@ end
function test_delete_bridged_variable(
m::MOIB.AbstractBridgeOptimizer, vi::MOI.VariableIndex, S::Type,
nvars::Int, list_num_constraints::Tuple; used_bridges = 1, num_bridged = 1, used_constraints = 1)
warn_incomplete_list_num_constraints(typeof(MOIB.bridge(m, vi)), list_num_constraints)
function num_bridges()
return count(bridge -> true, values(MOIB.Variable.bridges(m)))
end
Expand Down Expand Up @@ -66,6 +82,7 @@ end
function test_delete_bridged_variables(
m::MOIB.AbstractBridgeOptimizer, vis::Vector{MOI.VariableIndex}, S::Type,
nvars::Int, list_num_constraints::Tuple; used_bridges = 1, num_bridged = 1)
warn_incomplete_list_num_constraints(typeof(MOIB.bridge(m, vis[1])), list_num_constraints)
function num_bridges()
return count(bridge -> true, values(MOIB.Variable.bridges(m)))
end
Expand Down