Skip to content

Commit 9bf36c7

Browse files
committed
Adding tests to NLv2 motivated by 58996fa
1 parent 58996fa commit 9bf36c7

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

pyomo/repn/tests/ampl/test_nlv2.py

+125
Original file line numberDiff line numberDiff line change
@@ -1683,6 +1683,131 @@ def test_presolve_named_expressions(self):
16831683
G0 2 #obj
16841684
0 0
16851685
1 0
1686+
""",
1687+
OUT.getvalue(),
1688+
)
1689+
)
1690+
1691+
def test_presolve_zero_coef(self):
1692+
m = ConcreteModel()
1693+
m.x = Var()
1694+
m.y = Var()
1695+
m.z = Var()
1696+
m.obj = Objective(expr=m.x**2 + m.y**2 + m.z**2)
1697+
m.c1 = Constraint(expr=m.x == m.y + m.z + 1.5)
1698+
m.c2 = Constraint(expr=m.z == -m.y)
1699+
1700+
OUT = io.StringIO()
1701+
with LoggingIntercept() as LOG:
1702+
nlinfo = nl_writer.NLWriter().write(
1703+
m, OUT, symbolic_solver_labels=True, linear_presolve=True
1704+
)
1705+
self.assertEqual(LOG.getvalue(), "")
1706+
1707+
self.assertEqual(nlinfo.eliminated_vars[0], (m.x, 1.5))
1708+
self.assertIs(nlinfo.eliminated_vars[1][0], m.y)
1709+
self.assertExpressionsEqual(
1710+
nlinfo.eliminated_vars[1][1], LinearExpression([-1.0 * m.z])
1711+
)
1712+
1713+
self.assertEqual(
1714+
*nl_diff(
1715+
"""g3 1 1 0 # problem unknown
1716+
1 0 1 0 0 #vars, constraints, objectives, ranges, eqns
1717+
0 1 0 0 0 0 #nonlinear constrs, objs; ccons: lin, nonlin, nd, nzlb
1718+
0 0 #network constraints: nonlinear, linear
1719+
0 1 0 #nonlinear vars in constraints, objectives, both
1720+
0 0 0 1 #linear network variables; functions; arith, flags
1721+
0 0 0 0 0 #discrete variables: binary, integer, nonlinear (b,c,o)
1722+
0 1 #nonzeros in Jacobian, obj. gradient
1723+
3 1 #max name lengths: constraints, variables
1724+
0 0 0 0 0 #common exprs: b,c,o,c1,o1
1725+
O0 0 #obj
1726+
o54 #sumlist
1727+
3 #(n)
1728+
o5 #^
1729+
n1.5
1730+
n2
1731+
o5 #^
1732+
o16 #-
1733+
v0 #z
1734+
n2
1735+
o5 #^
1736+
v0 #z
1737+
n2
1738+
x0 #initial guess
1739+
r #0 ranges (rhs's)
1740+
b #1 bounds (on variables)
1741+
3 #z
1742+
k0 #intermediate Jacobian column lengths
1743+
G0 1 #obj
1744+
0 0
1745+
""",
1746+
OUT.getvalue(),
1747+
)
1748+
)
1749+
1750+
m.c3 = Constraint(expr=m.x == 2)
1751+
OUT = io.StringIO()
1752+
with LoggingIntercept() as LOG:
1753+
with self.assertRaisesRegex(
1754+
nl_writer.InfeasibleConstraintException,
1755+
r"model contains a trivially infeasible constraint 0.5 == 0.0\*y",
1756+
):
1757+
nlinfo = nl_writer.NLWriter().write(
1758+
m, OUT, symbolic_solver_labels=True, linear_presolve=True
1759+
)
1760+
self.assertEqual(LOG.getvalue(), "")
1761+
1762+
m.c1.set_value(m.x >= m.y + m.z + 1.5)
1763+
OUT = io.StringIO()
1764+
with LoggingIntercept() as LOG:
1765+
nlinfo = nl_writer.NLWriter().write(
1766+
m, OUT, symbolic_solver_labels=True, linear_presolve=True
1767+
)
1768+
self.assertEqual(LOG.getvalue(), "")
1769+
1770+
self.assertIs(nlinfo.eliminated_vars[0][0], m.y)
1771+
self.assertExpressionsEqual(
1772+
nlinfo.eliminated_vars[0][1], LinearExpression([-1.0 * m.z])
1773+
)
1774+
self.assertEqual(nlinfo.eliminated_vars[1], (m.x, 2))
1775+
1776+
self.assertEqual(
1777+
*nl_diff(
1778+
"""g3 1 1 0 # problem unknown
1779+
1 1 1 0 0 #vars, constraints, objectives, ranges, eqns
1780+
0 1 0 0 0 0 #nonlinear constrs, objs; ccons: lin, nonlin, nd, nzlb
1781+
0 0 #network constraints: nonlinear, linear
1782+
0 1 0 #nonlinear vars in constraints, objectives, both
1783+
0 0 0 1 #linear network variables; functions; arith, flags
1784+
0 0 0 0 0 #discrete variables: binary, integer, nonlinear (b,c,o)
1785+
0 1 #nonzeros in Jacobian, obj. gradient
1786+
3 1 #max name lengths: constraints, variables
1787+
0 0 0 0 0 #common exprs: b,c,o,c1,o1
1788+
C0 #c1
1789+
n0
1790+
O0 0 #obj
1791+
o54 #sumlist
1792+
3 #(n)
1793+
o5 #^
1794+
n2
1795+
n2
1796+
o5 #^
1797+
o16 #-
1798+
v0 #z
1799+
n2
1800+
o5 #^
1801+
v0 #z
1802+
n2
1803+
x0 #initial guess
1804+
r #1 ranges (rhs's)
1805+
1 0.5 #c1
1806+
b #1 bounds (on variables)
1807+
3 #z
1808+
k0 #intermediate Jacobian column lengths
1809+
G0 1 #obj
1810+
0 0
16861811
""",
16871812
OUT.getvalue(),
16881813
)

0 commit comments

Comments
 (0)