Skip to content

Commit 362544d

Browse files
authored
[Bridges] error on set(_, ::ConstraintSet with variable and constraint bridges (#2472)
1 parent 009004c commit 362544d

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/Bridges/bridge_optimizer.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,10 @@ function MOI.set(
14441444
# mapped through the bridge substitution. This includes moving constants
14451445
# into the set.
14461446
if Variable.has_bridges(Variable.bridges(b))
1447+
if is_bridged(b, ci)
1448+
# If `ci` is also ConstraintBridged, then we give up as the current code path is known to contain unresolved issues, see https://github.com/jump-dev/MathOptInterface.jl/issues/2452
1449+
throw(MOI.SetAttributeNotAllowed(attr))
1450+
end
14471451
func = MOI.get(b, MOI.ConstraintFunction(), ci)
14481452
# Updating the set will not modify the function, so we don't care about
14491453
# the first argument. We only care about the new set.

test/Bridges/bridge_optimizer.jl

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,28 +1232,34 @@ function test_issue_2452_multiple_variable_bridges()
12321232
index_map = MOI.copy_to(dest, src)
12331233
set = MOI.get(dest, MOI.ConstraintSet(), index_map[c])
12341234
@test set == MOI.EqualTo(3.0)
1235-
MOI.set(dest, MOI.ConstraintSet(), index_map[c], set)
1236-
@test MOI.get(dest, MOI.ConstraintSet(), index_map[c]) == set
1237-
new_set = MOI.EqualTo(2.0)
1238-
MOI.set(dest, MOI.ConstraintSet(), index_map[c], new_set)
1239-
@test MOI.get(dest, MOI.ConstraintSet(), index_map[c]) == new_set
1235+
@test_throws(
1236+
MOI.SetAttributeNotAllowed,
1237+
MOI.set(dest, MOI.ConstraintSet(), index_map[c], set),
1238+
)
12401239
return
12411240
end
12421241

1243-
function test_issue_2452()
1242+
function test_2452()
1243+
F, S = MOI.VectorAffineFunction{Float64}, MOI.Zeros
12441244
src = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}())
12451245
x = MOI.add_variable(src)
12461246
MOI.add_constraint(src, x, MOI.GreaterThan(1.0))
1247-
c = MOI.add_constraint(src, 2.0 * x, MOI.EqualTo(3.0))
1247+
set = MOI.EqualTo(3.0)
1248+
c = MOI.add_constraint(src, 2.0 * x, set)
12481249
dest = MOI.instantiate(Model2452{Float64}; with_bridge_type = Float64)
12491250
index_map = MOI.copy_to(dest, src)
1250-
set = MOI.get(dest, MOI.ConstraintSet(), index_map[c])
1251-
@test set == MOI.EqualTo(3.0)
1252-
MOI.set(dest, MOI.ConstraintSet(), index_map[c], set)
1251+
y = only(MOI.get(dest.model, MOI.ListOfVariableIndices()))
1252+
ci = only(MOI.get(dest.model, MOI.ListOfConstraintIndices{F,S}()))
12531253
@test MOI.get(dest, MOI.ConstraintSet(), index_map[c]) == set
1254-
new_set = MOI.EqualTo(2.0)
1255-
MOI.set(dest, MOI.ConstraintSet(), index_map[c], new_set)
1256-
@test MOI.get(dest, MOI.ConstraintSet(), index_map[c]) == new_set
1254+
@test (
1255+
MOI.get(dest.model, MOI.ConstraintFunction(), ci),
1256+
MOI.Utilities.operate(vcat, Float64, -1.0 + 2.0 * y),
1257+
)
1258+
@test MOI.get(dest.model, MOI.ConstraintSet(), ci) == MOI.Zeros(1)
1259+
@test_throws(
1260+
MOI.SetAttributeNotAllowed,
1261+
MOI.set(dest, MOI.ConstraintSet(), index_map[c], set),
1262+
)
12571263
return
12581264
end
12591265

0 commit comments

Comments
 (0)