Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add set_normalized_coefficients #2968

Merged
merged 7 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/src/manual/constraints.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions docs/src/reference/constraints.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ constraint_by_name
```@docs
normalized_coefficient
set_normalized_coefficient
set_normalized_coefficients

normalized_rhs
set_normalized_rhs
Expand Down
41 changes: 41 additions & 0 deletions src/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,47 @@ 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 `con_ref` to
`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.

```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, 5 x] ∈ 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)

Expand Down
12 changes: 12 additions & 0 deletions test/constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,18 @@ function test_Model_change_coefficient(::Any, ::Any)
)
end

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, [(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

function test_Model_change_rhs(::Any, ::Any)
model = JuMP.Model()
x = @variable(model)
Expand Down