Skip to content

Commit

Permalink
[Utilities] fix checking supports_constraint in default_copy_to
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Oct 30, 2024
1 parent 0843e5c commit 38f940e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
23 changes: 19 additions & 4 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)
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 Expand Up @@ -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
Expand All @@ -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)
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()

0 comments on commit 38f940e

Please sign in to comment.