>>> import pyomo.environ as pyo
When setting Var
values (by either calling Var.set_value()
or setting the value
attribute), Pyomo will validate the incoming value by checking that the value is in
the Var.domain
. Any values not in the domain will generate this warning:
>>> m = pyo.ConcreteModel() >>> m.x = pyo.Var(domain=pyo.Integers) >>> m.x = 0.5 WARNING (W1001): Setting Var 'x' to a value 0.5 (float) not in domain Integers. See also https://pyomo.readthedocs.io/en/stable/errors.html#w1001 >>> print(m.x.value) 0.5
Users can bypass all domain validation by setting the value using:
>>> m.x.set_value(0.75, skip_validation=True) >>> print(m.x.value) 0.75
When setting :pyVar
values (by either calling set_value()
or setting the value
attribute), Pyomo will validate the incoming value by checking that the value is within the range specified by Var.bounds
. Any values outside the bounds will generate this warning:
>>> m = pyo.ConcreteModel() >>> m.x = pyo.Var(domain=pyo.Integers, bounds=(1, 5)) >>> m.x = 0 WARNING (W1002): Setting Var 'x' to a numeric value 0 outside the bounds (1, 5). See also https://pyomo.readthedocs.io/en/stable/errors.html#w1002 >>> print(m.x.value) 0
Users can bypass all domain validation by setting the value using:
>>> m.x.set_value(10, skip_validation=True) >>> print(m.x.value) 10
Pyomo leverages a recursive walker (the :py~pyomo.core.expr.visitor.StreamBasedExpressionVisitor
) to traverse (walk) expression trees. For most expressions, this recursive walker is the most efficient. However, Python has a relatively shallow recursion limit (generally, 1000 frames). The recursive walker is designed to monitor the stack depth and cleanly switch to a nonrecursive walker before hitting the stack limit. However, there are two (rare) cases where the Python stack limit can still generate a :pyRecursionError
exception:
- Starting the walker with fewer than :py
pyomo.core.expr.visitor.RECURSION_LIMIT
available frames. - Callbacks that require more than 2 * :py
pyomo.core.expr.visitor.RECURSION_LIMIT
frames.
The (default) recursive walker will catch the exception and restart the walker from the beginning in non-recursive mode, issuing this warning. The caution is that any partial work done by the walker before the exception was raised will be lost, potentially leaving the walker in an inconsistent state. Users can avoid this by
- avoiding recursive callbacks
- restructuring the system design to avoid triggering the walker with few available stack frames
- directly calling the :py
~pyomo.core.expr.visitor.StreamBasedExpressionVisitor.walk_expression_nonrecursive()
walker method
>>> import sys >>> import pyomo.core.expr.visitor as visitor >>> from pyomo.core.tests.unit.test_visitor import fill_stack >>> expression_depth = visitor.StreamBasedExpressionVisitor( ... exitNode=lambda node, data: max(data) + 1 if data else 1) >>> m = pyo.ConcreteModel() >>> m.x = pyo.Var() >>> @m.Expression(range(35)) ... def e(m, i): ... return m.e[i-1] if i else m.x >>> expression_depth.walk_expression(m.e[34]) 36 >>> fill_stack(sys.getrecursionlimit() - visitor.get_stack_depth() - 30, ... expression_depth.walk_expression, ... m.e[34]) WARNING (W1003): Unexpected RecursionError walking an expression tree. See also https://pyomo.readthedocs.io/en/stable/errors.html#w1003 36 >>> fill_stack(sys.getrecursionlimit() - visitor.get_stack_depth() - 30, ... expression_depth.walk_expression_nonrecursive, ... m.e[34]) 36
Variable domains are always Pyomo Set
or RangeSet
objects. This includes global sets like Reals
, Integers
, Binary
, NonNegativeReals
, etc., as well as model-specific Set
instances. The Var.domain
setter will attempt to convert assigned values to a Pyomo Set, with any failures leading to this warning (and an exception from the converter):
>>> m = pyo.ConcreteModel() >>> m.x = pyo.Var() >>> m.x.domain = 5 Traceback (most recent call last): ... TypeError: Cannot create a Set from data that does not support __contains__... ERROR (E2001): 5 is not a valid domain. Variable domains must be an instance of a Pyomo Set or convertible to a Pyomo Set. See also https://pyomo.readthedocs.io/en/stable/errors.html#e2001