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

Fix copy_to with unsupported variable and constraint attributes #2333

Closed
wants to merge 2 commits into from
Closed
Changes from all 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
101 changes: 49 additions & 52 deletions src/Utilities/copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
dest::MOI.ModelLike,
src::MOI.ModelLike,
index_map::IndexMap,
vis_src::Vector{MOI.VariableIndex},
indices::Vector{MOI.VariableIndex},
)

Pass the variable attributes from the model `src` to the model `dest`.
Expand All @@ -70,36 +70,10 @@
dest::MOI.ModelLike,
src::MOI.ModelLike,
index_map::IndexMap,
vis_src::Vector{MOI.VariableIndex},
indices::Vector{MOI.VariableIndex},
)
for attr in MOI.get(src, MOI.ListOfVariableAttributesSet())
if !MOI.supports(dest, attr, MOI.VariableIndex)
if attr == MOI.VariableName() || attr == MOI.VariablePrimalStart()
continue # Skipping names and start values is okay.
end
end
_pass_attribute(dest, src, index_map, vis_src, attr)
end
return
end

function _pass_attribute(
dest::MOI.ModelLike,
src::MOI.ModelLike,
index_map::IndexMap,
vis_src::Vector{MOI.VariableIndex},
attr::MOI.AbstractVariableAttribute,
)
for x in vis_src
value = MOI.get(src, attr, x)
if value !== nothing
MOI.set(
dest,
attr,
index_map[x],
map_indices(index_map, attr, value),
)
end
_pass_attribute(dest, src, index_map, indices, attr)
end
return
end
Expand All @@ -109,7 +83,7 @@
dest::MOI.ModelLike,
src::MOI.ModelLike,
index_map::IndexMap,
cis_src::Vector{MOI.ConstraintIndex{F,S}},
indices::Vector{MOI.ConstraintIndex{F,S}},
) where {F,S}

Pass the constraint attributes of `F`-in-`S` constraints from the model `src` to
Expand All @@ -119,39 +93,62 @@
dest::MOI.ModelLike,
src::MOI.ModelLike,
index_map::IndexMap,
cis_src::Vector{MOI.ConstraintIndex{F,S}},
indices::Vector{MOI.ConstraintIndex{F,S}},
) where {F,S}
for attr in MOI.get(src, MOI.ListOfConstraintAttributesSet{F,S}())
if !MOI.supports(dest, attr, MOI.ConstraintIndex{F,S})
if (
attr == MOI.ConstraintName() ||
attr == MOI.ConstraintPrimalStart() ||
attr == MOI.ConstraintDualStart()
)
continue # Skipping names and start values is okay.
end
end
_pass_attribute(dest, src, index_map, cis_src, attr)
_pass_attribute(dest, src, index_map, indices, attr)
end
return
end

_is_skippable(::MOI.ConstraintName) = true
_is_skippable(::MOI.ConstraintPrimalStart) = true
_is_skippable(::MOI.ConstraintDualStart) = true
_is_skippable(::MOI.VariableName) = true
_is_skippable(::MOI.VariablePrimalStart) = true
_is_skippable(::Any) = false

function _pass_attribute(
dest::MOI.ModelLike,
src::MOI.ModelLike,
index_map::IndexMap,
cis_src::Vector{MOI.ConstraintIndex{F,S}},
attr::MOI.AbstractConstraintAttribute,
) where {F,S}
for ci in cis_src
value = MOI.get(src, attr, ci)
indices::Vector{<:MOI.Index},
attr::Union{MOI.AbstractVariableAttribute,MOI.AbstractConstraintAttribute},
)
if _is_skippable(attr)
return _pass_attribute_skippable(dest, src, index_map, indices, attr)
end
for index in indices
value = MOI.get(src, attr, index)
if value !== nothing
MOI.set(
dest,
attr,
index_map[ci],
map_indices(index_map, attr, value),
)
dest_value = map_indices(index_map, attr, value)
MOI.set(dest, attr, index_map[index], dest_value)
end
end
return

Check warning on line 128 in src/Utilities/copy.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/copy.jl#L127-L128

Added lines #L127 - L128 were not covered by tests
end

function _pass_attribute_skippable(dest, src, index_map, indices, attr)
if !MOI.supports(dest, attr, eltype(indices))
return
end
for index in indices
value = MOI.get(src, attr, index)
if value !== nothing
dest_value = map_indices(index_map, attr, value)
try
MOI.set(dest, attr, index_map[index], dest_value)
catch err
if err isa MOI.UnsupportedAttribute

Check warning on line 142 in src/Utilities/copy.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/copy.jl#L142

Added line #L142 was not covered by tests
# This can happen because a bridge does not support the
# attribute
elseif err isa MOI.SetAttributeNotAllowed{typeof(attr)}

Check warning on line 145 in src/Utilities/copy.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/copy.jl#L145

Added line #L145 was not covered by tests
# This can happen if the model doesn't support setting the
# attribute right now
else
rethrow(err)
end
end
end
end
return
Expand Down
Loading