Skip to content

Commit 33aae24

Browse files
authored
[Utilities] add modify_constants for MOI.modify in MatrixOfConstraints (#2300)
1 parent 491ef42 commit 33aae24

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

docs/src/submodules/Utilities/reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Utilities.OneBasedIndexing
118118
Utilities.load_constants
119119
Utilities.function_constants
120120
Utilities.set_from_constants
121+
Utilities.modify_constants
121122
```
122123

123124
```@docs

src/Utilities/matrix_of_constraints.jl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,22 @@ This function should be implemented to be usable as storage of constants for
219219
"""
220220
function set_from_constants end
221221

222+
"""
223+
modify_constants(constants, row::Integer, new_constant::T) where {T}
224+
modify_constants(
225+
constants,
226+
rows::AbstractVector{<:Integer},
227+
new_constants::AbstractVector{T},
228+
) where {T}
229+
230+
Modify `constants` in-place to store `new_constant` in the `row` row, or rows
231+
`rows`.
232+
233+
This function must be implemented to enable [`MOI.ScalarConstantChange`](@ref)
234+
and [`MOI.VectorConstantChange`](@ref) for [`MatrixOfConstraints`](@ref).
235+
"""
236+
function modify_constants end
237+
222238
###
223239
### Interface for the .sets field
224240
###
@@ -652,3 +668,36 @@ function MOI.get(
652668
MOI.throw_if_not_valid(model, ci)
653669
return set_from_constants(model.constants, S, rows(model, ci))
654670
end
671+
672+
function MOI.modify(
673+
model::MatrixOfConstraints,
674+
ci::MOI.ConstraintIndex,
675+
change::Union{MOI.ScalarConstantChange,MOI.VectorConstantChange},
676+
)
677+
try
678+
modify_constants(model.constants, rows(model, ci), change.new_constant)
679+
catch
680+
throw(MOI.ModifyConstraintNotAllowed(ci, change))
681+
end
682+
return
683+
end
684+
685+
function modify_constants(
686+
b::AbstractVector{T},
687+
row::Integer,
688+
new_constant::T,
689+
) where {T}
690+
b[row] = new_constant
691+
return
692+
end
693+
694+
function modify_constants(
695+
b::AbstractVector{T},
696+
rows::AbstractVector{<:Integer},
697+
new_constants::AbstractVector{T},
698+
) where {T}
699+
for (row, new_constant) in zip(rows, new_constants)
700+
modify_constants(b, row, new_constant)
701+
end
702+
return
703+
end

test/Utilities/matrix_of_constraints.jl

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,60 @@ function test_set_types_fallback()
635635
return
636636
end
637637

638+
function test_modify_vectorsets()
639+
model = _new_VectorSets()
640+
src = MOI.Utilities.Model{Int}()
641+
x = MOI.add_variables(src, 2)
642+
c = MOI.add_constraint(
643+
src,
644+
MOI.VectorAffineFunction{Int}(
645+
MOI.VectorAffineTerm.(1, MOI.ScalarAffineTerm.(1, x)),
646+
[1, 3],
647+
),
648+
MOI.SecondOrderCone(2),
649+
)
650+
index_map = MOI.copy_to(model, src)
651+
f_c = MOI.get(model, MOI.ConstraintFunction(), index_map[c])
652+
@test f_c.constants == [1, 3]
653+
MOI.modify(model, index_map[c], MOI.VectorConstantChange([4, 5]))
654+
f_c = MOI.get(model, MOI.ConstraintFunction(), index_map[c])
655+
@test f_c.constants == [4, 5]
656+
return
657+
end
658+
659+
function test_modify_set_constants()
660+
model = MOI.Utilities.Model{Float64}()
661+
x = MOI.add_variables(model, 3)
662+
f = MOI.Utilities.operate(vcat, Float64, 1.0 .* x...)
663+
p_cone = MOI.PowerCone(0.1)
664+
p_ref = MOI.add_constraint(model, f, p_cone)
665+
d_cone = MOI.DualPowerCone(0.2)
666+
d_ref = MOI.add_constraint(model, f, d_cone)
667+
cache = MOI.Utilities.GenericOptimizer{
668+
Float64,
669+
MOI.Utilities.ObjectiveContainer{Float64},
670+
MOI.Utilities.VariablesContainer{Float64},
671+
MOI.Utilities.MatrixOfConstraints{
672+
Float64,
673+
MOI.Utilities.MutableSparseMatrixCSC{
674+
Float64,
675+
Int,
676+
MOI.Utilities.OneBasedIndexing,
677+
},
678+
_SetConstants{Float64},
679+
PowerSets{Float64},
680+
},
681+
}()
682+
index_map = MOI.copy_to(cache, model)
683+
ci = index_map[p_ref]
684+
change = MOI.VectorConstantChange([4.0, 5.0, 6.0])
685+
@test_throws(
686+
MOI.ModifyConstraintNotAllowed(ci, change),
687+
MOI.modify(cache, ci, change),
688+
)
689+
return
690+
end
691+
638692
end
639693

640694
TestMatrixOfConstraints.runtests()

0 commit comments

Comments
 (0)