Skip to content

Commit 30b859a

Browse files
committed
[Bridges] fix deleting variable with constraint bridged from scalar to vector
1 parent 7e27018 commit 30b859a

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

src/Bridges/bridge_optimizer.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -511,14 +511,6 @@ function _delete_variables_in_variables_constraints(
511511
vis::Vector{MOI.VariableIndex},
512512
)
513513
c_map = Constraint.bridges(b)::Constraint.Map
514-
# Delete all `MOI.VectorOfVariables` constraints of these variables.
515-
# We reverse for the same reason as for `VariableIndex` below.
516-
# As the iterators are lazy, when the inner bridge constraint is deleted,
517-
# it won't be part of the iteration.
518-
for ci in
519-
Iterators.reverse(Constraint.vector_of_variables_constraints(c_map))
520-
_delete_variables_in_vector_of_variables_constraint(b, vis, ci)
521-
end
522514
# Delete all `MOI.VariableIndex` constraints of these variables.
523515
for vi in vis
524516
# If a bridged `VariableIndex` constraints creates a second one,
@@ -532,6 +524,15 @@ function _delete_variables_in_variables_constraints(
532524
end
533525
end
534526
end
527+
# Delete all `MOI.VectorOfVariables` constraints of these variables.
528+
# We reverse for the same reason as for `VariableIndex` below.
529+
# As the iterators are lazy, when the inner bridge constraint is deleted,
530+
# it won't be part of the iteration.
531+
for ci in
532+
Iterators.reverse(Constraint.vector_of_variables_constraints(c_map))
533+
_delete_variables_in_vector_of_variables_constraint(b, vis, ci)
534+
end
535+
return
535536
end
536537

537538
function _delete_variables_in_bridged_objective(

test/Bridges/bridge_optimizer.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,35 @@ function test_BridgeRequiresFiniteDomainError()
14631463
return
14641464
end
14651465

1466+
MOI.Utilities.@model(
1467+
Model2817,
1468+
(),
1469+
(),
1470+
(MOI.Nonnegatives,),
1471+
(),
1472+
(),
1473+
(),
1474+
(),
1475+
(MOI.VectorAffineFunction,)
1476+
);
1477+
1478+
function MOI.supports_constraint(
1479+
::Model2817,
1480+
::Type{MOI.VariableIndex},
1481+
::Type{S},
1482+
) where {S<:MOI.AbstractScalarSet}
1483+
return false
1484+
end
1485+
1486+
function test_issue_2817()
1487+
model = MOI.Bridges.full_bridge_optimizer(Model2817{Float64}(), Float64);
1488+
x, _ = MOI.add_constrained_variable(model, MOI.Interval(0.0, 1.0));
1489+
MOI.delete(model, x)
1490+
@test !MOI.is_valid(model, x)
1491+
@test MOI.is_empty(model)
1492+
return
1493+
end
1494+
14661495
end # module
14671496

14681497
TestBridgeOptimizer.runtests()

0 commit comments

Comments
 (0)