Skip to content

Make it work with the vector modifications interface #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 30, 2022
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
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name = "ParametricOptInterface"
uuid = "0ce4ce61-57bf-432b-a095-efac525d185e"
authors = ["Tomás Gutierrez <tomasfmgutierrez@gmail.com>"]
version = "0.3.5"
version = "0.4.0"

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

[compat]
MathOptInterface = "1"
MathOptInterface = "1.3"
julia = "1.6"

[extras]
Expand Down
31 changes: 21 additions & 10 deletions src/update_parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,21 @@ function update_parameter_in_quadratic_constraints_pv!(model::Optimizer)
end
end
old_ci = model.quadratic_added_cache[ci]
changes = Vector{MOI.ScalarCoefficientChange}(
undef,
length(new_coeff_per_variable),
)
i = 1
for (vi, value) in new_coeff_per_variable
MOI.modify(
model.optimizer,
old_ci,
MOI.ScalarCoefficientChange(vi, value),
)
changes[i] = MOI.ScalarCoefficientChange(vi, value)
i += 1
end
# Make multiple changes at once.
MOI.modify(
model.optimizer,
fill(old_ci, length(new_coeff_per_variable)),
changes,
)
end
return model
end
Expand Down Expand Up @@ -347,13 +355,16 @@ function update_parameter_in_quadratic_objective_pv!(model::Optimizer)
end

F_pv = MOI.get(model.optimizer, MOI.ObjectiveFunctionType())
changes = Vector{MOI.ScalarCoefficientChange}(
undef,
length(new_coeff_per_variable),
)
i = 1
for (vi, value) in new_coeff_per_variable
MOI.modify(
model.optimizer,
MOI.ObjectiveFunction{F_pv}(),
MOI.ScalarCoefficientChange(vi, value),
)
changes[i] = MOI.ScalarCoefficientChange(vi, value)
i += 1
end
MOI.modify(model.optimizer, MOI.ObjectiveFunction{F_pv}(), changes)
return model
end

Expand Down
61 changes: 61 additions & 0 deletions test/modifications_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
@testset "Multiple modifications" begin
model = POI.Optimizer(MOI.Utilities.Model{Float64}())

x = MOI.add_variables(model, 3)

saf = MOI.ScalarAffineFunction(
[
MOI.ScalarAffineTerm(1.0, x[1]),
MOI.ScalarAffineTerm(1.0, x[2]),
MOI.ScalarAffineTerm(1.0, x[3]),
],
0.0,
)
ci1 = MOI.add_constraint(model, saf, MOI.LessThan(1.0))
ci2 = MOI.add_constraint(model, saf, MOI.LessThan(2.0))

MOI.set(
model,
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
saf,
)

fc1 = MOI.get(model, MOI.ConstraintFunction(), ci1)
@test MOI.coefficient.(fc1.terms) == [1.0, 1.0, 1.0]
fc2 = MOI.get(model, MOI.ConstraintFunction(), ci2)
@test MOI.coefficient.(fc2.terms) == [1.0, 1.0, 1.0]
obj = MOI.get(
model,
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
)
@test MOI.coefficient.(obj.terms) == [1.0, 1.0, 1.0]

changes_cis = [
MOI.ScalarCoefficientChange(MOI.VariableIndex(1), 4.0)
MOI.ScalarCoefficientChange(MOI.VariableIndex(1), 0.5)
MOI.ScalarCoefficientChange(MOI.VariableIndex(3), 2.0)
]
MOI.modify(model, [ci1, ci2, ci2], changes_cis)

fc1 = MOI.get(model, MOI.ConstraintFunction(), ci1)
@test MOI.coefficient.(fc1.terms) == [4.0, 1.0, 1.0]
fc2 = MOI.get(model, MOI.ConstraintFunction(), ci2)
@test MOI.coefficient.(fc2.terms) == [0.5, 1.0, 2.0]

changes_obj = [
MOI.ScalarCoefficientChange(MOI.VariableIndex(1), 4.0)
MOI.ScalarCoefficientChange(MOI.VariableIndex(2), 10.0)
MOI.ScalarCoefficientChange(MOI.VariableIndex(3), 2.0)
]
MOI.modify(
model,
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
changes_obj,
)

obj = MOI.get(
model,
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
)
@test MOI.coefficient.(obj.terms) == [4.0, 10.0, 2.0]
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ include("dual_tests.jl")
include("quad_tests.jl")
include("sdp_tests.jl")
include("vector_affine_tests.jl")
include("modifications_tests.jl")
include("jump_tests.jl")