@@ -636,6 +636,112 @@ function test_set_types_fallback()
636636 return
637637end
638638
639+ function test_modify_scalar_coefficient_change ()
640+ model = MOI. Utilities. GenericOptimizer{
641+ Int,
642+ MOI. Utilities. ObjectiveContainer{Int},
643+ MOI. Utilities. VariablesContainer{Int},
644+ MOI. Utilities. MatrixOfConstraints{
645+ Int,
646+ MOI. Utilities. MutableSparseMatrixCSC{
647+ Int,
648+ Int,
649+ MOI. Utilities. OneBasedIndexing,
650+ },
651+ MOI. Utilities. Hyperrectangle{Int},
652+ ScalarSets{Int},
653+ },
654+ }()
655+ x = MOI. add_variable (model)
656+ y = MOI. add_variable (model)
657+ func = 2 x + 3 y
658+ set = MOI. EqualTo (1 )
659+ c = MOI. add_constraint (model, func, set)
660+ MOI. Utilities. final_touch (model, nothing )
661+ f = MOI. get (model, MOI. ConstraintFunction (), c)
662+ @test f ≈ 2 x + 3 y
663+ MOI. modify (model, c, MOI. ScalarCoefficientChange (x, 5 ))
664+ f = MOI. get (model, MOI. ConstraintFunction (), c)
665+ @test f ≈ 5 x + 3 y
666+ MOI. modify (model, c, MOI. ScalarCoefficientChange (y, 0 ))
667+ f = MOI. get (model, MOI. ConstraintFunction (), c)
668+ @test f ≈ 5 x + 0 y
669+ return
670+ end
671+
672+ function test_modify_scalar_coefficient_change_zero_based ()
673+ model = MOI. Utilities. GenericOptimizer{
674+ Float64,
675+ MOI. Utilities. ObjectiveContainer{Float64},
676+ MOI. Utilities. VariablesContainer{Float64},
677+ MOI. Utilities. MatrixOfConstraints{
678+ Float64,
679+ MOI. Utilities. MutableSparseMatrixCSC{
680+ Float64,
681+ Int,
682+ MOI. Utilities. ZeroBasedIndexing,
683+ },
684+ MOI. Utilities. Hyperrectangle{Float64},
685+ ScalarSets{Float64},
686+ },
687+ }()
688+ src = MOI. Utilities. Model {Float64} ()
689+ MOI. Utilities. loadfromstring! (
690+ src,
691+ """
692+ variables: x, y
693+ minobjective: x + y
694+ c: x + 2.0 * y <= 3.0
695+ """ ,
696+ )
697+ index_map = MOI. copy_to (model, src)
698+ c = MOI. get (model, MOI. ConstraintIndex, " c" )
699+ x = index_map[MOI. get (src, MOI. VariableIndex, " x" )]
700+ y = index_map[MOI. get (src, MOI. VariableIndex, " y" )]
701+ f = MOI. get (model, MOI. ConstraintFunction (), c)
702+ @test f ≈ 1.0 x + 2.0 y
703+ MOI. modify (model, c, MOI. ScalarCoefficientChange (x, 4.0 ))
704+ f = MOI. get (model, MOI. ConstraintFunction (), c)
705+ @test f ≈ 4.0 x + 2.0 y
706+ return
707+ end
708+
709+ function test_modify_scalar_coefficient_change_no_entry ()
710+ model = MOI. Utilities. GenericOptimizer{
711+ Int,
712+ MOI. Utilities. ObjectiveContainer{Int},
713+ MOI. Utilities. VariablesContainer{Int},
714+ MOI. Utilities. MatrixOfConstraints{
715+ Int,
716+ MOI. Utilities. MutableSparseMatrixCSC{
717+ Int,
718+ Int,
719+ MOI. Utilities. OneBasedIndexing,
720+ },
721+ MOI. Utilities. Hyperrectangle{Int},
722+ ScalarSets{Int},
723+ },
724+ }()
725+ x = MOI. add_variable (model)
726+ y = MOI. add_variable (model)
727+ func = 2 x
728+ set = MOI. EqualTo (1 )
729+ c = MOI. add_constraint (model, func, set)
730+ MOI. Utilities. final_touch (model, nothing )
731+ MOI. modify (model, c, MOI. ScalarCoefficientChange (y, 0 ))
732+ change = MOI. ScalarCoefficientChange (y, 3 )
733+ @test_throws (
734+ MOI. ModifyConstraintNotAllowed (
735+ c,
736+ change,
737+ " cannot set a new non-zero coefficient because no entry " *
738+ " exists in the sparse matrix of `MatrixOfConstraints`" ,
739+ ),
740+ MOI. modify (model, c, change),
741+ )
742+ return
743+ end
744+
639745function test_modify_vectorsets ()
640746 model = _new_VectorSets ()
641747 src = MOI. Utilities. Model {Int} ()
0 commit comments