简体   繁体   中英

Linear Programming in GEKKO Python

Currently solving a LP problem by GEKKO Library in python. The problem has like 8,000 variables. it runs fine with 462 variables if I increase the number of variables. The program shows an error "Exception: @error: Model Expression *** Error in syntax of function string: Invalid element: ,>=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. 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.

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:

线性规划

An even more efficient way is to use the built-in model building functions .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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