-
Notifications
You must be signed in to change notification settings - Fork 573
Factorable Programming for PWL Approximations #3821
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
Open
michaelbynum
wants to merge
18
commits into
Pyomo:main
Choose a base branch
from
michaelbynum:pwl_factorable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+676
−0
Open
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
4863d1f
initial draft of factorable programming transformation for pwl approx…
michaelbynum 4b36753
Merge remote-tracking branch 'origin/main' into pwl_factorable
michaelbynum c822381
tests for piecewise univariate nonlinear decomposition
michaelbynum 43f5e57
testing for univariate nonlinear decomposition
michaelbynum 6ae7449
add an option to univariate nonlinear decomposition to aggressively a…
michaelbynum 2b0bea7
handle NPV nodes in univariate nonlinear decomposition transformation
michaelbynum 23bc357
Merge branch 'main' into pwl_factorable
jsiirola ee96e07
NFC: apply black
jsiirola ce55a10
NFC: fix typo
jsiirola 00deabd
Merge remote-tracking branch 'origin/main' into pwl_factorable_aggres…
michaelbynum 37cd53c
Merge remote-tracking branch 'origin/main' into pwl_factorable
michaelbynum 96118c6
piecewise factorable aggressive substitution
michaelbynum 8eabf28
run black
michaelbynum 6fb7efb
Merge remote-tracking branch 'origin/main' into pwl_factorable
michaelbynum 834ebd6
Merge remote-tracking branch 'michaelbynum/pwl_factorable' into pwl_f…
michaelbynum 2869b9d
speed up factorable programming transformation
michaelbynum e322083
Merge remote-tracking branch 'origin/main' into pwl_factorable
michaelbynum 9722188
run black
michaelbynum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
pyomo/contrib/piecewise/tests/test_univariate_nonlinear_decomposition.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| from pyomo.common.unittest import TestCase, skipUnless | ||
| import pyomo.environ as pyo | ||
| from pyomo.contrib import piecewise | ||
| from pyomo.core.expr.compare import assertExpressionsEqual | ||
| from pyomo.common.dependencies import numpy_available, numpy | ||
| from pyomo.core.expr.numeric_expr import ProductExpression | ||
|
|
||
| pe = pyo | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is nonstandard, per the current project guidelines. |
||
|
|
||
|
|
||
| def _get_trans(): | ||
| return pyo.TransformationFactory( | ||
| 'contrib.piecewise.univariate_nonlinear_decomposition' | ||
| ) | ||
|
|
||
|
|
||
| class TestUnivariateNonlinearDecomposition(TestCase): | ||
| def test_multiterm(self): | ||
| m = pe.ConcreteModel() | ||
| m.x = pe.Var() | ||
| m.y = pe.Var() | ||
| m.z = pe.Var() | ||
| m.c = pe.Constraint(expr=m.x + pe.log(m.y + m.z) + 1 / pe.exp(m.x**0.5) <= 0) | ||
|
|
||
| trans = _get_trans() | ||
| trans.apply_to(m) | ||
| aux = m.auxiliary | ||
|
|
||
| assertExpressionsEqual(self, m.c.body, m.x + aux.x[3] + aux.x[2]) | ||
| assertExpressionsEqual(self, aux.c[1].expr, aux.x[1] == (m.y + m.z)) | ||
| assertExpressionsEqual(self, aux.c[2].expr, aux.x[2] == 1 / pyo.exp(m.x**0.5)) | ||
| assertExpressionsEqual(self, aux.c[3].expr, aux.x[3] == pyo.log(aux.x[1])) | ||
| self.assertEqual(m.x.lb, 0) | ||
| self.assertIsNone(m.x.ub) | ||
| self.assertIsNone(m.y.lb) | ||
| self.assertIsNone(m.y.ub) | ||
| self.assertIsNone(m.z.lb) | ||
| self.assertIsNone(m.z.ub) | ||
| self.assertEqual(aux.x[1].lb, 0) | ||
| self.assertIsNone(aux.x[1].ub) | ||
| self.assertEqual(aux.x[2].lb, 0) | ||
| self.assertEqual(aux.x[2].ub, 1) | ||
| self.assertIsNone(aux.x[3].lb) | ||
| self.assertIsNone(aux.x[3].ub) | ||
|
|
||
| def test_common_subexpressions(self): | ||
| m = pe.ConcreteModel() | ||
| m.x = pe.Var() | ||
| m.y = pe.Var() | ||
| m.z1 = pe.Var() | ||
| m.z2 = pe.Var() | ||
| e = -pe.log(m.x + m.y) | ||
| m.c1 = pe.Constraint(expr=m.z1 + e == 0) | ||
| m.c2 = pe.Constraint(expr=m.z2 + e == 0) | ||
|
|
||
| trans = _get_trans() | ||
| trans.apply_to(m) | ||
| aux = m.auxiliary | ||
|
|
||
| assertExpressionsEqual(self, m.c1.expr, m.z1 + aux.x[2] == 0) | ||
| assertExpressionsEqual(self, m.c2.expr, m.z2 + aux.x[2] == 0) | ||
| assertExpressionsEqual(self, aux.c[1].expr, aux.x[1] == m.x + m.y) | ||
| assertExpressionsEqual(self, aux.c[2].expr, aux.x[2] == -pe.log(aux.x[1])) | ||
|
|
||
| def test_product_fixed_variable(self): | ||
| m = pe.ConcreteModel() | ||
| m.x = pe.Var() | ||
| m.y = pe.Var() | ||
| m.z = pe.Var() | ||
| m.c = pe.Constraint(expr=2 * pe.log(m.x + m.y) <= 0) | ||
|
|
||
| trans = _get_trans() | ||
| trans.apply_to(m) | ||
| aux = m.auxiliary | ||
|
|
||
| assertExpressionsEqual(self, m.c.expr, 2 * pe.log(aux.x[1]) <= 0) | ||
| assertExpressionsEqual(self, aux.c[1].expr, aux.x[1] == m.x + m.y) | ||
|
|
||
| def test_product_variable_fixed(self): | ||
| m = pe.ConcreteModel() | ||
| m.x = pe.Var() | ||
| m.y = pe.Var() | ||
| m.z = pe.Var() | ||
| m.c = pe.Constraint(expr=pe.log(m.x + m.y) * 2 <= 0) | ||
|
|
||
| trans = _get_trans() | ||
| trans.apply_to(m) | ||
| aux = m.auxiliary | ||
|
|
||
| assertExpressionsEqual(self, m.c.expr, pe.log(aux.x[1]) * 2 <= 0) | ||
| assertExpressionsEqual(self, aux.c[1].expr, aux.x[1] == m.x + m.y) | ||
|
|
||
| def test_prod_sum_sum(self): | ||
| m = pe.ConcreteModel() | ||
| m.x1 = pe.Var() | ||
| m.x2 = pe.Var() | ||
| m.x3 = pe.Var() | ||
| m.x4 = pe.Var() | ||
| m.c = pe.Constraint(expr=(m.x1 + m.x2) * (m.x3 + m.x4) <= 1) | ||
|
|
||
| trans = _get_trans() | ||
| trans.apply_to(m) | ||
| aux = m.auxiliary | ||
|
|
||
| assertExpressionsEqual(self, m.c.expr, aux.x[1] * aux.x[2] <= 1) | ||
| assertExpressionsEqual(self, aux.c[1].expr, aux.x[1] == m.x1 + m.x2) | ||
| assertExpressionsEqual(self, aux.c[2].expr, aux.x[2] == m.x3 + m.x4) | ||
|
|
||
| def test_pow_sum_sum(self): | ||
| m = pe.ConcreteModel() | ||
| m.x1 = pe.Var() | ||
| m.x2 = pe.Var() | ||
| m.x3 = pe.Var() | ||
| m.x4 = pe.Var() | ||
| m.c = pe.Constraint(expr=(m.x1 + m.x2) ** (m.x3 + m.x4) <= 1) | ||
|
|
||
| trans = _get_trans() | ||
| trans.apply_to(m) | ||
| aux = m.auxiliary | ||
|
|
||
| assertExpressionsEqual(self, m.c.expr, aux.x[1] ** aux.x[2] <= 1) | ||
| assertExpressionsEqual(self, aux.c[1].expr, aux.x[1] == m.x1 + m.x2) | ||
| assertExpressionsEqual(self, aux.c[2].expr, aux.x[2] == m.x3 + m.x4) | ||
|
|
||
| def test_division_var_const(self): | ||
| m = pe.ConcreteModel() | ||
| m.x = pe.Var() | ||
| m.y = pe.Var() | ||
| m.c = pe.Constraint(expr=(m.x + m.y) / 2 <= 0) | ||
|
|
||
| trans = _get_trans() | ||
| trans.apply_to(m) | ||
| aux = m.auxiliary | ||
|
|
||
| assertExpressionsEqual(self, m.c.expr, (m.x + m.y) / 2 <= 0) | ||
|
|
||
| def test_division_sum_sum(self): | ||
| m = pe.ConcreteModel() | ||
| m.x1 = pe.Var() | ||
| m.x2 = pe.Var() | ||
| m.x3 = pe.Var() | ||
| m.x4 = pe.Var() | ||
| m.c = pe.Constraint(expr=(m.x1 + m.x2) / (m.x3 + m.x4) <= 1) | ||
|
|
||
| trans = _get_trans() | ||
| trans.apply_to(m) | ||
| aux = m.auxiliary | ||
|
|
||
| assertExpressionsEqual(self, m.c.expr, aux.x[1] * aux.x[3] <= 1) | ||
| assertExpressionsEqual(self, aux.c[1].expr, aux.x[1] == m.x1 + m.x2) | ||
| assertExpressionsEqual(self, aux.c[2].expr, aux.x[2] == m.x3 + m.x4) | ||
| assertExpressionsEqual(self, aux.c[3].expr, aux.x[3] * aux.x[2] == 1) | ||
|
|
||
| @skipUnless(numpy_available, "Numpy is not available") | ||
| def test_numpy_float(self): | ||
| m = pe.ConcreteModel() | ||
| m.x = pe.Var() | ||
| m.y = pe.Var() | ||
| m.z = pe.Var() | ||
| m.c = pe.Constraint( | ||
| expr=ProductExpression((numpy.float64(2.5), pe.log(m.x + m.y))) <= 0 | ||
| ) | ||
|
|
||
| trans = _get_trans() | ||
| trans.apply_to(m) | ||
| aux = m.auxiliary | ||
|
|
||
| assertExpressionsEqual(self, m.c.expr, 2.5 * pe.log(aux.x[1]) <= 0) | ||
| assertExpressionsEqual(self, aux.c[1].expr, aux.x[1] == m.x + m.y) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the copyright statement.