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
11 changes: 10 additions & 1 deletion src/Bridges/Constraint/bridges/functionize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,17 @@ function MOI.delete(
new_func = MOI.Utilities.eachscalar(func)[idx]
set = MOI.get(model, MOI.ConstraintSet(), bridge.constraint)
new_set = MOI.update_dimension(set, MOI.dimension(set) - 1)
MOI.delete(model, bridge.constraint)
# If we first do `MOI.delete` and then `MOI.add_constraint`,
# there might be an issue of `MOI.delete` ends up deleting a
# bridged variables.
# Indeed, in that case,
# `_delete_variables_in_vector_of_variables_constraint` might
# then try to get `get` `ConstraintFunction` for this bridge
# which will fail since the `bridge.constraint` is invalid.
# We avoid this issue by calling `add_constraint` first.
old_constraint = bridge.constraint
bridge.constraint = MOI.add_constraint(model, new_func, new_set)
MOI.delete(model, old_constraint)
return
end

Expand Down
4 changes: 3 additions & 1 deletion src/Bridges/Variable/map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,9 @@ That is, the variable indices bridged by this bridge or the bridges that
created it will not be unbridged in [`unbridged_function`](@ref).
"""
function call_in_context(map::Map, bridge_index::Int64, f::Function)
if iszero(bridge_index)
# This is a shortcut that is used in particular in the common case where
# no variable bridge is used.
if iszero(bridge_index) && iszero(map.current_context)
return f()
end
previous_context = map.current_context
Expand Down
15 changes: 15 additions & 0 deletions test/Bridges/lazy_bridge_optimizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2110,6 +2110,21 @@ function test_objective_conversion_cost(T = Float64)
return
end

function test_delete_index_in_vector(T::Type = Float64)
model = MOI.instantiate(StandardSDPAModel{T}; with_bridge_type = T)
x = MOI.add_variables(model, 4)
c = MOI.add_constraint(model, x, MOI.Nonpositives(4))
@test MOI.is_valid(model, c)
MOI.delete(model, x[3])
@test MOI.is_valid(model, c)
@test MOI.is_valid(model, x[1])
@test MOI.is_valid(model, x[2])
@test !MOI.is_valid(model, x[3])
@test MOI.is_valid(model, x[4])
@test MOI.get(model, MOI.ConstraintFunction(), c).variables == x[[1, 2, 4]]
return
end

end # module

TestBridgesLazyBridgeOptimizer.runtests()