Skip to content

Commit afab104

Browse files
make it work with the vector modifications interface
1 parent f085f71 commit afab104

File tree

3 files changed

+78
-7
lines changed

3 files changed

+78
-7
lines changed

src/update_parameters.jl

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,18 @@ function update_parameter_in_quadratic_constraints_pv!(model::Optimizer)
307307
end
308308
end
309309
old_ci = model.quadratic_added_cache[ci]
310+
changes = Vector{MOI.ScalarCoefficientChange}(undef, length(new_coeff_per_variable))
311+
i = 1
310312
for (vi, value) in new_coeff_per_variable
311-
MOI.modify(
313+
changes[i] = MOI.ScalarCoefficientChange(vi, value)
314+
i += 1
315+
end
316+
# Make multiple changes at once.
317+
MOI.modify(
312318
model.optimizer,
313-
old_ci,
314-
MOI.ScalarCoefficientChange(vi, value),
319+
fill(old_ci, length(new_coeff_per_variable)),
320+
changes,
315321
)
316-
end
317322
end
318323
return model
319324
end
@@ -347,13 +352,17 @@ function update_parameter_in_quadratic_objective_pv!(model::Optimizer)
347352
end
348353

349354
F_pv = MOI.get(model.optimizer, MOI.ObjectiveFunctionType())
355+
changes = Vector{MOI.ScalarCoefficientChange}(undef, length(new_coeff_per_variable))
356+
i = 1
350357
for (vi, value) in new_coeff_per_variable
351-
MOI.modify(
358+
changes[i] = MOI.ScalarCoefficientChange(vi, value)
359+
i += 1
360+
end
361+
MOI.modify(
352362
model.optimizer,
353363
MOI.ObjectiveFunction{F_pv}(),
354-
MOI.ScalarCoefficientChange(vi, value),
364+
changes,
355365
)
356-
end
357366
return model
358367
end
359368

test/modifications_tests.jl

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
@testset "Multiple modifications" begin
2+
model = POI.Optimizer(MOI.Utilities.Model{Float64}())
3+
4+
x = MOI.add_variables(model, 3)
5+
6+
saf = MOI.ScalarAffineFunction(
7+
[
8+
MOI.ScalarAffineTerm(1.0, x[1]),
9+
MOI.ScalarAffineTerm(1.0, x[2]),
10+
MOI.ScalarAffineTerm(1.0, x[3]),
11+
],
12+
0.0,
13+
)
14+
ci1 = MOI.add_constraint(model, saf, MOI.LessThan(1.0))
15+
ci2 = MOI.add_constraint(model, saf, MOI.LessThan(2.0))
16+
17+
MOI.set(
18+
model,
19+
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
20+
saf,
21+
)
22+
23+
fc1 = MOI.get(model, MOI.ConstraintFunction(), ci1)
24+
@test MOI.coefficient.(fc1.terms) == [1.0, 1.0, 1.0]
25+
fc2 = MOI.get(model, MOI.ConstraintFunction(), ci2)
26+
@test MOI.coefficient.(fc2.terms) == [1.0, 1.0, 1.0]
27+
obj = MOI.get(
28+
model,
29+
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
30+
)
31+
@test MOI.coefficient.(obj.terms) == [1.0, 1.0, 1.0]
32+
33+
changes_cis = [
34+
MOI.ScalarCoefficientChange(MOI.VariableIndex(1), 4.0)
35+
MOI.ScalarCoefficientChange(MOI.VariableIndex(1), 0.5)
36+
MOI.ScalarCoefficientChange(MOI.VariableIndex(3), 2.0)
37+
]
38+
MOI.modify(model, [ci1, ci2, ci2], changes_cis)
39+
40+
fc1 = MOI.get(model, MOI.ConstraintFunction(), ci1)
41+
@test MOI.coefficient.(fc1.terms) == [4.0, 1.0, 1.0]
42+
fc2 = MOI.get(model, MOI.ConstraintFunction(), ci2)
43+
@test MOI.coefficient.(fc2.terms) == [0.5, 1.0, 2.0]
44+
45+
changes_obj = [
46+
MOI.ScalarCoefficientChange(MOI.VariableIndex(1), 4.0)
47+
MOI.ScalarCoefficientChange(MOI.VariableIndex(2), 10.0)
48+
MOI.ScalarCoefficientChange(MOI.VariableIndex(3), 2.0)
49+
]
50+
MOI.modify(
51+
model,
52+
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
53+
changes_obj,
54+
)
55+
56+
obj = MOI.get(
57+
model,
58+
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
59+
)
60+
@test MOI.coefficient.(obj.terms) == [4.0, 10.0, 2.0]
61+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ include("dual_tests.jl")
2121
include("quad_tests.jl")
2222
include("sdp_tests.jl")
2323
include("vector_affine_tests.jl")
24+
include("modifications_tests.jl")
2425
include("jump_tests.jl")

0 commit comments

Comments
 (0)