From 93b20c78a5e7e494f12e57730e553a6af4f14837 Mon Sep 17 00:00:00 2001 From: Joaquim Garcia Date: Fri, 22 Mar 2024 12:40:52 -0300 Subject: [PATCH] add rhs batch modifications --- src/constraints.jl | 23 +++++++++++++++++++++-- test/test_constraint.jl | 23 +++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/constraints.jl b/src/constraints.jl index b5176857c5e..c37b986cde6 100644 --- a/src/constraints.jl +++ b/src/constraints.jl @@ -731,7 +731,7 @@ function add_constraint( end """ - set_normalized_rhs(constraint::ConstraintRef, value) + set_normalized_rhs(constraint::ConstraintRef, value::Number) Set the right-hand side term of `constraint` to `value`. @@ -758,7 +758,7 @@ con : 2 x ≤ 4 """ function set_normalized_rhs( con_ref::ConstraintRef{<:AbstractModel,MOI.ConstraintIndex{F,S}}, - value, + value::Number, ) where { T, S<:Union{MOI.LessThan{T},MOI.GreaterThan{T},MOI.EqualTo{T}}, @@ -773,6 +773,25 @@ function set_normalized_rhs( return end +function set_normalized_rhs( + constraints::AbstractVector{ + <:ConstraintRef{<:AbstractModel,MOI.ConstraintIndex{F,S}}, + }, + values::AbstractVector{<:Number}, +) where { + T, + S<:Union{MOI.LessThan{T},MOI.GreaterThan{T},MOI.EqualTo{T}}, + F<:Union{MOI.ScalarAffineFunction{T},MOI.ScalarQuadraticFunction{T}}, +} + MOI.set( + owner_model(first(constraints)), + MOI.ConstraintSet(), + constraints, + S.(convert.(T, values)), + ) + return +end + """ normalized_rhs(constraint::ConstraintRef) diff --git a/test/test_constraint.jl b/test/test_constraint.jl index c082d120368..23b23055f3b 100644 --- a/test/test_constraint.jl +++ b/test/test_constraint.jl @@ -1030,6 +1030,29 @@ function test_change_rhs() return end +function test_change_rhs_batch() + model = Model() + x = @variable(model) + con_ref1 = @constraint(model, 2 * x <= 1) + con_ref2 = @constraint(model, 3 * x <= 2) + @test normalized_rhs(con_ref1) == 1.0 + @test normalized_rhs(con_ref2) == 2.0 + set_normalized_rhs([con_ref1, con_ref2], [3.0, 4.0]) + @test normalized_rhs(con_ref1) == 3.0 + @test normalized_rhs(con_ref2) == 4.0 + con_ref1 = @constraint(model, 2 * x - 1 == 1) + con_ref2 = @constraint(model, 2 * x - 1 == 2) + @test normalized_rhs(con_ref1) == 2.0 + @test normalized_rhs(con_ref2) == 3.0 + set_normalized_rhs([con_ref1, con_ref2], [3.0, 4.0]) + @test normalized_rhs(con_ref1) == 3.0 + @test normalized_rhs(con_ref2) == 4.0 + con_ref1 = @constraint(model, 0 <= 2 * x) + con_ref2 = @constraint(model, 2 * x <= 1) + @test_throws MethodError set_normalized_rhs([con_ref1, con_ref2], [3, 3]) + return +end + function test_add_to_function_constant_scalar() model = Model() x = @variable(model)