Skip to content

Commit 52bf172

Browse files
authored
Fix MethodError when trying to modify a variable objective (#2278)
1 parent f8dcabe commit 52bf172

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

src/Utilities/objective_container.jl

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,13 @@ function MOI.modify(
274274
change::MOI.AbstractFunctionModification,
275275
) where {T}
276276
if o.single_variable !== nothing
277-
o.single_variable =
278-
modify_function!(something(o.single_variable), change)
277+
throw(
278+
MOI.ModifyObjectiveNotAllowed(
279+
change,
280+
"Cannot modify objective when there is a " *
281+
"`VariableIndex` objective.",
282+
),
283+
)
279284
elseif o.scalar_affine !== nothing
280285
o.scalar_affine = modify_function!(something(o.scalar_affine), change)
281286
elseif o.scalar_quadratic !== nothing
@@ -286,12 +291,17 @@ function MOI.modify(
286291
MOI.ModifyObjectiveNotAllowed(
287292
change,
288293
"Cannot modify objective when there is a " *
289-
"`ScalarNonlinearFunction` objective",
294+
"`ScalarNonlinearFunction` objective.",
290295
),
291296
)
292297
elseif o.vector_variables !== nothing
293-
o.vector_variables =
294-
modify_function!(something(o.vector_variables), change)
298+
throw(
299+
MOI.ModifyObjectiveNotAllowed(
300+
change,
301+
"Cannot modify objective when there is a " *
302+
"`VectorOfVariables` objective.",
303+
),
304+
)
295305
elseif o.vector_quadratic !== nothing
296306
o.vector_quadratic =
297307
modify_function!(something(o.vector_quadratic), change)
@@ -302,7 +312,7 @@ function MOI.modify(
302312
MOI.ModifyObjectiveNotAllowed(
303313
change,
304314
"Cannot modify objective when there is a " *
305-
"`VectorNonlinearFunction` objective",
315+
"`VectorNonlinearFunction` objective.",
306316
),
307317
)
308318
else

test/Utilities/objective_container.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,33 @@ function test_delete_variables_ScalarNonlinearFunction()
164164
return
165165
end
166166

167+
function test_modify_VariableIndex()
168+
model = MOI.Utilities.Model{Float64}()
169+
x = MOI.add_variable(model)
170+
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
171+
attr = MOI.ObjectiveFunction{typeof(x)}()
172+
MOI.set(model, attr, x)
173+
@test_throws(
174+
MOI.ModifyObjectiveNotAllowed,
175+
MOI.modify(model, attr, MOI.ScalarConstantChange(3.0)),
176+
)
177+
return
178+
end
179+
180+
function test_modify_VectorOfVariables()
181+
model = MOI.Utilities.Model{Float64}()
182+
x = MOI.add_variables(model, 2)
183+
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
184+
f = MOI.VectorOfVariables(x)
185+
attr = MOI.ObjectiveFunction{typeof(f)}()
186+
MOI.set(model, attr, f)
187+
@test_throws(
188+
MOI.ModifyObjectiveNotAllowed,
189+
MOI.modify(model, attr, MOI.VectorConstantChange([3.0, 4.0])),
190+
)
191+
return
192+
end
193+
167194
end # module
168195

169196
TestObjectiveContainer.runtests()

0 commit comments

Comments
 (0)