[英]GEKKO Error: @error: Insufficient Model Model must contain at least 1 variable and 1 equation STOPPING
I am currently working on a model Mixed-Integer Non-Linear Programming (Bin Packing problem) for a university project, and I am trying to solve it using GEKKO.我目前正在为一个大学项目研究 model 混合整数非线性规划(装箱问题),我正在尝试使用 GEKKO 解决它。
Any piece of advice would be much appreciated.任何建议将不胜感激。
# ___Initialize model___
m = GEKKO()
# ___Global options_____
m.options.SOLVER = 1
ni = 4550
nj =6
nk= 35
# variables:
x = m.Array(m.Var, (ni,nk,nj), lb=0, integer=True) #integer variable
y = m.Array(m.Var, (nj,nk), lb=0, ub=1, integer=True) #binary variable
# Parameters:
w1=m.Const(w)
h1=m.Const(h)
Psum_total=m.Const(value=sum_total)
P = m.Array(m.Param,ni)
for i in range(ni):
P[i].value = PPP[i]
V = m.Array(m.Param,ni)
for i in range(ni):
V[i].value=VV[i]
d= m.Array(m.Param,ni)
for i in range(ni):
d[i] = 50
Psum_vetor=m.Array(m.Param,nj)
for j in range(nj):
Psum_vetor[j].value=sumP[j]
#Intermediate
Lkj = [[m.Intermediate(m.sum([P[i]*x[i][k][j] for i in range(ni)])) for j in range(nj)] for k in range(nk)]
xyi = [m.Intermediate(m.sum([ x[i][k][j]*y[j][k] for k in range(nk) for j in range(nj)])) for i in range(ni)]
z= m.Intermediate(m.sum([xyi[i]*V[i] for i in range(ni)]))
s = Psum_total
for k in range(nk):
for j in range(nj):
s += Lkj[k][j]
# Equations
m.Equation([xyi[i] <= d[i] for i in range(ni)])
m.Equation([sum([ Lkj[k][j]*y[j][k] for j in range(nj)]) <= h1*w1 for k in range(nk)])
m.Equation([sum([Lkj[k][j]*y[j][k] for k in range(nk)]) == s/7 - P[j] for j in range(nj)])
m.Obj(-z)
m.options.IMODE = 3
m.solve()
print(x)
OUTPUT: OUTPUT:
apm 35.186.172.229_gk_model1 <br><pre> ----------------------------------------------------------------
APMonitor, Version 1.0.1
APMonitor Optimization Suite
----------------------------------------------------------------
--------- APM Model Size ------------
Each time step contains
Objects : 0
Constants : 0
Variables : 0
Intermediates: 0
Connections : 0
Equations : 0
Residuals : 0
@error: Insufficient Model
Model must contain at least 1 variable and 1 equation
STOPPING...
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-17-9adce22534dc> in <module>
101
102 # Solve minimization
--> 103 m.solve()
104
105 print(x)
/usr/local/lib/python3.7/dist-packages/gekko/gekko.py in solve(self, disp, debug, GUI, **kwargs)
2183 #print APM error message and die
2184 if (debug >= 1) and ('@error' in response):
-> 2185 raise Exception(response)
2186
2187 #load results
Exception: @error: Insufficient Model
Model must contain at least 1 variable and 1 equation
STOPPING...
According to output, it seems to be that it's not recognizing variables or equations.根据output,似乎是不识别变量或方程。
Thank you in advance for your help.预先感谢您的帮助。
Here is a version that solves successfully:这是一个成功解决的版本:
# ___Initialize model___
from gekko import GEKKO
m = GEKKO()
# ___Global options_____
m.options.SOLVER = 1
ni = 4
nj =6
nk= 3
# variables:
x = m.Array(m.Var, (ni,nk,nj), lb=0, integer=True) #integer variable
y = m.Array(m.Var, (nj,nk), lb=0, ub=1, integer=True) #binary variable
# Parameters:
w=1; h=1; sum_total=1
PPP=[1]*nj; VV = [1]*ni; sumP = [1]*nj
w1=m.Const(w)
h1=m.Const(h)
Psum_total=m.Const(value=sum_total)
P = m.Array(m.Param,nj)
for i in range(nj):
P[i].value = PPP[i]
V = m.Array(m.Param,ni)
for i in range(ni):
V[i].value=VV[i]
d= m.Array(m.Param,ni)
for i in range(ni):
d[i] = 50
Psum_vetor=m.Array(m.Param,nj)
for j in range(nj):
Psum_vetor[j].value=sumP[j]
#Intermediate
Lkj = [[m.Intermediate(m.sum([P[i]*x[i][k][j] for i in range(ni)])) for j in range(nj)] for k in range(nk)]
xyi = [m.Intermediate(m.sum([ x[i][k][j]*y[j][k] for k in range(nk) for j in range(nj)])) for i in range(ni)]
z= m.Intermediate(m.sum([xyi[i]*V[i] for i in range(ni)]))
s = Psum_total
for k in range(nk):
for j in range(nj):
s += Lkj[k][j]
# Equations
m.Equation([xyi[i] <= d[i] for i in range(ni)])
m.Equation([sum([ Lkj[k][j]*y[j][k] for j in range(nj)]) <= h1*w1 for k in range(nk)])
m.Equation([sum([Lkj[k][j]*y[j][k] for k in range(nk)]) == s/7 - P[j] for j in range(nj)])
m.Maximize(z)
m.options.IMODE = 3
m.solve()
print(x)
with solution:与解决方案:
Successful solution
---------------------------------------------------
Solver : APOPT (v1.0)
Solution time : 10.8742999999959 sec
Objective : -1.338429367332205E-014
Successful solution
---------------------------------------------------
[[[[0.0] [0.0] [0.0] [0.0] [0.0] [0.0]]
[[0.0] [0.0] [0.0] [0.0] [0.0] [0.0]]
[[0.0] [0.0] [0.0] [0.0] [0.0] [0.0]]]
[[[0.0] [0.0] [0.0] [0.0] [0.0] [0.0]]
[[0.0] [0.0] [0.0] [0.0] [0.0] [0.0]]
[[0.0] [0.0] [0.0] [0.0] [0.0] [0.0]]]
[[[0.0] [0.0] [0.0] [0.0] [0.0] [0.0]]
[[0.0] [0.0] [0.0] [0.0] [0.0] [0.0]]
[[0.0] [0.0] [0.0] [0.0] [0.0] [0.0]]]
[[[0.0] [0.0] [0.0] [0.0] [0.0] [0.0]]
[[0.0] [0.0] [0.0] [6.0] [0.0] [0.0]]
[[0.0] [0.0] [0.0] [0.0] [0.0] [0.0]]]]
The original problem size with 4550x6x35
+ 6x35
= 955710
binary variables may take a while to solve.具有
4550x6x35
+ 6x35
= 955710
二进制变量的原始问题大小可能需要一段时间才能解决。 The APOPT
solver options can be adjusted to improve solution speed.可以调整
APOPT
求解器选项以提高求解速度。
If there is an error with the example code, try:如果示例代码有错误,请尝试:
pip install gekko --upgrade
to upgrade the gekko version to the latest distribution.将 gekko 版本升级到最新的发行版。
I just see that it was an index error.我只是看到这是一个索引错误。 Thanks @Hedengren
感谢@Hedengren
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.