Robbybp / surrogate-vs-implicit

Comparing surrogate models and implicit function formulations for chemical process models
Other
1 stars 0 forks source link

Implicit Flowsheet including the mixer. #11

Closed sbugosen closed 7 months ago

sbugosen commented 10 months ago

This PR contains two files.

  1. The first file titled fullspace_fs_new_code_structure contains the full space formulation. Here, I created a Pyomo Block containing the reformer_mix and the reformer.
  2. The second file titled implicit_fs_mixer_included imports the full space flowsheet described above and attempts to solve it. This new formulation has 760 variables, which is similar to the amount of variables contained in the ALAMO formulation. However, after 1 iteration in Cyipopt, we get the following error:

RuntimeError: Linesearch iteration limit reached; remaining residual = -2.384185791015625e-07.

Robbybp commented 10 months ago

How long does this take to fail for you? When I run implicit_fs_mixer_included.py I get a reported 260 s spend in Ipopt. I get the following traceback:

******************************************************************************                                                                                                                                                                       
This program contains Ipopt, a library for large-scale nonlinear optimization.                                                                                                                                                                       
 Ipopt is released as open source code under the Eclipse Public License (EPL).                                                                                                                                                                       
         For more information visit https://github.com/coin-or/Ipopt                                                                                                                                                                                 
******************************************************************************                                                                                                                                                                       

This is Ipopt version 3.14.11, running with linear solver ma27.                                                                                                                                                                                      

Number of nonzeros in equality constraint Jacobian...:     2852                                                                                                                                                                                      
Number of nonzeros in inequality constraint Jacobian.:        4                                                                                                                                                                                      
Number of nonzeros in Lagrangian Hessian.............:     4359                                                                                                                                                                                      

Total number of variables............................:      760                                                                                                                                                                                      
                     variables with only lower bounds:       25                                                                                                                                                                                      
                variables with lower and upper bounds:      711                                                                                                                                                                                      
                     variables with only upper bounds:        0                                                                                                                                                                                      
Total number of equality constraints.................:      757                                                                                                                                                                                      
Total number of inequality constraints...............:        4                                                                                                                                                                                      
        inequality constraints with only lower bounds:        1                                                                                                                                                                                      
   inequality constraints with lower and upper bounds:        0                                                                                                                                                                                      
        inequality constraints with only upper bounds:        3                                                                                                                                                                                      

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls                                                                                                                                                                     
   0 -9.0909091e-02 1.32e+06 1.00e+00  -1.0 0.00e+00    -  0.00e+00 0.00e+00   0                                                                                                                                                                     
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Evaluation error during soft restoration phase step.                                                                                                                                                                                        
Warning: Evaluation error during soft restoration phase step.                                                                                                                                                                                        
Warning: Evaluation error during soft restoration phase step.                                                                                                                                                                                        
Restoration phase failed due to evaluation errors.                                                                                                                                                                                                   

Number of Iterations....: 0                                                                                                                                                                                                                          

                                   (scaled)                 (unscaled)                                                                                                                                                                               
Exception of type: Eval_Error in file "Algorithm/IpOrigIpoptNLP.cpp" at line 499:                                                                                                                                                                    
 Exception message: success && IsFiniteNumber(ret) evaluated false: Error evaluating the objective function                                                                                                                                          

Number of objective function evaluations             = 14                                                                                                                                                                                            
Number of objective gradient evaluations             = 2                                                                                                                                                                                             
Number of equality constraint evaluations            = 23                                                                                                                                                                                            
Number of inequality constraint evaluations          = 11                                                                                                                                                                                            
Number of equality constraint Jacobian evaluations   = 1                                                                                                                                                                                             
Number of inequality constraint Jacobian evaluations = 1                                                                                                                                                                                             
Number of Lagrangian Hessian evaluations             = 1                                                                                                                                                                                             
Total seconds in IPOPT                               = 259.271                                                                                                                                                                                       

EXIT: Invalid number in NLP function or derivative detected.                                                                                                                                                                                         
ERROR: Exception encountered during cyipopt solve:                                                                                                                                                                                                   
Traceback (most recent call last):    
...
...
...                                                                                                                                                                                                               
pyomo.common.errors.IterationLimitError: Linesearch iteration limit reached solving for variable 'fs.combined_reformer.reformer_mix.mixed_state[0.0].temperature' using constraint 'fs.combined_reformer.reformer_mix.enthalpy_mixing_equations[0.0]'
; remaining residual = -2.384185791015625e-07.               

Does this match what you see?

Robbybp commented 10 months ago

When I hard-code a new tolerance of 1e-6 for calculate_variable_from_constraint, I get the following:

******************************************************************************                                            
This program contains Ipopt, a library for large-scale nonlinear optimization.                                            
 Ipopt is released as open source code under the Eclipse Public License (EPL).                                            
         For more information visit https://github.com/coin-or/Ipopt                                                      
******************************************************************************                                            

This is Ipopt version 3.14.11, running with linear solver ma27.                                                           

Number of nonzeros in equality constraint Jacobian...:     2852                                                           
Number of nonzeros in inequality constraint Jacobian.:        4                                                           
Number of nonzeros in Lagrangian Hessian.............:     4359                                                           

Total number of variables............................:      760                                                           
                     variables with only lower bounds:       25                                                           
                variables with lower and upper bounds:      711                                                           
                     variables with only upper bounds:        0                                                           
Total number of equality constraints.................:      757                                                           
Total number of inequality constraints...............:        4                                                           
        inequality constraints with only lower bounds:        1                                                           
   inequality constraints with lower and upper bounds:        0                                                           
        inequality constraints with only upper bounds:        3                                                           

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls                                          
   0 -9.0909091e-02 1.32e+06 1.00e+00  -1.0 0.00e+00    -  0.00e+00 0.00e+00   0                                                                                                                                                                     
Warning: SOC step rejected due to evaluation error                                                                        
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                       
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                       
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                       
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                       
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                       
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                       
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                       
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                       
Warning: Cutting back alpha due to evaluation error                                                                       
Warning: Evaluation error during soft restoration phase step.                                                             
Warning: Evaluation error during soft restoration phase step.                                                                                                                                                                                        
Warning: Evaluation error during soft restoration phase step.                                                             
Restoration phase failed due to evaluation errors.                                                                                                                                                                                                   

Number of Iterations....: 0                                                                                                                                                                                                                          

                                   (scaled)                 (unscaled)                                                                                                                                                                               
Exception of type: Eval_Error in file "Algorithm/IpOrigIpoptNLP.cpp" at line 499:                                         
 Exception message: success && IsFiniteNumber(ret) evaluated false: Error evaluating the objective function                                                                                                                                          

Number of objective function evaluations             = 7                                                                                                                                                                                             
Number of objective gradient evaluations             = 1                                                                  
Number of equality constraint evaluations            = 22                                                                 
Number of inequality constraint evaluations          = 3                                                                  
Number of equality constraint Jacobian evaluations   = 1                                                                  
Number of inequality constraint Jacobian evaluations = 1                                                                  
Number of Lagrangian Hessian evaluations             = 1                                                                  
Total seconds in IPOPT                               = 78.237                                                             

EXIT: Invalid number in NLP function or derivative detected.                                                              
Traceback (most recent call last):                                                                                        
...
...
...
pyomo.contrib.pynumero.exceptions.PyNumeroEvaluationError: Error in AMPL evaluation                                                                                                                                                                  
sbugosen commented 10 months ago

How long does this take to fail for you? When I run implicit_fs_mixer_included.py I get a reported 260 s spend in Ipopt. I get the following traceback:

******************************************************************************                                                                                                                                                                       
This program contains Ipopt, a library for large-scale nonlinear optimization.                                                                                                                                                                       
 Ipopt is released as open source code under the Eclipse Public License (EPL).                                                                                                                                                                       
         For more information visit https://github.com/coin-or/Ipopt                                                                                                                                                                                 
******************************************************************************                                                                                                                                                                       

This is Ipopt version 3.14.11, running with linear solver ma27.                                                                                                                                                                                      

Number of nonzeros in equality constraint Jacobian...:     2852                                                                                                                                                                                      
Number of nonzeros in inequality constraint Jacobian.:        4                                                                                                                                                                                      
Number of nonzeros in Lagrangian Hessian.............:     4359                                                                                                                                                                                      

Total number of variables............................:      760                                                                                                                                                                                      
                     variables with only lower bounds:       25                                                                                                                                                                                      
                variables with lower and upper bounds:      711                                                                                                                                                                                      
                     variables with only upper bounds:        0                                                                                                                                                                                      
Total number of equality constraints.................:      757                                                                                                                                                                                      
Total number of inequality constraints...............:        4                                                                                                                                                                                      
        inequality constraints with only lower bounds:        1                                                                                                                                                                                      
   inequality constraints with lower and upper bounds:        0                                                                                                                                                                                      
        inequality constraints with only upper bounds:        3                                                                                                                                                                                      

iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls                                                                                                                                                                     
   0 -9.0909091e-02 1.32e+06 1.00e+00  -1.0 0.00e+00    -  0.00e+00 0.00e+00   0                                                                                                                                                                     
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Cutting back alpha due to evaluation error                                                                                                                                                                                                  
Warning: Evaluation error during soft restoration phase step.                                                                                                                                                                                        
Warning: Evaluation error during soft restoration phase step.                                                                                                                                                                                        
Warning: Evaluation error during soft restoration phase step.                                                                                                                                                                                        
Restoration phase failed due to evaluation errors.                                                                                                                                                                                                   

Number of Iterations....: 0                                                                                                                                                                                                                          

                                   (scaled)                 (unscaled)                                                                                                                                                                               
Exception of type: Eval_Error in file "Algorithm/IpOrigIpoptNLP.cpp" at line 499:                                                                                                                                                                    
 Exception message: success && IsFiniteNumber(ret) evaluated false: Error evaluating the objective function                                                                                                                                          

Number of objective function evaluations             = 14                                                                                                                                                                                            
Number of objective gradient evaluations             = 2                                                                                                                                                                                             
Number of equality constraint evaluations            = 23                                                                                                                                                                                            
Number of inequality constraint evaluations          = 11                                                                                                                                                                                            
Number of equality constraint Jacobian evaluations   = 1                                                                                                                                                                                             
Number of inequality constraint Jacobian evaluations = 1                                                                                                                                                                                             
Number of Lagrangian Hessian evaluations             = 1                                                                                                                                                                                             
Total seconds in IPOPT                               = 259.271                                                                                                                                                                                       

EXIT: Invalid number in NLP function or derivative detected.                                                                                                                                                                                         
ERROR: Exception encountered during cyipopt solve:                                                                                                                                                                                                   
Traceback (most recent call last):    
...
...
...                                                                                                                                                                                                               
pyomo.common.errors.IterationLimitError: Linesearch iteration limit reached solving for variable 'fs.combined_reformer.reformer_mix.mixed_state[0.0].temperature' using constraint 'fs.combined_reformer.reformer_mix.enthalpy_mixing_equations[0.0]'
; remaining residual = -2.384185791015625e-07.               

Does this match what you see?

I do get the same remaining residual and the same error. It takes for me 627 s. I'm still using an IDE and IPOPT 3.14.9 with MUMPS. I'll try to see what i can do with that variable and constraint.

'fs.combined_reformer.reformer_mix.mixed_state[0.0].temperature'
'fs.combined_reformer.reformer_mix.enthalpy_mixing_equations[0.0]'
Robbybp commented 10 months ago

I'm not 100% sure what's going on here. I'm not sure why the AMPL evaluation error isn't getting caught and handled. I also don't understand why we get the same termination status when we encounter the RuntimeError, which should be unhandled.

From what I see here, I think the problem we should try to debug is the original evaluation error. I will run with halt_on_evaluation_error=True in CyIpopt and try to debug.

Robbybp commented 10 months ago

In the meantime, can you add an optional calc_var_kwds argument to DecomposedImplicitFunctionBase in this file: https://github.com/Pyomo/pyomo/blob/main/pyomo/contrib/pynumero/algorithms/solvers/implicit_functions.py#L558? This option should be a dict of options that get sent to calculate_variable_from_constraint here: https://github.com/Pyomo/pyomo/blob/d04f6bbecce8261bd7dbb622a99489ecadf54e67/pyomo/contrib/pynumero/algorithms/solvers/implicit_functions.py#L558. This way we will be able to easily send arguments like {"eps": 1e-6} to calculate_variable_from_constraint from our ExternalPyomoModel constructor. You can implement this on a branch of Pyomo, then open a PR on the main Pyomo repository. Let me know if you have any questions about how to do this.

sbugosen commented 10 months ago

The procedure is:

  1. I fork the Pyomo repository to my Github account.
  2. I create a branch on this forked repository.
  3. Implement the changes.
  4. Open PR in main Pyomo repo.

Is this correct?

On Wed, Jan 10, 2024 at 2:29 PM Robert Parker @.***> wrote:

In the meantime, can you add an optional calc_var_kwds argument to DecomposedImplicitFunctionBase in this file: https://github.com/Pyomo/pyomo/blob/main/pyomo/contrib/pynumero/algorithms/solvers/implicit_functions.py#L558? This option should be a dict of options that get sent to calculate_variable_from_constraint here: https://github.com/Pyomo/pyomo/blob/d04f6bbecce8261bd7dbb622a99489ecadf54e67/pyomo/contrib/pynumero/algorithms/solvers/implicit_functions.py#L558. This way we will be able to easily send arguments like {"eps": 1e-6} to calculate_variable_from_constraint from our ExternalPyomoModel constructor. You can implement this on a branch of Pyomo, then open a PR on the main Pyomo repository. Let me know if you have any questions about how to do this.

— Reply to this email directly, view it on GitHub https://github.com/Robbybp/surrogate-vs-implicit/pull/11#issuecomment-1885764113, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2YFNXXRZJOI7NH5YRWCLU3YN4B5DAVCNFSM6AAAAABBSEQLQSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOBVG43DIMJRGM . You are receiving this because you authored the thread.Message ID: @.***>

sbugosen commented 10 months ago

Only these changes? I'm not entirely sure.

image
Robbybp commented 10 months ago

That is almost correct. Your line self._calc_var_kwds = calc_var_kwds or {} should be self._calc_var_kwds = {} if calc_var_kwds is None else calc_var_kwds. Then this functionality will need to be tested. I would just find one of the tests in pyomo/contrib/pynumero/algorithms/solvers/tests/test_implicit_functions.py, and add a calc_var_kwds = {"eps": 1e-6} option to the SccImplicitFunctionSolver constructor. It is difficult to make sure that this option is actually doing something, so I think just passing the option and making sure it doesn't error is sufficient.

sbugosen commented 10 months ago

Can I complete this once my computer is all set up at the office? I want to do everything over the command line using Linux.

Robbybp commented 10 months ago

That sounds good to me

Robbybp commented 7 months ago

Closing this PR as we are not actively working on it. We can re-visit this PR/branch when/if we work on this.