Skip to content

Conversation

@michaelbynum
Copy link
Contributor

Summary/Motivation:

This PR adds support for abs in Pyomo's numeric differentiation. Attempting symbolic differentiation on an abs expression with a non-constant argument will still raise an exception.

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.

m = pyo.ConcreteModel()
m.x = pyo.Var(initialize=2.0)
e = 2 * abs(m.x)
with self.assertRaises(DifferentiationException):
Copy link
Member

Choose a reason for hiding this comment

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

In general, I prefer assertRaisesRegex() so that we check that the correct message is being raised (I have run across bugs where we thought we were testing that Pyomo correctly handled an exception only to find that there was a bug that happened to raise the same exception).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Fixed.

@codecov
Copy link

codecov bot commented Dec 14, 2021

Codecov Report

Merging #2232 (5d20e4b) into main (b8dc143) will decrease coverage by 0.03%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2232      +/-   ##
==========================================
- Coverage   82.77%   82.73%   -0.04%     
==========================================
  Files         607      607              
  Lines       76842    76903      +61     
==========================================
+ Hits        63604    63629      +25     
- Misses      13238    13274      +36     
Flag Coverage Δ
linux 80.28% <100.00%> (-0.01%) ⬇️
osx 70.55% <100.00%> (+<0.01%) ⬆️
other 80.25% <100.00%> (+<0.01%) ⬆️
win 77.35% <100.00%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
pyomo/core/expr/calculus/diff_with_pyomo.py 98.99% <100.00%> (+0.08%) ⬆️
pyomo/contrib/mindtpy/MindtPy.py 74.60% <0.00%> (-25.40%) ⬇️
pyomo/contrib/mindtpy/iterate.py 76.37% <0.00%> (-1.69%) ⬇️

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 b8dc143...5d20e4b. Read the comment docs.

m.x = pyo.Var(initialize=2.0)
e = 2 * abs(m.x)
with self.assertRaises(DifferentiationException):
with self.assertRaisesRegexp(DifferentiationException, 'Cannot perform symbolic differentiation of abs\(x\)'):
Copy link
Member

Choose a reason for hiding this comment

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

The reference string needs to be declared as a raw string to avoid errors in recent Python versions. (I will fix that shortly)

@jsiirola jsiirola merged commit 4ca5f2e into Pyomo:main Dec 14, 2021
@michaelbynum michaelbynum deleted the diff branch February 22, 2022 00:01
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.

2 participants