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

[Utilities] fix checking supports_constraint in default_copy_to #2571

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
15 changes: 15 additions & 0 deletions src/Utilities/copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
odow marked this conversation as resolved.
Show resolved Hide resolved
throw(MOI.AddConstraintNotAllowed{F,S}())
end
end
end
index_map, vis_src, constraints_not_added =
_copy_variables_with_set(dest, src)
# Copy variable attributes
Expand Down
29 changes: 29 additions & 0 deletions test/Utilities/copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Loading