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 PyUtilibSimpleVisitor
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 overwritten by a user that is creating a subclass.
Parameters: node – a node in an expression tree Returns: nothing

xbfs
(node)[source]¶ Breadthfirst 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 toNone
.

xbfs_yield_leaves
(node)[source]¶ Breadthfirst 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 toNone
.


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 PyUtilibValueVisitor
class because all key methods are reimplemented.
dfs_postorder_stack
(node)[source]¶ Perform a depthfirst 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 overwritten by a user that is creating a subclass.
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 overwritten for a specific visitor application.
Parameters: node – a node in a tree Returns: (flag, value)
. Ifflag
is False, then the node is not a leaf andvalue
isNone
. 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=False)[source]¶ Note
This class is a customization of the PyUtilib
ValueVisitor
class that is tailored to support replacement of subtrees in a Pyomo expression tree. However, this class is not a subclass of the PyUtilibValueVisitor
class because all key methods are reimplemented.
dfs_postorder_stack
(node)[source]¶ Perform a depthfirst search in postorder using a stack implementation.
This method replaces subtrees. This method detects if the
visit()
method returns a different object. If so, then the node has been replaced and search process is adapted to replace all subsequent parent nodes in the tree.Note
This method has the same functionality as the PyUtilib
ValueVisitor.dfs_postorder_stack
method that is tailored to support the replacement of subtrees in a Pyomo expression tree.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 and clone nodes that have been expanded.
Note
This method normally does not need to be redefined by a user.
Parameters:  node – The node that will be cloned.
 values (list) – The list of child nodes that have been cloned. These values are used to define the cloned node.
Returns: The cloned node. Default is to simply return the node.

visiting_potential_leaf
(node)[source]¶ Visit a node and return a cloned node if it is a leaf.
Note
This method needs to be overwritten for a specific visitor application.
Parameters: node – a node in a tree Returns: (flag, value)
. Ifflag
is False, then the node is not a leaf andvalue
isNone
. Otherwise,value
is a cloned node.Return type: A tuple
