Skip to content

Commit 98fbb1d

Browse files
authored
Update set_map.jl
1 parent 74c0a40 commit 98fbb1d

File tree

1 file changed

+26
-37
lines changed

1 file changed

+26
-37
lines changed

test/Bridges/set_map.jl

+26-37
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
# Use of this source code is governed by an MIT-style license that can be found
55
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
66

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
99
module TestSetMapBridge
1010

11-
import MathOptInterface as MOI
11+
using Test
1212

13+
import MathOptInterface as MOI
1314

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`
1616
struct SwapSet <: MOI.AbstractVectorSet
1717
swap::Bool
1818
invertible::Bool
@@ -27,10 +27,7 @@ struct SwapBridge{T} <: MOI.Bridges.Constraint.SetMapBridge{
2727
MOI.VectorOfVariables,
2828
MOI.VectorOfVariables,
2929
}
30-
constraint::MOI.ConstraintIndex{
31-
MOI.VectorOfVariables,
32-
MOI.Nonnegatives,
33-
}
30+
constraint::MOI.ConstraintIndex{MOI.VectorOfVariables,MOI.Nonnegatives}
3431
set::SwapSet
3532
end
3633

@@ -51,14 +48,11 @@ end
5148
function MOI.Bridges.map_set(bridge::SwapBridge, set::SwapSet)
5249
if set.swap != bridge.set.swap
5350
error("Cannot change swap set")
54-
else
55-
return MOI.Nonnegatives(2)
5651
end
52+
return MOI.Nonnegatives(2)
5753
end
5854

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
6256

6357
function MOI.Bridges.map_function(bridge::SwapBridge, func)
6458
return swap(func, bridge.set.swap)
@@ -79,7 +73,11 @@ function MOI.Bridges.inverse_adjoint_map_function(bridge::SwapBridge, func)
7973
return swap(func, bridge.set.swap)
8074
end
8175

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
8381

8482
function runtests()
8583
for name in names(@__MODULE__; all = true)
@@ -92,34 +90,25 @@ function runtests()
9290
return
9391
end
9492

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-
10793
function test_set_set()
10894
model = MOI.Bridges.Constraint.SingleBridgeOptimizer{SwapBridge{Float64}}(
109-
MOI.Utilities.Model{Float64}()
95+
MOI.Utilities.Model{Float64}(),
11096
)
11197
x = MOI.add_variables(model, 2)
11298
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)),
121103
)
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
123112
end
124113

125114
function test_runtests()

0 commit comments

Comments
 (0)