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

Improve robustness of calculate_variable_from_constraint() #2812

Merged
merged 8 commits into from
May 1, 2023

Conversation

jsiirola
Copy link
Member

Fixes # .

Summary/Motivation:

#2806 accidentally changed the exception type expected when converting an expression to sympy fails (from TypeError to ValueError). This changes the exception back to TypeError.

In addition, this adds behavior to calculate_variable_from_constraint() so that if the user does not specify a specific differentiation method and symbolic differentiation with sympy fails, then it will (nearly silently) fall back on numeric differentiation. This resolves user experience issues encountered with tools that wrap calculate_variable_from_constraint() like solve_strongly_connected_components() (see, e.g., https://github.com/IDAES/idaes-pse/pull/1170/files#r1170267327).

Changes proposed in this PR:

  • revert change to exception type when converting ExternalFunction expression objects to sympy
  • calculate_variable_from_constraint(): implicitly fall back on numeric differentiation when symbolic differentiation fails and no differentiation mode was explicitly set

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 Apr 18, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.03 🎉

Comparison is base (21821ac) 87.14% compared to head (5bf0f1d) 87.17%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2812      +/-   ##
==========================================
+ Coverage   87.14%   87.17%   +0.03%     
==========================================
  Files         764      764              
  Lines       88262    88270       +8     
==========================================
+ Hits        76914    76949      +35     
+ Misses      11348    11321      -27     
Flag Coverage Δ
linux 84.23% <100.00%> (+0.04%) ⬆️
osx 73.86% <100.00%> (+<0.01%) ⬆️
other 84.40% <100.00%> (+0.07%) ⬆️
win 81.77% <100.00%> (+0.01%) ⬆️

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

Impacted Files Coverage Δ
pyomo/core/expr/sympy_tools.py 98.98% <100.00%> (+1.01%) ⬆️
pyomo/util/calc_var_value.py 100.00% <100.00%> (ø)

... and 12 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

pyomo/util/calc_var_value.py Show resolved Hide resolved
@jsiirola jsiirola merged commit b2101fd into Pyomo:main May 1, 2023
@jsiirola jsiirola deleted the calc-var-value-diff-mode branch May 1, 2023 19:45
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.

4 participants