Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up identify_variables / identify_mutable_parameters; deprecate SimpleExpressionVisitor #3436

Merged
merged 23 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b68a632
Simplify IdentifyVariableVisitor; improve cache robustness
jsiirola Nov 26, 2024
0f0f115
Move identify_mutable_parameters to build on identify_variables
jsiirola Nov 26, 2024
7a7d9e5
NonNumericValue should support the is_constant()/is_fixed() API
jsiirola Nov 26, 2024
ecad6ac
Move identify_components to use StreamBasedExpressionVisitor
jsiirola Nov 26, 2024
81ee01f
Fix bug in SimpleExpressionVisitor when passing a native_type as the …
jsiirola Nov 26, 2024
c5fceea
Deprecate SImpleExpressionVisitor
jsiirola Nov 26, 2024
46394f9
Add identify_variables tests for nested named expressions
jsiirola Nov 26, 2024
105fbca
Add basic tests for (deprecated) SimpleExpressionVisitor
jsiirola Nov 26, 2024
7de8b98
Improve efficiency / caching in get_vars_from_components()
jsiirola Nov 26, 2024
e05cba5
Merge branch 'main' into identify-vars
jsiirola Nov 26, 2024
2dd0f5e
NFC: fix typo
jsiirola Nov 26, 2024
ff03fe0
Merge branch 'main' into identify-vars
jsiirola Dec 2, 2024
ac92c1e
NFC: fix doc typo
jsiirola Dec 10, 2024
b9aee5b
Merge branch 'main' into identify-vars
jsiirola Jan 30, 2025
ea4fc58
Merge branch 'main' into identify-vars
jsiirola Jan 30, 2025
a3aa203
Use dict not list to store nested named expressions
jsiirola Jan 31, 2025
f592eae
Improve efficiency of identify_vars data structures
jsiirola Jan 31, 2025
2cddb8a
Merge remote-tracking branch 'refs/remotes/me/identify-vars' into ide…
jsiirola Feb 4, 2025
d75bc92
Update example to match documentation text
jsiirola Feb 4, 2025
2e85c53
NFC: fix typo
jsiirola Feb 4, 2025
258a112
NFC: apply black
jsiirola Feb 4, 2025
db9c375
Merge branch 'main' into identify-vars
blnicho Feb 5, 2025
8d1fff7
Merge branch 'main' into identify-vars
mrmundt Feb 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 12 additions & 25 deletions doc/OnlineDocs/explanation/philosophy/expressions/managing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,6 @@ tree:
seven event callbacks that users can hook into, providing very
fine-grained control over the expression walker.

:class:`SimpleExpressionVisitor <pyomo.core.expr.SimpleExpressionVisitor>`
A :func:`visitor` method is called for each node in the tree,
and the visitor class collects information about the tree.

:class:`ExpressionValueVisitor <pyomo.core.expr.ExpressionValueVisitor>`
When the :func:`visitor` method is called on each node in the
tree, the *values* of its children have been computed. The
Expand All @@ -166,25 +162,18 @@ These classes define a variety of suitable tree search methods:

* ``walk_expression``: depth-first traversal of the expression tree.

* :class:`SimpleExpressionVisitor <pyomo.core.expr.SimpleExpressionVisitor>`

* ``xbfs``: breadth-first search where leaf nodes are immediately visited
* ``xbfs_yield_leaves``: breadth-first search where leaf nodes are
immediately visited, and the visit method yields a value

* :class:`ExpressionValueVisitor <pyomo.core.expr.ExpressionValueVisitor>`

* ``dfs_postorder_stack``: postorder depth-first search using a
nonrecursive stack


To implement a visitor object, a user needs to provide specializations
for specific events. For legacy visitors based on the PyUtilib
visitor pattern (e.g., :class:`SimpleExpressionVisitor` and
:class:`ExpressionValueVisitor`), one must create a subclass of one of these
classes and override at least one of the following:
for specific events. For legacy visitors based on the PyUtilib visitor
pattern (e.g., :class:`ExpressionValueVisitor`), one must create a
subclass and override at least one of the following:

:func:`visitor`
:func:`visit`
Defines the operation that is performed when a node is visited. In
the :class:`ExpressionValueVisitor
<pyomo.core.expr.ExpressionValueVisitor>` and
Expand All @@ -196,10 +185,7 @@ classes and override at least one of the following:
Checks if the search should terminate with this node. If no,
then this method returns the tuple ``(False, None)``. If yes,
then this method returns ``(False, value)``, where *value* is
computed by this method. This method is not used in the
:class:`SimpleExpressionVisitor
<pyomo.core.expr.SimpleExpressionVisitor>` visitor
class.
computed by this method.

:func:`finalize`
This method defines the final value that is returned from the
Expand All @@ -216,19 +202,20 @@ callbacks, which are documented in the class documentation.
Detailed documentation of the APIs for these methods is provided
with the class documentation for these visitors.

SimpleExpressionVisitor Example
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
StreamBasedExpressionVisitor Example
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In this example, we describe an visitor class that counts the number
of nodes in an expression (including leaf nodes). Consider the following
class:

.. literalinclude:: /src/expr/managing_visitor1.spy
jsiirola marked this conversation as resolved.
Show resolved Hide resolved

The class constructor creates a counter, and the :func:`visit` method
increments this counter for every node that is visited. The :func:`finalize`
method returns the value of this counter after the tree has been walked. The
following function illustrates this use of this visitor class:
The :func:`initializeWalker` method creates a counter, and the
:func:`exitNode` method increments this counter for every node that is
visited. The :func:`finalizeResult` method returns the value of this
jsiirola marked this conversation as resolved.
Show resolved Hide resolved
counter after the tree has been walked. The following function
illustrates this use of this visitor class:

.. literalinclude:: /src/expr/managing_visitor2.spy

Expand Down
1 change: 0 additions & 1 deletion doc/OnlineDocs/reference/topical/expressions/visitors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ Visitor Classes
.. autosummary::

pyomo.core.expr.StreamBasedExpressionVisitor
pyomo.core.expr.SimpleExpressionVisitor
pyomo.core.expr.ExpressionValueVisitor
pyomo.core.expr.ExpressionReplacementVisitor
6 changes: 6 additions & 0 deletions pyomo/core/expr/numvalue.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ def __repr__(self):
def __call__(self, exception=None):
return self.value

def is_constant(self):
return True

def is_fixed(self):
return True


nonpyomo_leaf_types.add(NonNumericValue)

Expand Down
Loading
Loading