Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Bridges/Variable/map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -579,11 +579,13 @@ Throw an error if some bridged variables do not have any reverse mapping.
"""
function throw_if_cannot_unbridge(map::Map)
if map.unbridged_function === nothing
error(
"Cannot unbridge function because some variables are bridged by",
" variable bridges that do not support reverse mapping, for example,",
" `ZerosBridge`.",
err = MOI.GetAttributeNotAllowed(
MOI.ConstraintFunction(),
"Cannot unbridge function because some variables are bridged by " *
"variable bridges that do not support reverse mapping, for " *
"example, `ZerosBridge`.",
)
throw(err)
end
end

Expand Down
3 changes: 2 additions & 1 deletion test/Bridges/Variable/map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ function test_map()
S1 = typeof(set1)
v1, c1 = MOI.Bridges.Variable.add_key_for_bridge(map, () -> b1, set1)
MOI.is_valid(map, c1)
cannot_unbridge_err = ErrorException(
cannot_unbridge_err = MOI.GetAttributeNotAllowed(
MOI.ConstraintFunction(),
"Cannot unbridge function because some variables are bridged by variable" *
" bridges that do not support reverse mapping, for example, `ZerosBridge`.",
)
Expand Down
5 changes: 3 additions & 2 deletions test/Bridges/Variable/zeros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,15 @@ function test_zeros()
)
@test_throws err MOI.delete(bridged_mock, cyz)

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

Expand Down
11 changes: 11 additions & 0 deletions test/Bridges/bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,17 @@ function test_ListOfVariablesWithAttributeSet(T = Float64)
return
end

function test_cannot_unbridge_variable_function()
model = MOI.Bridges.Variable.Zeros{Float64}(MOI.Utilities.Model{Float64}())
x, c = MOI.add_constrained_variables(model, MOI.Zeros(1))
c2 = MOI.add_constraint(model, 1.0 * x[1], MOI.EqualTo(1.0))
@test_throws(
MOI.GetAttributeNotAllowed{MOI.ConstraintFunction},
MOI.get(model, MOI.ConstraintFunction(), c2),
)
return
end

end # module

TestBridgeOptimizer.runtests()