From f9cd36402c0681f2415cf31721678f1da5ef0ba9 Mon Sep 17 00:00:00 2001 From: Lim-jooyoung Date: Thu, 3 Dec 2026 09:18:52 +0900 Subject: [PATCH] Return (-inf, inf) for unsupported unary functions in the bounds walker 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. --- .../contrib/fbbt/expression_bounds_walker.py | 12 +++++++++- .../tests/test_expression_bounds_walker.py | 22 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/pyomo/contrib/fbbt/expression_bounds_walker.py b/pyomo/contrib/fbbt/expression_bounds_walker.py index 249bbfb2361..d7ae02d2990 100644 --- a/pyomo/contrib/fbbt/expression_bounds_walker.py +++ b/pyomo/contrib/fbbt/expression_bounds_walker.py @@ -216,8 +216,18 @@ def _handle_AbsExpression(visitor, node, arg): return interval_abs(*arg) +def _handle_unsupported_unary_function(visitor, node, arg): + logger.warning( + f"Unsupported unary function '{node.getname()}' found while walking " + "expression tree; returning (-inf, inf) for the expression bounds." + ) + return -inf, inf + + def _handle_UnaryFunctionExpression(visitor, node, arg): - return _unary_function_dispatcher[node.getname()](visitor, node, arg) + return _unary_function_dispatcher.get( + node.getname(), _handle_unsupported_unary_function + )(visitor, node, arg) def _handle_named_expression(visitor, node, arg): diff --git a/pyomo/contrib/fbbt/tests/test_expression_bounds_walker.py b/pyomo/contrib/fbbt/tests/test_expression_bounds_walker.py index 7f16484688c..fcd947d6c4c 100644 --- a/pyomo/contrib/fbbt/tests/test_expression_bounds_walker.py +++ b/pyomo/contrib/fbbt/tests/test_expression_bounds_walker.py @@ -21,6 +21,14 @@ acos, atan, sqrt, + sinh, + cosh, + tanh, + asinh, + acosh, + atanh, + ceil, + floor, inequality, Expr_if, Any, @@ -201,6 +209,20 @@ def test_abs_bounds(self): self.assertEqual(lb, 0) self.assertEqual(ub, 4) + def test_unsupported_unary_function_bounds(self): + m = self.make_model() + visitor = ExpressionBoundsVisitor() + for func in (sinh, cosh, tanh, asinh, acosh, atanh, ceil, floor): + expr = func(m.x) + with LoggingIntercept() as LOG: + self.assertEqual(visitor.walk_expression(expr), (-inf, inf)) + self.assertEqual( + LOG.getvalue(), + f"Unsupported unary function '{expr.getname()}' found while " + "walking expression tree; returning (-inf, inf) for the " + "expression bounds.\n", + ) + def test_leaf_bounds_cached(self): m = self.make_model() visitor = ExpressionBoundsVisitor()