From 1eb64d9a3835acfa873927da7e8b22acfa248fde Mon Sep 17 00:00:00 2001 From: John Siirola Date: Mon, 21 Aug 2023 11:21:58 -0600 Subject: [PATCH] LP: Improve handling of InvalidNumbers in Expr_if --- pyomo/repn/linear.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pyomo/repn/linear.py b/pyomo/repn/linear.py index f25f606c0e8..5b91bee6b99 100644 --- a/pyomo/repn/linear.py +++ b/pyomo/repn/linear.py @@ -436,7 +436,7 @@ def _handle_expr_if_const(visitor, node, arg1, arg2, arg3): _type, _test = arg1 assert _type is _CONSTANT if _test: - if _test != _test: + if _test != _test or test.__class__ is InvalidNumber: # nan return _handle_expr_if_nonlinear(visitor, node, arg1, arg2, arg3) return arg2 @@ -680,17 +680,26 @@ def _before_named_expression(visitor, child): def _before_expr_if(visitor, child): + # We want a _before_expr_if to catch constant test expressions so + # that we ONLY evaluate the appropriate branch. test, t, f = child.args if is_fixed(test): try: - test = test() + test = visitor._eval_expr(test) except: return True, None - subexpr = LinearRepnVisitor( + if test.__class__ is InvalidNumber: + return True, None + subexpr = visitor.__class__( visitor.subexpression_cache, visitor.var_map, visitor.var_order ).walk_expression(t if test else f) if subexpr.nonlinear is not None: return False, (_GENERAL, subexpr) + # This test is not needed for LINEAR, but is for the derived + # QUADRATIC. As this is the ONLY _before_* specialization + # needed for Quadratic, we will handle it here. + elif hasattr(subexpr, 'quadratic') and subexpr.quadratic: + return False, (ExprType._QUADRATIC, subexpr) elif subexpr.linear: return False, (_LINEAR, subexpr) else: