Skip to content

Commit

Permalink
black
Browse files Browse the repository at this point in the history
  • Loading branch information
emma58 committed Mar 27, 2024
1 parent 5ae3cf4 commit 0c25598
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 38 deletions.
30 changes: 18 additions & 12 deletions pyomo/gdp/plugins/bigm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions pyomo/gdp/plugins/bigm_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
107 changes: 83 additions & 24 deletions pyomo/gdp/tests/test_bigm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
),
]
),
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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):
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 0c25598

Please sign in to comment.