简体   繁体   English

GEKKO Python 中的线性规划

[英]Linear Programming in GEKKO Python

Currently solving a LP problem by GEKKO Library in python.目前正在通过 python 中的 GEKKO 库解决 LP 问题。 The problem has like 8,000 variables.这个问题有大约 8,000 个变量。 it runs fine with 462 variables if I increase the number of variables.如果我增加变量的数量,它在 462 个变量上运行良好。 The program shows an error "Exception: @error: Model Expression *** Error in syntax of function string: Invalid element: ,>=0"程序显示错误“异常:@error:Model 表达式 *** function 字符串的语法错误:无效元素:,>=0”

Any comment will be helpful.任何评论都会有所帮助。

#Initialize Model
m = GEKKO() 
#Set global options
m.options.solver = 3
m.options.IMODE = 3 #steady state optimization

#define parameter
Num_Cell= 463
H2 = m.Array(m.Var,Num_Cell,value = 0)
Demand = m.Const(value=20000000) 
D = np.zeros(Num_Cell)
F = Hardwood_Sawlog.SumOfTotal

for i in range(Num_Cell):
    H2[i].lower = 0
    H2[i].upper = F[i]
    D[i] = i

m.Equation(m.sum(H2[0:Num_Cell])==Demand)
m.Obj(np.dot(D,H2))
m.solve(disp=False,debug=True)

print('') 
print(H2)

The problem is that the symbolic form of m.Obj(np.dot(D,H2)) exceeds the maximum length of 15,000 characters with the larger problem.问题是m.Obj(np.dot(D,H2))的符号形式超过了 15,000 个字符的最大长度,问题较大。 Here is a version of your problem that produces an error:这是您的问题的一个版本,它会产生错误:

from gekko import GEKKO
import numpy as np
m = GEKKO() 
m.options.solver = 3
m.options.IMODE = 3
Num_Cell= 1000
H2 = m.Array(m.Var,Num_Cell,value = 0)
Demand = m.Const(value=20000000) 
D = np.zeros(Num_Cell)
for i in range(Num_Cell):
    H2[i].lower = 0
    H2[i].upper = 100
    D[i] = i
m.Equation(m.sum(H2[0:Num_Cell])==Demand)
m.Obj(np.dot(D,H2))
m.solve(disp=False,debug=True)
print('') 
print(H2)

Here is an alternative version that overcomes the problem with np.dot() constructing the long (>15,000 character) symbolic expression.这是一个替代版本,它克服了np.dot()构造长(> 15,000 个字符)符号表达式的问题。

from gekko import GEKKO
import numpy as np
m = GEKKO() 
m.options.solver = 1
m.options.IMODE = 3
Num_Cell= 1000
H2 = m.Array(m.Var,Num_Cell,value = 0)
Demand = m.Const(value=20000000) 
D = np.zeros(Num_Cell)
for i in range(Num_Cell):
    H2[i].lower = 0
    H2[i].upper = 1e10
    D[i] = i
m.Equation(m.sum(H2[0:Num_Cell])==Demand)
m.Minimize(m.sum([D[i]*H2[i] for i in range(Num_Cell)]))
m.solve(disp=True)
print('') 
print(H2)

There are linear programming model building functions in gekko that help with constructing large-scale LP problems with sparse matrices or large dense matrices: Gekko 中有线性规划gekko构建函数,可帮助构建具有稀疏矩阵或大型密集矩阵的大规模 LP 问题:

线性规划

An even more efficient way is to use the built-in model building functions .更有效的方法是使用内置的 model 构建函数

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

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