简体   繁体   English

将 GEKKO 与 MINLP 求解器一起使用时如何知道最大方程数

[英]How to know the maximum number of Equations when using GEKKO with an MINLP solver

I am working on an optimization problem and I am using gekko with an APOPT solver to solve it, sometimes and when I have a lot of variables, I get the following error "exception: @error: max equation length".我正在研究一个优化问题,我正在使用带有 APOPT 求解器的 gekko 来解决它,有时当我有很多变量时,我会收到以下错误“异常:@error:最大方程长度”。
How could I know the "max equation length"?我怎么知道“最大方程长度”?

Variables and equations are written as a text file to the temporary folder before they are compiled into byte-code for efficient calculation of residuals, objective functions, sparse 1st derivatives, and sparse 2nd derivatives with automatic differentiation.变量和方程在编译成字节码之前以文本文件的形式写入临时文件夹,以便通过自动微分有效计算残差、目标函数、稀疏一阶导数和稀疏二阶导数。 Equations are limited to 15,000 characters each in gekko models, not just with the APOPT solver.gekko模型中,每个方程限制为 15,000 个字符,而不仅仅是使用 APPT 求解器。 This limit could be extended, but it is to encourage model building methods that improve compilation and solution speed.此限制可以扩展,但它是为了鼓励提高编译和求解速度的模型构建方法。 A simple gekko model demonstrates the issue with x = sum(p) as a vector of 10,000 values.一个简单的gekko模型演示了将x = sum(p)作为 10,000 个值的向量的问题。

from gekko import GEKKO
m = GEKKO(remote=False)
p = m.Array(m.Param,10000,value=1)
x = m.Var(lb=0,integer=True)
m.Equation(x==sum(p))
m.options.SOLVER = 1
m.open_folder()
m.solve()

This gives an error that the maximum equation length is exceeded.这给出了超出最大方程长度的错误。

Exception: @error: Max Equation Length
 Error with line number:  10008

The run folder is opened with m.open_folder() .使用m.open_folder()打开运行文件夹。 The gekko model is in the file gk_model0.apm that is inspected with an text editor. gekko模型位于文件gk_model0.apm中,该文件使用文本编辑器进行检查。 There are a few ways to reduce the equation length for this problem.有几种方法可以减少这个问题的方程长度。

  1. The parameters p can be an ordinary numpy array, instead of gekko parameters.参数p可以是普通的numpy数组,而不是gekko参数。 The summation is pre-computed before it is written to the model file.总和在写入模型文件之前是预先计算的。
from gekko import GEKKO
import numpy as np
m = GEKKO(remote=False)
p = np.ones(10000)
x = m.Var(lb=0,integer=True)
m.Equation(x==sum(p))
m.options.SOLVER = 1
m.open_folder()
m.solve()

This gives the following gk_model0.apm model:这给出了以下gk_model0.apm模型:

Model
Variables
    int_v1 = 0, >= 0
End Variables
Equations
    int_v1=10000.0
End Equations
End Model
  1. If the parameters p need to be gekko parameters or variables, then use the m.sum() function instead of sum() .如果参数p需要是gekko参数或变量,则使用m.sum()函数而不是sum() The use of m.sum() is more efficient than sum() or np.sum() for gekko optimization problems and avoids the problem with maximum equation length.对于gekko优化问题,使用m.sum()sum()np.sum()更有效,并且避免了最大方程长度的问题。 The compilation time is longer than the first option.编译时间比第一个选项长。
from gekko import GEKKO
m = GEKKO(remote=False)
p = m.Array(m.Param,10000,value=1)
x = m.Var(lb=0,integer=True)
m.Equation(x==m.sum(p))
m.options.SOLVER = 1
m.solve()
  1. Use m.Intermediate() when possible to reduce the equation size.尽可能使用m.Intermediate()来减小方程大小。 This special type of intermediate variable incorporates model reduction principles where a quantity is explicitly calculated once and used in multiple places in the model.这种特殊类型的中间变量结合了模型简化原则,其中一个数量被明确计算一次并在模型中的多个位置使用。 There are additional suggestions in questions / answers such as How to fix Python Gekko Max Equation Length error问题/答案中有其他建议,例如如何修复 Python Gekko Max Equation Length 错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM