22
22
23
23
MOI. dimension (:: SwapSet ) = 2
24
24
25
- struct SwapBridge{T} <: MOI.Bridges.Constraint.SetMapBridge {
25
+ struct VariableSwapBridge{T} < :
26
+ MOI. Bridges. Variable. SetMapBridge{T,MOI. Nonnegatives,SwapSet}
27
+ variables:: MOI.Vector{MOI.VariableIndex}
28
+ constraint:: MOI.ConstraintIndex{MOI.VectorOfVariables,MOI.Nonnegatives}
29
+ set:: SwapSet
30
+ end
31
+
32
+ function MOI. Bridges. Variable. bridge_constrained_variable (
33
+ :: Type{VariableSwapBridge{T}} ,
34
+ model:: MOI.ModelLike ,
35
+ set:: SwapSet ,
36
+ ) where {T}
37
+ variables, constraint =
38
+ MOI. add_constrained_variables (model, MOI. Nonnegatives (2 ))
39
+ return VariableSwapBridge {T} (variables, constraint, set)
40
+ end
41
+
42
+ MOI. Bridges. map_set (bridge:: VariableSwapBridge , :: MOI.Nonnegatives ) = bridge. set
43
+
44
+ function MOI. Bridges. inverse_map_set (bridge:: VariableSwapBridge , set:: SwapSet )
45
+ if set. swap != bridge. set. swap
46
+ error (" Cannot change swap set" )
47
+ end
48
+ return MOI. Nonnegatives (2 )
49
+ end
50
+
51
+ function MOI. Bridges. map_function (
52
+ bridge:: VariableSwapBridge ,
53
+ func,
54
+ i:: MOI.Bridges.IndexInVector ,
55
+ )
56
+ return MOI. Bridges. map_function (bridge, func)[i. value]
57
+ end
58
+
59
+ # Workaround until https://github.com/jump-dev/MathOptInterface.jl/issues/2117 is fixed
60
+ MOI. Bridges. inverse_map_function (:: VariableSwapBridge , a:: Float64 ) = a
61
+
62
+ struct ConstraintSwapBridge{T} <: MOI.Bridges.Constraint.SetMapBridge {
26
63
T,
27
64
MOI. Nonnegatives,
28
65
SwapSet,
@@ -34,7 +71,7 @@ struct SwapBridge{T} <: MOI.Bridges.Constraint.SetMapBridge{
34
71
end
35
72
36
73
function MOI. Bridges. Constraint. bridge_constraint (
37
- :: Type{SwapBridge {T}} ,
74
+ :: Type{ConstraintSwapBridge {T}} ,
38
75
model:: MOI.ModelLike ,
39
76
func:: MOI.VectorOfVariables ,
40
77
set:: SwapSet ,
@@ -44,17 +81,24 @@ function MOI.Bridges.Constraint.bridge_constraint(
44
81
MOI. VectorOfVariables (swap (func. variables, set. swap)),
45
82
MOI. Nonnegatives (2 ),
46
83
)
47
- return SwapBridge {T} (ci, set)
84
+ return ConstraintSwapBridge {T} (ci, set)
48
85
end
49
86
50
- function MOI. Bridges. map_set (bridge:: SwapBridge , set:: SwapSet )
87
+ function MOI. Bridges. map_set (bridge:: ConstraintSwapBridge , set:: SwapSet )
51
88
if set. swap != bridge. set. swap
52
89
error (" Cannot change swap set" )
53
90
end
54
91
return MOI. Nonnegatives (2 )
55
92
end
56
93
57
- MOI. Bridges. inverse_map_set (bridge:: SwapBridge , :: MOI.Nonnegatives ) = bridge. set
94
+ function MOI. Bridges. inverse_map_set (
95
+ bridge:: ConstraintSwapBridge ,
96
+ :: MOI.Nonnegatives ,
97
+ )
98
+ return bridge. set
99
+ end
100
+
101
+ const SwapBridge{T} = Union{VariableSwapBridge{T},ConstraintSwapBridge{T}}
58
102
59
103
function MOI. Bridges. map_function (bridge:: SwapBridge , func)
60
104
return swap (func, bridge. set. swap)
@@ -90,19 +134,10 @@ function swap(f::MOI.VectorOfVariables, do_swap::Bool)
90
134
return MOI. VectorOfVariables (swap (f. variables, do_swap))
91
135
end
92
136
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
-
104
137
function test_other_error ()
105
- model = MOI. Bridges. Constraint. SingleBridgeOptimizer {SwapBridge{Float64}} (
138
+ model = MOI. Bridges. Constraint. SingleBridgeOptimizer{
139
+ ConstraintSwapBridge{Float64},
140
+ }(
106
141
MOI. Utilities. UniversalFallback (MOI. Utilities. Model {Float64} ()),
107
142
)
108
143
x = MOI. add_variables (model, 2 )
@@ -123,8 +158,11 @@ function test_other_error()
123
158
)
124
159
return
125
160
end
126
- function test_not_invertible ()
127
- model = MOI. Bridges. Constraint. SingleBridgeOptimizer {SwapBridge{Float64}} (
161
+
162
+ function test_constraint_not_invertible ()
163
+ model = MOI. Bridges. Constraint. SingleBridgeOptimizer{
164
+ ConstraintSwapBridge{Float64},
165
+ }(
128
166
MOI. Utilities. UniversalFallback (MOI. Utilities. Model {Float64} ()),
129
167
)
130
168
x = MOI. add_variables (model, 2 )
162
200
function test_runtests ()
163
201
for do_swap in [false , true ]
164
202
MOI. Bridges. runtests (
165
- SwapBridge ,
203
+ ConstraintSwapBridge ,
166
204
model -> begin
167
205
x = MOI. add_variables (model, 2 )
168
206
func = MOI. VectorOfVariables (x)
@@ -176,6 +214,28 @@ function test_runtests()
176
214
MOI. add_constraint (model, func, set)
177
215
end ,
178
216
)
217
+ MOI. Bridges. runtests (
218
+ VariableSwapBridge,
219
+ model -> begin
220
+ set = SwapSet (do_swap, NONE)
221
+ x = MOI. add_constrained_variables (model, set)
222
+ end ,
223
+ model -> begin
224
+ set = MOI. Nonnegatives (2 )
225
+ x = MOI. add_constrained_variables (model, set)
226
+ end ,
227
+ )
228
+ end
229
+ return
230
+ end
231
+
232
+ function runtests ()
233
+ for name in names (@__MODULE__ ; all = true )
234
+ if startswith (" $(name) " , " test_" )
235
+ @testset " $(name) " begin
236
+ getfield (@__MODULE__ , name)()
237
+ end
238
+ end
179
239
end
180
240
return
181
241
end
0 commit comments