Skip to content

Separate Linear Terms in Nonlinear to Piecewise Linear Transformation#3814

Merged
michaelbynum merged 16 commits intoPyomo:mainfrom
michaelbynum:pwl_approx
Mar 26, 2026
Merged

Separate Linear Terms in Nonlinear to Piecewise Linear Transformation#3814
michaelbynum merged 16 commits intoPyomo:mainfrom
michaelbynum:pwl_approx

Conversation

@michaelbynum
Copy link
Copy Markdown
Contributor

Summary/Motivation:

This PR modifies the contrib.piecewise.nonlinear_to_pwl transformation so that linear parts of a constraint always get separated from the nonlinear parts, even if additively_decompose is False. The idea is that

y >= exp(x) + x**3

should become

y >= PWL(exp(x) + x**3)

and not

0 >= PWL(exp(x) + x**3 - y)

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.

@michaelbynum michaelbynum requested a review from emma58 January 13, 2026 15:11
@codecov
Copy link
Copy Markdown

codecov bot commented Jan 13, 2026

Codecov Report

❌ Patch coverage is 97.77778% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 89.93%. Comparing base (52a29ae) to head (8431367).
⚠️ Report is 17 commits behind head on main.

Files with missing lines Patch % Lines
...mo/contrib/piecewise/transform/nonlinear_to_pwl.py 97.77% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3814      +/-   ##
==========================================
- Coverage   89.94%   89.93%   -0.01%     
==========================================
  Files         902      902              
  Lines      106354   106393      +39     
==========================================
+ Hits        95655    95680      +25     
- Misses      10699    10713      +14     
Flag Coverage Δ
builders 29.19% <11.11%> (-0.01%) ⬇️
default 86.23% <97.77%> (?)
expensive 35.64% <11.11%> (?)
linux 87.38% <97.77%> (-2.04%) ⬇️
linux_other 87.38% <97.77%> (-0.01%) ⬇️
oldsolvers 28.10% <11.11%> (-0.01%) ⬇️
osx 82.71% <97.77%> (-0.01%) ⬇️
win 85.80% <97.77%> (-0.02%) ⬇️
win_other 85.80% <97.77%> (-0.02%) ⬇️

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.

Copy link
Copy Markdown
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.

I think this makes sense. If you could put in a comment with the example you put in the PR description for why _separate_linear_parts is necessary, I think that would be helpful for future us. My guess is this is not currently the fastest thing we could possibly do, but for now, I'm not so concerned with that. @jsiirola, @sadavis1, @bammari, this might be worth glancing at if you have a minute.

Comment on lines +674 to +677
if x1 == x2:
nonlinear += coef * var_map[x1] ** 2
else:
nonlinear += coef * (var_map[x1] * var_map[x2])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does separating these two cases matter? I mean, obviously you make a different expression tree, but do we need it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think it matters to me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm going to leave this just for consistency with QuadraticRepn.

Comment on lines +684 to +686
if repn.multiplier_flag(repn.multiplier) != 1:
linear *= repn.multiplier
nonlinear *= repn.multiplier
Copy link
Copy Markdown
Contributor

@emma58 emma58 Jan 13, 2026

Choose a reason for hiding this comment

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

@jsiirola, can this happen? I have something in the back of my mind telling me multiplier is sure to be 1 at this point?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I just copied the to_expression code from the QuadraticRepn class and modified it slightly...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

which is to say that I have no idea. I'll take a look, though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh, I can't read. This question was for @jsiirola. My bad.

@michaelbynum
Copy link
Copy Markdown
Contributor Author

Okay, if this does make some sense, I'll clean it up a bit.

@michaelbynum
Copy link
Copy Markdown
Contributor Author

Okay, assuming tests pass, this should be good to go.

Copy link
Copy Markdown
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.

This looks good to me. I still have the multiplier question, if you can check that when you look, @jsiirola

@blnicho blnicho requested a review from jsiirola January 21, 2026 06:19
@github-project-automation github-project-automation bot moved this from Todo to Reviewer Approved in Pyomo 6.10 Jan 21, 2026
@blnicho blnicho moved this from Reviewer Approved to Review In Progress in Pyomo 6.10 Jan 21, 2026
linear *= repn.multiplier
nonlinear *= repn.multiplier

return linear, nonlinear
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

let's not lump the quadratic part back into the nonlinear part. additively decompose will separate it again.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I started to do this, but the min_dimension_to_additively_decompose option made it more complex than I thought was worthwhile.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm taking back what I said. I'm going to make this change. I was confused.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Okay, this is done, now.

@michaelbynum
Copy link
Copy Markdown
Contributor Author

Assuming tests pass, this should be ready for review.

@michaelbynum
Copy link
Copy Markdown
Contributor Author

So this is not ready for review yet.

@michaelbynum
Copy link
Copy Markdown
Contributor Author

Assuming tests pass, this should be ready for review.

Copy link
Copy Markdown
Member

@jsiirola jsiirola left a comment

Choose a reason for hiding this comment

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

A couple minor questions / suggestions....

0 >= PWL(exp(x) + x**3 - y)
"""
assert repn.multiplier == 1
linear_repn = QuadraticRepn()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If this will always just have constant + linear, why not use LinearRepn?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It feels weird to use a LinearRepn with a QuadraticRepnVisitor?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe. But I suppose no weirder than

linear_repn = QuadraticRepn()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So true... I should not be allowed to name things.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Me neither....

@michaelbynum michaelbynum merged commit 6ab0106 into Pyomo:main Mar 26, 2026
64 of 65 checks passed
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