Core Classes

The following are the two core classes documented here:

The remaining classes are the public classes for expressions, which developers may need to know about. The methods for these classes are not documented because they are described in the ExpressionBase class.

Sets with Expression Types

The following sets can be used to develop visitor patterns for Pyomo expressions.

pyomo.core.expr.numvalue.native_numeric_types = {<class 'numpy.uint64'>, <class 'numpy.int64'>, <class 'numpy.ndarray'>, <class 'bool'>, <class 'numpy.float16'>, <class 'numpy.float64'>, <class 'float'>, <class 'numpy.uint8'>, <class 'numpy.int8'>, <class 'numpy.float32'>, <class 'numpy.uint16'>, <class 'numpy.int16'>, <class 'int'>, <class 'numpy.uint32'>, <class 'numpy.int32'>}

set() -> new empty set object set(iterable) -> new set object

Build an unordered collection of unique elements.

pyomo.core.expr.numvalue.native_types = {<class 'numpy.uint64'>, <class 'numpy.int64'>, <class 'numpy.float64'>, <class 'numpy.uint32'>, <class 'numpy.int32'>, <class 'slice'>, <class 'bool'>, <class 'numpy.float32'>, <class 'numpy.uint16'>, <class 'numpy.int16'>, <class 'numpy.bool_'>, <class 'NoneType'>, <class 'str'>, <class 'numpy.float16'>, <class 'numpy.uint8'>, <class 'numpy.int8'>, <class 'bytes'>, <class 'numpy.ndarray'>, <class 'float'>, <class 'int'>}

set() -> new empty set object set(iterable) -> new set object

Build an unordered collection of unique elements.

pyomo.core.expr.numvalue.nonpyomo_leaf_types = {<class 'numpy.uint64'>, <class 'numpy.int64'>, <class 'numpy.float16'>, <class 'numpy.uint8'>, <class 'numpy.int8'>, <class 'numpy.float64'>, <class 'numpy.bool_'>, <class 'numpy.uint32'>, <class 'numpy.int32'>, <class 'bytes'>, <class 'slice'>, <class 'pyomo.core.expr.numvalue.NonNumericValue'>, <class 'numpy.ndarray'>, <class 'bool'>, <class 'float'>, <class 'numpy.float32'>, <class 'numpy.uint16'>, <class 'numpy.int16'>, <class 'int'>, <class 'NoneType'>, <class 'str'>}

set() -> new empty set object set(iterable) -> new set object

Build an unordered collection of unique elements.

NumericValue and ExpressionBase

class pyomo.core.expr.numvalue.NumericValue[source]

This is the base class for numeric values used in Pyomo.

__abs__()[source]

Absolute value

This method is called when Python processes the statement:

abs(self)
__add__(other)[source]

Binary addition

This method is called when Python processes the statement:

self + other
__bool__()[source]

Coerce the value to a bool

Numeric values can be coerced to bool only if the value / expression is constant. Fixed (but non-constant) or variable values will raise an exception.

Raises:PyomoException
__div__(other)[source]

Binary division

This method is called when Python processes the statement:

self / other
__eq__(other)[source]

Equal to operator

This method is called when Python processes the statement:

self == other
__float__()[source]

Coerce the value to a floating point

Numeric values can be coerced to float only if the value / expression is constant. Fixed (but non-constant) or variable values will raise an exception.

Raises:TypeError
__ge__(other)[source]

Greater than or equal operator

This method is called when Python processes statements of the form:

self >= other
other <= self
__getstate__()[source]

Prepare a picklable state of this instance for pickling.

Nominally, __getstate__() should execute the following:

state = super(Class, self).__getstate__()
for i in Class.__slots__:
    state[i] = getattr(self,i)
return state

However, in this case, the (nominal) parent class is ‘object’, and object does not implement __getstate__. So, we will check to make sure that there is a base __getstate__() to call. You might think that there is nothing to check, but multiple inheritance could mean that another class got stuck between this class and “object” in the MRO.

Further, since there are actually no slots defined here, the real question is to either return an empty dict or the parent’s dict.

__gt__(other)[source]

Greater than operator

This method is called when Python processes statements of the form:

self > other
other < self
__hash__ = None
__iadd__(other)[source]

Binary addition

This method is called when Python processes the statement:

self += other
__idiv__(other)[source]

Binary division

This method is called when Python processes the statement:

self /= other
__imul__(other)[source]

Binary multiplication

This method is called when Python processes the statement:

self *= other
__int__()[source]

Coerce the value to an integer

Numeric values can be coerced to int only if the value / expression is constant. Fixed (but non-constant) or variable values will raise an exception.

Raises:TypeError
__ipow__(other)[source]

Binary power

This method is called when Python processes the statement:

self **= other
__isub__(other)[source]

Binary subtraction

This method is called when Python processes the statement:

self -= other
__itruediv__(other)[source]

Binary division (when __future__.division is in effect)

This method is called when Python processes the statement:

self /= other
__le__(other)[source]

Less than or equal operator

This method is called when Python processes statements of the form:

self <= other
other >= self
__lt__(other)[source]

Less than operator

This method is called when Python processes statements of the form:

self < other
other > self
__mul__(other)[source]

Binary multiplication

This method is called when Python processes the statement:

self * other
__neg__()[source]

Negation

This method is called when Python processes the statement:

- self
__pos__()[source]

Positive expression

This method is called when Python processes the statement:

+ self
__pow__(other)[source]

Binary power

This method is called when Python processes the statement:

self ** other
__radd__(other)[source]

Binary addition

This method is called when Python processes the statement:

other + self
__rdiv__(other)[source]

Binary division

This method is called when Python processes the statement:

other / self
__rmul__(other)[source]

Binary multiplication

This method is called when Python processes the statement:

other * self

when other is not a NumericValue object.

__rpow__(other)[source]

Binary power

This method is called when Python processes the statement:

other ** self
__rsub__(other)[source]

Binary subtraction

This method is called when Python processes the statement:

other - self
__rtruediv__(other)[source]

Binary division (when __future__.division is in effect)

This method is called when Python processes the statement:

other / self
__setstate__(state)[source]

Restore a pickled state into this instance

Our model for setstate is for derived classes to modify the state dictionary as control passes up the inheritance hierarchy (using super() calls). All assignment of state -> object attributes is handled at the last class before ‘object’, which may – or may not (thanks to MRO) – be here.

__sub__(other)[source]

Binary subtraction

This method is called when Python processes the statement:

self - other
__truediv__(other)[source]

Binary division (when __future__.division is in effect)

This method is called when Python processes the statement:

self / other
_compute_polynomial_degree(values)[source]

Compute the polynomial degree of this expression given the degree values of its children.

Parameters:values (list) – A list of values that indicate the degree of the children expression.
Returns:None
getname(fully_qualified=False, name_buffer=None)[source]

If this is a component, return the component’s name on the owning block; otherwise return the value converted to a string

is_constant()[source]

Return True if this numeric value is a constant value

is_fixed()[source]

Return True if this is a non-constant value that has been fixed

is_indexed()[source]

Return True if this numeric value is an indexed object

is_numeric_type()[source]

Return True if this class is a Pyomo numeric object

is_potentially_variable()[source]

Return True if variables can appear in this expression

is_relational()[source]

Return True if this numeric value represents a relational expression.

polynomial_degree()[source]

Return the polynomial degree of the expression.

Returns:None
to_string(verbose=None, labeler=None, smap=None, compute_values=False)[source]

Return a string representation of the expression tree.

Parameters:
  • verbose (bool) – If True, then the the string representation consists of nested functions. Otherwise, the string representation is an algebraic equation. Defaults to False.
  • labeler – An object that generates string labels for variables in the expression tree. Defaults to None.
Returns:

A string representation for the expression tree.

class pyomo.core.expr.current.ExpressionBase(args)[source]

Bases: NumericValue

The base class for Pyomo expressions.

This class is used to define nodes in an expression tree.

Parameters:args (list or tuple) – Children of this node.
__call__(exception=True)[source]

Evaluate the value of the expression tree.

Parameters:exception (bool) – If False, then an exception raised while evaluating is captured, and the value returned is None. Default is True.
Returns:The value of the expression or None.
__getstate__()[source]

Pickle the expression object

Returns:The pickled state.
__init__(args)[source]
__str__()[source]

Returns a string description of the expression.

Note

The value of pyomo.core.expr.expr_common.TO_STRING_VERBOSE is used to configure the execution of this method. If this value is True, then the string representation is a nested function description of the expression. The default is False, which is an algebraic description of the expression.

Returns:A string.
_apply_operation(result)[source]

Compute the values of this node given the values of its children.

This method is called by the _EvaluationVisitor class. It must be over-written by expression classes to customize this logic.

Note

This method applies the logical operation of the operator to the arguments. It does not evaluate the arguments in the process, but assumes that they have been previously evaluated. But noted that if this class contains auxilliary data (e.g. like the numeric coefficients in the LinearExpression class, then those values must be evaluated as part of this function call. An uninitialized parameter value encountered during the execution of this method is considered an error.

Parameters:values (list) – A list of values that indicate the value of the children expressions.
Returns:A floating point value for this expression.
_associativity()[source]

Return the associativity of this operator.

Returns 1 if this operator is left-to-right associative or -1 if it is right-to-left associative. Any other return value will be interpreted as “not associative” (implying any arguments that are at this operator’s _precedence() will be enclosed in parens).

_compute_polynomial_degree(values)[source]

Compute the polynomial degree of this expression given the degree values of its children.

This method is called by the _PolynomialDegreeVisitor class. It can be over-written by expression classes to customize this logic.

Parameters:values (list) – A list of values that indicate the degree of the children expression.
Returns:A nonnegative integer that is the polynomial degree of the expression, or None. Default is None.
_is_fixed(values)[source]

Compute whether this expression is fixed given the fixed values of its children.

This method is called by the _IsFixedVisitor class. It can be over-written by expression classes to customize this logic.

Parameters:values (list) – A list of boolean values that indicate whether the children of this expression are fixed
Returns:A boolean that is True if the fixed values of the children are all True.
_to_string(values, verbose, smap, compute_values)[source]

Construct a string representation for this node, using the string representations of its children.

This method is called by the _ToStringVisitor class. It must must be defined in subclasses.

Parameters:
  • values (list) – The string representations of the children of this node.
  • verbose (bool) – If True, then the the string representation consists of nested functions. Otherwise, the string representation is an algebraic equation.
  • smap – If specified, this SymbolMap is used to cache labels for variables.
  • compute_values (bool) – If True, then parameters and fixed variables are evaluated before the expression string is generated.
Returns:

A string representation for this node.

arg(i)[source]

Return the i-th child node.

Parameters:i (int) – Nonnegative index of the child that is returned.
Returns:The i-th child node.
property args

Return the child nodes

Returns: Either a list or tuple (depending on the node storage
model) containing only the child nodes of this node
clone(substitute=None)[source]

Return a clone of the expression tree.

Note

This method does not clone the leaves of the tree, which are numeric constants and variables. It only clones the interior nodes, and expression leaf nodes like _MutableLinearExpression. However, named expressions are treated like leaves, and they are not cloned.

Parameters:substitute (dict) – a dictionary that maps object ids to clone objects generated earlier during the cloning process.
Returns:A new expression tree.
create_node_with_local_data(args, classtype=None)[source]

Construct a node using given arguments.

This method provides a consistent interface for constructing a node, which is used in tree visitor scripts. In the simplest case, this simply returns:

self.__class__(args)

But in general this creates an expression object using local data as well as arguments that represent the child nodes.

Parameters:args (list) – A list of child nodes for the new expression object
Returns:A new expression object with the same type as the current class.
create_potentially_variable_object()[source]

Create a potentially variable version of this object.

This method returns an object that is a potentially variable version of the current object. In the simplest case, this simply sets the value of __class__:

self.__class__ = self.__class__.__mro__[1]

Note that this method is allowed to modify the current object and return it. But in some cases it may create a new potentially variable object.

Returns:An object that is potentially variable.
getname(*args, **kwds)[source]

Return the text name of a function associated with this expression object.

In general, no arguments are passed to this function.

Parameters:
  • *arg – a variable length list of arguments
  • **kwds – keyword arguments
Returns:

A string name for the function.

is_constant()[source]

Return True if this expression is an atomic constant

This method contrasts with the is_fixed() method. This method returns True if the expression is an atomic constant, that is it is composed exclusively of constants and immutable parameters. NumericValue objects returning is_constant() == True may be simplified to their numeric value at any point without warning.

Note: This defaults to False, but gets redefined in sub-classes.

is_expression_type()[source]

Return True if this object is an expression.

This method obviously returns True for this class, but it is included in other classes within Pyomo that are not expressions, which allows for a check for expressions without evaluating the class type.

Returns:A boolean.
is_fixed()[source]

Return True if this expression contains no free variables.

Returns:A boolean.
is_named_expression_type()[source]

Return True if this object is a named expression.

This method returns False for this class, and it is included in other classes within Pyomo that are not named expressions, which allows for a check for named expressions without evaluating the class type.

Returns:A boolean.
is_potentially_variable()[source]

Return True if this expression might represent a variable expression.

This method returns True when (a) the expression tree contains one or more variables, or (b) the expression tree contains a named expression. In both cases, the expression cannot be treated as constant since (a) the variables may not be fixed, or (b) the named expressions may be changed at a later time to include non-fixed variables.

Returns:A boolean. Defaults to True for expressions.
nargs()[source]

Returns the number of child nodes.

By default, Pyomo expressions represent binary operations with two arguments.

Note

This function does not simply compute the length of _args_ because some expression classes use a subset of the _args_ array. Thus, it is imperative that developers use this method!

Returns:A nonnegative integer that is the number of child nodes.
polynomial_degree()[source]

Return the polynomial degree of the expression.

Returns:A non-negative integer that is the polynomial degree if the expression is polynomial, or None otherwise.
size()[source]

Return the number of nodes in the expression tree.

Returns:A nonnegative integer that is the number of interior and leaf nodes in the expression tree.
to_string(verbose=None, labeler=None, smap=None, compute_values=False)[source]

Return a string representation of the expression tree.

Parameters:
  • verbose (bool) – If True, then the the string representation consists of nested functions. Otherwise, the string representation is an algebraic equation. Defaults to False.
  • labeler – An object that generates string labels for variables in the expression tree. Defaults to None.
  • smap – If specified, this SymbolMap is used to cache labels for variables.
  • compute_values (bool) – If True, then parameters and fixed variables are evaluated before the expression string is generated. Default is False.
Returns:

A string representation for the expression tree.

Other Public Classes

class pyomo.core.expr.current.NegationExpression(args)[source]

Bases: ExpressionBase

Negation expressions:

- x
PRECEDENCE = 4
_apply_operation(result)[source]

Compute the values of this node given the values of its children.

This method is called by the _EvaluationVisitor class. It must be over-written by expression classes to customize this logic.

Note

This method applies the logical operation of the operator to the arguments. It does not evaluate the arguments in the process, but assumes that they have been previously evaluated. But noted that if this class contains auxilliary data (e.g. like the numeric coefficients in the LinearExpression class, then those values must be evaluated as part of this function call. An uninitialized parameter value encountered during the execution of this method is considered an error.

Parameters:values (list) – A list of values that indicate the value of the children expressions.
Returns:A floating point value for this expression.
_compute_polynomial_degree(result)[source]

Compute the polynomial degree of this expression given the degree values of its children.

This method is called by the _PolynomialDegreeVisitor class. It can be over-written by expression classes to customize this logic.

Parameters:values (list) – A list of values that indicate the degree of the children expression.
Returns:A nonnegative integer that is the polynomial degree of the expression, or None. Default is None.
_precedence()[source]
_to_string(values, verbose, smap, compute_values)[source]

Construct a string representation for this node, using the string representations of its children.

This method is called by the _ToStringVisitor class. It must must be defined in subclasses.

Parameters:
  • values (list) – The string representations of the children of this node.
  • verbose (bool) – If True, then the the string representation consists of nested functions. Otherwise, the string representation is an algebraic equation.
  • smap – If specified, this SymbolMap is used to cache labels for variables.
  • compute_values (bool) – If True, then parameters and fixed variables are evaluated before the expression string is generated.
Returns:

A string representation for this node.

getname(*args, **kwds)[source]

Return the text name of a function associated with this expression object.

In general, no arguments are passed to this function.

Parameters:
  • *arg – a variable length list of arguments
  • **kwds – keyword arguments
Returns:

A string name for the function.

nargs()[source]

Returns the number of child nodes.

By default, Pyomo expressions represent binary operations with two arguments.

Note

This function does not simply compute the length of _args_ because some expression classes use a subset of the _args_ array. Thus, it is imperative that developers use this method!

Returns:A nonnegative integer that is the number of child nodes.
class pyomo.core.expr.current.ExternalFunctionExpression(args, fcn=None)[source]

Bases: ExpressionBase

External function expressions

Example:

model = ConcreteModel()
model.a = Var()
model.f = ExternalFunction(library='foo.so', function='bar')
expr = model.f(model.a)
Parameters:
  • args (tuple) – children of this node
  • fcn – a class that defines this external function
_apply_operation(result)[source]

Compute the values of this node given the values of its children.

This method is called by the _EvaluationVisitor class. It must be over-written by expression classes to customize this logic.

Note

This method applies the logical operation of the operator to the arguments. It does not evaluate the arguments in the process, but assumes that they have been previously evaluated. But noted that if this class contains auxilliary data (e.g. like the numeric coefficients in the LinearExpression class, then those values must be evaluated as part of this function call. An uninitialized parameter value encountered during the execution of this method is considered an error.

Parameters:values (list) – A list of values that indicate the value of the children expressions.
Returns:A floating point value for this expression.
_compute_polynomial_degree(result)[source]

Compute the polynomial degree of this expression given the degree values of its children.

This method is called by the _PolynomialDegreeVisitor class. It can be over-written by expression classes to customize this logic.

Parameters:values (list) – A list of values that indicate the degree of the children expression.
Returns:A nonnegative integer that is the polynomial degree of the expression, or None. Default is None.
_fcn
_to_string(values, verbose, smap, compute_values)[source]

Construct a string representation for this node, using the string representations of its children.

This method is called by the _ToStringVisitor class. It must must be defined in subclasses.

Parameters:
  • values (list) – The string representations of the children of this node.
  • verbose (bool) – If True, then the the string representation consists of nested functions. Otherwise, the string representation is an algebraic equation.
  • smap – If specified, this SymbolMap is used to cache labels for variables.
  • compute_values (bool) – If True, then parameters and fixed variables are evaluated before the expression string is generated.
Returns:

A string representation for this node.

create_node_with_local_data(args, classtype=None)[source]

Construct a node using given arguments.

This method provides a consistent interface for constructing a node, which is used in tree visitor scripts. In the simplest case, this simply returns:

self.__class__(args)

But in general this creates an expression object using local data as well as arguments that represent the child nodes.

Parameters:args (list) – A list of child nodes for the new expression object
Returns:A new expression object with the same type as the current class.
get_arg_units()[source]

Return the units for this external functions arguments

get_units()[source]

Get the units of the return value for this external function

getname(*args, **kwds)[source]

Return the text name of a function associated with this expression object.

In general, no arguments are passed to this function.

Parameters:
  • *arg – a variable length list of arguments
  • **kwds – keyword arguments
Returns:

A string name for the function.

nargs()[source]

Returns the number of child nodes.

By default, Pyomo expressions represent binary operations with two arguments.

Note

This function does not simply compute the length of _args_ because some expression classes use a subset of the _args_ array. Thus, it is imperative that developers use this method!

Returns:A nonnegative integer that is the number of child nodes.
class pyomo.core.expr.current.ProductExpression(args)[source]

Bases: ExpressionBase

Product expressions:

x*y
PRECEDENCE = 4
_apply_operation(result)[source]

Compute the values of this node given the values of its children.

This method is called by the _EvaluationVisitor class. It must be over-written by expression classes to customize this logic.

Note

This method applies the logical operation of the operator to the arguments. It does not evaluate the arguments in the process, but assumes that they have been previously evaluated. But noted that if this class contains auxilliary data (e.g. like the numeric coefficients in the LinearExpression class, then those values must be evaluated as part of this function call. An uninitialized parameter value encountered during the execution of this method is considered an error.

Parameters:values (list) – A list of values that indicate the value of the children expressions.
Returns:A floating point value for this expression.
_compute_polynomial_degree(result)[source]

Compute the polynomial degree of this expression given the degree values of its children.

This method is called by the _PolynomialDegreeVisitor class. It can be over-written by expression classes to customize this logic.

Parameters:values (list) – A list of values that indicate the degree of the children expression.
Returns:A nonnegative integer that is the polynomial degree of the expression, or None. Default is None.
_is_fixed(args)[source]

Compute whether this expression is fixed given the fixed values of its children.

This method is called by the _IsFixedVisitor class. It can be over-written by expression classes to customize this logic.

Parameters:values (list) – A list of boolean values that indicate whether the children of this expression are fixed
Returns:A boolean that is True if the fixed values of the children are all True.
_precedence()[source]
_to_string(values, verbose, smap, compute_values)[source]

Construct a string representation for this node, using the string representations of its children.

This method is called by the _ToStringVisitor class. It must must be defined in subclasses.

Parameters:
  • values (list) – The string representations of the children of this node.
  • verbose (bool) – If True, then the the string representation consists of nested functions. Otherwise, the string representation is an algebraic equation.
  • smap – If specified, this SymbolMap is used to cache labels for variables.
  • compute_values (bool) – If True, then parameters and fixed variables are evaluated before the expression string is generated.
Returns:

A string representation for this node.

getname(*args, **kwds)[source]

Return the text name of a function associated with this expression object.

In general, no arguments are passed to this function.

Parameters:
  • *arg – a variable length list of arguments
  • **kwds – keyword arguments
Returns:

A string name for the function.

class pyomo.core.expr.current.DivisionExpression(args)[source]

Bases: ExpressionBase

Division expressions:

x/y
PRECEDENCE = 4
_apply_operation(result)[source]

Compute the values of this node given the values of its children.

This method is called by the _EvaluationVisitor class. It must be over-written by expression classes to customize this logic.

Note

This method applies the logical operation of the operator to the arguments. It does not evaluate the arguments in the process, but assumes that they have been previously evaluated. But noted that if this class contains auxilliary data (e.g. like the numeric coefficients in the LinearExpression class, then those values must be evaluated as part of this function call. An uninitialized parameter value encountered during the execution of this method is considered an error.

Parameters:values (list) – A list of values that indicate the value of the children expressions.
Returns:A floating point value for this expression.
_compute_polynomial_degree(result)[source]

Compute the polynomial degree of this expression given the degree values of its children.

This method is called by the _PolynomialDegreeVisitor class. It can be over-written by expression classes to customize this logic.

Parameters:values (list) – A list of values that indicate the degree of the children expression.
Returns:A nonnegative integer that is the polynomial degree of the expression, or None. Default is None.
_precedence()[source]
_to_string(values, verbose, smap, compute_values)[source]

Construct a string representation for this node, using the string representations of its children.

This method is called by the _ToStringVisitor class. It must must be defined in subclasses.

Parameters:
  • values (list) – The string representations of the children of this node.
  • verbose (bool) – If True, then the the string representation consists of nested functions. Otherwise, the string representation is an algebraic equation.
  • smap – If specified, this SymbolMap is used to cache labels for variables.
  • compute_values (bool) – If True, then parameters and fixed variables are evaluated before the expression string is generated.
Returns:

A string representation for this node.

getname(*args, **kwds)[source]

Return the text name of a function associated with this expression object.

In general, no arguments are passed to this function.

Parameters:
  • *arg – a variable length list of arguments
  • **kwds – keyword arguments
Returns:

A string name for the function.

nargs()[source]

Returns the number of child nodes.

By default, Pyomo expressions represent binary operations with two arguments.

Note

This function does not simply compute the length of _args_ because some expression classes use a subset of the _args_ array. Thus, it is imperative that developers use this method!

Returns:A nonnegative integer that is the number of child nodes.
class pyomo.core.expr.current.InequalityExpression(args, strict)[source]

Bases: _LinearOperatorExpression

Inequality expressions, which define less-than or less-than-or-equal relations:

x < y
x <= y
Parameters:
  • args (tuple) – child nodes
  • strict (bool) – a flag that indicates whether the inequality is strict
PRECEDENCE = 9
_apply_operation(result)[source]

Compute the values of this node given the values of its children.

This method is called by the _EvaluationVisitor class. It must be over-written by expression classes to customize this logic.

Note

This method applies the logical operation of the operator to the arguments. It does not evaluate the arguments in the process, but assumes that they have been previously evaluated. But noted that if this class contains auxilliary data (e.g. like the numeric coefficients in the LinearExpression class, then those values must be evaluated as part of this function call. An uninitialized parameter value encountered during the execution of this method is considered an error.

Parameters:values (list) – A list of values that indicate the value of the children expressions.
Returns:A floating point value for this expression.
_precedence()[source]
_strict
_to_string(values, verbose, smap, compute_values)[source]

Construct a string representation for this node, using the string representations of its children.

This method is called by the _ToStringVisitor class. It must must be defined in subclasses.

Parameters:
  • values (list) – The string representations of the children of this node.
  • verbose (bool) – If True, then the the string representation consists of nested functions. Otherwise, the string representation is an algebraic equation.
  • smap – If specified, this SymbolMap is used to cache labels for variables.
  • compute_values (bool) – If True, then parameters and fixed variables are evaluated before the expression string is generated.
Returns:

A string representation for this node.

create_node_with_local_data(args)[source]

Construct a node using given arguments.

This method provides a consistent interface for constructing a node, which is used in tree visitor scripts. In the simplest case, this simply returns:

self.__class__(args)

But in general this creates an expression object using local data as well as arguments that represent the child nodes.

Parameters:args (list) – A list of child nodes for the new expression object
Returns:A new expression object with the same type as the current class.
is_constant()[source]

Return True if this expression is an atomic constant

This method contrasts with the is_fixed() method. This method returns True if the expression is an atomic constant, that is it is composed exclusively of constants and immutable parameters. NumericValue objects returning is_constant() == True may be simplified to their numeric value at any point without warning.

Note: This defaults to False, but gets redefined in sub-classes.

is_potentially_variable()[source]

Return True if this expression might represent a variable expression.

This method returns True when (a) the expression tree contains one or more variables, or (b) the expression tree contains a named expression. In both cases, the expression cannot be treated as constant since (a) the variables may not be fixed, or (b) the named expressions may be changed at a later time to include non-fixed variables.

Returns:A boolean. Defaults to True for expressions.
is_relational()[source]

Return True if this numeric value represents a relational expression.

nargs()[source]

Returns the number of child nodes.

By default, Pyomo expressions represent binary operations with two arguments.

Note

This function does not simply compute the length of _args_ because some expression classes use a subset of the _args_ array. Thus, it is imperative that developers use this method!

Returns:A nonnegative integer that is the number of child nodes.
property strict
class pyomo.core.expr.current.EqualityExpression(args)[source]

Bases: _LinearOperatorExpression

Equality expression:

x == y
PRECEDENCE = 9
_apply_operation(result)[source]

Compute the values of this node given the values of its children.

This method is called by the _EvaluationVisitor class. It must be over-written by expression classes to customize this logic.

Note

This method applies the logical operation of the operator to the arguments. It does not evaluate the arguments in the process, but assumes that they have been previously evaluated. But noted that if this class contains auxilliary data (e.g. like the numeric coefficients in the LinearExpression class, then those values must be evaluated as part of this function call. An uninitialized parameter value encountered during the execution of this method is considered an error.

Parameters:values (list) – A list of values that indicate the value of the children expressions.
Returns:A floating point value for this expression.
_precedence()[source]
_to_string(values, verbose, smap, compute_values)[source]

Construct a string representation for this node, using the string representations of its children.

This method is called by the _ToStringVisitor class. It must must be defined in subclasses.

Parameters:
  • values (list) – The string representations of the children of this node.
  • verbose (bool) – If True, then the the string representation consists of nested functions. Otherwise, the string representation is an algebraic equation.
  • smap – If specified, this SymbolMap is used to cache labels for variables.
  • compute_values (bool) – If True, then parameters and fixed variables are evaluated before the expression string is generated.
Returns:

A string representation for this node.

is_constant()[source]

Return True if this expression is an atomic constant

This method contrasts with the is_fixed() method. This method returns True if the expression is an atomic constant, that is it is composed exclusively of constants and immutable parameters. NumericValue objects returning is_constant() == True may be simplified to their numeric value at any point without warning.

Note: This defaults to False, but gets redefined in sub-classes.

is_potentially_variable()[source]

Return True if this expression might represent a variable expression.

This method returns True when (a) the expression tree contains one or more variables, or (b) the expression tree contains a named expression. In both cases, the expression cannot be treated as constant since (a) the variables may not be fixed, or (b) the named expressions may be changed at a later time to include non-fixed variables.

Returns:A boolean. Defaults to True for expressions.
is_relational()[source]

Return True if this numeric value represents a relational expression.

nargs()[source]

Returns the number of child nodes.

By default, Pyomo expressions represent binary operations with two arguments.

Note

This function does not simply compute the length of _args_ because some expression classes use a subset of the _args_ array. Thus, it is imperative that developers use this method!

Returns:A nonnegative integer that is the number of child nodes.
class pyomo.core.expr.current.SumExpression(args)[source]

Bases: SumExpressionBase

Sum expression:

x + y
Parameters:args (list) – Children nodes
PRECEDENCE = 6
_apply_operation(result)[source]

Compute the values of this node given the values of its children.

This method is called by the _EvaluationVisitor class. It must be over-written by expression classes to customize this logic.

Note

This method applies the logical operation of the operator to the arguments. It does not evaluate the arguments in the process, but assumes that they have been previously evaluated. But noted that if this class contains auxilliary data (e.g. like the numeric coefficients in the LinearExpression class, then those values must be evaluated as part of this function call. An uninitialized parameter value encountered during the execution of this method is considered an error.

Parameters:values (list) – A list of values that indicate the value of the children expressions.
Returns:A floating point value for this expression.
_nargs
_precedence()[source]
_shared_args
_to_string(values, verbose, smap, compute_values)[source]

Construct a string representation for this node, using the string representations of its children.

This method is called by the _ToStringVisitor class. It must must be defined in subclasses.

Parameters:
  • values (list) – The string representations of the children of this node.
  • verbose (bool) – If True, then the the string representation consists of nested functions. Otherwise, the string representation is an algebraic equation.
  • smap – If specified, this SymbolMap is used to cache labels for variables.
  • compute_values (bool) – If True, then parameters and fixed variables are evaluated before the expression string is generated.
Returns:

A string representation for this node.

add(new_arg)[source]
create_node_with_local_data(args, classtype=None)[source]

Construct a node using given arguments.

This method provides a consistent interface for constructing a node, which is used in tree visitor scripts. In the simplest case, this simply returns:

self.__class__(args)

But in general this creates an expression object using local data as well as arguments that represent the child nodes.

Parameters:args (list) – A list of child nodes for the new expression object
Returns:A new expression object with the same type as the current class.
is_constant()[source]

Return True if this expression is an atomic constant

This method contrasts with the is_fixed() method. This method returns True if the expression is an atomic constant, that is it is composed exclusively of constants and immutable parameters. NumericValue objects returning is_constant() == True may be simplified to their numeric value at any point without warning.

Note: This defaults to False, but gets redefined in sub-classes.

is_potentially_variable()[source]

Return True if this expression might represent a variable expression.

This method returns True when (a) the expression tree contains one or more variables, or (b) the expression tree contains a named expression. In both cases, the expression cannot be treated as constant since (a) the variables may not be fixed, or (b) the named expressions may be changed at a later time to include non-fixed variables.

Returns:A boolean. Defaults to True for expressions.
nargs()[source]

Returns the number of child nodes.

By default, Pyomo expressions represent binary operations with two arguments.

Note

This function does not simply compute the length of _args_ because some expression classes use a subset of the _args_ array. Thus, it is imperative that developers use this method!

Returns:A nonnegative integer that is the number of child nodes.
class pyomo.core.expr.current.GetItemExpression(args)[source]

Bases: ExpressionBase

Expression to call __getitem__() on the base object.

PRECEDENCE = 1
_apply_operation(result)[source]

Compute the values of this node given the values of its children.

This method is called by the _EvaluationVisitor class. It must be over-written by expression classes to customize this logic.

Note

This method applies the logical operation of the operator to the arguments. It does not evaluate the arguments in the process, but assumes that they have been previously evaluated. But noted that if this class contains auxilliary data (e.g. like the numeric coefficients in the LinearExpression class, then those values must be evaluated as part of this function call. An uninitialized parameter value encountered during the execution of this method is considered an error.

Parameters:values (list) – A list of values that indicate the value of the children expressions.
Returns:A floating point value for this expression.
_args_
_compute_polynomial_degree(result)[source]

Compute the polynomial degree of this expression given the degree values of its children.

This method is called by the _PolynomialDegreeVisitor class. It can be over-written by expression classes to customize this logic.

Parameters:values (list) – A list of values that indicate the degree of the children expression.
Returns:A nonnegative integer that is the polynomial degree of the expression, or None. Default is None.
_is_fixed(values)[source]

Compute whether this expression is fixed given the fixed values of its children.

This method is called by the _IsFixedVisitor class. It can be over-written by expression classes to customize this logic.

Parameters:values (list) – A list of boolean values that indicate whether the children of this expression are fixed
Returns:A boolean that is True if the fixed values of the children are all True.
_precedence()[source]
_resolve_template(args)[source]
_to_string(values, verbose, smap, compute_values)[source]

Construct a string representation for this node, using the string representations of its children.

This method is called by the _ToStringVisitor class. It must must be defined in subclasses.

Parameters:
  • values (list) – The string representations of the children of this node.
  • verbose (bool) – If True, then the the string representation consists of nested functions. Otherwise, the string representation is an algebraic equation.
  • smap – If specified, this SymbolMap is used to cache labels for variables.
  • compute_values (bool) – If True, then parameters and fixed variables are evaluated before the expression string is generated.
Returns:

A string representation for this node.

getname(*args, **kwds)[source]

Return the text name of a function associated with this expression object.

In general, no arguments are passed to this function.

Parameters:
  • *arg – a variable length list of arguments
  • **kwds – keyword arguments
Returns:

A string name for the function.

is_potentially_variable()[source]

Return True if this expression might represent a variable expression.

This method returns True when (a) the expression tree contains one or more variables, or (b) the expression tree contains a named expression. In both cases, the expression cannot be treated as constant since (a) the variables may not be fixed, or (b) the named expressions may be changed at a later time to include non-fixed variables.

Returns:A boolean. Defaults to True for expressions.
nargs()[source]

Returns the number of child nodes.

By default, Pyomo expressions represent binary operations with two arguments.

Note

This function does not simply compute the length of _args_ because some expression classes use a subset of the _args_ array. Thus, it is imperative that developers use this method!

Returns:A nonnegative integer that is the number of child nodes.
class pyomo.core.expr.current.Expr_ifExpression(IF_=None, THEN_=None, ELSE_=None)[source]

Bases: ExpressionBase

A logical if-then-else expression:

Expr_if(IF_=x, THEN_=y, ELSE_=z)
Parameters:
  • IF (expression) – A relational expression
  • THEN (expression) – An expression that is used if IF_ is true.
  • ELSE (expression) – An expression that is used if IF_ is false.
_apply_operation(result)[source]

Compute the values of this node given the values of its children.

This method is called by the _EvaluationVisitor class. It must be over-written by expression classes to customize this logic.

Note

This method applies the logical operation of the operator to the arguments. It does not evaluate the arguments in the process, but assumes that they have been previously evaluated. But noted that if this class contains auxilliary data (e.g. like the numeric coefficients in the LinearExpression class, then those values must be evaluated as part of this function call. An uninitialized parameter value encountered during the execution of this method is considered an error.

Parameters:values (list) – A list of values that indicate the value of the children expressions.
Returns:A floating point value for this expression.
_compute_polynomial_degree(result)[source]

Compute the polynomial degree of this expression given the degree values of its children.

This method is called by the _PolynomialDegreeVisitor class. It can be over-written by expression classes to customize this logic.

Parameters:values (list) – A list of values that indicate the degree of the children expression.
Returns:A nonnegative integer that is the polynomial degree of the expression, or None. Default is None.
_else
_if
_is_fixed(args)[source]

Compute whether this expression is fixed given the fixed values of its children.

This method is called by the _IsFixedVisitor class. It can be over-written by expression classes to customize this logic.

Parameters:values (list) – A list of boolean values that indicate whether the children of this expression are fixed
Returns:A boolean that is True if the fixed values of the children are all True.
_then
_to_string(values, verbose, smap, compute_values)[source]

Construct a string representation for this node, using the string representations of its children.

This method is called by the _ToStringVisitor class. It must must be defined in subclasses.

Parameters:
  • values (list) – The string representations of the children of this node.
  • verbose (bool) – If True, then the the string representation consists of nested functions. Otherwise, the string representation is an algebraic equation.
  • smap – If specified, this SymbolMap is used to cache labels for variables.
  • compute_values (bool) – If True, then parameters and fixed variables are evaluated before the expression string is generated.
Returns:

A string representation for this node.

getname(*args, **kwds)[source]

Return the text name of a function associated with this expression object.

In general, no arguments are passed to this function.

Parameters:
  • *arg – a variable length list of arguments
  • **kwds – keyword arguments
Returns:

A string name for the function.

is_constant()[source]

Return True if this expression is an atomic constant

This method contrasts with the is_fixed() method. This method returns True if the expression is an atomic constant, that is it is composed exclusively of constants and immutable parameters. NumericValue objects returning is_constant() == True may be simplified to their numeric value at any point without warning.

Note: This defaults to False, but gets redefined in sub-classes.

is_potentially_variable()[source]

Return True if this expression might represent a variable expression.

This method returns True when (a) the expression tree contains one or more variables, or (b) the expression tree contains a named expression. In both cases, the expression cannot be treated as constant since (a) the variables may not be fixed, or (b) the named expressions may be changed at a later time to include non-fixed variables.

Returns:A boolean. Defaults to True for expressions.
nargs()[source]

Returns the number of child nodes.

By default, Pyomo expressions represent binary operations with two arguments.

Note

This function does not simply compute the length of _args_ because some expression classes use a subset of the _args_ array. Thus, it is imperative that developers use this method!

Returns:A nonnegative integer that is the number of child nodes.
class pyomo.core.expr.current.UnaryFunctionExpression(args, name=None, fcn=None)[source]

Bases: ExpressionBase

An expression object used to define intrinsic functions (e.g. sin, cos, tan).

Parameters:
  • args (tuple) – Children nodes
  • name (string) – The function name
  • fcn – The function that is used to evaluate this expression
_apply_operation(result)[source]

Compute the values of this node given the values of its children.

This method is called by the _EvaluationVisitor class. It must be over-written by expression classes to customize this logic.

Note

This method applies the logical operation of the operator to the arguments. It does not evaluate the arguments in the process, but assumes that they have been previously evaluated. But noted that if this class contains auxilliary data (e.g. like the numeric coefficients in the LinearExpression class, then those values must be evaluated as part of this function call. An uninitialized parameter value encountered during the execution of this method is considered an error.

Parameters:values (list) – A list of values that indicate the value of the children expressions.
Returns:A floating point value for this expression.
_compute_polynomial_degree(result)[source]

Compute the polynomial degree of this expression given the degree values of its children.

This method is called by the _PolynomialDegreeVisitor class. It can be over-written by expression classes to customize this logic.

Parameters:values (list) – A list of values that indicate the degree of the children expression.
Returns:A nonnegative integer that is the polynomial degree of the expression, or None. Default is None.
_fcn
_name
_to_string(values, verbose, smap, compute_values)[source]

Construct a string representation for this node, using the string representations of its children.

This method is called by the _ToStringVisitor class. It must must be defined in subclasses.

Parameters:
  • values (list) – The string representations of the children of this node.
  • verbose (bool) – If True, then the the string representation consists of nested functions. Otherwise, the string representation is an algebraic equation.
  • smap – If specified, this SymbolMap is used to cache labels for variables.
  • compute_values (bool) – If True, then parameters and fixed variables are evaluated before the expression string is generated.
Returns:

A string representation for this node.

create_node_with_local_data(args, classtype=None)[source]

Construct a node using given arguments.

This method provides a consistent interface for constructing a node, which is used in tree visitor scripts. In the simplest case, this simply returns:

self.__class__(args)

But in general this creates an expression object using local data as well as arguments that represent the child nodes.

Parameters:args (list) – A list of child nodes for the new expression object
Returns:A new expression object with the same type as the current class.
getname(*args, **kwds)[source]

Return the text name of a function associated with this expression object.

In general, no arguments are passed to this function.

Parameters:
  • *arg – a variable length list of arguments
  • **kwds – keyword arguments
Returns:

A string name for the function.

nargs()[source]

Returns the number of child nodes.

By default, Pyomo expressions represent binary operations with two arguments.

Note

This function does not simply compute the length of _args_ because some expression classes use a subset of the _args_ array. Thus, it is imperative that developers use this method!

Returns:A nonnegative integer that is the number of child nodes.
class pyomo.core.expr.current.AbsExpression(arg)[source]

Bases: UnaryFunctionExpression

An expression object for the abs() function.

Parameters:args (tuple) – Children nodes
create_node_with_local_data(args, classtype=None)[source]

Construct a node using given arguments.

This method provides a consistent interface for constructing a node, which is used in tree visitor scripts. In the simplest case, this simply returns:

self.__class__(args)

But in general this creates an expression object using local data as well as arguments that represent the child nodes.

Parameters:args (list) – A list of child nodes for the new expression object
Returns:A new expression object with the same type as the current class.