Utilities to Manage and Analyze Expressions
expression_to_string(expr, verbose=None, labeler=None, smap=None, compute_values=False)
Return a string representation of an expression.
- expr (ExpressionBase) – The root node of an expression tree.
- verbose (bool) – If
True, then the output is a nested functional form. Otherwise, the output is an algebraic expression. Default is retrieved from
- labeler (Callable) – If specified, this labeler is used to generate the string representation for leaves (Var / Param objects) in the expression.
- smap (SymbolMap) – If specified, this
SymbolMapis used to cache labels.
- compute_values (bool) – If
True, then parameters and fixed variables are evaluated before the expression string is generated. Default is
- Returns – A string representation for the expression.
A function that returns a tuple consisting of (1) a flag indicated whether the expression is linear, and (2) a list of tuples that represents the terms in the linear expression.
Parameters: expr (expression) – The root node of an expression tree Returns: A tuple with the form
(flag, list). If
False, then a nonlinear term has been found, and
listis a list of tuples:
(coef, value). If
None, then this represents a constant term with value
valueis a variable object, and
coefis the numeric coefficient.
A function that is used to clone an expression.
Cloning is equivalent to calling
copy.deepcopywith no Block scope. That is, the expression tree is duplicated, but no Pyomo components (leaf nodes or named Expressions) are duplicated.
- expr – The expression that will be cloned.
- substitute (dict) – A dictionary mapping object ids to
objects. This dictionary has the same semantics as
the memo object used with
copy.deepcopy. Defaults to None, which indicates that no user-defined dictionary is used.
The cloned expression.
evaluate_expression(exp, exception=True, constant=False)
Evaluate the value of the expression.
- expr – The root node of an expression tree.
- exception (bool) – A flag that indicates whether
exceptions are raised. If this flag is
False, then an exception that occurs while evaluating the expression is caught and the return value is
None. Default is
- constant (bool) – If True, constant expressions are evaluated and returned but nonconstant expressions raise either FixedExpressionError or NonconstantExpressionError (default=False).
A floating point value if the expression evaluates normally, or
Noneif an exception occurs and is caught.
A generator that yields a sequence of nodes in an expression tree that belong to a specified set.
Each node that is found.
A generator that yields a sequence of variables in an expression tree.
- expr – The root node of an expression tree.
- include_fixed (bool) – If
True, then this generator will yield variables whose value is fixed. Defaults to
Each variable that is found.
differentiate(expr, wrt=None, wrt_list=None, mode=Modes.reverse_numeric)
Return derivative of expression.
This function returns the derivative of expr with respect to one or more variables. The type of the return value depends on the arguments wrt, wrt_list, and mode. See below for details.
- expr (pyomo.core.expr.numeric_expr.NumericExpression) – The expression to differentiate
- wrt (pyomo.core.base.var._GeneralVarData) – If specified, this function will return the derivative with respect to wrt. wrt is normally a _GeneralVarData, but could also be a _ParamData. wrt and wrt_list cannot both be specified.
- wrt_list (list of pyomo.core.base.var._GeneralVarData) – If specified, this function will return the derivative with respect to each element in wrt_list. A list will be returned where the values are the derivatives with respect to the corresponding entry in wrt_list.
- mode (pyomo.core.expr.calculus.derivatives.Modes) –
Specifies the method to use for differentiation. Should be one of the members of the Modes enum:
- The pyomo expression will be converted to a sympy expression. Differentiation will then be done with sympy, and the result will be converted back to a pyomo expression. The sympy mode only does symbolic differentiation. The sympy mode requires exactly one of wrt and wrt_list to be specified.
- Symbolic differentiation will be performed directly with the pyomo expression in reverse mode. If neither wrt nor wrt_list are specified, then a ComponentMap is returned where there will be a key for each node in the expression tree, and the values will be the symbolic derivatives.
- Numeric differentiation will be performed directly with the pyomo expression in reverse mode. If neither wrt nor wrt_list are specified, then a ComponentMap is returned where there will be a key for each node in the expression tree, and the values will be the floating point values of the derivatives at the current values of the variables.
res – The value or expression of the derivative(s)
ComponentMap, or list
A class for tracking assigned labels for modeling components.
Symbol maps are used, for example, when writing problem files for input to an optimizer.
A symbol map should never be pickled. This class is typically constructed by solvers and writers, and it may be owned by models.
We should change the API to not use camelcase.
used to compute a string label from an object