Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MethodError when trying to modify a variable objective #2278

Merged
merged 2 commits into from
Sep 21, 2023
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 @@
change::MOI.AbstractFunctionModification,
) where {T}
if o.single_variable !== nothing
o.single_variable =
modify_function!(something(o.single_variable), change)
throw(

Check warning on line 277 in src/Utilities/objective_container.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/objective_container.jl#L277

Added line #L277 was not covered by tests
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 @@
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(

Check warning on line 298 in src/Utilities/objective_container.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/objective_container.jl#L298

Added line #L298 was not covered by tests
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 @@
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()
Loading