From 6f119d8304b828824c43e29b5eb45308632d43f3 Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 21 Sep 2023 11:13:35 +1200 Subject: [PATCH 1/2] Fix MethodError when trying to modify a variable objective --- src/Utilities/objective_container.jl | 18 ++++++++++++++---- test/Utilities/objective_container.jl | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/Utilities/objective_container.jl b/src/Utilities/objective_container.jl index 59f046d8a0..31216b2077 100644 --- a/src/Utilities/objective_container.jl +++ b/src/Utilities/objective_container.jl @@ -274,8 +274,13 @@ function MOI.modify( change::MOI.AbstractFunctionModification, ) where {T} if o.single_variable !== nothing - o.single_variable = - modify_function!(something(o.single_variable), change) + throw( + MOI.ModifyObjectiveNotAllowed( + change, + "Cannot modify objective when there is a " * + "`VariableIndex` objective", + ), + ) elseif o.scalar_affine !== nothing o.scalar_affine = modify_function!(something(o.scalar_affine), change) elseif o.scalar_quadratic !== nothing @@ -290,8 +295,13 @@ function MOI.modify( ), ) elseif o.vector_variables !== nothing - o.vector_variables = - modify_function!(something(o.vector_variables), change) + throw( + MOI.ModifyObjectiveNotAllowed( + change, + "Cannot modify objective when there is a " * + "`VariableIndex` objective", + ), + ) elseif o.vector_quadratic !== nothing o.vector_quadratic = modify_function!(something(o.vector_quadratic), change) diff --git a/test/Utilities/objective_container.jl b/test/Utilities/objective_container.jl index 865806feba..02c177ed1e 100644 --- a/test/Utilities/objective_container.jl +++ b/test/Utilities/objective_container.jl @@ -164,6 +164,33 @@ function test_delete_variables_ScalarNonlinearFunction() return end +function test_modify_VariableIndex() + model = MOI.Utilities.Model{Float64}() + x = MOI.add_variable(model) + MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE) + attr = MOI.ObjectiveFunction{typeof(x)}() + MOI.set(model, attr, x) + @test_throws( + MOI.ModifyObjectiveNotAllowed, + MOI.modify(model, attr, MOI.ScalarConstantChange(3.0)), + ) + return +end + +function test_modify_VectorOfVariables() + model = MOI.Utilities.Model{Float64}() + x = MOI.add_variables(model, 2) + MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE) + f = MOI.VectorOfVariables(x) + attr = MOI.ObjectiveFunction{typeof(f)}() + MOI.set(model, attr, f) + @test_throws( + MOI.ModifyObjectiveNotAllowed, + MOI.modify(model, attr, MOI.VectorConstantChange([3.0, 4.0])), + ) + return +end + end # module TestObjectiveContainer.runtests() From 8ef7623caabd4bda704a790b0231e7a7e65433f8 Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 21 Sep 2023 15:18:26 +1200 Subject: [PATCH 2/2] Update --- src/Utilities/objective_container.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Utilities/objective_container.jl b/src/Utilities/objective_container.jl index 31216b2077..5ebce67930 100644 --- a/src/Utilities/objective_container.jl +++ b/src/Utilities/objective_container.jl @@ -278,7 +278,7 @@ function MOI.modify( MOI.ModifyObjectiveNotAllowed( change, "Cannot modify objective when there is a " * - "`VariableIndex` objective", + "`VariableIndex` objective.", ), ) elseif o.scalar_affine !== nothing @@ -291,7 +291,7 @@ function MOI.modify( MOI.ModifyObjectiveNotAllowed( change, "Cannot modify objective when there is a " * - "`ScalarNonlinearFunction` objective", + "`ScalarNonlinearFunction` objective.", ), ) elseif o.vector_variables !== nothing @@ -299,7 +299,7 @@ function MOI.modify( MOI.ModifyObjectiveNotAllowed( change, "Cannot modify objective when there is a " * - "`VariableIndex` objective", + "`VectorOfVariables` objective.", ), ) elseif o.vector_quadratic !== nothing @@ -312,7 +312,7 @@ function MOI.modify( MOI.ModifyObjectiveNotAllowed( change, "Cannot modify objective when there is a " * - "`VectorNonlinearFunction` objective", + "`VectorNonlinearFunction` objective.", ), ) else