Skip to content

Commit

Permalink
add rhs batch modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Joaquim Garcia committed Mar 22, 2024
1 parent b23a8de commit 93b20c7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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}},
Expand All @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions test/test_constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 93b20c7

Please sign in to comment.