|
16 | 16 | from pyomo.common.fileutils import import_file, PYOMO_ROOT_DIR |
17 | 17 | from pyomo.common.log import LoggingIntercept |
18 | 18 | import pyomo.common.unittest as unittest |
| 19 | +from pyomo.core.expr.compare import assertExpressionsEqual |
19 | 20 |
|
20 | 21 | from pyomo.environ import ( |
21 | 22 | BooleanVar, ConcreteModel, Constraint, LogicalConstraint, |
@@ -665,5 +666,64 @@ def test_only_multiple_bigm_bound_constraints(self): |
665 | 666 | self.check_pretty_bound_constraints(cons[1], m.x2, {m.d1: 3, m.d2: 10, |
666 | 667 | m.d3: 1}, lb=False) |
667 | 668 |
|
668 | | - # TODO: now we check that the other constraints were transformed with |
669 | | - # normal bigm |
| 669 | + cons = mbm.get_transformed_constraints(m.d1.func) |
| 670 | + self.assertEqual(len(cons), 2) |
| 671 | + lb = cons[0] |
| 672 | + ub = cons[1] |
| 673 | + assertExpressionsEqual(self, lb.expr, 0.0 <= m.x1 + m.x2 - m.d - |
| 674 | + (-1030.0)*(1 - m.d1.binary_indicator_var)) |
| 675 | + # [ESJ 11/23/22]: It's really hard to use assertExpressionsEqual on the |
| 676 | + # ub constraints because SumExpressions are sharing args, I think. So |
| 677 | + # when they get constructed in the transformation (because they come |
| 678 | + # after the lb constraints), there are nested SumExpressions. Instead of |
| 679 | + # trying to reproduce them I am just building a "flat" SumExpression |
| 680 | + # with generate_standard_repn and comparing that. |
| 681 | + self.assertIsNone(ub.lower) |
| 682 | + self.assertEqual(ub.upper, 0) |
| 683 | + repn = generate_standard_repn(ub.body) |
| 684 | + self.assertTrue(repn.is_linear()) |
| 685 | + simplified = repn.constant + sum( |
| 686 | + repn.linear_coefs[i]*repn.linear_vars[i] |
| 687 | + for i in range(len(repn.linear_vars))) |
| 688 | + assertExpressionsEqual( |
| 689 | + self, |
| 690 | + simplified, |
| 691 | + m.x1 + m.x2 - m.d + 1030.0*m.d1.binary_indicator_var - 1030.0) |
| 692 | + |
| 693 | + cons = mbm.get_transformed_constraints(m.d2.func) |
| 694 | + self.assertEqual(len(cons), 2) |
| 695 | + lb = cons[0] |
| 696 | + ub = cons[1] |
| 697 | + print(lb.expr) |
| 698 | + assertExpressionsEqual(self, lb.expr, 0.0 <= 2*m.x1 + 4*m.x2 + 7 - m.d - |
| 699 | + (-1093.0)*(1 - m.d2.binary_indicator_var)) |
| 700 | + self.assertIsNone(ub.lower) |
| 701 | + self.assertEqual(ub.upper, 0) |
| 702 | + repn = generate_standard_repn(ub.body) |
| 703 | + self.assertTrue(repn.is_linear()) |
| 704 | + simplified = repn.constant + sum( |
| 705 | + repn.linear_coefs[i]*repn.linear_vars[i] |
| 706 | + for i in range(len(repn.linear_vars))) |
| 707 | + assertExpressionsEqual( |
| 708 | + self, |
| 709 | + simplified, |
| 710 | + 2*m.x1 + 4*m.x2 - m.d + 1107.0*m.d2.binary_indicator_var - 1100.0) |
| 711 | + |
| 712 | + cons = mbm.get_transformed_constraints(m.d3.func) |
| 713 | + self.assertEqual(len(cons), 2) |
| 714 | + lb = cons[0] |
| 715 | + ub = cons[1] |
| 716 | + print(lb.expr) |
| 717 | + assertExpressionsEqual(self, lb.expr, 0.0 <= m.x1 - 5*m.x2 - 3 - m.d - |
| 718 | + (-1113.0)*(1 - m.d3.binary_indicator_var)) |
| 719 | + self.assertIsNone(ub.lower) |
| 720 | + self.assertEqual(ub.upper, 0) |
| 721 | + repn = generate_standard_repn(ub.body) |
| 722 | + self.assertTrue(repn.is_linear()) |
| 723 | + simplified = repn.constant + sum( |
| 724 | + repn.linear_coefs[i]*repn.linear_vars[i] |
| 725 | + for i in range(len(repn.linear_vars))) |
| 726 | + assertExpressionsEqual( |
| 727 | + self, |
| 728 | + simplified, |
| 729 | + m.x1 - 5*m.x2 - m.d + 1107.0*m.d3.binary_indicator_var - 1110.0) |
0 commit comments