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
3 changes: 0 additions & 3 deletions src/Bridges/Constraint/bridges/IndicatorToMILPBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ function MOI.delete(
model::MOI.ModelLike,
bridge::IndicatorToMILPBridge{T},
) where {T}
if bridge.slack === nothing
return # final_touch not called, so we can safely skip
end
MOI.delete.(model, bridge.slack_bounds)
MOI.delete(model, bridge.constraint)
MOI.delete(model, bridge.slack::MOI.VariableIndex)
Expand Down
2 changes: 2 additions & 0 deletions src/Bridges/Constraint/bridges/RSOCtoSOCBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@

function MOI.Bridges.map_function(::Type{<:RSOCtoSOCBridge{T}}, func) where {T}
scalars = MOI.Utilities.eachscalar(func)
# We cannot construct MOI.RotatedSecondOrderCone(1)
@assert length(scalars) >= 2

Check warning on line 63 in src/Bridges/Constraint/bridges/RSOCtoSOCBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/RSOCtoSOCBridge.jl#L63

Added line #L63 was not covered by tests
t, u, x = scalars[1], scalars[2], scalars[3:end]
ts = MOI.Utilities.operate!(/, T, t, sqrt(T(2)))
us = MOI.Utilities.operate!(/, T, u, sqrt(T(2)))
Expand Down
9 changes: 3 additions & 6 deletions src/Bridges/Constraint/bridges/SOCtoPSDBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,9 @@

function MOI.Bridges.map_function(::Type{<:RSOCtoPSDBridge{T}}, func) where {T}
scalars = MOI.Utilities.eachscalar(func)
if length(scalars) < 2
error(
"Unable to bridge RotatedSecondOrderCone to PSD because the ",
"dimension is too small: got $(length(scalars)), expected >= 2.",
)
elseif length(scalars) == 2
# We cannot construct MOI.RotatedSecondOrderCone(1)
@assert length(scalars) >= 2
if length(scalars) == 2

Check warning on line 261 in src/Bridges/Constraint/bridges/SOCtoPSDBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SOCtoPSDBridge.jl#L260-L261

Added lines #L260 - L261 were not covered by tests
return func
end
# Input is (t, u, x), and we need [t x'; x 2uI]
Expand Down
15 changes: 8 additions & 7 deletions src/Bridges/Constraint/bridges/SOCtoRSOCBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@
::Type{<:SOCtoRSOCBridge},
set::MOI.SecondOrderCone,
)
if MOI.dimension(set) == 1
error(
"Unable to reformulate a `SecondOrderCone` into a " *
"`RotatedSecondOrderCone` because the dimension of `1` is too " *
"small",
)
end
return MOI.RotatedSecondOrderCone(MOI.dimension(set))
end

Expand All @@ -69,6 +62,14 @@

function MOI.Bridges.map_function(::Type{<:SOCtoRSOCBridge{T}}, func) where {T}
scalars = MOI.Utilities.eachscalar(func)
if length(scalars) < 2
err = DimensionMismatch(

Check warning on line 66 in src/Bridges/Constraint/bridges/SOCtoRSOCBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SOCtoRSOCBridge.jl#L65-L66

Added lines #L65 - L66 were not covered by tests
"Unable to reformulate a `SecondOrderCone` into a " *
"`RotatedSecondOrderCone` because the output dimension is too " *
"small",
)
throw(err)

Check warning on line 71 in src/Bridges/Constraint/bridges/SOCtoRSOCBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/SOCtoRSOCBridge.jl#L71

Added line #L71 was not covered by tests
end
t, u, x = scalars[1], scalars[2], scalars[3:end]
ts = MOI.Utilities.operate!(/, T, t, sqrt(T(2)))
us = MOI.Utilities.operate!(/, T, u, sqrt(T(2)))
Expand Down
3 changes: 0 additions & 3 deletions src/Bridges/Constraint/bridges/SOS1ToMILPBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ function MOI.get(::MOI.ModelLike, ::MOI.ConstraintSet, bridge::SOS1ToMILPBridge)
end

function MOI.delete(model::MOI.ModelLike, bridge::SOS1ToMILPBridge)
if isempty(bridge.variables)
return
end
MOI.delete(model, bridge.equal_to)
for ci in bridge.less_than
MOI.delete(model, ci)
Expand Down
3 changes: 0 additions & 3 deletions src/Bridges/Constraint/bridges/SOS2ToMILPBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ function MOI.get(::MOI.ModelLike, ::MOI.ConstraintSet, bridge::SOS2ToMILPBridge)
end

function MOI.delete(model::MOI.ModelLike, bridge::SOS2ToMILPBridge)
if isempty(bridge.variables)
return
end
MOI.delete(model, bridge.equal_to)
for ci in bridge.less_than
MOI.delete(model, ci)
Expand Down
20 changes: 10 additions & 10 deletions src/Bridges/Constraint/bridges/ScalarSlackBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@
return 1
end

function MOI.get(
::_AbstractSlackBridge{T,VF,ZS,F,S},
::MOI.NumberOfConstraints{VF,S},
)::Int64 where {T,VF,ZS,F,S}
return 1
end

function MOI.get(
bridge::_AbstractSlackBridge{T,VF,ZS,F},
::MOI.ListOfConstraintIndices{F,ZS},
) where {T,VF,ZS,F}
return [bridge.equality]
end

function MOI.get(

Check warning on line 35 in src/Bridges/Constraint/bridges/ScalarSlackBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ScalarSlackBridge.jl#L35

Added line #L35 was not covered by tests
::_AbstractSlackBridge{T,VF,ZS,F,S},
::MOI.NumberOfConstraints{VF,S},
)::Int64 where {T,VF,ZS,F,S}
return 1

Check warning on line 39 in src/Bridges/Constraint/bridges/ScalarSlackBridge.jl

View check run for this annotation

Codecov / codecov/patch

src/Bridges/Constraint/bridges/ScalarSlackBridge.jl#L39

Added line #L39 was not covered by tests
end

function MOI.get(
bridge::_AbstractSlackBridge{T,VF,ZS,F,S},
::MOI.ListOfConstraintIndices{VF,S},
Expand All @@ -55,20 +55,20 @@
# that the original set was the same as the slacked set.
return error(
"Internal error: this method should never be called because it " *
"represents and invalid state. Please open an issue to report.",
"represents an invalid state. Please open an issue to report.",
)
end

function MOI.get(
bridge::_AbstractSlackBridge{T,VF,S,F,S},
::MOI.ListOfConstraintIndices{F,S},
::MOI.ListOfConstraintIndices{VF,S},
) where {T,VF,S,F}
# This method is needed to resolve a possible ambiguity reported by
# Test.detect_ambiguities. It can't happen in practice because it would mean
# that the original set was the same as the slacked set.
return error(
"Internal error: this method should never be called because it " *
"represents and invalid state. Please open an issue to report.",
"represents an invalid state. Please open an issue to report.",
)
end

Expand Down
10 changes: 6 additions & 4 deletions src/Bridges/Objective/bridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ end

function MOI.set(
::MOI.ModelLike,
::MOI.ObjectiveSense,
attr::MOI.ObjectiveSense,
bridge::AbstractBridge,
::MOI.OptimizationSense,
)
return throw(
ArgumentError(
MOI.SetAttributeNotAllowed(
attr,
"Objective bridge of type `$(typeof(bridge))` does not support " *
"modifying the objective sense. As a workaround, set the sense " *
"to `MOI.FEASIBILITY_SENSE` to clear the objective function and " *
Expand All @@ -111,11 +112,12 @@ end

function MOI.get(
::MOI.ModelLike,
::MOI.ObjectiveFunction,
attr::MOI.ObjectiveFunction,
bridge::AbstractBridge,
)
return throw(
ArgumentError(
MOI.GetAttributeNotAllowed(
attr,
"ObjectiveFunction bridge of type `$(typeof(bridge))` does not" *
" support getting the objective function.",
),
Expand Down
1 change: 1 addition & 0 deletions src/Bridges/bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ function MOI.delete(b::AbstractBridgeOptimizer, vis::Vector{MOI.VariableIndex})
else
MOI.delete(b.model, vis)
end
return
end

function MOI.delete(b::AbstractBridgeOptimizer, vi::MOI.VariableIndex)
Expand Down
20 changes: 20 additions & 0 deletions test/Bridges/Constraint/RSOCBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,26 @@ function test_runtests()
return
end

function test_dimension_mismatch_SOCR()
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Constraint.SOCR{Float64}(inner)
@test_throws(
DimensionMismatch,
MOI.add_constrained_variables(model, MOI.SecondOrderCone(1)),
)
return
end

function test_dimension_mismatch_RSOC()
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Constraint.RSOC{Float64}(inner)
@test_throws(
DimensionMismatch,
MOI.add_constrained_variables(model, MOI.RotatedSecondOrderCone(1)),
)
return
end

end # module

TestConstraintRSOC.runtests()
10 changes: 10 additions & 0 deletions test/Bridges/Constraint/SOCtoPSDBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,16 @@ function test_bridging_cost_RSOCtoPSD()
return
end

function test_dimension_mismatch_RSOC_to_PSD()
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Constraint.RSOCtoPSD{Float64}(inner)
@test_throws(
DimensionMismatch,
MOI.add_constrained_variables(model, MOI.RotatedSecondOrderCone(1)),
)
return
end

end # module

TestConstraintSOCtoPSD.runtests()
41 changes: 41 additions & 0 deletions test/Bridges/Constraint/ScalarSlackBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,47 @@ function test_runtests()
return
end

function test_basis_status()
inner = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
)
model = MOI.Bridges.Constraint.ScalarSlack{Float64}(inner)
x = MOI.add_variable(model)
c = MOI.add_constraint(model, 1.0 * x, MOI.GreaterThan(1.0))
y = MOI.get(inner, MOI.ListOfVariableIndices())
MOI.set.(inner, MOI.VariableBasisStatus(), y, MOI.BASIC)
@test MOI.get(model, MOI.ConstraintBasisStatus(), c) == MOI.BASIC
d = MOI.add_constraint(model, 1.0 * x, MOI.Interval(1.0, 2.0))
z = last(MOI.get(inner, MOI.ListOfVariableIndices()))
MOI.set(inner, MOI.VariableBasisStatus(), z, MOI.SUPER_BASIC)
@test MOI.get(model, MOI.ConstraintBasisStatus(), d) == MOI.SUPER_BASIC
return
end

function test_internal_error()
F, S = MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}
BT = MOI.Bridges.Constraint.ScalarSlackBridge{Float64,F,S}
model = MOI.Utilities.Model{Float64}()
x = MOI.add_variable(model)
set = MOI.EqualTo(1.0)
bridge = MOI.Bridges.Constraint.bridge_constraint(BT, model, 1.0 * x, set)
@test_throws(
ErrorException(
"Internal error: this method should never be called because it " *
"represents an invalid state. Please open an issue to report.",
),
MOI.get(bridge, MOI.NumberOfConstraints{MOI.VariableIndex,S}()),
)
@test_throws(
ErrorException(
"Internal error: this method should never be called because it " *
"represents an invalid state. Please open an issue to report.",
),
MOI.get(bridge, MOI.ListOfConstraintIndices{MOI.VariableIndex,S}()),
)
return
end

end # module

TestConstraintSlack.runtests()
27 changes: 27 additions & 0 deletions test/Bridges/Constraint/SplitIntervalBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,33 @@ function test_runtests_vector()
return
end

function test_get_function()
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Constraint.SplitInterval{Float64}(inner)
x = MOI.add_variable(model)
c = MOI.add_constraint(model, 1.0 * x, MOI.Interval(-Inf, Inf))
@test MOI.get(model, MOI.ConstraintFunction(), c) ≈ 1.0 * x
return
end

function test_modify_set()
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Constraint.SplitInterval{Float64}(inner)
x = MOI.add_variable(model)
c = MOI.add_constraint(model, 1.0 * x, MOI.Interval(-Inf, Inf))
for set in [
MOI.Interval(-Inf, Inf),
MOI.Interval(1.0, 2.0),
MOI.Interval(2.0, 3.0),
MOI.Interval(-Inf, Inf),
]
MOI.set(model, MOI.ConstraintSet(), c, set)
@test MOI.get(model, MOI.ConstraintSet(), c) == set
@test MOI.get(model, MOI.ConstraintFunction(), c) ≈ 1.0 * x
end
return
end

end # module

TestConstraintSplitInterval.runtests()
16 changes: 16 additions & 0 deletions test/Bridges/Constraint/VectorizeBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,22 @@ function test_VectorNonlinearFunction()
return
end

function test_constraint_primal_ray()
inner = MOI.Utilities.MockOptimizer(
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
)
model = MOI.Bridges.Constraint.Vectorize{Float64}(inner)
x = MOI.add_variable(model)
c = MOI.add_constraint(model, 1.0 * x, MOI.EqualTo(3.0))
MOI.set(inner, MOI.PrimalStatus(), MOI.INFEASIBILITY_CERTIFICATE)
y = only(MOI.get(inner, MOI.ListOfVariableIndices()))
MOI.set(inner, MOI.VariablePrimal(), y, 1.23)
@test MOI.get(model, MOI.ConstraintPrimal(), c) == 1.23
MOI.set(inner, MOI.PrimalStatus(), MOI.FEASIBLE_POINT)
@test MOI.get(model, MOI.ConstraintPrimal(), c) == 1.23
return
end

end # module

TestConstraintVectorize.runtests()
5 changes: 3 additions & 2 deletions test/Bridges/Objective/SlackBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function test_SlackBridge_ObjectiveSense_modify()
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
@test_throws(
ArgumentError,
MOI.SetAttributeNotAllowed,
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE),
)
MOI.set(model, MOI.ObjectiveSense(), MOI.FEASIBILITY_SENSE)
Expand Down Expand Up @@ -328,7 +328,8 @@ function test_original()
("y", MOI.GreaterThan{Float64}(2.0)),
],
)
err = ArgumentError(
err = MOI.SetAttributeNotAllowed(
MOI.ObjectiveSense(),
"Objective bridge of type `$(MOI.Bridges.Objective.SlackBridge{Float64,MOI.ScalarQuadraticFunction{Float64},MOI.ScalarQuadraticFunction{Float64}})`" *
" does not support modifying the objective sense. As a workaround, set" *
" the sense to `MOI.FEASIBILITY_SENSE` to clear the objective function" *
Expand Down
22 changes: 22 additions & 0 deletions test/Bridges/Variable/HermitianToSymmetricPSDBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ function test_runtests()
return
end

function test_delete()
inner = MOI.Utilities.Model{Float64}()
model = MOI.Bridges.Variable.HermitianToSymmetricPSD{Float64}(inner)
set = MOI.HermitianPositiveSemidefiniteConeTriangle(2)
x, _ = MOI.add_constrained_variables(model, set)
MOI.delete(model, x)
@test MOI.is_empty(inner)
return
end

function test_set_variable_primal_start()
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Variable.HermitianToSymmetricPSD{Float64}(inner)
set = MOI.HermitianPositiveSemidefiniteConeTriangle(2)
x, _ = MOI.add_constrained_variables(model, set)
MOI.set(model, MOI.VariablePrimalStart(), x[1], 1.0)
@test MOI.get(model, MOI.VariablePrimalStart(), x[1]) == 1.0
MOI.set(model, MOI.VariablePrimalStart(), x[1], nothing)
@test MOI.get(model, MOI.VariablePrimalStart(), x[1]) === nothing
return
end

end # module

TestVariableHermitianToSymmetricPSD.runtests()
8 changes: 8 additions & 0 deletions test/Bridges/Variable/NonposToNonnegBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ function test_runtests()
return
end

function test_adjoint_map_function()
inner = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
model = MOI.Bridges.Variable.NonposToNonneg{Float64}(inner)
x, _ = MOI.add_constrained_variables(model, MOI.Nonpositives(1))
@test MOI.Bridges.adjoint_map_function(model.map[only(x)], 1.23) == -1.23
return
end

end # module

TestVariableFlipSign.runtests()
Loading
Loading