Skip to content

Commit

Permalink
Merge pull request #2407 from michaelbynum/nl_error
Browse files Browse the repository at this point in the history
Appsi nl writer: better error message when all variables are fixed
  • Loading branch information
blnicho authored May 20, 2022
2 parents bd202f8 + d9fb55f commit 92b3c53
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pyomo/contrib/appsi/cmodel/src/nl_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ void NLWriter::write(std::string filename) {
// now write the header

int n_vars = all_vars.size();
if (n_vars == 0) {
throw py::value_error("there are not any unfixed variables in the problem");
}

f << "g3 1 1 0\n";
f << n_vars << " ";
Expand Down
15 changes: 15 additions & 0 deletions pyomo/contrib/appsi/writers/tests/test_nl_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@

@unittest.skipUnless(cmodel_available, 'appsi extensions are not available')
class TestNLWriter(unittest.TestCase):
def test_all_vars_fixed(self):
m = pe.ConcreteModel()
m.x = pe.Var()
m.y = pe.Var()
m.obj = pe.Objective(expr=m.x**2 + m.y**2)
m.c1 = pe.Constraint(expr=m.y >= pe.exp(m.x))
m.c2 = pe.Constraint(expr=m.y >= (m.x - 1)**2)
m.x.fix(1)
m.y.fix(2)
writer = appsi.writers.NLWriter()
with TempfileManager:
fname = TempfileManager.create_tempfile(suffix='.appsi.nl')
with self.assertRaisesRegex(ValueError, 'there are not any unfixed variables in the problem'):
writer.write(m, fname)

def _write_and_check_header(self, m, correct_lines):
writer = appsi.writers.NLWriter()
with TempfileManager:
Expand Down

0 comments on commit 92b3c53

Please sign in to comment.