GAMS writer: map non-finite float values to GAMS special values (NA / INF) - #4033
Open
cjck944084735-dot wants to merge 1 commit into
Open
GAMS writer: map non-finite float values to GAMS special values (NA / INF)#4033cjck944084735-dot wants to merge 1 commit into
cjck944084735-dot wants to merge 1 commit into
Conversation
Python renders non-finite floats as the strings 'nan'/'inf', which are not valid GAMS numeric literals, so exporting a model whose variables or fixed expressions hold such values produces a .gms file that GAMS rejects with errors like "the symbol nan is undefined". Route all numeric output in the GAMS writer through a gams_ftoa() wrapper that maps them to GAMS's special values NA and INF. Fixes Pyomo#3911
13 tasks
Contributor
|
@cjck944084735-dot, thank you for the contribution. One question about the intent behind this PR: We've been working on changes to the expression system so that we propagate |
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 #3911 .
(Supersedes #4029, which was closed by policy for not using this template; the branch and changes are identical.)
Summary/Motivation:
The GAMS writer emits Python's string rendering of non-finite floats (
nan,inf,-inf), which are not valid GAMS numeric literals. Exporting a model whose variables or fixed expressions hold such values produces a.gmsfile that GAMS rejects with errors likethe symbol nan is undefined(reported in #3911). All numeric output inpyomo/repn/plugins/gams_writer.pygoes throughftoa()(format'%.17g'), which renders non-finite floats asnan/inf, e.g. on currentmainwith defaultwarmstart=True:GAMS's special numeric literals are
NAandINF/-INF, so every one of those lines is rejected by GAMS. This hits all value-emitting paths of the writer: constraint bounds, expression constants (fixed Vars/Params), warmstart.lvalues, and fixed-variable.fxvalues.Changes proposed in this PR:
gams_ftoa()wrapper that post-processes theftoa()result:nanmaps to GAMSNA, and±infmaps to±INF(including the parenthesized(-inf)branch).gams_ftoa().test_nonfinite_var_to_string(expression path with fixednan/inf/-infVars) andtest_nonfinite_var_values_write_gams(full model write asserting.lwarmstart values, the fixed-expression constant, and that no barenan/infappears in the exported file).After the change the same model exports as:
Two notes on float semantics that the tests document:
-1 * nanisnan(the sign is lost), so- zwithz = nanexports as+ NA; and-infvalues in monomials are parenthesized byftoa, hence+ (-INF).Local verification: full existing suite
pyomo/repn/tests/gams/passes (24 passed, 1 skipped), andblack --checkis clean.Note for reviewers:
pyomo/repn/plugins/gams_writer_v2.pyhas the same pattern for warmstart values (ostream.write(f"{v}.l = {pyomo_v.value};\n"), line ~537). I left it untouched to keep this PR focused on the defaultgamswriter from the issue; happy to apply the equivalent mapping there in this PR or a follow-up, whichever the maintainers prefer.AI-Use Disclosure
or
AI tools contributed to the development of this PR
Review process (select ONE):
Notes for reviewers (optional): Where should reviewers focus? (1) The
gams_ftoa()mapping ofnan/inftoNA/INF— please confirmNA(rather than an error) is the desired semantic for undefined variable values in exported GAMS files. (2) Whethergams_writer_v2should be fixed in this PR or a follow-up.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: