Skip to content

Commit bba88cc

Browse files
Merge pull request jump-dev#96 from jump-dev/gb/multiple_modifies
Make it work with the vector modifications interface
2 parents 46aaebb + 1c221eb commit bba88cc

File tree

4 files changed

+85
-12
lines changed

4 files changed

+85
-12
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
name = "ParametricOptInterface"
22
uuid = "0ce4ce61-57bf-432b-a095-efac525d185e"
33
authors = ["Tomás Gutierrez <tomasfmgutierrez@gmail.com>"]
4-
version = "0.3.5"
4+
version = "0.4.0"
55

66
[deps]
77
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
88

99
[compat]
10-
MathOptInterface = "1"
10+
MathOptInterface = "1.3"
1111
julia = "1.6"
1212

1313
[extras]

src/update_parameters.jl

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,21 @@ 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}(
311+
undef,
312+
length(new_coeff_per_variable),
313+
)
314+
i = 1
310315
for (vi, value) in new_coeff_per_variable
311-
MOI.modify(
312-
model.optimizer,
313-
old_ci,
314-
MOI.ScalarCoefficientChange(vi, value),
315-
)
316+
changes[i] = MOI.ScalarCoefficientChange(vi, value)
317+
i += 1
316318
end
319+
# Make multiple changes at once.
320+
MOI.modify(
321+
model.optimizer,
322+
fill(old_ci, length(new_coeff_per_variable)),
323+
changes,
324+
)
317325
end
318326
return model
319327
end
@@ -347,13 +355,16 @@ function update_parameter_in_quadratic_objective_pv!(model::Optimizer)
347355
end
348356

349357
F_pv = MOI.get(model.optimizer, MOI.ObjectiveFunctionType())
358+
changes = Vector{MOI.ScalarCoefficientChange}(
359+
undef,
360+
length(new_coeff_per_variable),
361+
)
362+
i = 1
350363
for (vi, value) in new_coeff_per_variable
351-
MOI.modify(
352-
model.optimizer,
353-
MOI.ObjectiveFunction{F_pv}(),
354-
MOI.ScalarCoefficientChange(vi, value),
355-
)
364+
changes[i] = MOI.ScalarCoefficientChange(vi, value)
365+
i += 1
356366
end
367+
MOI.modify(model.optimizer, MOI.ObjectiveFunction{F_pv}(), changes)
357368
return model
358369
end
359370

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)