Fix LinearRepnVisitor to substitute fixed vars already in var_map - #4034
Open
cjck944084735-dot wants to merge 1 commit into
Open
Fix LinearRepnVisitor to substitute fixed vars already in var_map#4034cjck944084735-dot wants to merge 1 commit into
cjck944084735-dot wants to merge 1 commit into
Conversation
record_monomial only applied the fixed-variable (constant) handling when the variable had not yet been registered in the visitor var_map. When a variable was registered in bulk by the TemplateVarRecorder while it was still free, and was fixed afterwards, subsequent walk_expression calls treated it as an unrestricted variable: the LinearStandardFormCompiler would then reject expressions involving it as nonlinear even though they are linear after substitution. Check var.fixed before the var_map membership test so fixed variables always contribute their value as a constant. Fixes Pyomo#3851
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.
Fixes #3851 .
Summary/Motivation:
LinearRepnVisitor.record_monomialonly applied the fixed-variable (constant) handling when the variable had not yet been registered in the visitor'svar_map. When a variable was registered in bulk by theTemplateVarRecorderwhile it was still free and was fixed afterwards, subsequentwalk_expressioncalls found it in thevar_mapand treated it as an unrestricted variable: fixed values were not substituted and expressions involving them were misclassified as nonlinear. This is exactly the failure mode reported in #3851, including through theLinearStandardFormCompiler's use of aTemplateVarRecorder.Changes proposed in this PR:
record_monomialnow checksvar.fixedbefore thevar_mapmembership test, so a fixed variable always contributes its value as a constant, regardless of when it was registered.pyomo/repn/tests/test_linear.py::TestFixedVarAfterRegistration, using the reporter's scenario (a variable registered by walking one expression while free, fixed, then used in another expression):test_fixed_var_after_registration:x[1] - 1/x[2]withx[2].fix(2)now yieldsconstant=-0.5, linear={x[1]: 1}, nonlinear=None;test_fixed_var_substituted_before_registration: the fixed variable never becomes a linear term nor leaks into the nonlinear remainder.After the change, the reporter's
r2isLinearRepn(mult=1, const=-0.5, linear={x[1]: 1}, nonlinear=None)instead of carryingnonlinear=-(1/x[2]).Local verification
pyomo/repn/tests/: 357 passed (355 existing + 2 new), 48 skipped.pyomo/core/tests/: 4392 passed — no regressions.black --checkclean on the changed files.AI-Use Disclosure
or
AI tools contributed to the development of this PR
Review process (select ONE):
Notes for reviewers (optional): The behavior change is narrow: a fixed variable that was registered in
var_mapwhile free is now handled as a constant. Callers that relied on such variables appearing as linear terms would have received incorrect coefficients (the fixed value was never substituted), so this should be strictly a correctness improvement. One thing I would appreciate a second look at: whether any writer depends on fixed-but-registered variables still occupying a column invar_map/var_list.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: