Repository navigation
[FileFormats.MPS] merge repeated terms when writing - #3160
Merged
Merged
Conversation
The writer wrote one COLUMNS entry per term, so an objective with a repeated variable produced repeated (column, row) entries. Readers disagree on these: MOI sums them, but HiGHS keeps the first one.
blegat
reviewed
Dec 2, 2026
| flip_sign::Bool = false, | ||
| ) where {T} | ||
| # Readers disagree on repeated (column, row) entries, so merge them here. | ||
| if !MOI.Utilities.is_canonical(func) |
Member
There was a problem hiding this comment.
We already check is_canonical now ?
MathOptInterface.jl/src/Utilities/functions.jl
Lines 1126 to 1131 in 122edff
Contributor
Author
There was a problem hiding this comment.
canonical still copies an already-canonical ScalarAffineFunction (through copy_if_modifiable), so the check skips that copy in the usual case, where add_constraint has already merged the terms. On a canonical function with 1,000 terms, canonical(f) allocates 16,480 bytes and the checked form allocates nothing. I can drop the check if you prefer the shorter form.
Member
There was a problem hiding this comment.
Good point, we should avoid the copy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The MPS writer writes one
COLUMNSentry per affine term, so an objective with a repeated variable gets repeated(column, row)entries. On master, theCOLUMNSsection written byis
MOI reads this file back as
3x(#2787), but HiGHS 1.15.1 and 1.16.0 read the objective as1x. I changed the writer to merge repeated affine terms, as_write_q_matrixalready does for quadratic terms, so the example writesx OBJ 3.Constraints added with
add_constraintare already merged, but the objective, and a function set withMOI.ConstraintFunction, are stored as given, so the test covers both. Sincecanonicalalso drops zero coefficients, an objective of0 xorx - xno longer writes anOBJentry forx(master writesx OBJ 0, orx OBJ 1andx OBJ -1). Ifxhas no other entry, the writer still writesx OBJ 0, as it does for any empty column.I check
is_canonicalfirst, so functions that are already canonical skipcanonicaland are written as before. Writing a model with 200,000 rows and up to 5 terms per row takes about 0.95 s both on master and with this PR (median of 5 runs), and the output is byte-identical.