Skip to content

Commit

Permalink
Merge pull request Pyomo#1946 from michaelbynum/eval
Browse files Browse the repository at this point in the history
reduce errors logged with value when exception is False
  • Loading branch information
michaelbynum authored Apr 26, 2021
2 parents 344bcb3 + 26b6f58 commit d099b32
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
9 changes: 6 additions & 3 deletions pyomo/core/expr/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,9 @@ def accept(node, data, child_result, child_idx):

class _EvaluationVisitor(ExpressionValueVisitor):

def __init__(self, exception):
self.exception = exception

def visit(self, node, values):
""" Visit nodes that have been expanded """
return node._apply_operation(values)
Expand All @@ -946,9 +949,9 @@ def visiting_potential_leaf(self, node):
return False, None

if node.is_numeric_type():
return True, value(node)
return True, value(node, exception=self.exception)
elif node.is_logical_type():
return True, value(node)
return True, value(node, exception=self.exception)
else:
return True, node

Expand Down Expand Up @@ -1036,7 +1039,7 @@ def evaluate_expression(exp, exception=True, constant=False):
if constant:
visitor = _EvaluateConstantExpressionVisitor()
else:
visitor = _EvaluationVisitor()
visitor = _EvaluationVisitor(exception=exception)
try:
return visitor.dfs_postorder_stack(exp)

Expand Down
14 changes: 14 additions & 0 deletions pyomo/core/tests/unit/test_numeric_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

from filecmp import cmp
import pyomo.common.unittest as unittest
from pyomo.common.log import LoggingIntercept
from io import StringIO

from pyomo.environ import ConcreteModel, AbstractModel, RangeSet, Var, Param, Set, Constraint, ConstraintList, Expression, Objective, Reals, ExternalFunction, PositiveReals, log10, exp, floor, ceil, log, cos, sin, tan, acos, asin, atan, sinh, cosh, tanh, acosh, asinh, atanh, sqrt, value, quicksum, sum_product, is_fixed, is_constant
from pyomo.kernel import variable, expression, objective
Expand Down Expand Up @@ -5244,5 +5246,17 @@ def con_rule(model):
self.assertEqual(polynomial_degree(m.c1.body), 1)


class TestEvaluation(unittest.TestCase):
def test_log_error(self):
m = ConcreteModel()
m.x = Var()
e = m.x**2
os = StringIO()
with LoggingIntercept(os, 'pyomo'):
e_val = value(e, exception=False)
self.assertIsNone(e_val)
self.assertEqual(os.getvalue(), '')


if __name__ == "__main__":
unittest.main()

0 comments on commit d099b32

Please sign in to comment.