Skip to content

Commit c0197d3

Browse files
authored
[Bridges] fix modify MultirowChange for Constraint.SetMap (#2662)
1 parent e1aecd6 commit c0197d3

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/Bridges/Constraint/set_map.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,20 @@ end
262262
function MOI.modify(
263263
model::MOI.ModelLike,
264264
bridge::BT,
265-
change::MOI.MultirowChange,
266-
) where {BT<:MultiSetMapBridge}
265+
change::MOI.MultirowChange{T},
266+
) where {T,BT<:MultiSetMapBridge{T}}
267267
# It is important here that `change.new_coefficients` contains the complete
268268
# new sparse column associated to the variable. Calling modify twice with
269269
# part of the column won't work since the linear map might reset all the
270270
# column each time.
271-
coefficients = MOI.Bridges.map_function(BT, change.new_coefficients)
272-
new_change = MOI.MultirowChange(change.variable, coefficients)
271+
n = MOI.dimension(MOI.get(model, MOI.ConstraintSet(), bridge.constraint))
272+
dense = zeros(T, n)
273+
for (row, value) in change.new_coefficients
274+
dense[row] += value
275+
end
276+
map_dense = MOI.Bridges.map_function(BT, dense)
277+
sparse = Tuple{Int64,T}[rv for rv in enumerate(map_dense) if !iszero(rv[2])]
278+
new_change = MOI.MultirowChange(change.variable, sparse)
273279
MOI.modify(model, bridge.constraint, new_change)
274280
return
275281
end

test/Bridges/Constraint/NormOneConeToNormConeBridge.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,25 @@ function test_runtests_norm_inf()
6565
)
6666
return
6767
end
68+
69+
function test_modify_MultirowChange()
70+
inner = MOI.Utilities.Model{Float64}()
71+
model = MOI.Bridges.Constraint.NormOneConeToNormCone{Float64}(inner)
72+
x = MOI.add_variables(model, 2)
73+
f = MOI.Utilities.vectorize(1.0 .* x)
74+
c = MOI.add_constraint(model, f, MOI.NormOneCone(2))
75+
@test (
76+
MOI.get(model, MOI.ConstraintFunction(), c),
77+
MOI.Utilities.vectorize([1.0 * x[1], 1.0 * x[2]]),
78+
)
79+
MOI.modify(model, c, MOI.MultirowChange(x[2], [(2, 2.0)]))
80+
@test (
81+
MOI.get(model, MOI.ConstraintFunction(), c),
82+
MOI.Utilities.vectorize([1.0 * x[1], 2.0 * x[2]]),
83+
)
84+
return
85+
end
86+
6887
end # module
6988

7089
TestConstraintNormSpecialCase.runtests()

0 commit comments

Comments
 (0)