Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()