Skip to content

Fix KeyError in compute_bounds_on_expr for hyperbolic, ceil and floor functions - #4126

Open
mmporong wants to merge 1 commit into
Pyomo:mainfrom
mmporong:bounds-walker-unsupported-unary
Open

mmporong wants to merge 1 commit into
Pyomo:mainfrom
mmporong:bounds-walker-unsupported-unary

Conversation

@mmporong

@mmporong mmporong commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4125 .

Summary/Motivation:

compute_bounds_on_expr (ExpressionBoundsVisitor) raises KeyError for any expression that contains a unary function without an entry in _unary_function_dispatcher. Of the 18 intrinsic functions Pyomo provides, 8 are missing: sinh, cosh, tanh, asinh, acosh, atanh, ceil and floor. As reported in #4125, this breaks IDAES DiagnosticsToolbox.report_structural_issues() on models that use tanh.

The walker already returns (-inf, inf) with a warning for expression node types it does not recognize (ExpressionBoundsExitNodeDispatcher.unexpected_expression_type), and the FBBT leaf-to-root map (_unary_leaf_to_root_map) returns (-inf, inf) for these function names. This PR applies the same fallback to unary functions that have no handler.

Changes proposed in this PR:

  • pyomo/contrib/fbbt/expression_bounds_walker.py: look up the unary function handler with dict.get and fall back to a handler that logs a warning and returns (-inf, inf). _unary_function_dispatcher stays a plain dict, so a handler registered at runtime (like the workaround in ExpressionBoundsVisitor / compute_bounds_on_expr raises KeyError for hyperbolic functions (tanh, sinh, cosh, ...) #4125) still takes precedence.
  • pyomo/contrib/fbbt/tests/test_expression_bounds_walker.py: add test_unsupported_unary_function_bounds, which walks each of the 8 functions and checks the bounds and the warning. It fails on main with KeyError: 'sinh' and passes with this change.

AI-Use Disclosure

  • AI tools were NOT used during the preparation of this PR

or

  • 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):

    • Rewritten: All AI-generated content was rewritten by me before being committed.
    • 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
    • As-is: AI-generated content was commited directly to the repository

Notes for reviewers (optional):

  • The fallback logs a warning, following unexpected_expression_type in the same walker. The FBBT leaf-to-root map returns (-inf, inf) for these names without a warning. If you would rather keep the walker silent here, the warning can be dropped.
  • Exact bounds for the monotone functions (tanh, sinh, asinh, ...) would need new functions in interval.py and handlers in both FBBT and this walker. This PR only removes the KeyError.
  • Checked with Python 3.12: pyomo/contrib/fbbt/tests gives 73 passed and 28 skipped (72 passed and 28 skipped on main). Together with the other users of the walker (pyomo/gdp/tests/test_bigm.py, pyomo/gdp/tests/test_mbigm.py, pyomo/contrib/piecewise/tests and pyomo/contrib/alternative_solutions/tests/test_shifted_lp.py) it gives 319 passed and 97 skipped. black --check and typos pass on the two files. The reproducer from ExpressionBoundsVisitor / compute_bounds_on_expr raises KeyError for hyperbolic functions (tanh, sinh, cosh, ...) #4125 now prints (None, None) with the warning for all 8 functions instead of KeyError.

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.

ExpressionBoundsVisitor looked up unary function handlers with a plain
dict index, so sinh, cosh, tanh, asinh, acosh, atanh, ceil and floor
raised KeyError from compute_bounds_on_expr. Fall back to (-inf, inf)
with a warning, as the walker already does for unexpected node types.
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.

ExpressionBoundsVisitor / compute_bounds_on_expr raises KeyError for hyperbolic functions (tanh, sinh, cosh, ...)

1 participant