Skip to content

Commit dee196e

Browse files
authored
Merge pull request #216 from rafabench/rb/moi_multiple_modifications
Add MOI.modify for multiple changes at once
2 parents 9949fe9 + d203670 commit dee196e

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

src/MOI_wrapper/MOI_wrapper.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,6 +2167,46 @@ function MOI.modify(
21672167
return
21682168
end
21692169

2170+
function MOI.modify(
2171+
model::Optimizer,
2172+
cis::Vector{MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64},S}},
2173+
changes::Vector{MOI.ScalarCoefficientChange{Float64}},
2174+
) where {S}
2175+
nels = length(cis)
2176+
@assert nels == length(changes)
2177+
rows = Vector{Cint}(undef, nels)
2178+
cols = Vector{Cint}(undef, nels)
2179+
coefs = Vector{Cdouble}(undef, nels)
2180+
for i in 1:nels
2181+
rows[i] = Cint(_info(model, cis[i]).row)
2182+
cols[i] = column(model, changes[i].variable)
2183+
coefs[i] = changes[i].new_coefficient
2184+
end
2185+
for row in unique(rows)
2186+
nnz = glp_get_mat_row(model, row, C_NULL, C_NULL)
2187+
indices, coefficients = zeros(Cint, nnz), zeros(Cdouble, nnz)
2188+
glp_get_mat_row(model, row, offset(indices), offset(coefficients))
2189+
idxs_changed_in_row = findall(x -> x == row, rows)
2190+
for i in idxs_changed_in_row
2191+
index = something(findfirst(isequal(cols[i]), indices), 0)
2192+
if index > 0
2193+
coefficients[index] = coefs[i]
2194+
else
2195+
push!(indices, cols[i])
2196+
push!(coefficients, coefs[i])
2197+
end
2198+
end
2199+
glp_set_mat_row(
2200+
model,
2201+
row,
2202+
length(indices),
2203+
offset(indices),
2204+
offset(coefficients),
2205+
)
2206+
end
2207+
return
2208+
end
2209+
21702210
function MOI.set(
21712211
model::Optimizer,
21722212
::MOI.ConstraintFunction,

test/MOI_wrapper.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,51 @@ function test_variable_basis_status()
556556
return
557557
end
558558

559+
function test_multiple_modifications()
560+
model = GLPK.Optimizer()
561+
562+
x = MOI.add_variables(model, 3)
563+
564+
saf = MOI.ScalarAffineFunction(
565+
[
566+
MOI.ScalarAffineTerm(1.0, x[1]),
567+
MOI.ScalarAffineTerm(1.0, x[2]),
568+
MOI.ScalarAffineTerm(1.0, x[3]),
569+
],
570+
0.0,
571+
)
572+
ci1 = MOI.add_constraint(model, saf, MOI.LessThan(1.0))
573+
ci2 = MOI.add_constraint(model, saf, MOI.LessThan(2.0))
574+
575+
MOI.set(
576+
model,
577+
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
578+
saf,
579+
)
580+
581+
fc1 = MOI.get(model, MOI.ConstraintFunction(), ci1)
582+
@test MOI.coefficient.(fc1.terms) == [1.0, 1.0, 1.0]
583+
fc2 = MOI.get(model, MOI.ConstraintFunction(), ci2)
584+
@test MOI.coefficient.(fc2.terms) == [1.0, 1.0, 1.0]
585+
obj = MOI.get(
586+
model,
587+
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
588+
)
589+
@test MOI.coefficient.(obj.terms) == [1.0, 1.0, 1.0]
590+
591+
changes_cis = [
592+
MOI.ScalarCoefficientChange(MOI.VariableIndex(1), 4.0)
593+
MOI.ScalarCoefficientChange(MOI.VariableIndex(1), 0.5)
594+
MOI.ScalarCoefficientChange(MOI.VariableIndex(3), 2.0)
595+
]
596+
MOI.modify(model, [ci1, ci2, ci2], changes_cis)
597+
598+
fc1 = MOI.get(model, MOI.ConstraintFunction(), ci1)
599+
@test MOI.coefficient.(fc1.terms) == [4.0, 1.0, 1.0]
600+
fc2 = MOI.get(model, MOI.ConstraintFunction(), ci2)
601+
@test MOI.coefficient.(fc2.terms) == [0.5, 1.0, 2.0]
602+
end
603+
559604
end # module
560605

561606
TestMOIWrapper.runtests()

0 commit comments

Comments
 (0)