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: no variables appear #3411

Open
alma-walmsley opened this issue Nov 8, 2024 · 0 comments · May be fixed by #3445
Open

NL Writer: no variables appear #3411

alma-walmsley opened this issue Nov 8, 2024 · 0 comments · May be fixed by #3445

Comments

@alma-walmsley
Copy link
Contributor

Summary

I want to know if there is a way to detect that all variables in the model have been fixed (and therefore solving can be skipped). Currently the nl_writer throws a ValueError if there are no variables to solve for.

Rationale

I am working on a model (within IDAES) which has both variables and expressions. The user chooses which variables or expressions to set (for example, they are required to set one of mass flow or molar flow). If the user tries to set the value for an expression, then a constraint is added. If the user tries to set a variable, then the model will use fix() to fix the variable directly.

  • Add a mass flow constraint, solves for molar flow:
m = ConcreteModel()
m.flow_mol = Var()
m.flow_mass = Expression(expr=m.flow_mol * 2)
m.mass_constraint = Constraint(expr=m.flow_mass == 10)
  • Fixing the molar flow directly
m = ConcreteModel()
m.flow_mol = Var()
m.flow_mass = Expression(expr=m.flow_mol * 2)
m.flow_mol.fix(5)

The first example runs fine, because the variable is unfixed and the solver adjusts it to find the correct solution. Meanwhile, the second example fails to solve; the nl writer raises ValueError: No variables appear in the Pyomo model constraints or objective. This is not supported by the NL file interface. It does make sense that a solve is not needed - all the variables have been fixed and so any expressions can just be evaluated.

However, in my case, it is difficult to determine when exactly this case will happen, since the user chooses what variables/expressions to set. I am not sure if there is an easy way to check this? I could do a try...catch around the solve command, but I am hesitant to do this since it raises a ValueError which is pretty generic (I do want to error if there are other problems).

Description

My suggestion is to make the Exception more specific:

class NLWriterNoVariablesAppearException(ValueError):
    pass

# ... nl_writer.py, line 316:
if not info.variables:
    raise NLWriterNoVariablesAppearException("No variables appear ...")

And then I can import this specific Exception and do a try...catch:

try:
    solver.solve()
except NLWriterNoVariablesAppearException:
    pass  # don't need to solve for anything
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant