Skip to content

Commit a21c804

Browse files
committed
Add test
1 parent 3c17ebf commit a21c804

File tree

2 files changed

+73
-21
lines changed

2 files changed

+73
-21
lines changed

src/Bridges/Variable/set_map.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ function MOI.set(
192192
if value === nothing
193193
MOI.set(model, attr, bridge.variables[i.value], nothing)
194194
else
195-
bridged_value = MOI.Bridges.inverse_map_function(typeof(bridge), value)
195+
bridged_value = MOI.Bridges.inverse_map_function(bridge, value)
196196
MOI.set(model, attr, bridge.variables[i.value], bridged_value)
197197
end
198198
return

test/Bridges/set_map.jl

Lines changed: 72 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,45 @@ end
2222

2323
MOI.dimension(::SwapSet) = 2
2424

25-
struct SwapBridge{T} <: MOI.Bridges.Constraint.SetMapBridge{
25+
struct VariableSwapBridge{T} <: MOI.Bridges.Variable.SetMapBridge{
26+
T,
27+
MOI.Nonnegatives,
28+
SwapSet,
29+
}
30+
variables::MOI.Vector{MOI.VariableIndex}
31+
constraint::MOI.ConstraintIndex{MOI.VectorOfVariables,MOI.Nonnegatives}
32+
set::SwapSet
33+
end
34+
35+
function MOI.Bridges.Variable.bridge_constrained_variable(
36+
::Type{VariableSwapBridge{T}},
37+
model::MOI.ModelLike,
38+
set::SwapSet,
39+
) where {T}
40+
variables, constraint = MOI.add_constrained_variables(
41+
model,
42+
MOI.Nonnegatives(2),
43+
)
44+
return VariableSwapBridge{T}(variables, constraint, set)
45+
end
46+
47+
MOI.Bridges.map_set(bridge::VariableSwapBridge, ::MOI.Nonnegatives) = bridge.set
48+
49+
function MOI.Bridges.inverse_map_set(bridge::VariableSwapBridge, set::SwapSet)
50+
if set.swap != bridge.set.swap
51+
error("Cannot change swap set")
52+
end
53+
return MOI.Nonnegatives(2)
54+
end
55+
56+
function MOI.Bridges.map_function(bridge::VariableSwapBridge, func, i::MOI.Bridges.IndexInVector)
57+
return MOI.Bridges.map_function(bridge, func)[i.value]
58+
end
59+
60+
# Workaround until https://github.com/jump-dev/MathOptInterface.jl/issues/2117 is fixed
61+
MOI.Bridges.inverse_map_function(::VariableSwapBridge, a::Float64) = a
62+
63+
struct ConstraintSwapBridge{T} <: MOI.Bridges.Constraint.SetMapBridge{
2664
T,
2765
MOI.Nonnegatives,
2866
SwapSet,
@@ -34,7 +72,7 @@ struct SwapBridge{T} <: MOI.Bridges.Constraint.SetMapBridge{
3472
end
3573

3674
function MOI.Bridges.Constraint.bridge_constraint(
37-
::Type{SwapBridge{T}},
75+
::Type{ConstraintSwapBridge{T}},
3876
model::MOI.ModelLike,
3977
func::MOI.VectorOfVariables,
4078
set::SwapSet,
@@ -44,17 +82,19 @@ function MOI.Bridges.Constraint.bridge_constraint(
4482
MOI.VectorOfVariables(swap(func.variables, set.swap)),
4583
MOI.Nonnegatives(2),
4684
)
47-
return SwapBridge{T}(ci, set)
85+
return ConstraintSwapBridge{T}(ci, set)
4886
end
4987

50-
function MOI.Bridges.map_set(bridge::SwapBridge, set::SwapSet)
88+
function MOI.Bridges.map_set(bridge::ConstraintSwapBridge, set::SwapSet)
5189
if set.swap != bridge.set.swap
5290
error("Cannot change swap set")
5391
end
5492
return MOI.Nonnegatives(2)
5593
end
5694

57-
MOI.Bridges.inverse_map_set(bridge::SwapBridge, ::MOI.Nonnegatives) = bridge.set
95+
MOI.Bridges.inverse_map_set(bridge::ConstraintSwapBridge, ::MOI.Nonnegatives) = bridge.set
96+
97+
const SwapBridge{T} = Union{VariableSwapBridge{T},ConstraintSwapBridge{T}}
5898

5999
function MOI.Bridges.map_function(bridge::SwapBridge, func)
60100
return swap(func, bridge.set.swap)
@@ -90,19 +130,8 @@ function swap(f::MOI.VectorOfVariables, do_swap::Bool)
90130
return MOI.VectorOfVariables(swap(f.variables, do_swap))
91131
end
92132

93-
function runtests()
94-
for name in names(@__MODULE__; all = true)
95-
if startswith("$(name)", "test_")
96-
@testset "$(name)" begin
97-
getfield(@__MODULE__, name)()
98-
end
99-
end
100-
end
101-
return
102-
end
103-
104133
function test_other_error()
105-
model = MOI.Bridges.Constraint.SingleBridgeOptimizer{SwapBridge{Float64}}(
134+
model = MOI.Bridges.Constraint.SingleBridgeOptimizer{ConstraintSwapBridge{Float64}}(
106135
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
107136
)
108137
x = MOI.add_variables(model, 2)
@@ -123,8 +152,9 @@ function test_other_error()
123152
)
124153
return
125154
end
126-
function test_not_invertible()
127-
model = MOI.Bridges.Constraint.SingleBridgeOptimizer{SwapBridge{Float64}}(
155+
156+
function test_constraint_not_invertible()
157+
model = MOI.Bridges.Constraint.SingleBridgeOptimizer{ConstraintSwapBridge{Float64}}(
128158
MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
129159
)
130160
x = MOI.add_variables(model, 2)
@@ -162,7 +192,7 @@ end
162192
function test_runtests()
163193
for do_swap in [false, true]
164194
MOI.Bridges.runtests(
165-
SwapBridge,
195+
ConstraintSwapBridge,
166196
model -> begin
167197
x = MOI.add_variables(model, 2)
168198
func = MOI.VectorOfVariables(x)
@@ -176,6 +206,28 @@ function test_runtests()
176206
MOI.add_constraint(model, func, set)
177207
end,
178208
)
209+
MOI.Bridges.runtests(
210+
VariableSwapBridge,
211+
model -> begin
212+
set = SwapSet(do_swap, NONE)
213+
x = MOI.add_constrained_variables(model, set)
214+
end,
215+
model -> begin
216+
set = MOI.Nonnegatives(2)
217+
x = MOI.add_constrained_variables(model, set)
218+
end,
219+
)
220+
end
221+
return
222+
end
223+
224+
function runtests()
225+
for name in names(@__MODULE__; all = true)
226+
if startswith("$(name)", "test_")
227+
@testset "$(name)" begin
228+
getfield(@__MODULE__, name)()
229+
end
230+
end
179231
end
180232
return
181233
end

0 commit comments

Comments
 (0)