Skip to content

Commit 95e16c4

Browse files
authored
[Utilities] fix adding unsupported constraints to AbstractModel (#2572)
1 parent 0843e5c commit 95e16c4

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

src/Utilities/model.jl

+21-7
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,16 @@ end
312312

313313
function MOI.add_constraint(
314314
model::AbstractModel,
315-
func::MOI.AbstractFunction,
316-
set::MOI.AbstractSet,
317-
)
315+
func::F,
316+
set::S,
317+
) where {F<:MOI.AbstractFunction,S<:MOI.AbstractSet}
318+
# We check supports_constraint here because it is a common practice for
319+
# AbstractModels to declare that they do not support particular constraints,
320+
# even though the underlying `.constraints` object does. See, for example,
321+
# the various models in MOI.FileFormats.
322+
if !MOI.supports_constraint(model, F, S)
323+
throw(MOI.UnsupportedConstraint{F,S}())
324+
end
318325
return MOI.add_constraint(model.constraints, func, set)
319326
end
320327

@@ -328,10 +335,17 @@ end
328335

329336
function MOI.add_constraint(
330337
model::AbstractModel,
331-
f::MOI.VariableIndex,
332-
s::MOI.AbstractScalarSet,
333-
)
334-
return MOI.add_constraint(model.variables, f, s)
338+
func::MOI.VariableIndex,
339+
set::S,
340+
) where {S<:MOI.AbstractScalarSet}
341+
# We check supports_constraint here because it is a common practice for
342+
# AbstractModels to declare that they do not support particular constraints,
343+
# even though the underlying `.constraints` object does. See, for example,
344+
# the various models in MOI.FileFormats.
345+
if !MOI.supports_constraint(model, MOI.VariableIndex, S)
346+
throw(MOI.UnsupportedConstraint{MOI.VariableIndex,S}())
347+
end
348+
return MOI.add_constraint(model.variables, func, set)
335349
end
336350

337351
# MOI.NumberOfConstraints

test/Utilities/model.jl

+29
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,35 @@ function test_vector_of_constraints_list_of_constraint_attributes_set()
515515
return
516516
end
517517

518+
function test_copy_to_unsupported_scalar_variable()
519+
F, S = MOI.VariableIndex, MOI.GreaterThan{Float64}
520+
model = MOI.Utilities.Model{Float64}()
521+
x, _ = MOI.add_constrained_variable(model, MOI.GreaterThan(1.0))
522+
dest = MOI.FileFormats.CBF.Model()
523+
@test_throws(MOI.UnsupportedConstraint{F,S}, MOI.copy_to(dest, model))
524+
return
525+
end
526+
527+
function test_copy_to_unsupported_vector_variables()
528+
F, S = MOI.VectorOfVariables, MOI.AllDifferent
529+
model = MOI.Utilities.Model{Float64}()
530+
x, _ = MOI.add_constrained_variables(model, MOI.AllDifferent(3))
531+
dest = MOI.FileFormats.CBF.Model()
532+
@test_throws(MOI.UnsupportedConstraint{F,S}, MOI.copy_to(dest, model))
533+
return
534+
end
535+
536+
function test_copy_to_unsupported_scalar_function()
537+
F, S = MOI.ScalarNonlinearFunction, MOI.EqualTo{Float64}
538+
model = MOI.Utilities.Model{Float64}()
539+
x = MOI.add_variable(model)
540+
f = MOI.ScalarNonlinearFunction(:log, Any[x])
541+
MOI.add_constraint(model, f, MOI.EqualTo(0.0))
542+
dest = MOI.FileFormats.CBF.Model()
543+
@test_throws(MOI.UnsupportedConstraint{F,S}, MOI.copy_to(dest, model))
544+
return
545+
end
546+
518547
end # module
519548

520549
TestModel.runtests()

0 commit comments

Comments
 (0)