Skip to content

Commit fdf66a2

Browse files
authored
Merge pull request #164 from jump-dev/gb/add_new_modify
Add multiple modifications interface
2 parents 9c829c2 + d27f15c commit fdf66a2

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

src/MOI/MOI_wrapper.jl

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3257,6 +3257,34 @@ function MOI.modify(
32573257
return
32583258
end
32593259

3260+
function MOI.modify(
3261+
model::Optimizer,
3262+
cis::Vector{MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64}, S}},
3263+
chgs::Vector{MOI.ScalarCoefficientChange{Float64}}
3264+
) where {S}
3265+
nels = length(cis)
3266+
@assert nels == length(chgs)
3267+
3268+
rows = Vector{Cint}(undef, nels)
3269+
cols = Vector{Cint}(undef, nels)
3270+
coefs = Vector{Float64}(undef, nels)
3271+
3272+
for i in 1:nels
3273+
rows[i] = Cint(_info(model, cis[i]).row)
3274+
cols[i] = Cint(_info(model, chgs[i].variable).column)
3275+
coefs[i] = chgs[i].new_coefficient
3276+
end
3277+
3278+
Xpress.chgmcoef(
3279+
model.inner,
3280+
nels,
3281+
rows,
3282+
cols,
3283+
coefs
3284+
)
3285+
return
3286+
end
3287+
32603288
function MOI.modify(
32613289
model::Optimizer,
32623290
c::MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}},
@@ -3269,6 +3297,26 @@ function MOI.modify(
32693297
return
32703298
end
32713299

3300+
function MOI.modify(
3301+
model::Optimizer,
3302+
c::MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}},
3303+
chgs::Vector{MOI.ScalarCoefficientChange{Float64}}
3304+
)
3305+
@assert model.objective_type == SCALAR_AFFINE
3306+
nels = length(chgs)
3307+
cols = Vector{Int}(undef, nels)
3308+
coefs = Vector{Float64}(undef, nels)
3309+
3310+
for i in 1:nels
3311+
cols[i] = _info(model, chgs[i].variable).column
3312+
coefs[i] = chgs[i].new_coefficient
3313+
end
3314+
3315+
Xpress.chgobj(model.inner, cols, coefs)
3316+
model.is_objective_set = true
3317+
return
3318+
end
3319+
32723320
"""
32733321
_replace_with_matching_sparsity!(
32743322
model::Optimizer,

src/api.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2271,7 +2271,7 @@ Used to change multiple coefficients in the matrix. If any coefficient does not
22712271
22722272
"""
22732273
function chgmcoef(prob::XpressProblem, ncoeffs, _mrow::Vector{<:Integer}, _mcol::Vector{<:Integer}, _dval)
2274-
@checked Lib.XPRSchgmcoef(prob, ncoeffs, Cint(_mrow .- 1), Cint(_mcol .- 1), _dval)
2274+
@checked Lib.XPRSchgmcoef(prob, ncoeffs, _mrow .- one(Cint), _mcol .- one(Cint), _dval)
22752275
end
22762276

22772277
# # Disable 64Bit versions do to reliability issues.

test/MathOptInterface/MOI_wrapper.jl

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,68 @@ function test_MIP_Start()
635635
@test isapprox(9945.0, computed_obj_value3; rtol = rtol, atol = atol)
636636
end
637637

638+
function test_multiple_modifications()
639+
model = Xpress.Optimizer(OUTPUTLOG = 0)
640+
641+
x = MOI.add_variables(model, 3)
642+
643+
saf = MOI.ScalarAffineFunction(
644+
[
645+
MOI.ScalarAffineTerm(1.0, x[1]),
646+
MOI.ScalarAffineTerm(1.0, x[2]),
647+
MOI.ScalarAffineTerm(1.0, x[3]),
648+
],
649+
0.0,
650+
)
651+
ci1 = MOI.add_constraint(model, saf, MOI.LessThan(1.0))
652+
ci2 = MOI.add_constraint(model, saf, MOI.LessThan(2.0))
653+
654+
MOI.set(
655+
model,
656+
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
657+
saf,
658+
)
659+
660+
fc1 = MOI.get(model, MOI.ConstraintFunction(), ci1)
661+
@test MOI.coefficient.(fc1.terms) == [1.0, 1.0, 1.0]
662+
fc2 = MOI.get(model, MOI.ConstraintFunction(), ci2)
663+
@test MOI.coefficient.(fc2.terms) == [1.0, 1.0, 1.0]
664+
obj = MOI.get(
665+
model,
666+
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
667+
)
668+
@test MOI.coefficient.(obj.terms) == [1.0, 1.0, 1.0]
669+
670+
changes_cis = [
671+
MOI.ScalarCoefficientChange(MOI.VariableIndex(1), 4.0)
672+
MOI.ScalarCoefficientChange(MOI.VariableIndex(1), 0.5)
673+
MOI.ScalarCoefficientChange(MOI.VariableIndex(3), 2.0)
674+
]
675+
MOI.modify(model, [ci1, ci2, ci2], changes_cis)
676+
677+
fc1 = MOI.get(model, MOI.ConstraintFunction(), ci1)
678+
@test MOI.coefficient.(fc1.terms) == [4.0, 1.0, 1.0]
679+
fc2 = MOI.get(model, MOI.ConstraintFunction(), ci2)
680+
@test MOI.coefficient.(fc2.terms) == [0.5, 1.0, 2.0]
681+
682+
changes_obj = [
683+
MOI.ScalarCoefficientChange(MOI.VariableIndex(1), 4.0)
684+
MOI.ScalarCoefficientChange(MOI.VariableIndex(2), 10.0)
685+
MOI.ScalarCoefficientChange(MOI.VariableIndex(3), 2.0)
686+
]
687+
MOI.modify(
688+
model,
689+
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
690+
changes_obj,
691+
)
692+
693+
obj = MOI.get(
694+
model,
695+
MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
696+
)
697+
@test MOI.coefficient.(obj.terms) == [4.0, 10.0, 2.0]
698+
end
699+
638700
end
639701

640702
TestMOIWrapper.runtests()

0 commit comments

Comments
 (0)