From a6438bad0ddb80fb563c640084f3b8b2a78ba9f9 Mon Sep 17 00:00:00 2001 From: Jason Krizan <34923517+orthorhombic@users.noreply.github.com> Date: Sun, 18 Aug 2024 20:26:47 -0400 Subject: [PATCH 1/2] Change from %r to %s for NumPy 2.0 compatibility --- pyomo/repn/plugins/nl_writer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyomo/repn/plugins/nl_writer.py b/pyomo/repn/plugins/nl_writer.py index 8f51f0b0fba..0b24ff34245 100644 --- a/pyomo/repn/plugins/nl_writer.py +++ b/pyomo/repn/plugins/nl_writer.py @@ -2365,7 +2365,7 @@ class text_nl_debug_template(object): # NOTE: to support scaling and substitutions, we do NOT include the # 'v' or the EOL here: var = '%s' - const = 'n%r\n' + const = 'n%s\n' string = 'h%d:%s\n' monomial = product + const + var.replace('%', '%%') multiplier = product + const From f3a07ed6de0f847138458c6c3c45689289bc6fc9 Mon Sep 17 00:00:00 2001 From: John Siirola Date: Mon, 19 Aug 2024 14:07:43 -0600 Subject: [PATCH 2/2] Add test for #3352 --- pyomo/repn/tests/ampl/test_nlv2.py | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/pyomo/repn/tests/ampl/test_nlv2.py b/pyomo/repn/tests/ampl/test_nlv2.py index 0bfdb22a2ba..35030caeb52 100644 --- a/pyomo/repn/tests/ampl/test_nlv2.py +++ b/pyomo/repn/tests/ampl/test_nlv2.py @@ -2761,6 +2761,38 @@ def test_nested_external_expressions(self): 0 0 1 0 2 0 +""", + OUT.getvalue(), + ) + ) + + @unittest.skipUnless(numpy_available, "test requires numpy") + def test_objective_numpy_const(self): + # This tests issue #3352 + m = ConcreteModel() + m.e = Expression(expr=numpy.float64(0)) + m.obj = Objective(expr=m.e) + + OUT = io.StringIO() + nl_writer.NLWriter().write(m, OUT, linear_presolve=False, scale_model=True) + self.assertEqual( + *nl_diff( + """g3 1 1 0 #problem unknown + 0 0 1 0 0 #vars, constraints, objectives, ranges, eqns + 0 0 0 0 0 0 #nonlinear constrs, objs; ccons: lin, nonlin, nd, nzlb + 0 0 #network constraints: nonlinear, linear + 0 0 0 #nonlinear vars in constraints, objectives, both + 0 0 0 1 #linear network variables; functions; arith, flags + 0 0 0 0 0 #discrete variables: binary, integer, nonlinear (b,c,o) + 0 0 #nonzeros in Jacobian, obj. gradient + 0 0 #max name lengths: constraints, variables + 0 0 0 0 0 #common exprs: b,c,o,c1,o1 +O0 0 +n0 +x0 +r +b +k-1 """, OUT.getvalue(), )