Python GEKKO:如何避免从 IPOPT 引发错误 "Solved to Acceptable Level."

Python GEKKO: How to avoid raising an error from IPOPT "Solved to Acceptable Level."

我正在模拟中求解一组方程(IMODE = 1,SOLVER = 3)。 IPOPT 求解器求解到可接受的水平并退出,但 gekko return 对此出错并执行 return 我的解决方案。根据 IPOPT 文档,可接受级别的公差为 1.0e-6,这与 gekko 使用的 OTOL 和 RTOL 的默认值(以及我正在使用的值)相同。我能够修改 gekko.py 源代码以获得对 return 的回答,但这样做我绕过了所有类型的错误。我不希望绕过所有错误,因为它们显然有助于调试其他问题,如不可行性。是否有我遗漏的 m.solve 选项,或者在 IPOPT 解决到可接受的水平时不触发错误的其他方法?

处理求解器错误的一种方法是将求解命令包装在 tryexcept 语句中。 APPINFO output may give you guidance on what type of error was encountered and let you respond differently to "infeasible solution", "solved to acceptable level", or other IPOPT error codes.

try:
   m.solve(disp=True)
except:
   print('Solver error, looking at APPINFO')
   if m.options.APPINFO==1:
      print('APPINFO=1')
   elif m.options.APPINFO==2:
      print('APPINFO=2')

另一种选择是try a different solver such as APOPT or BPOPT

m.options.SOLVER = 1

编辑: 当 Gekko 引发求解器异常时,参数 APPINFO 不会更新。使用 debug=0 尝试以下操作:

m.solve(disp=True,debug=0)
if m.options.APPINFO!=0:
   print('Solver error, looking at APPINFO')
if m.options.APPINFO==1:
   print('APPINFO=1')
elif m.options.APPINFO==2:
   print('APPINFO=2')

我刚刚更新了 Gekko,这样远程解决也将绕过引发的异常并完成处理带有 APPINFO 信息的选项文件。 APPINFO信息在运行本地运行目录下options.json,在gk_post_solve.py.[=24中用load_JSON读入=]