From 7fd25ff078d7958c0b95e0c336b5b736615e307e Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 29 Apr 2022 09:53:50 +1200 Subject: [PATCH 1/7] Add set_normalized_coefficients --- docs/src/reference/constraints.md | 1 + src/constraints.jl | 43 +++++++++++++++++++++++++++++++ test/constraint.jl | 23 +++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/docs/src/reference/constraints.md b/docs/src/reference/constraints.md index c0bbe2e045e..75ee5a5aef1 100644 --- a/docs/src/reference/constraints.md +++ b/docs/src/reference/constraints.md @@ -27,6 +27,7 @@ constraint_by_name ```@docs normalized_coefficient set_normalized_coefficient +set_normalized_coefficients normalized_rhs set_normalized_rhs diff --git a/src/constraints.jl b/src/constraints.jl index 83593d988f7..8ebd4c27902 100644 --- a/src/constraints.jl +++ b/src/constraints.jl @@ -682,6 +682,49 @@ function set_normalized_coefficient( return end +""" + set_normalized_coefficients( + con_ref::ConstraintRef, + variable, + new_coefficients::Vector{Tuple{Int64,T}}, + ) + +Set the coefficients of `variable` in the constraint `constraint` to +`new_coefficients`. + +Note that prior to this step, JuMP will aggregate multiple terms containing the +same variable. + +```jldoctest; setup = :(using JuMP), filter=r"≤|<=" +model = Model() +@variable(model, x) +@constraint(model, con, [2x + 3x, 4x] in MOI.Nonnegatives(2)) +set_normalized_coefficients(con, x, [(1, 2.0), (2, 5.0)]) +con + +# output + +con : [2 x, 5x] ∈ MathOptInterface.Nonnegatives(2) +``` +""" +function set_normalized_coefficients( + con_ref::ConstraintRef{<:AbstractModel,<:MOI.ConstraintIndex{F}}, + variable, + new_coefficients::Vector{Tuple{Int64,T}}, +) where { + T, + F<:Union{MOI.VectorAffineFunction{T},MOI.VectorQuadraticFunction{T}}, +} + model = owner_model(con_ref) + MOI.modify( + backend(model), + index(con_ref), + MOI.MultirowChange(index(variable), new_coefficients), + ) + model.is_model_dirty = true + return +end + """ normalized_coefficient(con_ref::ConstraintRef, variable::VariableRef) diff --git a/test/constraint.jl b/test/constraint.jl index a6414a79308..85f4e2d9d16 100644 --- a/test/constraint.jl +++ b/test/constraint.jl @@ -834,6 +834,29 @@ function test_Model_change_coefficient(::Any, ::Any) ) end +function test_Model_change_coefficients(::Any, ::Any) + model = JuMP.Model() + x = @variable(model) + model = Model() + @variable(model, x) + @constraint(model, con, [2x + 3x, 4x] in MOI.Nonnegatives(2)) + @test JuMP.isequal_canonical( + JuMP.constraint_object(con).func, + [5.0x, 4.0x], + ) + set_normalized_coefficients(con, x, [(1, 3.0),]) + @test JuMP.isequal_canonical( + JuMP.constraint_object(con).func, + [3.0x, 4.0x], + ) + set_normalized_coefficients(con, x, [(1, 2.0), (2, 5.0)]) + @test JuMP.isequal_canonical( + JuMP.constraint_object(con).func, + [2.0x, 5.0x], + ) + return +end + function test_Model_change_rhs(::Any, ::Any) model = JuMP.Model() x = @variable(model) From 813ea90297d0f6f2d712b3023722dfbc59896900 Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 29 Apr 2022 10:39:24 +1200 Subject: [PATCH 2/7] Fix formatting --- test/constraint.jl | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/test/constraint.jl b/test/constraint.jl index 85f4e2d9d16..bc34d237cc8 100644 --- a/test/constraint.jl +++ b/test/constraint.jl @@ -840,20 +840,11 @@ function test_Model_change_coefficients(::Any, ::Any) model = Model() @variable(model, x) @constraint(model, con, [2x + 3x, 4x] in MOI.Nonnegatives(2)) - @test JuMP.isequal_canonical( - JuMP.constraint_object(con).func, - [5.0x, 4.0x], - ) - set_normalized_coefficients(con, x, [(1, 3.0),]) - @test JuMP.isequal_canonical( - JuMP.constraint_object(con).func, - [3.0x, 4.0x], - ) - set_normalized_coefficients(con, x, [(1, 2.0), (2, 5.0)]) - @test JuMP.isequal_canonical( - JuMP.constraint_object(con).func, - [2.0x, 5.0x], - ) + @test JuMP.isequal_canonical(JuMP.constraint_object(con).func, [5.0x, 4.0x]) + set_normalized_coefficients(con, x, [(Int64(1), 3.0),]) + @test JuMP.isequal_canonical(JuMP.constraint_object(con).func, [3.0x, 4.0x]) + set_normalized_coefficients(con, x, [(Int64(1), 2.0), (Int64(2), 5.0)]) + @test JuMP.isequal_canonical(JuMP.constraint_object(con).func, [2.0x, 5.0x]) return end From 8dd83a6b38196f31efb218c7a49b9ccaec7033e8 Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 29 Apr 2022 10:57:45 +1200 Subject: [PATCH 3/7] fix formatting II --- src/constraints.jl | 5 +---- test/constraint.jl | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/constraints.jl b/src/constraints.jl index 8ebd4c27902..0ab6dee51ab 100644 --- a/src/constraints.jl +++ b/src/constraints.jl @@ -711,10 +711,7 @@ function set_normalized_coefficients( con_ref::ConstraintRef{<:AbstractModel,<:MOI.ConstraintIndex{F}}, variable, new_coefficients::Vector{Tuple{Int64,T}}, -) where { - T, - F<:Union{MOI.VectorAffineFunction{T},MOI.VectorQuadraticFunction{T}}, -} +) where {T,F<:Union{MOI.VectorAffineFunction{T},MOI.VectorQuadraticFunction{T}}} model = owner_model(con_ref) MOI.modify( backend(model), diff --git a/test/constraint.jl b/test/constraint.jl index bc34d237cc8..d16b2ac02e3 100644 --- a/test/constraint.jl +++ b/test/constraint.jl @@ -841,7 +841,7 @@ function test_Model_change_coefficients(::Any, ::Any) @variable(model, x) @constraint(model, con, [2x + 3x, 4x] in MOI.Nonnegatives(2)) @test JuMP.isequal_canonical(JuMP.constraint_object(con).func, [5.0x, 4.0x]) - set_normalized_coefficients(con, x, [(Int64(1), 3.0),]) + set_normalized_coefficients(con, x, [(Int64(1), 3.0)]) @test JuMP.isequal_canonical(JuMP.constraint_object(con).func, [3.0x, 4.0x]) set_normalized_coefficients(con, x, [(Int64(1), 2.0), (Int64(2), 5.0)]) @test JuMP.isequal_canonical(JuMP.constraint_object(con).func, [2.0x, 5.0x]) From ba726d6ca55372325cf97371a23876085804594f Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Fri, 29 Apr 2022 12:50:54 +1200 Subject: [PATCH 4/7] Update src/constraints.jl --- src/constraints.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/constraints.jl b/src/constraints.jl index 0ab6dee51ab..e297d9d4d6e 100644 --- a/src/constraints.jl +++ b/src/constraints.jl @@ -689,7 +689,7 @@ end new_coefficients::Vector{Tuple{Int64,T}}, ) -Set the coefficients of `variable` in the constraint `constraint` to +Set the coefficients of `variable` in the constraint `con_ref` to `new_coefficients`. Note that prior to this step, JuMP will aggregate multiple terms containing the From 1ed79969d32da2abce4b80bbd0c790e14c4e18ab Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Fri, 29 Apr 2022 17:27:07 +1200 Subject: [PATCH 5/7] Update src/constraints.jl Co-authored-by: Joaquim Dias Garcia --- src/constraints.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/constraints.jl b/src/constraints.jl index e297d9d4d6e..df2559591da 100644 --- a/src/constraints.jl +++ b/src/constraints.jl @@ -692,8 +692,8 @@ end Set the coefficients of `variable` in the constraint `con_ref` to `new_coefficients`. -Note that prior to this step, JuMP will aggregate multiple terms containing the -same variable. +Note that prior to this step, during constraint creation, JuMP will aggregate +multiple terms containing the same variable. ```jldoctest; setup = :(using JuMP), filter=r"≤|<=" model = Model() From 928ce5006628f6e8adc4b9775c71a7fadf65d320 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Sat, 30 Apr 2022 09:11:29 +1200 Subject: [PATCH 6/7] Update src/constraints.jl --- src/constraints.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/constraints.jl b/src/constraints.jl index df2559591da..9b1c15e4521 100644 --- a/src/constraints.jl +++ b/src/constraints.jl @@ -704,7 +704,7 @@ con # output -con : [2 x, 5x] ∈ MathOptInterface.Nonnegatives(2) +con : [2 x, 5 x] ∈ MathOptInterface.Nonnegatives(2) ``` """ function set_normalized_coefficients( From d019f5f75a9aaa16ab62f649bcf5922df11c9a4e Mon Sep 17 00:00:00 2001 From: odow Date: Mon, 16 May 2022 12:17:33 +1200 Subject: [PATCH 7/7] More documentation --- docs/src/manual/constraints.md | 26 ++++++++++++++++++++++++++ src/constraints.jl | 3 ++- test/constraint.jl | 2 -- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/docs/src/manual/constraints.md b/docs/src/manual/constraints.md index bd988399fe2..b0cf22cafd6 100644 --- a/docs/src/manual/constraints.md +++ b/docs/src/manual/constraints.md @@ -614,6 +614,8 @@ con : 2 x ∈ [-4.0, -2.0] ## Modify a variable coefficient +### Scalar constraints + To modify the coefficients for a linear term (modifying the coefficient of a quadratic term is not supported) in a constraint, use [`set_normalized_coefficient`](@ref). To query the current coefficient, use @@ -635,6 +637,30 @@ julia> normalized_coefficient(con, x[2]) [`set_normalized_coefficient`](@ref) sets the coefficient of the normalized constraint. See [Normalization](@ref) for more details. +### Vector constraints + +To modify the coefficients of a vector-valued constraint, use +[`set_normalized_coefficients`](@ref). +```jldoctest +julia> model = Model(); + +julia> @variable(model, x) +x + +julia> @constraint(model, con, [2x + 3x, 4x] in MOI.Nonnegatives(2)) +con : [5 x, 4 x] ∈ MathOptInterface.Nonnegatives(2) + +julia> set_normalized_coefficients(con, x, [(1, 3.0)]) + +julia> con +con : [3 x, 4 x] ∈ MathOptInterface.Nonnegatives(2) + +julia> set_normalized_coefficients(con, x, [(1, 2.0), (2, 5.0)]) + +julia> con +con : [2 x, 5 x] ∈ MathOptInterface.Nonnegatives(2) +``` + ## Delete a constraint Use [`delete`](@ref) to delete a constraint from a model. Use [`is_valid`](@ref) diff --git a/src/constraints.jl b/src/constraints.jl index 9b1c15e4521..0b33183e0c0 100644 --- a/src/constraints.jl +++ b/src/constraints.jl @@ -690,7 +690,8 @@ end ) Set the coefficients of `variable` in the constraint `con_ref` to -`new_coefficients`. +`new_coefficients`, where each element in `new_coefficients` is a tuple which +maps the row to a new coefficient. Note that prior to this step, during constraint creation, JuMP will aggregate multiple terms containing the same variable. diff --git a/test/constraint.jl b/test/constraint.jl index d16b2ac02e3..e30c4013e69 100644 --- a/test/constraint.jl +++ b/test/constraint.jl @@ -835,8 +835,6 @@ function test_Model_change_coefficient(::Any, ::Any) end function test_Model_change_coefficients(::Any, ::Any) - model = JuMP.Model() - x = @variable(model) model = Model() @variable(model, x) @constraint(model, con, [2x + 3x, 4x] in MOI.Nonnegatives(2))