diff --git a/src/FileFormats/CBF/read.jl b/src/FileFormats/CBF/read.jl index afdca96748..c20026f03e 100644 --- a/src/FileFormats/CBF/read.jl +++ b/src/FileFormats/CBF/read.jl @@ -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) @@ -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 @@ -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