Skip to content

[FileFormats.MPS] merge repeated terms when writing - #3160

Merged
blegat merged 1 commit into
jump-dev:masterfrom
chriscoey:cc/mps-write-merge-terms
Dec 2, 2026
Merged

blegat merged 1 commit into
jump-dev:masterfrom
chriscoey:cc/mps-write-merge-terms

Conversation

@chriscoey

Copy link
Copy Markdown
Contributor

The MPS writer writes one COLUMNS entry per affine term, so an objective with a repeated variable gets repeated (column, row) entries. On master, the COLUMNS section written by

import MathOptInterface as MOI
model = MOI.FileFormats.MPS.Model()
MOI.Utilities.loadfromstring!(
    model,
    """
    variables: x
    minobjective: 1.0 * x + 2.0 * x
    c: 1.0 * x >= 1.0
    """,
)
print(sprint(write, model))

is

COLUMNS
    x         c         1
    x         OBJ       1
    x         OBJ       2

MOI reads this file back as 3x (#2787), but HiGHS 1.15.1 and 1.16.0 read the objective as 1x. I changed the writer to merge repeated affine terms, as _write_q_matrix already does for quadratic terms, so the example writes x OBJ 3.

Constraints added with add_constraint are already merged, but the objective, and a function set with MOI.ConstraintFunction, are stored as given, so the test covers both. Since canonical also drops zero coefficients, an objective of 0 x or x - x no longer writes an OBJ entry for x (master writes x OBJ 0, or x OBJ 1 and x OBJ -1). If x has no other entry, the writer still writes x OBJ 0, as it does for any empty column.

I check is_canonical first, so functions that are already canonical skip canonical and 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.

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.
flip_sign::Bool = false,
) where {T}
# Readers disagree on repeated (column, row) entries, so merge them here.
if !MOI.Utilities.is_canonical(func)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already check is_canonical now ?

function canonical(f::MOI.AbstractFunction)
if is_canonical(f)
return copy_if_modifiable(f)
end
return canonicalize!(copy(f))
end

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, we should avoid the copy

@blegat
blegat merged commit 3c19f25 into jump-dev:master Dec 2, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants