Skip to content

Bugfix: lp_writer SOS constraints and row_order#3678

Merged
mrmundt merged 5 commits intoPyomo:mainfrom
mrmundt:lpwriter_bug
Aug 6, 2025
Merged

Bugfix: lp_writer SOS constraints and row_order#3678
mrmundt merged 5 commits intoPyomo:mainfrom
mrmundt:lpwriter_bug

Conversation

@mrmundt
Copy link
Copy Markdown
Contributor

@mrmundt mrmundt commented Aug 5, 2025

Fixes NA

Summary/Motivation:

In starting work on the new bar_writer, I stumbled across a few bugs in lp_v2 that no one has hit yet but definitely don't work. Primarily, the bugs were:

  • Missing import statement
  • Accidentally misused letter
  • Entirely skipped edge case to correct handle when a model has SOS constraints and is using row_order in the writer config.

Changes proposed in this PR:

  • Fix two small bugs
  • Refactor ordered_active_constraints to pull out row_map logic (for reusability)
  • Bugfix using new row_order2row_map function
  • index_set is now a reserved name, so SOS example fixed so it'll actually run
  • Test using SOS example as a baseline

Legal Acknowledgement

By contributing to this software project, I have read the contribution guide and agree to the following terms and conditions for my contribution:

  1. I agree my contributions are submitted under the BSD license.
  2. I represent I am authorized to make the contributions and grant the license. If my employer has rights to intellectual property that includes these contributions, I represent that I have received permission to make contributions and grant the required license on behalf of that employer.

model = pyo.ConcreteModel()

model.index_set = pyo.Set(initialize=[1, 2])
model.idx_set = pyo.Set(initialize=[1, 2])
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.

This example clearly was not being tested. Is it now?

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.

I suspect a large chunk of our examples are not being tested. That's a bigger effort than this PR. If you want me to cobble something together for this exact example, I can, but I'd recommend that we turn it into a targeted PEP instead.

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.

'Eyyyy, this is, in fact, already a PEP: #2962

Comment on lines +779 to +783
def ordered_active_constraints(model, config):
sorter = FileDeterminism_to_SortComponents(config.file_determinism)
constraints = model.component_data_objects(Constraint, active=True, sort=sorter)

row_map = row_order2row_map(config)
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.

While it is good to avoid the duplicate code, from an efficiency / consistency standpoint, this is less than ideal (row_order2row_map is being called twice. I think I would add row_map as an argument of this function.

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.

I would like to go through the standard form, LP, and NL writers and refactor this logic to remove redundancy & enforce uniformity (e.g., I am pretty sure that the NLv2 is ignoring row_order!). But, I am comfortable merging this PR first. The redundant call to row_order2row_map only hits users with both SOSConstraint components and who are specifying a row_order (and realistically, it's not that big of a hit).

@blnicho blnicho moved this from Todo to Review In Progress in August 2025 Release Aug 6, 2025
@github-project-automation github-project-automation bot moved this from Review In Progress to Reviewer Approved in August 2025 Release Aug 6, 2025
@mrmundt mrmundt merged commit 5dbc835 into Pyomo:main Aug 6, 2025
64 of 65 checks passed
@github-project-automation github-project-automation bot moved this from Reviewer Approved to Done in August 2025 Release Aug 6, 2025
@mrmundt mrmundt deleted the lpwriter_bug branch August 11, 2025 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects

Development

Successfully merging this pull request may close these issues.

3 participants