Skip to content

Commit

Permalink
Merge pull request #3135 from jsiirola/nl-error-empty-model
Browse files Browse the repository at this point in the history
NLv2: only raise exception for empty models in the legacy API
  • Loading branch information
michaelbynum authored Feb 14, 2024
2 parents 7c429b1 + 1462403 commit 7e9da37
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions pyomo/repn/plugins/nl_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,18 @@ def __call__(self, model, filename, solver_capability, io_options):
row_fname
) as ROWFILE, _open(col_fname) as COLFILE:
info = self.write(model, FILE, ROWFILE, COLFILE, config=config)
if not info.variables:
# This exception is included for compatibility with the
# original NL writer v1.
os.remove(filename)
if config.symbolic_solver_labels:
os.remove(row_fname)
os.remove(col_fname)
raise ValueError(
"No variables appear in the Pyomo model constraints or"
" objective. This is not supported by the NL file interface"
)

# Historically, the NL writer communicated the external function
# libraries back to the ASL interface through the PYOMO_AMPLFUNC
# environment variable.
Expand Down Expand Up @@ -854,13 +866,6 @@ def write(self, model):
con_vars = con_vars_linear | con_vars_nonlinear
all_vars = con_vars | obj_vars
n_vars = len(all_vars)
if n_vars < 1:
# TODO: Remove this. This exception is included for
# compatibility with the original NL writer v1.
raise ValueError(
"No variables appear in the Pyomo model constraints or"
" objective. This is not supported by the NL file interface"
)

continuous_vars = set()
binary_vars = set()
Expand Down

0 comments on commit 7e9da37

Please sign in to comment.