【问题标题】:How do I find GEKKO application success status?我如何找到 GEKKO 申请成功状态?
【发布时间】:2023-01-13 08:33:48
【问题描述】:

我在 try .. except 构造中运行 m.solve() 以优雅地处理求解器由于最大迭代或收敛到不可行而引发的任何异常,但想询问 APPINFO 和 APPSTATUS 以确定是否找到解决方案。我很惊讶地发现我似乎总是得到 APPINFO=0APPSTATUS=1,即使求解器报告未找到解决方案。

我在解释 APPINFO 和 APPSTATUS 上的文档时遗漏了什么?

重现错误的一段代码。

from gekko import GEKKO

m=GEKKO(remote=False)

m.x=m.Var()
m.y=m.Var()

m.total=m.Intermediate(m.x+m.y)

m.Equation(m.total>20)  #if included, no feasible solution exists
m.Equation(m.x<9)
m.Equation(m.y<9)
m.Maximize(m.total)
m.options.SOLVER=3
try:
   m.solve()
except Exception as e:
    print('Exception',e)
    
print('APPINFO', m.options.APPINFO)
print('APPSTATUS', m.options.APPSTATUS)

【问题讨论】:

    标签: python gekko


    【解决方案1】:

    当 Gekko 无法解决时,使用 debug=False 不引发异常。当出现异常时,结果不会加载回m.options

    from gekko import GEKKO
    
    m=GEKKO(remote=False)
    
    m.x=m.Var()
    m.y=m.Var()
    
    m.total=m.Intermediate(m.x+m.y)
    
    m.Equation(m.total>20)  #if included, no feasible solution exists
    m.Equation(m.x<9)
    m.Equation(m.y<9)
    m.Maximize(m.total)
    m.options.SOLVER=3
    m.solve(debug=False)
        
    print('APPINFO', m.options.APPINFO)
    print('APPSTATUS', m.options.APPSTATUS)
    

    这会产生正确的错误响应:

     ---------------------------------------------------
     Solver         :  IPOPT (v3.12)
     Solution time  :  0.0156 sec
     Objective      :  -18.023281704731964
     Unsuccessful with error code  0
     ---------------------------------------------------
     
     Creating file: infeasibilities.txt
     Use command apm_get(server,app,'infeasibilities.txt') to retrieve file
     @error: Solution Not Found
    
    APPINFO 2
    APPSTATUS 0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-23
      • 2020-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-30
      • 1970-01-01
      相关资源
      最近更新 更多