diff --git a/pyomo/repn/plugins/nl_writer.py b/pyomo/repn/plugins/nl_writer.py index dd3a344fb90..03225190917 100644 --- a/pyomo/repn/plugins/nl_writer.py +++ b/pyomo/repn/plugins/nl_writer.py @@ -847,6 +847,8 @@ def write(self, model): if id(v) not in var_map: _id = id(v) var_map[_id] = v + var_bounds[_id] = v.bounds + var_values[_id] = v.value con_vars_linear.add(_id) obj_vars = obj_vars_linear | obj_vars_nonlinear diff --git a/pyomo/repn/tests/ampl/test_nlv2.py b/pyomo/repn/tests/ampl/test_nlv2.py index f6fb93dff3a..099a6fcf096 100644 --- a/pyomo/repn/tests/ampl/test_nlv2.py +++ b/pyomo/repn/tests/ampl/test_nlv2.py @@ -1225,6 +1225,120 @@ def OBJ(m): ) ) + def test_sos_orphaned_variable_writes(self): + # Regression test for issue #3719: a variable that appears only + # in an SOS constraint (never in the objective or any other + # constraint) must still be written, including its bounds. + m = pyo.ConcreteModel() + m.lam = pyo.Var(range(4), domain=pyo.Binary) + m.const_sos = pyo.SOSConstraint(sos=1, var=m.lam, weights=list(range(4))) + OUT = io.StringIO() + with LoggingIntercept() as LOG: + nl_writer.NLWriter().write(m, OUT) + self.assertEqual(LOG.getvalue(), "") + self.assertEqual( + *nl_diff( + """g3 1 1 0 # problem unknown + 4 0 0 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 + 4 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 +S0 4 sosno +0 1 +1 1 +2 1 +3 1 +S0 4 ref +0 0 +1 1 +2 2 +3 3 +x0 +r +b +0 0 1 +0 0 1 +0 0 1 +0 0 1 +k3 +0 +0 +0 +""", + OUT.getvalue(), + ) + ) + + def test_sos_orphaned_integer_variable_writes(self): + # Regression test for issue #3719: an Integer variable that + # appears only in an SOS constraint exercises the var_bounds + # lookup used to classify integer vs. binary variables. + m = pyo.ConcreteModel() + m.lam = pyo.Var(range(4), domain=pyo.Integers, bounds=(0, 5)) + m.const_sos = pyo.SOSConstraint(sos=1, var=m.lam, weights=list(range(4))) + OUT = io.StringIO() + with LoggingIntercept() as LOG: + nl_writer.NLWriter().write(m, OUT) + self.assertEqual(LOG.getvalue(), "") + self.assertEqual( + *nl_diff( + """g3 1 1 0 # problem unknown + 4 0 0 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 4 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 +S0 4 sosno +0 1 +1 1 +2 1 +3 1 +S0 4 ref +0 0 +1 1 +2 2 +3 3 +x0 +r +b +0 0 5 +0 0 5 +0 0 5 +0 0 5 +k3 +0 +0 +0 +""", + OUT.getvalue(), + ) + ) + + def test_sos_linked_variable_writes(self): + # Regression test for issue #3719: when SOS variables also appear + # in a constraint (or objective) the NL writer must succeed. + m = pyo.ConcreteModel() + m.lam = pyo.Var(range(4), domain=pyo.Binary) + m.const_sos = pyo.SOSConstraint(sos=1, var=m.lam, weights=list(range(4))) + # Link every lam variable into a real constraint so it enters var_map. + m.link = pyo.Constraint(expr=sum(m.lam[i] for i in range(4)) == 1) + m.obj = pyo.Objective(expr=m.lam[0]) + OUT = io.StringIO() + with LoggingIntercept() as LOG: + nl_writer.NLWriter().write(m, OUT) + self.assertEqual(LOG.getvalue(), "") + # The output must be non-empty and contain the SOS suffix block. + self.assertIn("S0", OUT.getvalue()) + @unittest.skipUnless(numpy_available, "test requires numpy") def test_nonfloat_constants(self): import pyomo.environ as pyo