Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NL writer: Add custom exception for no free variables #3445

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,6 +38,7 @@
check_sparse_matrix_specific_order,
)
import pyomo.contrib.pynumero.interfaces.tests.external_grey_box_models as ex_models
from pyomo.common.errors import EmptyModelError


class TestExternalGreyBoxModel(unittest.TestCase):
Expand Down Expand Up @@ -612,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(ValueError):
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,6 +41,7 @@
check_sparse_matrix_specific_order,
)
import pyomo.contrib.pynumero.interfaces.tests.external_grey_box_models as ex_models
from pyomo.common.errors import EmptyModelError


class TestExternalGreyBoxAsNLP(unittest.TestCase):
Expand Down Expand Up @@ -1033,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(ValueError):
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
4 changes: 2 additions & 2 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 DeveloperError, InfeasibleConstraintException
from pyomo.common.errors import InfeasibleConstraintException, EmptyModelError
from pyomo.common.gc_manager import PauseGC
from pyomo.common.timing import TicTocTimer

Expand Down Expand Up @@ -320,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 ValueError(
raise EmptyModelError(
"No variables appear in the Pyomo model constraints or"
" objective. This is not supported by the NL file interface"
)
Expand Down
Loading