From 52bf172b27d6e2e08dc3dda966315226e8d4cac3 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 21 Sep 2023 19:37:18 +1200 Subject: [PATCH] Fix MethodError when trying to modify a variable objective (#2278) --- src/Utilities/objective_container.jl | 22 ++++++++++++++++------ test/Utilities/objective_container.jl | 27 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/Utilities/objective_container.jl b/src/Utilities/objective_container.jl index 59f046d8a0..5ebce67930 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 @@ -286,12 +291,17 @@ function MOI.modify( MOI.ModifyObjectiveNotAllowed( change, "Cannot modify objective when there is a " * - "`ScalarNonlinearFunction` objective", + "`ScalarNonlinearFunction` objective.", ), ) 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 " * + "`VectorOfVariables` objective.", + ), + ) elseif o.vector_quadratic !== nothing o.vector_quadratic = modify_function!(something(o.vector_quadratic), change) @@ -302,7 +312,7 @@ function MOI.modify( MOI.ModifyObjectiveNotAllowed( change, "Cannot modify objective when there is a " * - "`VectorNonlinearFunction` objective", + "`VectorNonlinearFunction` objective.", ), ) else 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()