jump-dev / JuMP.jl

Modeling language for Mathematical Optimization (linear, mixed-integer, conic, semidefinite, nonlinear)
http://jump.dev/JuMP.jl/
Other
2.22k stars 393 forks source link

Add batched modification methods #3716

Closed joaquimg closed 6 months ago

joaquimg commented 6 months ago

Modify problems bit by bit can be surprisingly slow in some solvers (like Xpress). Batch modifications like this solve the problems. I was working on a model that changes from 750s to 150s build+modify+solve time after using batch modifications.

rhs would be weird in plural as rhss, I don't know what to do about it.

codecov[bot] commented 6 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 98.41%. Comparing base (1aedb44) to head (48012e9). Report is 2 commits behind head on master.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #3716 +/- ## ========================================== + Coverage 98.37% 98.41% +0.03% ========================================== Files 43 43 Lines 5737 5813 +76 ========================================== + Hits 5644 5721 +77 + Misses 93 92 -1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

pedroripper commented 6 months ago

Hey everyone!

I tried to use this new dispatch for set_normalized_coefficient but got an error... here is a MWE

using JuMP
using HiGHS

model = Model(HiGHS.Optimizer)

@variable(model, x[1:3] >= 0)
@constraint(model, c[i in 1:3], x[i] == i)

constraint_coeffs = Vector{JuMP.ConstraintRef}()
variables = Vector{JuMP.VariableRef}()
coefficients = Vector{Float64}()

for i in 1:3
    push!(constraint_coeffs, c[i])
    push!(variables, x[i])
    push!(coefficients, 5)
end

set_normalized_coefficient(constraint_coeffs, variables, coefficients)

And when I am more specific about the type of the constraint reference vector, it works

using JuMP
using HiGHS

model = Model(HiGHS.Optimizer)

@variable(model, x[1:3] >= 0)
@constraint(model, c[i in 1:3], x[i] == i)

constraint_coeffs =  Vector{JuMP.ConstraintRef{JuMP.Model, MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}}, ScalarShape}}()
variables = Vector{JuMP.VariableRef}()
coefficients = Vector{Float64}()

for i in 1:3
    push!(constraint_coeffs, c[i])
    push!(variables, x[i])
    push!(coefficients, 5)
end

set_normalized_coefficient(constraint_coeffs, variables, coefficients)
odow commented 6 months ago

@joaquimg I'll fix the error messages.

@pedroripper this is expected behavior because of the abstract Vector{ConstraintRef}.

franckgaga commented 6 months ago

@joaquimg @odow @blegat I just wanted to thank you for this excellent contribution, you are doing amazing work!

ModelPredictiveControl.jl uses OSQP.jl by default and it also has this issue. I raised an issue (#3566) in which I was suspecting a type instability for the bad performance. It was not the culprit at all. Using the new set_normalized_rhs and set_objective_coefficient methods with vectors drastically improved the performance ! :partying_face: