diff --git a/pyomo/repn/ampl.py b/pyomo/repn/ampl.py index 9e043290b68..c6056bd9592 100644 --- a/pyomo/repn/ampl.py +++ b/pyomo/repn/ampl.py @@ -113,7 +113,7 @@ class TextNLDebugTemplate(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 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(), )