diff --git a/src/Utilities/copy.jl b/src/Utilities/copy.jl index 76a34dc75f..8beb2cfada 100644 --- a/src/Utilities/copy.jl +++ b/src/Utilities/copy.jl @@ -383,6 +383,21 @@ function default_copy_to(dest::MOI.ModelLike, src::MOI.ModelLike) error("Model $(typeof(dest)) does not support copy_to.") end MOI.empty!(dest) + for (F, S) in MOI.get(src, MOI.ListOfConstraintTypesPresent()) + if F == MOI.VariableIndex + if !MOI.supports_add_constrained_variable(dest, S) + throw(MOI.AddConstraintNotAllowed{F,S}()) + end + elseif F == MOI.VectorOfVariables + if !MOI.supports_add_constrained_variables(dest, S) + throw(MOI.AddConstraintNotAllowed{F,S}()) + end + else + if !MOI.supports_add_constrainet(dest, F, S) + throw(MOI.AddConstraintNotAllowed{F,S}()) + end + end + end index_map, vis_src, constraints_not_added = _copy_variables_with_set(dest, src) # Copy variable attributes @@ -498,8 +513,8 @@ function _add_variable_with_domain( src, index_map, f, - ci::MOI.ConstraintIndex{MOI.VariableIndex,<:MOI.AbstractScalarSet}, -) + ci::MOI.ConstraintIndex{MOI.VariableIndex,S}, +) where {S<:MOI.AbstractScalarSet} set = MOI.get(src, MOI.ConstraintSet(), ci) dest_x, dest_ci = MOI.add_constrained_variable(dest, set) index_map[only(f)] = dest_x @@ -512,8 +527,8 @@ function _add_variable_with_domain( src, index_map, f, - ci::MOI.ConstraintIndex{MOI.VectorOfVariables,<:MOI.AbstractVectorSet}, -) + ci::MOI.ConstraintIndex{MOI.VectorOfVariables,S}, +) where {S<:MOI.AbstractVectorSet} set = MOI.get(src, MOI.ConstraintSet(), ci) dest_x, dest_ci = MOI.add_constrained_variables(dest, set) for (fi, xi) in zip(f, dest_x) diff --git a/test/Utilities/copy.jl b/test/Utilities/copy.jl index ce39a7e83d..b51624c51f 100644 --- a/test/Utilities/copy.jl +++ b/test/Utilities/copy.jl @@ -1003,6 +1003,35 @@ function test_copy_to_sorted_sets() return end +function test_add_constraint_not_allowed_scalar_variable() + F, S = MOI.VariableIndex, MOI.GreaterThan{Float64} + model = MOI.Utilities.Model{Float64}() + x, _ = MOI.add_constrained_variable(model, MOI.GreaterThan(1.0)) + dest = MOI.FileFormats.CBF.Model() + @test_throws(MOI.AddConstraintNotAllowed{F,S}, MOI.copy_to(dest, model)) + return +end + +function test_add_constraint_not_allowed_vector_variables() + F, S = MOI.VectorOfVariables, MOI.AllDifferent + model = MOI.Utilities.Model{Float64}() + x, _ = MOI.add_constrained_variables(model, MOI.AllDifferent(3)) + dest = MOI.FileFormats.CBF.Model() + @test_throws(MOI.AddConstraintNotAllowed{F,S}, MOI.copy_to(dest, model)) + return +end + +function test_add_constraint_not_allowed_scalar_function() + F, S = MOI.ScalarNonlinearFunction, MOI.EqualTo{Float64} + model = MOI.Utilities.Model{Float64}() + x = MOI.add_variable(model) + f = MOI.ScalarNonlinearFunction(:log, Any[x]) + MOI.add_constraint(model, f, MOI.EqualTo(0.0)) + dest = MOI.FileFormats.CBF.Model() + @test_throws(MOI.AddConstraintNotAllowed{F,S}, MOI.copy_to(dest, model)) + return +end + end # module TestCopy.runtests()