From e3b637311419f2c64ef11708b1354b60808aaf62 Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 11 Apr 2024 13:56:31 +1200 Subject: [PATCH 1/2] [FileFormats.CBF] read functions as VectorOfVariables if possible --- src/FileFormats/CBF/read.jl | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/FileFormats/CBF/read.jl b/src/FileFormats/CBF/read.jl index afdca96748..aff5987e63 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,11 @@ 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 +425,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 From 2e755a8a7d3e91ee305f1babc8b4fc7bfb972754 Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 11 Apr 2024 14:04:56 +1200 Subject: [PATCH 2/2] Fix formatting --- src/FileFormats/CBF/read.jl | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/FileFormats/CBF/read.jl b/src/FileFormats/CBF/read.jl index aff5987e63..c20026f03e 100644 --- a/src/FileFormats/CBF/read.jl +++ b/src/FileFormats/CBF/read.jl @@ -403,11 +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, - _simplify_f_if_possible(con_func), - con_set, - ) + MOI.add_constraint(model, _simplify_f_if_possible(con_func), con_set) row_idx += cone_dim end