You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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:
classNLWriterNoVariablesAppearException(ValueError):
pass# ... nl_writer.py, line 316:ifnotinfo.variables:
raiseNLWriterNoVariablesAppearException("No variables appear ...")
And then I can import this specific Exception and do a try...catch:
try:
solver.solve()
exceptNLWriterNoVariablesAppearException:
pass# don't need to solve for anything
The text was updated successfully, but these errors were encountered:
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.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 aValueError
which is pretty generic (I do want to error if there are other problems).Description
My suggestion is to make the Exception more specific:
And then I can import this specific Exception and do a try...catch:
The text was updated successfully, but these errors were encountered: