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

[FileFormats.CBF] read functions as VectorOfVariables if possible #2477

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
26 changes: 23 additions & 3 deletions src/FileFormats/CBF/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,27 @@ function _read_DCOORD(io::IO, data::_CBFReadData)
return
end

"""
_simplify_f_if_possible(f::MOI.VectorAffineFunction)

This function returns a `MOI.VectorOfVariables` if `f` is trivially equivalent
to `I * x + 0`. This may often happen when reading constraints that could have
been written as `x in K`, but were instead written as `I * x + 0 in K`.
"""
function _simplify_f_if_possible(f::MOI.VectorAffineFunction)
if length(f.terms) != length(f.constants)
return f
end
for (i, term) in enumerate(f.terms)
if term.output_index != i ||
!isone(term.scalar_term.coefficient) ||
!iszero(f.constants[i])
return f
end
end
return MOI.VectorOfVariables([t.scalar_term.variable for t in f.terms])
end

"""
Base.read!(io::IO, model::FileFormats.CBF.Model)

Expand Down Expand Up @@ -382,7 +403,7 @@ function Base.read!(io::IO, model::Model)
)
end
con_set = _cbf_to_moi_cone(data, cone_str, cone_dim)
MOI.add_constraint(model, con_func, con_set)
MOI.add_constraint(model, _simplify_f_if_possible(con_func), con_set)
row_idx += cone_dim
end

Expand All @@ -400,10 +421,9 @@ function Base.read!(io::IO, model::Model)
)
MOI.add_constraint(
model,
con_func,
_simplify_f_if_possible(con_func),
MOI.PositiveSemidefiniteConeTriangle(side_dim),
)
end

return
end
Loading