From 7815ea6b877d376c69c1ccc067d5fa589609e1cf Mon Sep 17 00:00:00 2001 From: Alma Walmsley Date: Tue, 31 Dec 2024 22:03:15 +1300 Subject: [PATCH] add `EmptyModelError` to pyomo common errors, add to nl writers --- pyomo/common/errors.py | 9 +++++++++ .../interfaces/tests/test_external_grey_box_model.py | 4 ++-- .../interfaces/tests/test_pyomo_grey_box_nlp.py | 4 ++-- pyomo/repn/plugins/ampl/ampl_.py | 3 ++- pyomo/repn/plugins/nl_writer.py | 11 ++--------- 5 files changed, 17 insertions(+), 14 deletions(-) diff --git a/pyomo/common/errors.py b/pyomo/common/errors.py index 3c82f2b07c1..5940875445d 100644 --- a/pyomo/common/errors.py +++ b/pyomo/common/errors.py @@ -244,3 +244,12 @@ class TemplateExpressionError(ValueError): def __init__(self, template, *args, **kwds): self.template = template super(TemplateExpressionError, self).__init__(*args, **kwds) + + +class EmptyModelError(PyomoException, ValueError): + """ + Exception class used to throw an error for an empty + Pyomo model (i.e., no variables or constraints). + """ + + pass diff --git a/pyomo/contrib/pynumero/interfaces/tests/test_external_grey_box_model.py b/pyomo/contrib/pynumero/interfaces/tests/test_external_grey_box_model.py index b1c473977a7..00b3bd2bbb8 100644 --- a/pyomo/contrib/pynumero/interfaces/tests/test_external_grey_box_model.py +++ b/pyomo/contrib/pynumero/interfaces/tests/test_external_grey_box_model.py @@ -38,7 +38,7 @@ check_sparse_matrix_specific_order, ) import pyomo.contrib.pynumero.interfaces.tests.external_grey_box_models as ex_models -from pyomo.repn.plugins.nl_writer import NLWriterEmptyModelError +from pyomo.common.errors import EmptyModelError class TestExternalGreyBoxModel(unittest.TestCase): @@ -613,7 +613,7 @@ def test_error_no_variables(self): m.egb = ExternalGreyBoxBlock() m.egb.set_external_model(ex_models.PressureDropSingleOutput()) m.obj = pyo.Objective(expr=1) - with self.assertRaises(NLWriterEmptyModelError): + with self.assertRaises(EmptyModelError): pyomo_nlp = PyomoGreyBoxNLP(m) def test_error_fixed_inputs_outputs(self): diff --git a/pyomo/contrib/pynumero/interfaces/tests/test_pyomo_grey_box_nlp.py b/pyomo/contrib/pynumero/interfaces/tests/test_pyomo_grey_box_nlp.py index f358d08d6a5..6bcdcc83acb 100644 --- a/pyomo/contrib/pynumero/interfaces/tests/test_pyomo_grey_box_nlp.py +++ b/pyomo/contrib/pynumero/interfaces/tests/test_pyomo_grey_box_nlp.py @@ -41,7 +41,7 @@ check_sparse_matrix_specific_order, ) import pyomo.contrib.pynumero.interfaces.tests.external_grey_box_models as ex_models -from pyomo.repn.plugins.nl_writer import NLWriterEmptyModelError +from pyomo.common.errors import EmptyModelError class TestExternalGreyBoxAsNLP(unittest.TestCase): @@ -1034,7 +1034,7 @@ def test_error_no_variables(self): m.egb = ExternalGreyBoxBlock() m.egb.set_external_model(ex_models.PressureDropSingleOutput()) m.obj = pyo.Objective(expr=1) - with self.assertRaises(NLWriterEmptyModelError): + with self.assertRaises(EmptyModelError): pyomo_nlp = PyomoNLPWithGreyBoxBlocks(m) def test_error_fixed_inputs_outputs(self): diff --git a/pyomo/repn/plugins/ampl/ampl_.py b/pyomo/repn/plugins/ampl/ampl_.py index cc99e9cfdae..679dde5f490 100644 --- a/pyomo/repn/plugins/ampl/ampl_.py +++ b/pyomo/repn/plugins/ampl/ampl_.py @@ -22,6 +22,7 @@ from pyomo.common.fileutils import find_library from pyomo.common.gc_manager import PauseGC +from pyomo.common.errors import EmptyModelError from pyomo.opt import ProblemFormat, AbstractProblemWriter, WriterFactory import pyomo.core.expr as EXPR from pyomo.core.expr.numvalue import ( @@ -1361,7 +1362,7 @@ def _print_model_NL( subsection_timer.reset() if len(full_var_list) < 1: - raise ValueError( + raise EmptyModelError( "No variables appear in the Pyomo model constraints or" " objective. This is not supported by the NL file interface" ) diff --git a/pyomo/repn/plugins/nl_writer.py b/pyomo/repn/plugins/nl_writer.py index 833a7881699..2b74b035760 100644 --- a/pyomo/repn/plugins/nl_writer.py +++ b/pyomo/repn/plugins/nl_writer.py @@ -25,7 +25,7 @@ document_kwargs_from_configdict, ) from pyomo.common.deprecation import relocated_module_attribute -from pyomo.common.errors import InfeasibleConstraintException, PyomoException +from pyomo.common.errors import InfeasibleConstraintException, EmptyModelError from pyomo.common.gc_manager import PauseGC from pyomo.common.timing import TicTocTimer @@ -85,13 +85,6 @@ ) -class NLWriterEmptyModelError(PyomoException): - """ - A custom exception to allow handling of a - model with no free variables. - """ - - # TODO: make a proper base class class NLWriterInfo(object): """Return type for NLWriter.write() @@ -327,7 +320,7 @@ def __call__(self, model, filename, solver_capability, io_options): if config.symbolic_solver_labels: os.remove(row_fname) os.remove(col_fname) - raise NLWriterEmptyModelError( + raise EmptyModelError( "No variables appear in the Pyomo model constraints or" " objective. This is not supported by the NL file interface" )