4
4
# Use of this source code is governed by an MIT-style license that can be found
5
5
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
6
6
7
- # Test with a bridge for which the map is defined on the bridge
8
- # value and not the bridge type
7
+ # Test with a bridge for which the map is defined on the bridge value and not
8
+ # the bridge type
9
9
module TestSetMapBridge
10
10
11
- import MathOptInterface as MOI
11
+ using Test
12
12
13
+ import MathOptInterface as MOI
13
14
14
- # Constraints `[f[2], f[1]]` if `swap` and otherwise
15
- # `f` to `Nonnegatives`
15
+ # Constraints `[f[2], f[1]]` if `swap` and otherwise `f` to `Nonnegatives`
16
16
struct SwapSet <: MOI.AbstractVectorSet
17
17
swap:: Bool
18
18
invertible:: Bool
@@ -27,10 +27,7 @@ struct SwapBridge{T} <: MOI.Bridges.Constraint.SetMapBridge{
27
27
MOI. VectorOfVariables,
28
28
MOI. VectorOfVariables,
29
29
}
30
- constraint:: MOI.ConstraintIndex {
31
- MOI. VectorOfVariables,
32
- MOI. Nonnegatives,
33
- }
30
+ constraint:: MOI.ConstraintIndex{MOI.VectorOfVariables,MOI.Nonnegatives}
34
31
set:: SwapSet
35
32
end
36
33
51
48
function MOI. Bridges. map_set (bridge:: SwapBridge , set:: SwapSet )
52
49
if set. swap != bridge. set. swap
53
50
error (" Cannot change swap set" )
54
- else
55
- return MOI. Nonnegatives (2 )
56
51
end
52
+ return MOI. Nonnegatives (2 )
57
53
end
58
54
59
- function MOI. Bridges. inverse_map_set (bridge:: SwapBridge , :: MOI.Nonnegatives )
60
- return bridge. set
61
- end
55
+ MOI. Bridges. inverse_map_set (bridge:: SwapBridge , :: MOI.Nonnegatives ) = bridge. set
62
56
63
57
function MOI. Bridges. map_function (bridge:: SwapBridge , func)
64
58
return swap (func, bridge. set. swap)
@@ -79,7 +73,11 @@ function MOI.Bridges.inverse_adjoint_map_function(bridge::SwapBridge, func)
79
73
return swap (func, bridge. set. swap)
80
74
end
81
75
82
- using Test
76
+ swap (x, swap:: Bool ) = swap ? [x[2 ], x[1 ]] : x
77
+
78
+ function swap (f:: MOI.VectorOfVariables , do_swap:: Bool )
79
+ return MOI. VectorOfVariables (swap (f. variables, do_swap))
80
+ end
83
81
84
82
function runtests ()
85
83
for name in names (@__MODULE__ ; all = true )
@@ -92,34 +90,25 @@ function runtests()
92
90
return
93
91
end
94
92
95
- function swap (x, swap:: Bool )
96
- if swap
97
- return [x[2 ], x[1 ]]
98
- else
99
- return x
100
- end
101
- end
102
-
103
- function swap (f:: MOI.VectorOfVariables , do_swap:: Bool )
104
- return MOI. VectorOfVariables (swap (f. variables, do_swap))
105
- end
106
-
107
93
function test_set_set ()
108
94
model = MOI. Bridges. Constraint. SingleBridgeOptimizer {SwapBridge{Float64}} (
109
- MOI. Utilities. Model {Float64} ()
95
+ MOI. Utilities. Model {Float64} (),
110
96
)
111
97
x = MOI. add_variables (model, 2 )
112
98
func = MOI. VectorOfVariables (x)
113
- set = SwapSet (true , false )
114
- ci = MOI. add_constraint (model, func, set)
115
- err = ErrorException (" Cannot change swap set" )
116
- @test_throws err MOI. set (model, MOI. ConstraintSet (), ci, SwapSet (false , false ))
117
- attr = MOI. ConstraintFunction ()
118
- err = MOI. GetAttributeNotAllowed (
119
- attr,
120
- " Cannot get MathOptInterface.ConstraintFunction() as the constraint is reformulated through a linear transformation that is not invertible.no luck no luck" ,
99
+ ci = MOI. add_constraint (model, func, SwapSet (true , false ))
100
+ @test_throws (
101
+ ErrorException (" Cannot change swap set" ),
102
+ MOI. set (model, MOI. ConstraintSet (), ci, SwapSet (false , false )),
121
103
)
122
- @test_throws err MOI. get (model, attr, ci)
104
+ @test_throws (
105
+ MOI. GetAttributeNotAllowed (
106
+ MOI. ConstraintFunction (),
107
+ " Cannot get MathOptInterface.ConstraintFunction() as the constraint is reformulated through a linear transformation that is not invertible.no luck no luck" ,
108
+ ),
109
+ MOI. get (model, MOI. ConstraintFunction (), ci),
110
+ )
111
+ return
123
112
end
124
113
125
114
function test_runtests ()
0 commit comments