Skip to content

Commit cd5cd9b

Browse files
authored
[Bridges] throw better error when variable cannot be unbridged (#2415)
1 parent b64dbe1 commit cd5cd9b

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

src/Bridges/Variable/map.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -579,11 +579,13 @@ Throw an error if some bridged variables do not have any reverse mapping.
579579
"""
580580
function throw_if_cannot_unbridge(map::Map)
581581
if map.unbridged_function === nothing
582-
error(
583-
"Cannot unbridge function because some variables are bridged by",
584-
" variable bridges that do not support reverse mapping, for example,",
585-
" `ZerosBridge`.",
582+
err = MOI.GetAttributeNotAllowed(
583+
MOI.ConstraintFunction(),
584+
"Cannot unbridge function because some variables are bridged by " *
585+
"variable bridges that do not support reverse mapping, for " *
586+
"example, `ZerosBridge`.",
586587
)
588+
throw(err)
587589
end
588590
end
589591

test/Bridges/Variable/map.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ function test_map()
5454
S1 = typeof(set1)
5555
v1, c1 = MOI.Bridges.Variable.add_key_for_bridge(map, () -> b1, set1)
5656
MOI.is_valid(map, c1)
57-
cannot_unbridge_err = ErrorException(
57+
cannot_unbridge_err = MOI.GetAttributeNotAllowed(
58+
MOI.ConstraintFunction(),
5859
"Cannot unbridge function because some variables are bridged by variable" *
5960
" bridges that do not support reverse mapping, for example, `ZerosBridge`.",
6061
)

test/Bridges/Variable/zeros.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,15 @@ function test_zeros()
8484
)
8585
@test_throws err MOI.delete(bridged_mock, cyz)
8686

87-
err = ErrorException(
87+
err = MOI.GetAttributeNotAllowed(
88+
MOI.ConstraintFunction(),
8889
"Cannot unbridge function because some variables are bridged by" *
8990
" variable bridges that do not support reverse mapping, for example," *
9091
" `ZerosBridge`.",
9192
)
9293
@test_throws err MOI.get(bridged_mock, MOI.ObjectiveFunction{typeof(obj)}())
9394
# With `c1`, the function does not contain any variable so it tests that it
94-
# also throws an error even if it never calls `variable_unbridged_function`.
95+
# also throws an error even if it never calls `variable_unbridged_function`
9596
@test_throws err MOI.get(bridged_mock, MOI.ConstraintFunction(), c1)
9697
@test_throws err MOI.get(bridged_mock, MOI.ConstraintFunction(), c2)
9798

test/Bridges/bridge_optimizer.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,17 @@ function test_ListOfVariablesWithAttributeSet(T = Float64)
11761176
return
11771177
end
11781178

1179+
function test_cannot_unbridge_variable_function()
1180+
model = MOI.Bridges.Variable.Zeros{Float64}(MOI.Utilities.Model{Float64}())
1181+
x, c = MOI.add_constrained_variables(model, MOI.Zeros(1))
1182+
c2 = MOI.add_constraint(model, 1.0 * x[1], MOI.EqualTo(1.0))
1183+
@test_throws(
1184+
MOI.GetAttributeNotAllowed{MOI.ConstraintFunction},
1185+
MOI.get(model, MOI.ConstraintFunction(), c2),
1186+
)
1187+
return
1188+
end
1189+
11791190
end # module
11801191

11811192
TestBridgeOptimizer.runtests()

0 commit comments

Comments
 (0)