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

Multiple dispatch for relational expression generation #3483

Open
wants to merge 29 commits into
base: main
Choose a base branch
from

Conversation

jsiirola
Copy link
Member

@jsiirola jsiirola commented Feb 20, 2025

Fixes # .

Summary/Motivation:

NOTE: This PR includes #3551; review / merge that PR first.

This is a continuation of the work moving Pyomo expression generation to use multiple dispatch for more efficient / standardized argument processing. This migrates the relational expression generation. Overall, it provides more standardized and efficient argument processing. Overall performance on the performance test suite is improved (in particular, a 3.5% improvement in create_instance:

image

Changes proposed in this PR:

  • Move relational expression generation to use multiple dispatch
  • Move some definitions (enums and dispatcher utilities) into expr_common
  • Improved solution to managing the circular dependency between relations and numeric expressions (with better code documentation)

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.

@jsiirola
Copy link
Member Author

(Do not attempt to squeeze this into the release)

@blnicho blnicho requested review from emma58 and blnicho February 25, 2025 20:00
Comment on lines 684 to 715
_lt_dispatcher = collections.defaultdict(lambda: _register_new_lt_handler)
_lt_type_handler_mapping = _binary_op_dispatcher_type_mapping(
_lt_dispatcher,
{
(ARG_TYPE.NATIVE, ARG_TYPE.NATIVE): _lt_native,
(ARG_TYPE.NATIVE, ARG_TYPE.PARAM): _lt_any_param,
(ARG_TYPE.NATIVE, ARG_TYPE.OTHER): _lt_expr,
(ARG_TYPE.NATIVE, ARG_TYPE.INEQUALITY): _lt_expr_ineq,
(ARG_TYPE.NATIVE, ARG_TYPE.INVALID_RELATIONAL): _lt_invalid,
(ARG_TYPE.PARAM, ARG_TYPE.NATIVE): _lt_param_any,
(ARG_TYPE.PARAM, ARG_TYPE.PARAM): _lt_param,
(ARG_TYPE.PARAM, ARG_TYPE.OTHER): _lt_param_any,
(ARG_TYPE.PARAM, ARG_TYPE.INEQUALITY): _lt_param_ineq,
(ARG_TYPE.PARAM, ARG_TYPE.INVALID_RELATIONAL): _lt_invalid,
(ARG_TYPE.OTHER, ARG_TYPE.NATIVE): _lt_expr,
(ARG_TYPE.OTHER, ARG_TYPE.PARAM): _lt_any_param,
(ARG_TYPE.OTHER, ARG_TYPE.OTHER): _lt_expr,
(ARG_TYPE.OTHER, ARG_TYPE.INEQUALITY): _lt_expr_ineq,
(ARG_TYPE.OTHER, ARG_TYPE.INVALID_RELATIONAL): _lt_invalid,
(ARG_TYPE.INEQUALITY, ARG_TYPE.NATIVE): _lt_ineq_expr,
(ARG_TYPE.INEQUALITY, ARG_TYPE.PARAM): _lt_ineq_param,
(ARG_TYPE.INEQUALITY, ARG_TYPE.OTHER): _lt_ineq_expr,
(ARG_TYPE.INEQUALITY, ARG_TYPE.INEQUALITY): _lt_invalid,
(ARG_TYPE.INEQUALITY, ARG_TYPE.INVALID_RELATIONAL): _lt_invalid,
(ARG_TYPE.INVALID_RELATIONAL, ARG_TYPE.NATIVE): _lt_invalid,
(ARG_TYPE.INVALID_RELATIONAL, ARG_TYPE.PARAM): _lt_invalid,
(ARG_TYPE.INVALID_RELATIONAL, ARG_TYPE.OTHER): _lt_invalid,
(ARG_TYPE.INVALID_RELATIONAL, ARG_TYPE.INEQUALITY): _lt_invalid,
(ARG_TYPE.INVALID_RELATIONAL, ARG_TYPE.INVALID_RELATIONAL): _lt_invalid,
},
ARG_TYPE,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know if there is a "better" way to do this, but there are three versions of this map that are all "almost" the same, and it makes my little "minimize repeat code" heart sad.

Copy link
Contributor

@emma58 emma58 left a comment

Choose a reason for hiding this comment

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

Sorry for the long-delayed review... This is really pretty! A few questions, but otherwise it looks good.

Comment on lines +95 to +99
class RELATIONAL_ARG_TYPE(enums.IntEnum, metaclass=enums.ExtendedEnumType):
__base_enum__ = NUMERIC_ARG_TYPE

INEQUALITY = 100
INVALID_RELATIONAL = 101
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this have equality in it? Why doesn't it?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is the list of argument types for relational expressions. As an Equality expression is not allowed to be the argument for another numeric / relational expression, we will never need to do special dispatch for it (it will fall back on getting mapped to INVALID).

Copy link

codecov bot commented Mar 26, 2025

Codecov Report

Attention: Patch coverage is 90.86957% with 21 lines in your changes missing coverage. Please review.

Project coverage is 88.68%. Comparing base (a9d673d) to head (20d6067).

Files with missing lines Patch % Lines
pyomo/core/expr/relational_expr.py 86.27% 21 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3483      +/-   ##
==========================================
- Coverage   88.69%   88.68%   -0.01%     
==========================================
  Files         888      888              
  Lines      101886   101976      +90     
==========================================
+ Hits        90365    90439      +74     
- Misses      11521    11537      +16     
Flag Coverage Δ
builders 26.55% <48.26%> (+<0.01%) ⬆️
default 84.80% <90.86%> (?)
expensive 33.95% <50.43%> (?)
linux 86.15% <90.86%> (-2.29%) ⬇️
linux_other 86.15% <90.86%> (-0.01%) ⬇️
osx 76.06% <90.86%> (+<0.01%) ⬆️
win 84.63% <90.86%> (-0.01%) ⬇️
win_other 84.63% <90.86%> (-0.01%) ⬇️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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