-
Notifications
You must be signed in to change notification settings - Fork 579
Support a reusable 'var_cache' within the IdentifyVariableVisitor
#3966
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
Open
jsiirola
wants to merge
5
commits into
Pyomo:main
Choose a base branch
from
jsiirola:identify_vars_seen_cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+188
−22
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9b4265c
Support a reusable 'seen' cache within the IdentifyVariableVisitor
jsiirola 3dd8245
NFC: fix typo
jsiirola 037981a
Fix import, add tests
jsiirola 01b9e7b
Rename 'seen' -> 'var_cache' for clarity/consistency
jsiirola 33064ab
Only return newly-found variables from IdentifyVariableVisitor.walk_e…
jsiirola File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # ____________________________________________________________________________________ | ||
| # | ||
| # Pyomo: Python Optimization Modeling Objects | ||
| # Copyright (c) 2008-2026 National Technology and Engineering Solutions of Sandia, LLC | ||
| # Under the terms of Contract DE-NA0003525 with National Technology and Engineering | ||
| # Solutions of Sandia, LLC, the U.S. Government retains certain rights in this | ||
| # software. This software is distributed under the 3-clause BSD License. | ||
| # ____________________________________________________________________________________ | ||
|
|
||
| import pyomo.environ as pyo | ||
| from pyomo.common import unittest | ||
| from pyomo.util.vars_from_expressions import get_vars, get_vars_from_components | ||
|
|
||
|
|
||
| class TestVarsFromExpressions(unittest.TestCase): | ||
| def test_get_vars(self): | ||
| m = pyo.ConcreteModel() | ||
| m.x = pyo.Var(list(range(5))) | ||
| m.c1 = pyo.Constraint(expr=m.x[0] + m.x[1] == 0) | ||
| m.c2 = pyo.Constraint(expr=m.x[1] + m.x[2] == 0) | ||
| m.obj = pyo.Objective(expr=m.x[3] + m.x[4]) | ||
|
|
||
| self.assertEqual(list(get_vars(m)), [m.x[0], m.x[1], m.x[2], m.x[3], m.x[4]]) | ||
| # verify the default values for active and include_fixed | ||
| m.x[0].fix(0) | ||
| m.c2.deactivate() | ||
| self.assertEqual(list(get_vars(m)), [m.x[1], m.x[3], m.x[4]]) | ||
|
|
||
| def test_get_vars_from_components(self): | ||
| m = pyo.ConcreteModel() | ||
| m.x = pyo.Var(list(range(5))) | ||
| m.c1 = pyo.Constraint(expr=m.x[0] + m.x[1] == 0) | ||
| m.c2 = pyo.Constraint(expr=m.x[1] + m.x[2] == 0) | ||
| m.obj = pyo.Objective(expr=m.x[3] + m.x[4]) | ||
|
|
||
| self.assertEqual( | ||
| list(get_vars_from_components(m, pyo.Constraint)), [m.x[0], m.x[1], m.x[2]] | ||
| ) | ||
| self.assertEqual( | ||
| list(get_vars_from_components(m, pyo.Objective)), [m.x[3], m.x[4]] | ||
| ) | ||
| self.assertEqual( | ||
| list(get_vars_from_components(m, (pyo.Constraint, pyo.Objective))), | ||
| [m.x[0], m.x[1], m.x[2], m.x[3], m.x[4]], | ||
| ) | ||
|
|
||
| # verify the default values for active and include_fixed | ||
| m.x[0].fix(0) | ||
| m.c2.deactivate() | ||
| self.assertEqual( | ||
| list(get_vars_from_components(m, pyo.Constraint)), [m.x[0], m.x[1], m.x[2]] | ||
| ) | ||
| self.assertEqual( | ||
| list(get_vars_from_components(m, pyo.Objective)), [m.x[3], m.x[4]] | ||
| ) | ||
| self.assertEqual( | ||
| list(get_vars_from_components(m, (pyo.Constraint, pyo.Objective))), | ||
| [m.x[0], m.x[1], m.x[2], m.x[3], m.x[4]], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use a component set?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly for historical reasons? I think that
identify_variablesactually predated the modern implementation ofComponentSet, and I was trying to do a minimal change here.We should consider reworking the implementation to be based on ComponentSet. There might be a slight overhead increase (I am guessing the cost of an extra function call per leaf node) - we should benchmark that. (Although I am not expecting it to show up, as the core things like model generation, compilation, and writers do not use it).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A quick test with PMedian (test8) shows that using a ComponentSet within
identify_variablesis 10-12% slower. One possibility is that we could advertise that the walker takes a ComponentSet, but internally we operate directly on the underlying_datadictionary?