From 0c25598b30c3150851c3434d703aaa929c559911 Mon Sep 17 00:00:00 2001 From: Emma Johnson Date: Tue, 26 Mar 2024 21:00:19 -0600 Subject: [PATCH] black --- pyomo/gdp/plugins/bigm.py | 30 +++++---- pyomo/gdp/plugins/bigm_mixin.py | 10 ++- pyomo/gdp/tests/test_bigm.py | 107 +++++++++++++++++++++++++------- 3 files changed, 109 insertions(+), 38 deletions(-) diff --git a/pyomo/gdp/plugins/bigm.py b/pyomo/gdp/plugins/bigm.py index 118fa6935d7..d715d913db8 100644 --- a/pyomo/gdp/plugins/bigm.py +++ b/pyomo/gdp/plugins/bigm.py @@ -213,12 +213,7 @@ def _apply_to_impl(self, instance, **kwds): bigM = self._config.bigM for t in preprocessed_targets: if t.ctype is Disjunction: - self._transform_disjunctionData( - t, - t.index(), - bigM, - gdp_tree, - ) + self._transform_disjunctionData(t, t.index(), bigM, gdp_tree) # issue warnings about anything that was in the bigM args dict that we # didn't use @@ -275,15 +270,21 @@ def _transform_disjunct(self, obj, bigM, transBlock, gdp_tree): # comparing the two relaxations. # # Transform each component within this disjunct - self._transform_block_components(obj, obj, bigM, arg_list, suffix_list, - indicator_expression) + self._transform_block_components( + obj, obj, bigM, arg_list, suffix_list, indicator_expression + ) # deactivate disjunct to keep the writers happy obj._deactivate_without_fixing_indicator() def _transform_constraint( - self, obj, disjunct, bigMargs, arg_list, disjunct_suffix_list, - indicator_expression + self, + obj, + disjunct, + bigMargs, + arg_list, + disjunct_suffix_list, + indicator_expression, ): # add constraint to the transformation block, we'll transform it there. transBlock = disjunct._transformation_block() @@ -355,8 +356,13 @@ def _transform_constraint( bigm_src[c] = (lower, upper) self._add_constraint_expressions( - c, i, M, disjunct.binary_indicator_var, newConstraint, constraint_map, - indicator_expression=indicator_expression + c, + i, + M, + disjunct.binary_indicator_var, + newConstraint, + constraint_map, + indicator_expression=indicator_expression, ) # deactivate because we relaxed diff --git a/pyomo/gdp/plugins/bigm_mixin.py b/pyomo/gdp/plugins/bigm_mixin.py index 300509d81f8..1c3fcb2c64a 100644 --- a/pyomo/gdp/plugins/bigm_mixin.py +++ b/pyomo/gdp/plugins/bigm_mixin.py @@ -232,8 +232,14 @@ def _estimate_M(self, expr, constraint): return tuple(M) def _add_constraint_expressions( - self, c, i, M, indicator_var, newConstraint, constraint_map, - indicator_expression=None + self, + c, + i, + M, + indicator_var, + newConstraint, + constraint_map, + indicator_expression=None, ): # Since we are both combining components from multiple blocks and using # local names, we need to make sure that the first index for diff --git a/pyomo/gdp/tests/test_bigm.py b/pyomo/gdp/tests/test_bigm.py index 95c4652e387..3174a95292e 100644 --- a/pyomo/gdp/tests/test_bigm.py +++ b/pyomo/gdp/tests/test_bigm.py @@ -1885,7 +1885,7 @@ def check_bigM_constraint(self, cons, variable, M, indicator_var): assertExpressionsEqual( self, cons.body, - variable - float(M) * (1 - indicator_var.get_associated_binary()) + variable - float(M) * (1 - indicator_var.get_associated_binary()), ) def check_inner_xor_constraint(self, inner_disjunction, outer_disjunct, bigm): @@ -1953,11 +1953,7 @@ def test_transformed_constraints(self): ), 1, EXPR.MonomialTermExpression( - ( - -1, - m.disjunct[1] - .binary_indicator_var, - ) + (-1, m.disjunct[1].binary_indicator_var) ), ] ), @@ -1971,8 +1967,15 @@ def test_transformed_constraints(self): assertExpressionsEqual( self, cons1ub.expr, - m.z - 10.0*(1 - m.disjunct[1].innerdisjunct[0].binary_indicator_var + - 1 - m.disjunct[1].binary_indicator_var) <= 0.0 + m.z + - 10.0 + * ( + 1 + - m.disjunct[1].innerdisjunct[0].binary_indicator_var + + 1 + - m.disjunct[1].binary_indicator_var + ) + <= 0.0, ) cons2 = bigm.get_transformed_constraints(m.disjunct[1].innerdisjunct[1].c) @@ -1981,8 +1984,15 @@ def test_transformed_constraints(self): assertExpressionsEqual( self, cons2lb.expr, - 5.0 <= m.z - (-5.0)*(1 - m.disjunct[1].innerdisjunct[1].binary_indicator_var - + 1 - m.disjunct[1].binary_indicator_var) + 5.0 + <= m.z + - (-5.0) + * ( + 1 + - m.disjunct[1].innerdisjunct[1].binary_indicator_var + + 1 + - m.disjunct[1].binary_indicator_var + ), ) cons3 = bigm.get_transformed_constraints(m.simpledisjunct.innerdisjunct0.c) @@ -1991,8 +2001,15 @@ def test_transformed_constraints(self): assertExpressionsEqual( self, cons3ub.expr, - m.x - 7.0*(1 - m.simpledisjunct.innerdisjunct0.binary_indicator_var + 1 - - m.simpledisjunct.binary_indicator_var) <= 2.0 + m.x + - 7.0 + * ( + 1 + - m.simpledisjunct.innerdisjunct0.binary_indicator_var + + 1 + - m.simpledisjunct.binary_indicator_var + ) + <= 2.0, ) cons4 = bigm.get_transformed_constraints(m.simpledisjunct.innerdisjunct1.c) @@ -2001,8 +2018,15 @@ def test_transformed_constraints(self): assertExpressionsEqual( self, cons4lb.expr, - m.x - (-13.0)*(1 - m.simpledisjunct.innerdisjunct1.binary_indicator_var - + 1 - m.simpledisjunct.binary_indicator_var) >= 4.0 + m.x + - (-13.0) + * ( + 1 + - m.simpledisjunct.innerdisjunct1.binary_indicator_var + + 1 + - m.simpledisjunct.binary_indicator_var + ) + >= 4.0, ) # Here we check that the xor constraint from @@ -2132,7 +2156,12 @@ def check_hierarchical_nested_model(self, m, bigm): assertExpressionsEqual( self, cons.expr, - m.x[1]**2 + m.x[2]**2 + m.x[3]**2 + m.x[4]**2 - 143.0*(1 - m.disj1.binary_indicator_var) <= 1.0 + m.x[1] ** 2 + + m.x[2] ** 2 + + m.x[3] ** 2 + + m.x[4] ** 2 + - 143.0 * (1 - m.disj1.binary_indicator_var) + <= 1.0, ) disj2c = bigm.get_transformed_constraints(m.disjunct_block.disj2.c) @@ -2141,7 +2170,12 @@ def check_hierarchical_nested_model(self, m, bigm): assertExpressionsEqual( self, cons.expr, - (3 - m.x[1])**2 + (3 - m.x[2])**2 + (3 - m.x[3])**2 + (3 - m.x[4])**2 - 99.0*(1 - m.disjunct_block.disj2.binary_indicator_var) <= 1.0 + (3 - m.x[1]) ** 2 + + (3 - m.x[2]) ** 2 + + (3 - m.x[3]) ** 2 + + (3 - m.x[4]) ** 2 + - 99.0 * (1 - m.disjunct_block.disj2.binary_indicator_var) + <= 1.0, ) # inner disjunction constraints @@ -2153,7 +2187,18 @@ def check_hierarchical_nested_model(self, m, bigm): assertExpressionsEqual( self, cons.expr, - m.x[1]**2 + m.x[2]**2 + m.x[3]**2 + m.x[4]**2 - 143.0*(1 - m.disjunct_block.disj2.disjunction_disjuncts[0].binary_indicator_var + 1 - m.disjunct_block.disj2.binary_indicator_var) <= 1.0 + m.x[1] ** 2 + + m.x[2] ** 2 + + m.x[3] ** 2 + + m.x[4] ** 2 + - 143.0 + * ( + 1 + - m.disjunct_block.disj2.disjunction_disjuncts[0].binary_indicator_var + + 1 + - m.disjunct_block.disj2.binary_indicator_var + ) + <= 1.0, ) innerd2c = bigm.get_transformed_constraints( @@ -2164,7 +2209,18 @@ def check_hierarchical_nested_model(self, m, bigm): assertExpressionsEqual( self, cons.expr, - (3 - m.x[1])**2 + (3 - m.x[2])**2 + (3 - m.x[3])**2 + (3 - m.x[4])**2 - 99.0*(1 - m.disjunct_block.disj2.disjunction_disjuncts[1].binary_indicator_var + 1 - m.disjunct_block.disj2.binary_indicator_var) <= 1.0 + (3 - m.x[1]) ** 2 + + (3 - m.x[2]) ** 2 + + (3 - m.x[3]) ** 2 + + (3 - m.x[4]) ** 2 + - 99.0 + * ( + 1 + - m.disjunct_block.disj2.disjunction_disjuncts[1].binary_indicator_var + + 1 + - m.disjunct_block.disj2.binary_indicator_var + ) + <= 1.0, ) def test_hierarchical_badly_ordered_targets(self): @@ -2211,17 +2267,20 @@ def test_constraints_not_enforced_when_an_ancestor_indicator_is_False(self): m.right.disjunction = Disjunction(expr=[m.right.left, m.right.right]) m.disjunction = Disjunction(expr=[m.left, m.right]) - m.equiv_left = LogicalConstraint(expr=m.left.left.indicator_var.equivalent_to( - m.right.left.indicator_var)) - m.equiv_right = LogicalConstraint(expr=m.left.right.indicator_var.equivalent_to( - m.right.right.indicator_var)) + m.equiv_left = LogicalConstraint( + expr=m.left.left.indicator_var.equivalent_to(m.right.left.indicator_var) + ) + m.equiv_right = LogicalConstraint( + expr=m.left.right.indicator_var.equivalent_to(m.right.right.indicator_var) + ) m.obj = Objective(expr=m.x) TransformationFactory('gdp.bigm').apply_to(m) results = SolverFactory('gurobi').solve(m) - self.assertEqual(results.solver.termination_condition, - TerminationCondition.optimal) + self.assertEqual( + results.solver.termination_condition, TerminationCondition.optimal + ) self.assertTrue(value(m.right.indicator_var)) self.assertFalse(value(m.left.indicator_var)) self.assertTrue(value(m.right.right.indicator_var))