forked from Pyomo/pyomo
-
Notifications
You must be signed in to change notification settings - Fork 2
Preserve affine GDPopt expressions with mutable-parameter coefficients #25
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
bernalde
wants to merge
4
commits into
fix/issue-4036-gdpopt-affine-mutable-param
Choose a base branch
from
review/issue-4036-gdpopt-affine-mutable-param
base: fix/issue-4036-gdpopt-affine-mutable-param
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
213e420
Preserve affine GDPopt expressions with mutable parameters
bernalde 8b3f420
Address GDPopt affine classifier review feedback
bernalde 744e74b
Add GDPopt affine routing regression guards
bernalde 3cdcaaf
Strengthen GDPopt subproblem routing guard
bernalde 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
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
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
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
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
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
247 changes: 247 additions & 0 deletions
247
pyomo/contrib/gdpopt/tests/test_gloa_affine_mutable_param.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,247 @@ | ||
| # ____________________________________________________________________________________ | ||
| # | ||
| # Pyomo: Python Optimization Modeling Objects | ||
| # Copyright (c) 2008-2026 National Technology and Engineering Solutions of Sandia, LLC | ||
| # Under the terms of Contract DE-NA0003525 with National Technology and Engineering | ||
| # Solutions of Sandia, LLC, the U.S. Government retains certain rights in this | ||
| # software. This software is distributed under the 3-clause BSD License. | ||
| # ____________________________________________________________________________________ | ||
|
|
||
| import logging | ||
| from unittest.mock import patch | ||
|
|
||
| import pyomo.environ as pyo | ||
| from pyomo.common import unittest | ||
| from pyomo.contrib.gdpopt.branch_and_bound import GDP_LBB_Solver | ||
| from pyomo.contrib.gdpopt.create_oa_subproblems import add_util_block | ||
| from pyomo.contrib.gdpopt.util import is_affine, move_nonlinear_objective_to_constraints | ||
| from pyomo.gdp import Disjunction | ||
| from pyomo.opt import TerminationCondition | ||
| from pyomo.repn import generate_standard_repn | ||
|
|
||
|
|
||
| class _StopAfterLBBPreprocessing(Exception): | ||
| pass | ||
|
|
||
|
|
||
| def _add_mutable_crf(m): | ||
| """Add the dimensionless mutable-parameter coefficient from issue #4036.""" | ||
| m.rate = pyo.Param(initialize=0.075, mutable=True) | ||
| m.years = pyo.Param(initialize=30, mutable=True) | ||
| m.crf = pyo.Expression(expr=m.rate / (1 - (1 + m.rate) ** (-m.years))) | ||
|
|
||
|
|
||
| @unittest.skipUnless( | ||
| pyo.SolverFactory('highs').available(exception_flag=False), 'HiGHS is not available' | ||
| ) | ||
| class TestGLOAAffineMutableParameterExpressions(unittest.TestCase): | ||
| def test_gloa_keeps_affine_objective(self): | ||
| m = pyo.ConcreteModel() | ||
| m.x = pyo.Var(bounds=(0, 10)) | ||
| _add_mutable_crf(m) | ||
| m.choose_x = Disjunction(expr=[[m.x == 1], [m.x == 9]]) | ||
| m.obj = pyo.Objective(expr=m.x / m.crf, sense=pyo.maximize) | ||
|
|
||
| self.assertIsNone(m.obj.expr.polynomial_degree()) | ||
| self.assertTrue(generate_standard_repn(m.obj.expr).is_linear()) | ||
| self.assertTrue(is_affine(m.obj.expr)) | ||
|
|
||
| result = pyo.SolverFactory('gdpopt.gloa').solve( | ||
| m, init_algorithm='no_init', iterlim=1, mip_solver='highs' | ||
| ) | ||
|
|
||
| self.assertEqual( | ||
| result.solver.termination_condition, TerminationCondition.maxIterations | ||
| ) | ||
| self.assertAlmostEqual(result.problem.upper_bound, 9 / pyo.value(m.crf)) | ||
|
|
||
| def test_gloa_does_not_replace_affine_objective(self): | ||
| m = pyo.ConcreteModel() | ||
| m.x = pyo.Var(bounds=(0, 10)) | ||
| _add_mutable_crf(m) | ||
| m.obj = pyo.Objective(expr=m.x / m.crf, sense=pyo.maximize) | ||
| util_block = add_util_block(m) | ||
| util_block.algebraic_variable_list = [] | ||
|
|
||
| original_obj = move_nonlinear_objective_to_constraints( | ||
| util_block, logging.getLogger(__name__) | ||
| ) | ||
|
|
||
| self.assertIsNone(original_obj) | ||
| self.assertTrue(m.obj.active) | ||
| self.assertFalse(hasattr(util_block, 'objective_value')) | ||
|
|
||
| def test_gloa_keeps_affine_constraint(self): | ||
| m = pyo.ConcreteModel() | ||
| m.x = pyo.Var(bounds=(0, 10)) | ||
| _add_mutable_crf(m) | ||
| m.limit = pyo.Constraint(expr=m.x / m.crf <= 9 / m.crf) | ||
| m.choose_x = Disjunction(expr=[[m.x == 1], [m.x == 10]]) | ||
| m.obj = pyo.Objective(expr=m.x, sense=pyo.maximize) | ||
|
|
||
| self.assertIsNone(m.limit.body.polynomial_degree()) | ||
| self.assertTrue(generate_standard_repn(m.limit.body).is_linear()) | ||
| self.assertTrue(is_affine(m.limit.body)) | ||
|
|
||
| result = pyo.SolverFactory('gdpopt.gloa').solve( | ||
| m, init_algorithm='no_init', iterlim=1, mip_solver='highs' | ||
| ) | ||
|
|
||
| self.assertEqual( | ||
| result.solver.termination_condition, TerminationCondition.maxIterations | ||
| ) | ||
| self.assertAlmostEqual(result.problem.upper_bound, 1) | ||
|
|
||
| def test_continuous_affine_model_uses_mip_solver(self): | ||
| m = pyo.ConcreteModel() | ||
| m.x = pyo.Var(bounds=(0, 10)) | ||
| _add_mutable_crf(m) | ||
| m.limit = pyo.Constraint(expr=m.x / m.crf <= 9 / m.crf) | ||
| m.obj = pyo.Objective(expr=m.x / m.crf, sense=pyo.maximize) | ||
|
|
||
| result = pyo.SolverFactory('gdpopt.gloa').solve( | ||
| m, mip_solver='highs', nlp_solver='not_available' | ||
| ) | ||
|
|
||
| self.assertAlmostEqual(result.problem.lower_bound, 9 / pyo.value(m.crf)) | ||
| self.assertAlmostEqual(result.problem.upper_bound, 9 / pyo.value(m.crf)) | ||
| self.assertAlmostEqual(pyo.value(m.x), 9) | ||
|
|
||
| def test_affine_subproblem_uses_mip_solver(self): | ||
| """Keep an unfixed CRF-coefficient constraint on the LP subproblem.""" | ||
| m = pyo.ConcreteModel() | ||
| m.selector = pyo.Var(bounds=(0, 1)) | ||
| m.x = pyo.Var(bounds=(0, 10)) | ||
| _add_mutable_crf(m) | ||
| m.limit = pyo.Constraint(expr=m.x / m.crf <= 1 / m.crf) | ||
| m.choose_selector = Disjunction(expr=[[m.selector == 0], [m.selector == 1]]) | ||
| m.obj = pyo.Objective(expr=m.x, sense=pyo.maximize) | ||
|
|
||
| def check_subproblem(solver, subproblem, util_block): | ||
| self.assertTrue(subproblem.limit.active) | ||
| self.assertFalse(subproblem.x.fixed) | ||
| self.assertIsNone(subproblem.limit.body.polynomial_degree()) | ||
| self.assertTrue(is_affine(subproblem.limit.body)) | ||
|
|
||
| result = pyo.SolverFactory('gdpopt.gloa').solve( | ||
| m, | ||
| init_algorithm='no_init', | ||
| mip_solver='highs', | ||
| nlp_solver='not_available', | ||
| subproblem_presolve=False, | ||
| call_before_subproblem_solve=check_subproblem, | ||
| ) | ||
|
|
||
| self.assertEqual( | ||
| result.solver.termination_condition, TerminationCondition.optimal | ||
| ) | ||
| self.assertAlmostEqual(result.problem.lower_bound, 1) | ||
| self.assertAlmostEqual(result.problem.upper_bound, 1) | ||
| self.assertAlmostEqual(pyo.value(m.x), 1) | ||
|
|
||
| def test_set_covering_skips_affine_disjuncts(self): | ||
| """Do not initialize disjuncts whose constraints are all affine.""" | ||
| m = pyo.ConcreteModel() | ||
| m.x = pyo.Var(bounds=(0, 10)) | ||
| _add_mutable_crf(m) | ||
| m.choose_x = Disjunction( | ||
| expr=[[m.x / m.crf <= 1 / m.crf], [m.x / m.crf >= 2 / m.crf]] | ||
| ) | ||
| m.obj = pyo.Objective(expr=m.x) | ||
| solver = pyo.SolverFactory('gdpopt.gloa') | ||
|
|
||
| result = solver.solve( | ||
| m, | ||
| init_algorithm='set_covering', | ||
| set_cover_iterlim=1, | ||
| mip_solver='highs', | ||
| nlp_solver='not_available', | ||
| ) | ||
|
|
||
| self.assertEqual(solver.initialization_iteration, 0) | ||
| self.assertEqual( | ||
| result.solver.termination_condition, TerminationCondition.optimal | ||
| ) | ||
| self.assertAlmostEqual(pyo.value(m.x), 0) | ||
|
|
||
| def test_gloa_does_not_generate_cuts_for_affine_constraint(self): | ||
| m = pyo.ConcreteModel() | ||
| m.x = pyo.Var(bounds=(0, 10)) | ||
| _add_mutable_crf(m) | ||
| m.limit = pyo.Constraint(expr=m.x / m.crf <= 9 / m.crf) | ||
| m.choose_x = Disjunction(expr=[[m.x == 1], [m.x == 10]]) | ||
| m.obj = pyo.Objective(expr=m.x, sense=pyo.maximize) | ||
|
|
||
| with patch( | ||
| 'pyomo.contrib.gdpopt.gloa.mc', | ||
| side_effect=AssertionError('affine constraint sent to MC++'), | ||
| ): | ||
| result = pyo.SolverFactory('gdpopt.gloa').solve( | ||
| m, init_algorithm='no_init', mip_solver='highs', nlp_solver='highs' | ||
| ) | ||
|
|
||
| self.assertEqual( | ||
| result.solver.termination_condition, TerminationCondition.optimal | ||
| ) | ||
| self.assertAlmostEqual(result.problem.upper_bound, 1) | ||
|
|
||
|
|
||
| class TestLBBAffineMutableParameterExpressions(unittest.TestCase): | ||
| def test_lbb_keeps_affine_constraints_in_root_relaxation(self): | ||
| """Inspect LBB preprocessing without requiring an optional MINLP solver.""" | ||
| m = pyo.ConcreteModel() | ||
| m.x = pyo.Var(bounds=(0, 10)) | ||
| _add_mutable_crf(m) | ||
| m.choose_x = Disjunction( | ||
| expr=[[m.x / m.crf <= 1 / m.crf], [m.x / m.crf >= 2 / m.crf]] | ||
| ) | ||
| m.obj = pyo.Objective(expr=m.x) | ||
|
|
||
| def stop_after_preprocessing(solver, node_data, node_model, config): | ||
| util_block = node_model.component(solver.original_util_block.name) | ||
| constraints = [ | ||
| constr | ||
| for disjunct in util_block.disjunct_list | ||
| for constr in disjunct.component_data_objects( | ||
| pyo.Constraint, active=None | ||
| ) | ||
| ] | ||
| self.assertEqual(len(constraints), 2) | ||
| self.assertTrue(all(constr.active for constr in constraints)) | ||
| self.assertEqual(len(util_block.disjunct_to_nonlinear_constraints), 0) | ||
| raise _StopAfterLBBPreprocessing | ||
|
|
||
| with patch.object( | ||
| GDP_LBB_Solver, '_prescreen_node', new=stop_after_preprocessing | ||
| ): | ||
| with self.assertRaises(_StopAfterLBBPreprocessing): | ||
| pyo.SolverFactory('gdpopt.lbb').solve(m, minlp_solver='not_available') | ||
|
|
||
|
|
||
| @unittest.skipUnless( | ||
| pyo.SolverFactory('glpk').available(exception_flag=False), 'GLPK is not available' | ||
| ) | ||
| class TestGLOAAffineExprIf(unittest.TestCase): | ||
| def test_gloa_keeps_affine_expr_if_constraint(self): | ||
| m = pyo.ConcreteModel() | ||
| m.x = pyo.Var(bounds=(0, 10)) | ||
| m.sw = pyo.Var(bounds=(0, 1), initialize=1) | ||
| m.sw.fix(1) | ||
| m.limit = pyo.Constraint( | ||
| expr=pyo.Expr_if(IF=m.sw >= 0.5, THEN=m.x, ELSE=2 * m.x) <= 1 | ||
| ) | ||
| m.choose_x = Disjunction(expr=[[m.x >= 0], [m.x == 10]]) | ||
| m.obj = pyo.Objective(expr=m.x, sense=pyo.maximize) | ||
|
|
||
| self.assertEqual(m.limit.body.polynomial_degree(), 1) | ||
| self.assertFalse(generate_standard_repn(m.limit.body).is_linear()) | ||
| self.assertTrue(is_affine(m.limit.body)) | ||
|
|
||
| result = pyo.SolverFactory('gdpopt.gloa').solve( | ||
| m, init_algorithm='no_init', iterlim=1, mip_solver='glpk' | ||
| ) | ||
|
|
||
| self.assertEqual( | ||
| result.solver.termination_condition, TerminationCondition.maxIterations | ||
| ) | ||
| self.assertAlmostEqual(result.problem.upper_bound, 1) | ||
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.
Nonblocking: this pins the new classifier but not the divergence that motivates the change.assertTrue(generate_standard_repn(...).is_linear())would still hold if a future Pyomo release taughtpolynomial_degree()to handle this expression, and at that point the fixture would silently stop exercising the misclassification it exists to guard.Asserting the other half documents the mechanism and fails loudly if the premise ever moves:
Same for
m.limit.bodyon line 73. I confirmed both hold at this head:m.obj.expr.polynomial_degree()andm.limit.body.polynomial_degree()are eachNone.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.
Addressed in
8b3f4206f: both the objective and constraint regressions nowassert that
polynomial_degree()isNonebefore asserting that the standardrepresentation is linear. The focused module passes all 6 tests.