Visitor Classes
- class pyomo.core.expr.SimpleExpressionVisitor[source]
Note
This class is a customization of the PyUtilib
SimpleVisitorclass that is tailored to efficiently walk Pyomo expression trees. However, this class is not a subclass of the PyUtilibSimpleVisitorclass 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.xbfsmethod. 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]
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_leavesmethod. 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.ExpressionValueVisitor[source]
Note
This class is a customization of the PyUtilib
ValueVisitorclass that is tailored to efficiently walk Pyomo expression trees. However, this class is not a subclass of the PyUtilibValueVisitorclass 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_stackmethod. 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). Ifflagis False, then the node is not a leaf andvalueisNone. Otherwise,valueis the computed value for this node.- Return type:
A tuple