From 116d6722f28d975d0a32539a16e39e336771c549 Mon Sep 17 00:00:00 2001 From: guilhermebodin Date: Thu, 28 Apr 2022 19:31:55 -0300 Subject: [PATCH 1/2] Add multiple modifications interface --- src/MOI/MOI_wrapper.jl | 47 +++++++++++++++++++++ src/api.jl | 2 +- test/MathOptInterface/MOI_wrapper.jl | 62 ++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 1 deletion(-) diff --git a/src/MOI/MOI_wrapper.jl b/src/MOI/MOI_wrapper.jl index 38018bd3..afcc68f9 100644 --- a/src/MOI/MOI_wrapper.jl +++ b/src/MOI/MOI_wrapper.jl @@ -3257,6 +3257,34 @@ function MOI.modify( return end +function MOI.modify( + model::Optimizer, + cis::Vector{MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64}, <:Any}}, + chgs::Vector{MOI.ScalarCoefficientChange{Float64}} +) + nels = length(cis) + @assert nels == length(chgs) + + rows = Vector{Int}(undef, nels) + cols = Vector{Int}(undef, nels) + coefs = Vector{Float64}(undef, nels) + + for i in 1:nels + rows[i] = _info(model, cis[i]).row + cols[i] = _info(model, chgs[i].variable).column + coefs[i] = chgs[i].new_coefficient + end + + Xpress.chgmcoef( + model.inner, + nels, + rows, + cols, + coefs + ) + return +end + function MOI.modify( model::Optimizer, c::MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}, @@ -3269,6 +3297,25 @@ function MOI.modify( return end +function MOI.modify( + model::Optimizer, + c::MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}, + chgs::Vector{MOI.ScalarCoefficientChange{Float64}} +) + @assert model.objective_type == SCALAR_AFFINE + cols = Vector{Int}(undef, nels) + coefs = Vector{Float64}(undef, nels) + + for i in 1:nels + cols[i] = _info(model, chgs[i].variable).column + coefs[i] = chgs[i].new_coefficient + end + + Xpress.chgobj(model.inner, cols, coefs) + model.is_objective_set = true + return +end + """ _replace_with_matching_sparsity!( model::Optimizer, diff --git a/src/api.jl b/src/api.jl index 466bef93..99753262 100644 --- a/src/api.jl +++ b/src/api.jl @@ -2271,7 +2271,7 @@ Used to change multiple coefficients in the matrix. If any coefficient does not """ function chgmcoef(prob::XpressProblem, ncoeffs, _mrow::Vector{<:Integer}, _mcol::Vector{<:Integer}, _dval) - @checked Lib.XPRSchgmcoef(prob, ncoeffs, Cint(_mrow .- 1), Cint(_mcol .- 1), _dval) + @checked Lib.XPRSchgmcoef(prob, ncoeffs, _mrow .- 1, _mcol .- 1, _dval) end # # Disable 64Bit versions do to reliability issues. diff --git a/test/MathOptInterface/MOI_wrapper.jl b/test/MathOptInterface/MOI_wrapper.jl index 80dbdb34..eb7d9a97 100644 --- a/test/MathOptInterface/MOI_wrapper.jl +++ b/test/MathOptInterface/MOI_wrapper.jl @@ -635,6 +635,68 @@ function test_MIP_Start() @test isapprox(9945.0, computed_obj_value3; rtol = rtol, atol = atol) end +function test_multiple_modifications() + model = Xpress.Optimizer(OUTPUTLOG = 0) + + 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 + end TestMOIWrapper.runtests() \ No newline at end of file From d27f15cf93cb5a3ed6e65bbe96efe017a34df6e1 Mon Sep 17 00:00:00 2001 From: guilhermebodin Date: Fri, 29 Apr 2022 15:57:34 -0300 Subject: [PATCH 2/2] fix errors with Cint --- src/MOI/MOI_wrapper.jl | 13 +++++++------ src/api.jl | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/MOI/MOI_wrapper.jl b/src/MOI/MOI_wrapper.jl index afcc68f9..a5a2b87c 100644 --- a/src/MOI/MOI_wrapper.jl +++ b/src/MOI/MOI_wrapper.jl @@ -3259,19 +3259,19 @@ end function MOI.modify( model::Optimizer, - cis::Vector{MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64}, <:Any}}, + cis::Vector{MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64}, S}}, chgs::Vector{MOI.ScalarCoefficientChange{Float64}} -) +) where {S} nels = length(cis) @assert nels == length(chgs) - rows = Vector{Int}(undef, nels) - cols = Vector{Int}(undef, nels) + rows = Vector{Cint}(undef, nels) + cols = Vector{Cint}(undef, nels) coefs = Vector{Float64}(undef, nels) for i in 1:nels - rows[i] = _info(model, cis[i]).row - cols[i] = _info(model, chgs[i].variable).column + rows[i] = Cint(_info(model, cis[i]).row) + cols[i] = Cint(_info(model, chgs[i].variable).column) coefs[i] = chgs[i].new_coefficient end @@ -3303,6 +3303,7 @@ function MOI.modify( chgs::Vector{MOI.ScalarCoefficientChange{Float64}} ) @assert model.objective_type == SCALAR_AFFINE + nels = length(chgs) cols = Vector{Int}(undef, nels) coefs = Vector{Float64}(undef, nels) diff --git a/src/api.jl b/src/api.jl index 99753262..d72e3c35 100644 --- a/src/api.jl +++ b/src/api.jl @@ -2271,7 +2271,7 @@ Used to change multiple coefficients in the matrix. If any coefficient does not """ function chgmcoef(prob::XpressProblem, ncoeffs, _mrow::Vector{<:Integer}, _mcol::Vector{<:Integer}, _dval) - @checked Lib.XPRSchgmcoef(prob, ncoeffs, _mrow .- 1, _mcol .- 1, _dval) + @checked Lib.XPRSchgmcoef(prob, ncoeffs, _mrow .- one(Cint), _mcol .- one(Cint), _dval) end # # Disable 64Bit versions do to reliability issues.