Skip to content

Commit

Permalink
add EmptyModelError to pyomo common errors, add to nl writers
Browse files Browse the repository at this point in the history
  • Loading branch information
alma-walmsley committed Dec 31, 2024
1 parent 0b9cdcf commit 7815ea6
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
9 changes: 9 additions & 0 deletions pyomo/common/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion pyomo/repn/plugins/ampl/ampl_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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"
)
Expand Down
11 changes: 2 additions & 9 deletions pyomo/repn/plugins/nl_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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"
)
Expand Down

0 comments on commit 7815ea6

Please sign in to comment.