contrib.cp: Adding beforeChild handling for bools in logical expressions #3315
contrib.cp: Adding beforeChild handling for bools in logical expressions #3315emma58 merged 8 commits intoPyomo:mainfrom
Conversation
…l be binary and play nice with algebraic expressions as we build them
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #3315 +/- ##
==========================================
+ Coverage 86.46% 88.50% +2.04%
==========================================
Files 801 868 +67
Lines 96758 99223 +2465
==========================================
+ Hits 83661 87819 +4158
+ Misses 13097 11404 -1693
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
| if child.__class__ is bool: | ||
| # If we encounter a bool, we are going to need to treat it as | ||
| # binary explicitly because we are finally pedantic enough in the | ||
| # expression system to not allow some of the mixing we will need | ||
| # (like summing a LinearExpression with a bool) | ||
| return False, int(child) | ||
|
|
||
| if child.__class__ in EXPR.native_types: | ||
| return False, child |
There was a problem hiding this comment.
This is fine, however two questions:
- Would the following be more efficient:
if child.__class__ in EXPR.native_types:
if child.__class__ is bool:
# If we encounter a bool, we are going to need to treat it as
# binary explicitly because we are finally pedantic enough in the
# expression system to not allow some of the mixing we will need
# (like summing a LinearExpression with a bool)
return False, int(child)
return False, child- are there concerns with float constants here? For Param, we error if the value is fractional, but not here. If the same concern applies, then maybe we should just do:
if child.__class__ in EXPR.native_types:
val = int(child)
if val != child:
raise ValueError("...")
return False, val(which covers both the fractional values and the bool case)
There was a problem hiding this comment.
Oh, good catch! But I think it's actually the reverse: I shouldn't check in Param or here because it won't be required when we probably someday allow relational expressions to be in logical expressions here. And we are checking for integrality in places where we need it (e.g., atmost, atleast, and exactly)
…re-proof and it is redundant with the same check in the expression nodes where it actually is required
Fixes # .
Summary/Motivation:
This fixes a bug in the logical-to-disjunctive walker where we would hit our own carefulness in terms of not mixing algebraic and logical expressions. Specifically, the following code
resulted in this error in the walker:
Changes proposed in this PR:
booltypes specifically inbeforeChildin the logical-to-disjunctive walker, and casts them to integer so they will be binary.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: