Visitor Classes

class pyomo.core.expr.current.SimpleExpressionVisitor[source]

Note

This class is a customization of the PyUtilib SimpleVisitor class that is tailored to efficiently walk Pyomo expression trees. However, this class is not a subclass of the PyUtilib SimpleVisitor class because all key methods are reimplemented.

finalize()[source]

Return the “final value” of the search.

The default implementation returns None, because the traditional visitor pattern does not return a value.

Returns:The final value after the search. Default is None.
visit(node)[source]

Visit a node in an expression tree and perform some operation on it.

This method should be over-written by a user that is creating a sub-class.

Parameters:node – a node in an expression tree
Returns:nothing
xbfs(node)[source]

Breadth-first search of an expression tree, except that leaf nodes are immediately visited.

Note

This method has the same functionality as the PyUtilib SimpleVisitor.xbfs method. The difference is that this method is tailored to efficiently walk Pyomo expression trees.

Parameters:node – The root node of the expression tree that is searched.
Returns:The return value is determined by the finalize() function, which may be defined by the user. Defaults to None.
xbfs_yield_leaves(node)[source]

Breadth-first search of an expression tree, except that leaf nodes are immediately visited.

Note

This method has the same functionality as the PyUtilib SimpleVisitor.xbfs_yield_leaves method. The difference is that this method is tailored to efficiently walk Pyomo expression trees.

Parameters:node – The root node of the expression tree that is searched.
Returns:The return value is determined by the finalize() function, which may be defined by the user. Defaults to None.
class pyomo.core.expr.current.ExpressionValueVisitor[source]

Note

This class is a customization of the PyUtilib ValueVisitor class that is tailored to efficiently walk Pyomo expression trees. However, this class is not a subclass of the PyUtilib ValueVisitor class because all key methods are reimplemented.

dfs_postorder_stack(node)[source]

Perform a depth-first search in postorder using a stack implementation.

Note

This method has the same functionality as the PyUtilib ValueVisitor.dfs_postorder_stack method. The difference is that this method is tailored to efficiently walk Pyomo expression trees.

Parameters:node – The root node of the expression tree that is searched.
Returns:The return value is determined by the finalize() function, which may be defined by the user.
finalize(ans)[source]

This method defines the return value for the search methods in this class.

The default implementation returns the value of the initial node (aka the root node), because this visitor pattern computes and returns value for each node to enable the computation of this value.

Parameters:ans – The final value computed by the search method.
Returns:The final value after the search. Defaults to simply returning ans.
visit(node, values)[source]

Visit a node in a tree and compute its value using the values of its children.

This method should be over-written by a user that is creating a sub-class.

Parameters:
  • node – a node in a tree
  • values – a list of values of this node’s children
Returns:

The value for this node, which is computed using values

visiting_potential_leaf(node)[source]

Visit a node and return its value if it is a leaf.

Note

This method needs to be over-written for a specific visitor application.

Parameters:node – a node in a tree
Returns:(flag, value). If flag is False, then the node is not a leaf and value is None. Otherwise, value is the computed value for this node.
Return type:A tuple
class pyomo.core.expr.current.ExpressionReplacementVisitor(substitute=None, descend_into_named_expressions=True, remove_named_expressions=True)[source]
dfs_postorder_stack(expr)[source]

DEPRECATED.

Deprecated since version 6.2: ExpressionReplacementVisitor: this walker has been ported to derive from StreamBasedExpressionVisitor. dfs_postorder_stack() has been replaced with walk_expression()