Skip to content

Block initialization improvement#1703

Merged
michaelbynum merged 7 commits intoPyomo:masterfrom
jsiirola:block-initialization
Nov 15, 2020
Merged

Block initialization improvement#1703
michaelbynum merged 7 commits intoPyomo:masterfrom
jsiirola:block-initialization

Conversation

@jsiirola
Copy link
Copy Markdown
Member

Fixes #N/A

Summary/Motivation:

Block was inconsistent in how it applied certain initialization routines. This made it particularly onerous when declaring derived Block classes that wanted to perform additional initialization (outside of a rule). This PR implements a general cleanup of the Block initialization, including:

  • switch to using the Initializer() function
  • deprecation of the (best that I can tell completely unused) options= argument
  • centralizing all block data initialization in _getitem_when_not_present

Changes proposed in this PR:

  • switch Block to leverage Initializer()
  • standardize initialization in _getitem_when_not_present
  • resolve edge cases for constructing nested abstract blocks (and add tests)
  • deprecate the Block(options=...) argument
  • add a Block(dense=...) argument to control if indexed blocks are created densely when no rule is provided (default = True)
  • general cleanup of imports
  • switch to using the @deprecated() decorator

Legal Acknowledgement

By contributing to this software project, I have read the contribution guide and agree to the following terms and conditions for my contribution:

  1. I agree my contributions are submitted under the BSD license.
  2. I represent I am authorized to make the contributions and grant the license. If my employer has rights to intellectual property that includes these contributions, I represent that I have received permission to make contributions and grant the required license on behalf of that employer.

- switch Block to leverage Initializer()
- standardize initialization in _getitem_when_not_present
- resolve edge cases for constructing nested abstract blocks
- improve testing
- deprecate the Block(options=...) argument
@codecov
Copy link
Copy Markdown

codecov bot commented Nov 12, 2020

Codecov Report

Merging #1703 (614cef4) into master (a37b3ce) will decrease coverage by 0.18%.
The diff coverage is 96.22%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1703      +/-   ##
==========================================
- Coverage   72.77%   72.59%   -0.19%     
==========================================
  Files         630      630              
  Lines       90042    90419     +377     
==========================================
+ Hits        65532    65643     +111     
- Misses      24510    24776     +266     
Impacted Files Coverage Δ
pyomo/core/base/block.py 90.45% <96.07%> (+1.14%) ⬆️
pyomo/core/base/PyomoModel.py 78.52% <100.00%> (ø)
pyomo/mpec/complementarity.py 95.67% <100.00%> (+0.02%) ⬆️
pyomo/contrib/pynumero/sparse/mpi_block_matrix.py 48.60% <0.00%> (-17.77%) ⬇️
pyomo/contrib/mindtpy/iterate.py 75.77% <0.00%> (-2.49%) ⬇️
pyomo/common/errors.py 100.00% <0.00%> (ø)
pyomo/solvers/plugins/testdriver/mip.py 0.00% <0.00%> (ø)
pyomo/solvers/plugins/testdriver/model.py 0.00% <0.00%> (ø)
pyomo/pysp/scenariotree/tree_structure.py 83.78% <0.00%> (+0.22%) ⬆️
pyomo/solvers/plugins/solvers/BARON.py 82.86% <0.00%> (+2.44%) ⬆️
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a37b3ce...614cef4. Read the comment docs.

# to be initialized with data from
# _BlockConstruction.data as they are transferred over.
if obj is not _block and isinstance(obj, _BlockData):
_block.transfer_attributes_from(obj)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my understanding, does this work for classes that inherit from blocks? I think so...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This should work for derived blocks. That said, if the derived block class has non-component attributes that hold state information that shouldn't be moved over, then they need to define a _Block_reserved_words class attribute that extends the set defined on Block._Block_reserved_words.

_concrete = kwargs.pop('concrete', False)
# As dense applies to the whole container, we will not use an
# initializer
self._dense = kwargs.pop('dense', True)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I understand the implications of this. Were blocks not dense before?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocks were dense if you specified a rule (and indexed them with a finite set). This was unfortunate, as Complementary relied on the face that if the rule was omitted the Block was sparse. This does change the default behavior so that Blocks (indexed by finite sets) are not dense by default. I don't think that it really changes anything for people, especially as I don't know of anyone who was relying on that particular edge case.

@michaelbynum
Copy link
Copy Markdown
Contributor

@jsiirola Thanks for the explanations. They were very helpful.

@michaelbynum michaelbynum merged commit e39912a into Pyomo:master Nov 15, 2020
@jsiirola jsiirola deleted the block-initialization branch December 1, 2020 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants