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

Add active filter to flattener #2643

Merged
merged 27 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3a9a4e2
apply jsiirolas patch in #2529 to flatten.py
Robbybp Nov 30, 2022
e8343e6
add active flag to flatten_components_along_sets
Robbybp Dec 1, 2022
f63deea
add active flag to flatten_dae_components
Robbybp Dec 1, 2022
d752461
start some documentation for flattener
Robbybp Dec 4, 2022
ee21825
improve docstrings
Robbybp Dec 4, 2022
f73d821
flattener api reference documentation
Robbybp Dec 4, 2022
f031e1d
add doc on motivation for flattener
Robbybp Dec 4, 2022
2224c22
start tests for flattener with deactivated blocks
Robbybp Dec 4, 2022
254da6c
uncomment code testing with active=False
Robbybp Dec 4, 2022
ff1cbd9
only pass active flag to component_objects if ctype is an ActiveCompo…
Robbybp Dec 4, 2022
4d8f911
use common function to build model in tests
Robbybp Dec 7, 2022
6a15bf9
put hashref method on common base class
Robbybp Dec 7, 2022
803872d
make sure slices have at least one active data before yielding
Robbybp Dec 7, 2022
a71f770
tests for partially deactived slices and constraints
Robbybp Dec 7, 2022
4569bb7
clarify how active is handled for slices
Robbybp Dec 7, 2022
c8ba427
Merge branch 'main' of https://github.com/pyomo/pyomo into flatten-ac…
Robbybp Dec 7, 2022
478f836
do not attempt to iterate if no slicing has been performed
Robbybp Dec 7, 2022
5d607e3
make sure we sliced at least one set before attempting to duplicate
Robbybp Dec 8, 2022
9230de7
tests to exercise generating scalar constraints with active flag
Robbybp Dec 8, 2022
4df9b31
Merge branch 'main' into flatten-active-blocks
Robbybp Dec 9, 2022
6d10061
Merge branch 'main' into flatten-active-blocks
Robbybp Jan 17, 2023
242c4cb
Merge branch 'main' into flatten-active-blocks
blnicho Jan 27, 2023
a416b24
Merge branch 'main' into flatten-active-blocks
Robbybp Feb 1, 2023
a3a367a
Merge branch 'main' into flatten-active-blocks
Robbybp Feb 7, 2023
d2fe6e3
Merge branch 'main' into flatten-active-blocks
blnicho Feb 13, 2023
3948155
Merge branch 'main' into flatten-active-blocks
Robbybp Feb 14, 2023
dbfd792
Addressing reviewer comments
blnicho Feb 14, 2023
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
44 changes: 44 additions & 0 deletions doc/OnlineDocs/advanced_topics/flattener/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"Flattening" a Pyomo model
==========================

.. autosummary::

pyomo.dae.flatten

.. toctree::
:maxdepth: 1

motivation.rst
reference.rst

What does it mean to flatten a model?
-------------------------------------
When accessing components in a block-structured model, we use
``component_objects`` or ``component_data_objects`` to access all objects
of a specific ``Component`` or ``ComponentData`` type.
The generated objects may be thought of as a "flattened" representation
of the model, as they may be accessed without any knowledge of the model's
block structure.
These methods are very useful, but it is still challenging to use them
to access specific components.
Specifically, we often want to access "all components indexed by some set,"
or "all component data at a particular index of this set."
In addition, we often want to generate even the components in a block that
Robbybp marked this conversation as resolved.
Show resolved Hide resolved
is indexed by our particular set, as these components may be thought of as
"implicitly indexed" by this set.
The ``pyomo.dae.flatten`` module aims to address this use case by providing
utilities to generate all components indexed, explicitly or implicitly, by
user-provided sets.

**When we say "flatten a model," we mean "generate all components in the model,
preserving all user-specified indexing sets."**

Data structures
---------------
The components returned are either ``ComponentData`` objects, for components
not indexed by any of the provided sets, and references-to-slices, for
blnicho marked this conversation as resolved.
Show resolved Hide resolved
components indexed, explicitly or implicitly, by the provided sets.
Slices are necessary as they can encode "implicit indexing" -- where a
component is contained in an indexed block. It is natural to return references
to these slices, so they may be accessed and manipulated like any other
component.
26 changes: 26 additions & 0 deletions doc/OnlineDocs/advanced_topics/flattener/motivation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Motivation
==========

The ``pyomo.dae.flatten`` module was originally developed to assist with
dynamic optimization. A very common operation in dynamic or multi-period
optimization is to initialize all time-indexed variables to their values
at a specific time point. However, for variables indexed by time and
arbitrary other indexing sets, this is difficult to do in a way that does
does not depend on the variable we are initializing. Things get worse
when we consider that a time index can exist on a parent block rather
than the component itself.

By "reshaping" time-indexed variables in a model into references indexed
only by time, the ``flatten_dae_components`` function allows us to perform
operations that depend on knowledge of time indices without knowing
anything about the variables that we are operating on.

This "flattened representation" of a model turns out to be useful for
dynamic optimization in a variety of other contexts. Examples include
constructing a tracking objective function and plotting results.
This representation is also useful in cases where we want to preserve
indexing along more than one set, as in PDE-constrained optimization.
The ``flatten_components_along_sets`` function allows partitioning
components while preserving multiple indexing sets.
In such a case, time and space-indexed data for a given variable is useful
for purposes such as initialization, visualization, and stability analysis.
14 changes: 14 additions & 0 deletions doc/OnlineDocs/advanced_topics/flattener/reference.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
API reference
=============

.. autosummary::

pyomo.dae.flatten.slice_component_along_sets
pyomo.dae.flatten.flatten_components_along_sets
pyomo.dae.flatten.flatten_dae_components

.. autofunction:: pyomo.dae.flatten.slice_component_along_sets

.. autofunction:: pyomo.dae.flatten.flatten_components_along_sets

.. autofunction:: pyomo.dae.flatten.flatten_dae_components
1 change: 1 addition & 0 deletions doc/OnlineDocs/advanced_topics/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Advanced Topics
persistent_solvers.rst
units_container.rst
linearexpression.rst
flattener/index.rst
Loading