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 is_reference API #1740

Merged
merged 12 commits into from
Jan 19, 2021
Merged

Add is_reference API #1740

merged 12 commits into from
Jan 19, 2021

Conversation

Robbybp
Copy link
Contributor

@Robbybp Robbybp commented Dec 9, 2020

Addresses #1640 .

Summary/Motivation:

Several users have noticed that there is no officially supported way to distinguish a component that was created by the Reference function. However, it is very useful to be able to make this distinction. Also, in some cases, it is useful to get the object (slice) from which a reference was created.

This PR addresses the first point by adding an is_reference owns_data method to PyomoObject, and the second by tacking on a referent attribute to the component returned by the Reference function.

Changes proposed in this PR:

  • Add owns_data method to PyomoObject and IndexedComponent
  • Add referent attribute to component returned by Reference
  • Use owns_data to address a hiccup in IndexedComponent that causes iteration over references-to-references to be incredibly slow.

One intent of this PR is to provide a concrete talking point for the proposals in #1640.

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.

@codecov
Copy link

codecov bot commented Dec 10, 2020

Codecov Report

Merging #1740 (778aee5) into master (7f14b34) will increase coverage by 2.20%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1740      +/-   ##
==========================================
+ Coverage   72.76%   74.96%   +2.20%     
==========================================
  Files         629      638       +9     
  Lines       91423    91845     +422     
==========================================
+ Hits        66522    68855    +2333     
+ Misses      24901    22990    -1911     
Impacted Files Coverage Δ
pyomo/core/base/indexed_component.py 87.11% <100.00%> (+0.14%) ⬆️
pyomo/core/base/reference.py 97.60% <100.00%> (+0.01%) ⬆️
pyomo/core/pyomoobject.py 94.44% <100.00%> (+0.69%) ⬆️
pyomo/checker/plugins/checkers/model/rule.py 73.68% <0.00%> (-17.43%) ⬇️
pyomo/checker/runner.py 88.60% <0.00%> (-9.97%) ⬇️
pyomo/solvers/plugins/converter/model.py 75.00% <0.00%> (-5.90%) ⬇️
pyomo/repn/plugins/baron_writer.py 73.74% <0.00%> (-3.64%) ⬇️
examples/dae/simulator_ode_example.py 50.00% <0.00%> (-3.34%) ⬇️
examples/dae/simulator_ode_multindex_example.py 60.65% <0.00%> (-3.14%) ⬇️
pyomo/opt/parallel/pyro.py 88.18% <0.00%> (-1.64%) ⬇️
... and 88 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 7f14b34...778aee5. Read the comment docs.

@blnicho blnicho changed the title Reference API [WIP] Reference API Dec 16, 2020
@Robbybp
Copy link
Contributor Author

Robbybp commented Dec 28, 2020

As @jsiirola has pointed out, owns_data might be a better name than is_reference as the former is more general. In particular, if we ever want to decouple references and slices, we could end up with something like a SliceComponent function that creates something distinct from a "reference" that doesn't own its own data.

I've changed the method name here to reflect this and have added some tests.

@michaelbynum
Copy link
Contributor

I like this a lot.

@Robbybp Robbybp changed the title [WIP] Reference API Reference API Dec 31, 2020
@Robbybp
Copy link
Contributor Author

Robbybp commented Jan 5, 2021

@carldlaird @michaelbynum @andrewlee94 We are trying to come up with the name for the method to test if an object owns its own data. Any suggestions on an is_* method that accomplishes this? Our best idea for now is is_reference, but this may not be general enough.

Copy link
Member

@blnicho blnicho left a comment

Choose a reason for hiding this comment

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

One minor question, otherwise this looks fine.

@@ -258,6 +258,10 @@ def is_indexed(self):
"""Return true if this component is indexed"""
return self._index is not UnindexedComponent_set

def is_reference(self):
""" Return True if this component is a reference. """
Copy link
Member

Choose a reason for hiding this comment

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

Should we expand this description to include the idea of a reference "not owning" it's data objects?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that's a good idea. Have updated.

@jsiirola jsiirola changed the title Reference API Add is_reference API Jan 19, 2021
@jsiirola jsiirola merged commit dcd3fd8 into Pyomo:master Jan 19, 2021
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.

5 participants