Skip to content

Commit b3ac168

Browse files
committed
Add delete and supports tests
1 parent ee597f1 commit b3ac168

File tree

7 files changed

+44
-12
lines changed

7 files changed

+44
-12
lines changed

src/Bridges/Variable/zeros.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ function MOI.get(bridge::ZerosBridge, ::MOI.ListOfVariableIndices)
3939
return MOI.VariableIndex[]
4040
end
4141

42+
# References
43+
function MOI.delete(::MOI.ModelLike, ::ZerosBridge) end
44+
4245
# Attributes, Bridge acting as a constraint
4346

4447
function MOI.get(::MOI.ModelLike, ::MOI.ConstraintPrimal,

src/Bridges/bridge.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ function MOI.get(::AbstractBridge,
3838
return MOI.ConstraintIndex{F, S}[]
3939
end
4040

41+
"""
42+
MOI.supports(model::MOI.ModelLike, attr::MOI.AbstractConstraintAttribute,
43+
BT::Type{<:AbstractBridge})
44+
45+
Return a `Bool` indicating whether `BT` supports setting `attr` to `model`.
46+
"""
47+
function MOI.supports(::MOI.ModelLike, ::MOI.AbstractConstraintAttribute,
48+
::Type{<:AbstractBridge})
49+
return false
50+
end
51+
4152
"""
4253
function MOI.get(model::MOI.ModelLike, attr::MOI.AbstractConstraintAttribute,
4354
bridge::AbstractBridge)

src/Bridges/bridge_optimizer.jl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,6 @@ function MOIU.supports_default_copy_to(b::AbstractBridgeOptimizer,
131131
end
132132

133133
# References
134-
function _constraint_index(b::AbstractBridgeOptimizer, i::Integer)
135-
F, S = b.constraint_types[i]
136-
return MOI.ConstraintIndex{F, S}(i)
137-
end
138134
function MOI.is_valid(b::AbstractBridgeOptimizer, vi::MOI.VariableIndex)
139135
if is_bridged(b, vi)
140136
return haskey(Variable.bridges(b), vi)
@@ -201,14 +197,15 @@ end
201197
function MOI.delete(b::AbstractBridgeOptimizer, ci::MOI.ConstraintIndex)
202198
if is_bridged(b, ci)
203199
MOI.throw_if_not_valid(b, ci)
204-
MOI.delete(b, bridge(b, ci))
200+
br = bridge(b, ci)
205201
if is_variable_bridged(b, ci)
206202
error("Cannot delete constraint index of bridged constrained",
207203
" variables, delete the scalar variable or the vector of",
208204
" variables instead.")
209205
else
210206
delete!(Constraint.bridges(b), ci)
211207
end
208+
MOI.delete(b, br)
212209
if Constraint.has_bridges(Constraint.bridges(b))
213210
b.name_to_con = nothing
214211
end

test/Bridges/Variable/flip_sign.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ config = MOIT.TestConfig()
2424
y = vis[4]
2525
@test y.value == -1
2626

27+
@test MOI.supports(bridged_mock, MOI.VariablePrimalStart(), MOI.VariableIndex)
2728
MOI.set(bridged_mock, MOI.VariablePrimalStart(), y, 1.0)
2829
x, y_flipped, z, s = MOI.get(mock, MOI.ListOfVariableIndices())
2930
@test MOI.get(mock, MOI.VariablePrimalStart(), y_flipped) == -1

test/Bridges/Variable/free.jl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,8 @@ end
8484
@test MOI.get(bridged_mock, MOI.ConstraintPrimal(), cy) == [-100.0]
8585
@test MOI.get(bridged_mock, MOI.ConstraintDual(), cy) == [0.0]
8686

87-
MOI.set(bridged_mock, MOI.VariablePrimalStart(), x, 1.0)
88-
MOI.set(bridged_mock, MOI.VariablePrimalStart(), y, -1.0)
87+
@test MOI.supports(bridged_mock, MOI.VariablePrimalStart(), MOI.VariableIndex)
88+
MOI.set(bridged_mock, MOI.VariablePrimalStart(), [x, y], [1.0, -1.0])
8989
xa, xb, ya, yb = MOI.get(mock, MOI.ListOfVariableIndices())
90-
@test MOI.get(mock, MOI.VariablePrimalStart(), xa) == 1
91-
@test MOI.get(mock, MOI.VariablePrimalStart(), xb) == 0
92-
@test MOI.get(mock, MOI.VariablePrimalStart(), ya) == 0
93-
@test MOI.get(mock, MOI.VariablePrimalStart(), yb) == -1
90+
@test MOI.get(mock, MOI.VariablePrimalStart(), [xa, xb, ya, yb]) == [1.0, 0.0, 0.0, -1.0]
9491
end

test/Bridges/Variable/zeros.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ MOI.set(bridged_mock, MOI.ObjectiveFunction{typeof(obj)}(), obj)
3131
@test MOIB.Variable.unbridged_map(MOIB.bridge(bridged_mock, y), y, MOIB.Variable.IndexInVector(1)) === nothing
3232
@test MOIB.Variable.unbridged_map(MOIB.bridge(bridged_mock, z), z, MOIB.Variable.IndexInVector(2)) === nothing
3333

34+
err = ErrorException(
35+
"Cannot delete constraint index of bridged constrained variables, delete" *
36+
" the scalar variable or the vector of variables instead."
37+
)
38+
@test_throws err MOI.delete(bridged_mock, cyz)
39+
3440
err = ErrorException(
3541
"Cannot add two `VectorOfVariables`-in-`MathOptInterface.Zeros` on the" *
3642
" same first variable MathOptInterface.VariableIndex(-1)."
@@ -88,6 +94,7 @@ err = ArgumentError(
8894
end
8995

9096
@testset "Query" begin
97+
@test MOI.get(bridged_mock, MOI.ConstraintFunction(), cyz).variables == yz
9198
@test MOI.get(mock, MOI.NumberOfVariables()) == 1
9299
@test MOI.get(mock, MOI.ListOfVariableIndices()) == [x]
93100
@test MOI.get(bridged_mock, MOI.NumberOfVariables()) == 3
@@ -103,3 +110,18 @@ end
103110
MOI.set(bridged_mock, MOI.ObjectiveFunction{typeof(fx)}(), fx)
104111
@test MOI.get(bridged_mock, MOI.ObjectiveFunction{typeof(fx)}()) == fx
105112
end
113+
114+
@testset "Delete" begin
115+
message = string("Cannot delete variable as it is constrained with other",
116+
" other variables in a `MOI.VectorOfVariables`.")
117+
err = MOI.DeleteNotAllowed(y, message)
118+
@test_throws err MOI.delete(bridged_mock, y)
119+
err = MOI.DeleteNotAllowed(z, message)
120+
@test_throws err MOI.delete(bridged_mock, z)
121+
test_delete_bridged_variables(bridged_mock, yz, MOI.Zeros, 3, (
122+
(MOI.SingleVariable, MOI.GreaterThan{Float64}, 1),
123+
))
124+
@test MOI.is_valid(bridged_mock, x)
125+
@test !MOI.is_valid(bridged_mock, y)
126+
@test !MOI.is_valid(bridged_mock, z)
127+
end

test/Bridges/bridge_optimizer.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ mock = MOIU.MockOptimizer(NoIntervalModel{Float64}())
2828
bridged_mock = MOIB.Constraint.LessToGreater{Float64}(MOIB.Constraint.SplitInterval{Float64}(mock))
2929

3030
@testset "Unsupported constraint attribute" begin
31-
attr = UnknownConstraintAttribute()
31+
attr = MOIT.UnknownConstraintAttribute()
3232
err = ArgumentError(
3333
"Bridge of type `MathOptInterface.Bridges.Constraint.SplitIntervalBridge{Float64,MathOptInterface.SingleVariable}` " *
3434
"does not support accessing the attribute `$attr`.")
3535
x = MOI.add_variable(bridged_mock)
3636
ci = MOI.add_constraint(bridged_mock, MOI.SingleVariable(x),
3737
MOI.Interval(0.0, 1.0))
38+
@test !MOI.supports(bridged_mock, attr, typeof(ci))
3839
@test_throws err MOI.get(bridged_mock, attr, ci)
3940
end
4041

0 commit comments

Comments
 (0)