Pyomo / pyomo

An object-oriented algebraic modeling language in Python for structured optimization problems.
https://www.pyomo.org
Other
2.01k stars 517 forks source link

ApplicationError from Ipopt not caught by Pyomo (5.5) #472

Closed maajdl closed 2 years ago

maajdl commented 6 years ago

Hello,

I often have an ApplicationError triggered by Ipopt (Windows, not checked on Linux). This terminates Pyomo abruptly. I solved that easily for my own purpose by:

      - ctypes.windll.kernel32.SetErrorMode(3)
      - catching the exception that occurs
      - creating a  SolverResults   object to return to the caller
          (the crash occurs before any  SolverResults  being created by solve())

In this way the ApplicationError behaves as any other error that Ipopt would return. (I then simply try again with an Ipopt version that would hopefully do the job)

Maybe it could be useful to anticipate this ApplicationError directly within Pyomo to allow for a smooth exception handling.

Thanks,

Michel

maajdl commented 6 years ago

In another circumstance, I just got this error from Pyomo:

"Cannot load a SolverResults object with bad status: error"

Maybe the purpose of the SolverResults object is not totally clear for me. Nevertheless, it was useful that I could cre atethe SolverResults object to handle the ApplicationError . Maybe a SolverResults object should always be available whatever actually happens during solve() ? That's a further question.

Thanks,

Michel

jsiirola commented 6 years ago

Can you post the full stack trace that is generated by the ApplicationError exception?

I believe this may be an exception that Pyomo generates itself. We have started converting that exception into an error (see #372 where this was discussed / implemented for kernel models), and that should be propagated to the main Pyomo AML models.

maajdl commented 6 years ago

Here is what I get by printing the traceback object:

  File "C:\Users\mjtoys\Documents\_Work\Jupyter notebooks\cementPy\cementPy\simplemodel.py", line 60, in solve
    model.modelresult = solver.solve(model, tee=tee, keepfiles=keepfiles)
  File "C:\Users\mjtoys\Anaconda3\lib\site-packages\pyomo\opt\base\solvers.py", line 626, in solve
    "Solver (%s) did not exit normally" % self.name)

The simplemodel is just my model. The solver used is Ipopt 3.12.8 in the test.

These are discussions related to the Ipopt problem:

how to analyze "ipopt.exe has stopped working" ApplicationError pyutilib / Ipopt for some models, disappears if print_level option is specified ApplicationError pyutilib / Ipopt for some models, disappears if print_level option is specified

Thanks a lot

edmundus commented 5 years ago

In the following minimal example, the return status of IPOPT also leads to a crash on linux:

from pyomo.environ import *

def _func(m):
    return 1e10 *sum( [(m.a * exp(-m.b * m.x[i] ) + m.c * m.x[i] + m.d - m.y[i])**2 for i in range(m.len)])

def fit_params(_func,x,y,):
    lb2 = [   0,   0,-1e2,-1e2]
    ub2 = [ 1e2, 2e1,   0,-1e2] 
    m = ConcreteModel() 
    m.lb = Param([0,1,2,3],initialize={0:lb2[0],1:lb2[1],2:lb2[2],3:lb2[3]},mutable=True)
    m.ub = Param([0,1,2,3],initialize={0:ub2[0],1:ub2[1],2:ub2[2],3:ub2[3]},mutable=True)
    m.a = Var(bounds = (m.lb[0],m.ub[0]),initialize=1)
    m.b = Var(bounds = (m.lb[1],m.ub[1]),initialize=1)
    m.c = Var(bounds = (m.lb[2],m.ub[2]),initialize=-1)
    m.d = Var(bounds = (m.lb[3],m.ub[3]),initialize=0)
    m.len = len(y)
    m.x = x
    m.y = y
    m.obj = Objective(rule=_func,sense=minimize)   
    options={'max_cpu_time': 1}
    solver = SolverFactory('ipopt')
    results = solver.solve(m,options=options,tee=False)
    return value(m.a), value(m.b), value(m.c), value(m.d)

x = [-5, -4, -3, -2, -1,  0]
y = [ 0.,         -0.00733098, -0.01413552, -0.02045021, -0.02630905, -0.03174367]
print(fit_params(_func,x,y,))

i get the following error traceback:


  File "/home/lukas/test/pyomoError.py", line 27, in <module>
    print(fit_params(_func,x,y,))
  File "/home/lukas/test/pyomoError.py", line 22, in fit_params
    results = solver.solve(m,options=options,tee=False)
  File "/home/lukas/anaconda3/lib/python3.6/site-packages/pyomo/opt/base/solvers.py", line 659, in solve
    default_variable_value=self._default_variable_value)
  File "/home/lukas/anaconda3/lib/python3.6/site-packages/pyomo/core/base/PyomoModel.py", line 249, in load_from
    % str(results.solver.status))
ValueError: Cannot load a SolverResults object with bad status: error```
blnicho commented 2 years ago

The ValueError is expected behavior, by default Pyomo raises an error if the solver did not terminate optimally. Since we haven't heard reports from other users on the ApplicationError that was reported here I think this can be closed.