-
Notifications
You must be signed in to change notification settings - Fork 525
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
Expanded expression template support #1433
Conversation
This adds an explicit `_len` attribute so that _IndexedComponent_slice instances may share _call_stack lists without accidentally recording side effects.
This allows Expression nodes to hook into the enterNode/ExitNode events by providing an `args` that implements a context manager API.
Codecov Report
@@ Coverage Diff @@
## master #1433 +/- ##
==========================================
- Coverage 71.63% 69.26% -2.38%
==========================================
Files 590 596 +6
Lines 83042 85026 +1984
==========================================
- Hits 59491 58897 -594
- Misses 23551 26129 +2578
Continue to review full report at Codecov.
|
@jsiirola I can't seem to track down documentation on how the template expressions work. Do you happen to know where that lives? |
@qtothec, alas there is very little formal documentation written up (in part because things are still evolving). The short version is a "template" expression is just an extended expression tree; that is normal expression tree that also contains |
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.
This looks reasonable to me. I like the idea of a common base class where the is_... methods are documented - it makes the interfaces clearer.
pyomo/core/expr/numvalue.py
Outdated
@@ -532,7 +532,36 @@ def check_if_numeric_type_and_cache(obj): | |||
return retval | |||
|
|||
|
|||
class NumericValue(object): | |||
|
|||
class PyomoModelingObject(object): |
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.
I like this concept, not sure about the name. How about just PyomoObject? Can we also move this to a separate file - it is the base of all pyomo objects - components, expression nodes, etc.
""" | ||
return False | ||
|
||
def is_expression_type(self): |
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.
I find the name of this method confusing - this is a separate conversation, but now that we have a base class PyomoModelObject, we can have a discussion about the names and intent of these methods.
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.
Good to me if tests pass.
Fixes #N/A
(but provides the first steps for #752 and #1153)
Summary/Motivation:
This PR expands the template system to support hierarchical models and embedded sum expressions. Templates can now be formed for hierarchical models where the IndexTemplate(s) appear in the blocks and not just in Var/Param components. This also adds support for generating unexpanded templates of expressions containing summations. For example, considering the following "complex" rule:
The following now works:
To implement this, the PR proposes several core changes:
Add a new
PyomoModelingObject
base class. This slotized interface is a new common base class forNumericValue
and_ComponentBase
class hierarchies and defines the fundamentalis_*_type
methods (all of which return False). By implementing this as a common base class, it makes it easier to support classes that "blur" the distinction between numeric types, component types, and expression types. This declares the minimal set of methods that Pyomo classes (that is, classes appearing in models / expression trees that are not innative_types
) are expected to implement. These are:is_expression_type()
: True if this object is a non-leaf expression node. If True, the object is expected to implement theExpressionBase
API.is_numeric_type()
: True if the object can represent a numeric value. If True, then this object is expected to implement theNumericValue
API.is_component_type()
: True if this object is a Pyomo component or ComponentData. If True, the object is expected to implement the_ComponentBase
API.is variable_type()
: Preserved for backwards compatibility. A future PR will look at the possibility of deprecating / removing this method.is_parameter_type()
: Preserved for backwards compatibility. It is currently not used anywhere in the codebase, and will likely be deprecated in a future PR.is_named_expression_type()
: Preserved for backwards compatibility. A future PR will look at the possibility of deprecating / removing this method.Expanding the
StreamBasedExpressionVisitor
. This adds a newinitializeWalker
callback, and expands the API for thebeforeChild
,acceptChildResult
, andafterChild
methods to also include the index of the child node in the parent node's args list. The walker also allows nodes to return args that implement a context manager api (without forcing the walker to defineenterNode
&exitNode
).Expanded
IndexedComponent_slice
class. This class is now "public", more robust, and hashable (can be a key in adict
)Changes proposed in this PR:
StreamBasedExpressionVisitor
beforeChild
,acceptChildResult
, andafterChild
methodsinitializeWalker
callbackargs
data_IndexedComponent_slice
_IndexedComponent_slice
a "public" class (rename, dropping the leading_
)IndexedComponent_slice
objects asdict
keysIndexedComponent_slice
internal data structures to make them less susceptible to unintended side effects by borrowing concepts from SumExpression views.PyomoModelingObject
base class from which bothNumericValue
and_ComponentBase
derive.is_*_type
methods._UnindexedComponent_set
class with additional methods from the Set API_iter_impl
instead of directly overloading__iter__
.pyomo.core.expr.template_expr
(deprecatepyomo.core.base.template_expr
)GetAttrExpression
andTempateSumExpression
expression nodesresolve_template()
, andtemplatize_constraint()
methodsLegal Acknowledgement
By contributing to this software project, I have read the contribution guide and agree to the following terms and conditions for my contribution: