Skip to content

Add MOI.modify for multiple changes at once #216

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 2 commits into from
May 4, 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
40 changes: 40 additions & 0 deletions src/MOI_wrapper/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2155,6 +2155,46 @@ function MOI.modify(
return
end

function MOI.modify(
model::Optimizer,
cis::Vector{MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64},S}},
changes::Vector{MOI.ScalarCoefficientChange{Float64}},
) where {S}
nels = length(cis)
@assert nels == length(changes)
rows = Vector{Cint}(undef, nels)
cols = Vector{Cint}(undef, nels)
coefs = Vector{Cdouble}(undef, nels)
for i in 1:nels
rows[i] = Cint(_info(model, cis[i]).row)
cols[i] = column(model, changes[i].variable)
coefs[i] = changes[i].new_coefficient
end
for row in unique(rows)
nnz = glp_get_mat_row(model, row, C_NULL, C_NULL)
indices, coefficients = zeros(Cint, nnz), zeros(Cdouble, nnz)
glp_get_mat_row(model, row, offset(indices), offset(coefficients))
idxs_changed_in_row = findall(x -> x == row, rows)
for i in idxs_changed_in_row
index = something(findfirst(isequal(cols[i]), indices), 0)
if index > 0
coefficients[index] = coefs[i]
else
push!(indices, cols[i])
push!(coefficients, coefs[i])
end
end
glp_set_mat_row(
model,
row,
length(indices),
offset(indices),
offset(coefficients),
)
end
return
end

function MOI.set(
model::Optimizer,
::MOI.ConstraintFunction,
Expand Down
45 changes: 45 additions & 0 deletions test/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,51 @@ function test_variable_basis_status()
return
end

function test_multiple_modifications()
model = GLPK.Optimizer()

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]
end

end # module

TestMOIWrapper.runtests()