Skip to content

NLv2: only raise exception for empty models in the legacy API #3135

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

Merged
merged 3 commits into from
Feb 14, 2024
Merged
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
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