Skip to content

Fix NL writer handling of variables first encountered in SOS constraints (#3719) - #4130

Open
shivt23 wants to merge 1 commit into
Pyomo:mainfrom
shivt23:fix/3719-sos-var-handling
Open

shivt23 wants to merge 1 commit into
Pyomo:mainfrom
shivt23:fix/3719-sos-var-handling

Conversation

@shivt23

@shivt23 shivt23 commented Dec 4, 2026

Copy link
Copy Markdown

Fixes #3719

Summary/Motivation:

When an SOS constraint references variables that appear nowhere else in the model, the NL writer crashed with a cryptic KeyError while building the variable initialization lines.

The SOS loop already added such a variable to var_map, but var_bounds and var_values are built before that loop, so the variable was missing from both. A Binary variable crashed at the var_values lookup (line 1455); an Integer variable crashed earlier at the var_bounds lookup (line 869).

This PR supersedes #4121 — it corrects the handling instead of raising an error.

Changes proposed in this PR:

  • pyomo/repn/plugins/nl_writer.py: in the SOS loop, record var_bounds[_id] = v.bounds and var_values[_id] = v.value alongside var_map[_id] = v, following the export_nonlinear_variables block just above it.
  • pyomo/repn/tests/ampl/test_nlv2.py: added test_sos_orphaned_variable_writes and test_sos_orphaned_integer_variable_writes (each fails without the fix, at 1455 and 869 respectively), and kept test_sos_linked_variable_writes.

Test results: pyomo/repn/tests/ampl/test_nlv2.py — 43 passed, 4 skipped.

AI-Use Disclosure

  • AI tools contributed to the development of this PR

    • AI tools generated documentation (including the PR description/comments, code comments, and/or Sphinx documentation)
    • AI tools generated tests (baselines, examples, and/or code)
    • AI tools generated code (apart from tests)

    Review process (select ONE):

    • Reviewed/verified: I retained AI-generated content and verified it before committing. Verification included (as applicable):
      • Ran the code and fixed issues
      • Added and ran tests
      • Checked correctness/logic of code and tests
      • Checked for alignment with the contribution guide
      • Considered security implications

Notes for reviewers (optional): I claimed the issue, reproduced the bug locally, directed the fix approach, reviewed every change, and ran the tests. AI tooling (Claude Code) drafted the fix and tests under my direction.

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.

…nts (Pyomo#3719)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non-explicit error when SOS1 constraint is orphaned

1 participant