Skip to content

Commit

Permalink
Fix MethodError when trying to modify a variable objective (#2278)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Sep 21, 2023
1 parent f8dcabe commit 52bf172
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/Utilities/objective_container.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -302,7 +312,7 @@ function MOI.modify(
MOI.ModifyObjectiveNotAllowed(
change,
"Cannot modify objective when there is a " *
"`VectorNonlinearFunction` objective",
"`VectorNonlinearFunction` objective.",
),
)
else
Expand Down
27 changes: 27 additions & 0 deletions test/Utilities/objective_container.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 52bf172

Please sign in to comment.