问题
I am currently implementing a MINLP optimization problem in Python GEKKO for determining the optimal operational strategy of a trigeneration energy system. As I consider the energy demand during all periods of different representative days as input data, basically all my decision variables, intermediates, etc. are 2D arrays. I suspect that the declaration of the 2D intermediates is my problem. Right now I used list comprehension to declare 2D intermediates, but it seems like python cannot use these intermediates. Furthermore, the error This steady-state IMODE only allows scalar values. occurs.
Whenever I use the GEKKO m.Array function like this:
e_GT = m.Array(m.Intermediate(E_GT[z][p]/E_max_GT) for z in range(Z) for p in range(P), (Z,P))
it says, that the GEKKO object m.Intermediate cannot be called.
I would be very thankful if anyone could give me a hint.
Here is the complete code:
"""
Created on Fri Nov 22 10:18:33 2019
@author: julia
"""
# __Get GEKKO & numpy___
from gekko import GEKKO
import numpy as np
# ___Initialize model___
m = GEKKO()
# ___Global options_____
m.options.SOLVER = 1 # APOPT is MINLP Solver
# ______Constants_______
i = m.Const(value=0.05)
n = m.Const(value=10)
C_GT = m.Const(value=100000)
C_RB = m.Const(value=10000)
C_HB = m.Const(value=10000)
C_RS = m.Const(value=10000)
C_RE = m.Const(value=10000)
Z = 12
P = 24
E_min_GT = m.Const(value=1000)
E_max_GT = m.Const(value=50000)
F_max_GT = m.Const(value=100000)
Q_max_GT = m.Const(value=100000)
a = m.Const(value=1)
b = m.Const(value=1)
c = m.Const(value=1)
d = m.Const(value=1)
eta_RB = m.Const(value=0.01)
eta_HB = m.Const(value=0.01)
eta_RS = m.Const(value=0.01)
eta_RE = m.Const(value=0.01)
alpha = m.Const(value=0.01)
# ______Parameters______
T_z = m.Param([31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31])
C_p_Gas = m.Param(np.ones([P]))
C_p_Elec = m.Param(np.ones([P]))
E_d = np.ones([Z,P])
H_d = np.ones([Z,P])
K_d = np.ones([Z,P])
# _______Variables______
E_purch = m.Array(m.Var, (Z,P), lb=0)
E_GT = m.Array(m.Var, (Z,P), lb=0)
F_GT = m.Array(m.Var, (Z,P), lb=0)
Q_GT = m.Array(m.Var, (Z,P), lb=0)
Q_GT_RB = m.Array(m.Var, (Z,P), lb=0)
Q_disp = m.Array(m.Var, (Z,P), lb=0)
Q_HB = m.Array(m.Var, (Z,P), lb=0)
K_RS = m.Array(m.Var, (Z,P), lb=0)
K_RE = m.Array(m.Var, (Z,P), lb=0)
delta_GT = m.Array(m.Var, (Z,P), lb=0, ub=1, integer=True)
delta_RB = m.Array(m.Var, (Z,P), lb=0, ub=1, integer=True)
delta_HB = m.Array(m.Var, (Z,P), lb=0, ub=1, integer=True)
delta_RS = m.Array(m.Var, (Z,P), lb=0, ub=1, integer=True)
delta_RE = m.Array(m.Var, (Z,P), lb=0, ub=1, integer=True)
# ____Intermediates_____
R = m.Intermediate((i*(1+i)**n)/((1+i)**n-1))
e_min_GT = m.Intermediate(E_min_GT/E_max_GT)
e_GT = [m.Intermediate(E_GT[z][p]/E_max_GT) for z in range(Z) for p in range(P)]
f_GT = [m.Intermediate(F_GT[z][p]/F_max_GT) for z in range(Z) for p in range(P)]
q_GT = [m.Intermediate(Q_GT[z][p]/Q_max_GT) for z in range(Z) for p in range(P)]
Q_RB = [m.Intermediate(eta_RB*Q_GT_RB[z][p]*delta_RB[z][p]) for z in range(Z) for p in range(P)]
F_HB = [m.Intermediate(eta_HB*Q_HB[z][p]*delta_HB[z][p]) for z in range(Z) for p in range(P)]
Q_RS = [m.Intermediate(eta_RS*K_RS[z][p]*delta_RS[z][p]) for z in range(Z) for p in range(P)]
E_RE = [m.Intermediate(eta_RE*K_RE[z][p]*delta_RE[z][p]) for z in range(Z) for p in range(P)]
F_Gas = [m.Intermediate(F_GT[z][p] + eta_HB*Q_HB[z][p]*delta_HB[z][p]) for z in range(Z) for p in range(P)]
Cc = m.Intermediate(R*(C_GT + C_RB + C_HB + C_RS + C_RE))
Cr_z = m.Intermediate((sum(C_p_Gas[p]*F_Gas[z][p] + C_p_Elec[p]*E_purch[z][p]) for p in range(P)) for z in range(Z))
Cr = m.Intermediate(sum(Cr_z[z]*T_z[z]) for z in range(Z))
# ______Equations_______
m.Equation(e_min_GT[z][p]*delta_GT[z][p] <= e_GT[z][p] for z in range(Z) for p in range(P))
m.Equation(e_GT[z][p] <= 1*delta_GT[z][p] for z in range(Z) for p in range(P))
m.Equation(f_GT [z][p]== a*delta_GT[z][p] + b*e_GT[z][p] for z in range(Z) for p in range(P))
m.Equation(q_GT [z][p]== c*delta_GT[z][p] + d*e_GT[z][p] for z in range(Z) for p in range(P))
m.Equation(E_purch[z][p] + E_GT[z][p] == E_RE[z][p] + E_d[z][p] for z in range(Z) for p in range(P))
m.Equation(Q_GT[z][p] == Q_disp[z][p] + Q_GT_RB[z][p] for z in range(Z) for p in range(P))
m.Equation(Q_RB[z][p] + Q_HB[z][p] == Q_RS[z][p] + H_d[z][p] for z in range(Z) for p in range(P))
m.Equation(K_RS[z][p] + K_RE[z][p] == K_d[z][p] for z in range(Z) for p in range(P))
m.Equation(Q_disp[z][p] <= alpha*Q_GT[z][p] for z in range(Z) for p in range(P))
# ______Objective_______
m.Obj(Cc + Cr)
#_____Solve Problem_____
m.solve()
回答1:
Extra square brackets are needed for 2D list definition. This gives a 2D list with 3 rows and 4 columns.
[[p+10*z for p in range(3)] for z in range(4)]
# Result: [[0, 1, 2], [10, 11, 12], [20, 21, 22], [30, 31, 32]]
If you leave out the inner brackets, it is a 1D list of length 12.
[p+10*z for p in range(3) for z in range(4)]
# Result: [0, 10, 20, 30, 1, 11, 21, 31, 2, 12, 22, 32]
It also works when each element of the list is a Gekko Intermediate
.
[[m.Intermediate(p+10*z) for p in range(3)] for z in range(4)]
回答2:
To diagnose the problem, I added a call to open the run folder before the solve command.
#_____Solve Problem_____
m.open_folder()
m.solve()
I opened the gk_model0.apm
model file with a text editor to look at a text version of the model. At the bottom it shows that there are problems with the last two intermediates and 9 equations.
i2327=<generator object <genexpr> at 0x0E51BC30>
i2328=<generator object <genexpr> at 0x0E51BC30>
End Intermediates
Equations
<generator object <genexpr> at 0x0E51BC30>
<generator object <genexpr> at 0x0E51BC30>
<generator object <genexpr> at 0x0E51BC30>
<generator object <genexpr> at 0x0E51BC30>
<generator object <genexpr> at 0x0E51BC30>
<generator object <genexpr> at 0x0E51BC30>
<generator object <genexpr> at 0x0E51BC30>
<generator object <genexpr> at 0x0E51BC30>
<generator object <genexpr> at 0x0E51BC30>
minimize (i2326+i2328)
End Equations
End Model
This happens when the .value
property of a Param
or Var
is a lists or numpy array instead of a scalar value.
# ______Parameters______
#T_z = m.Param([31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31])
T_z = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
There were also some other problems such as
e_min_GT
referenced as an element of a list instead of a scalar value in the first equation- missing square brackets in the Intermediate list comprehensions to make the list 2D such as
[[2 for p in range(P)] for z in range(Z)]
- dimension mismatch from
for z in range(Z)] for p in range(P)]
instead offor p in range(P)] for z in range(Z)]
- for better efficiency, use
m.sum
instead ofsum
I made a few other changes as well. A differencing program should show them.
"""
Created on Fri Nov 22 10:18:33 2019
@author: julia
"""
# __Get GEKKO & numpy___
from gekko import GEKKO
import numpy as np
# ___Initialize model___
m = GEKKO()
# ___Global options_____
m.options.SOLVER = 1 # APOPT is MINLP Solver
# ______Constants_______
i = m.Const(value=0.05)
n = m.Const(value=10)
C_GT = m.Const(value=100000)
C_RB = m.Const(value=10000)
C_HB = m.Const(value=10000)
C_RS = m.Const(value=10000)
C_RE = m.Const(value=10000)
Z = 12
P = 24
E_min_GT = m.Const(value=1000)
E_max_GT = m.Const(value=50000)
F_max_GT = m.Const(value=100000)
Q_max_GT = m.Const(value=100000)
a = m.Const(value=1)
b = m.Const(value=1)
c = m.Const(value=1)
d = m.Const(value=1)
eta_RB = m.Const(value=0.01)
eta_HB = m.Const(value=0.01)
eta_RS = m.Const(value=0.01)
eta_RE = m.Const(value=0.01)
alpha = m.Const(value=0.01)
# ______Parameters______
T_z = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
C_p_Gas = np.ones(P)
C_p_Elec = np.ones(P)
E_d = np.ones([Z,P])
H_d = np.ones([Z,P])
K_d = np.ones([Z,P])
# _______Variables______
E_purch = m.Array(m.Var, (Z,P), lb=0)
E_GT = m.Array(m.Var, (Z,P), lb=0)
F_GT = m.Array(m.Var, (Z,P), lb=0)
Q_GT = m.Array(m.Var, (Z,P), lb=0)
Q_GT_RB = m.Array(m.Var, (Z,P), lb=0)
Q_disp = m.Array(m.Var, (Z,P), lb=0)
Q_HB = m.Array(m.Var, (Z,P), lb=0)
K_RS = m.Array(m.Var, (Z,P), lb=0)
K_RE = m.Array(m.Var, (Z,P), lb=0)
delta_GT = m.Array(m.Var, (Z,P), lb=0, ub=1, integer=True)
delta_RB = m.Array(m.Var, (Z,P), lb=0, ub=1, integer=True)
delta_HB = m.Array(m.Var, (Z,P), lb=0, ub=1, integer=True)
delta_RS = m.Array(m.Var, (Z,P), lb=0, ub=1, integer=True)
delta_RE = m.Array(m.Var, (Z,P), lb=0, ub=1, integer=True)
# ____Intermediates_____
R = m.Intermediate((i*(1+i)**n)/((1+i)**n-1))
e_min_GT = m.Intermediate(E_min_GT/E_max_GT)
e_GT = [[m.Intermediate(E_GT[z][p]/E_max_GT) for p in range(P)] for z in range(Z)]
f_GT = [[m.Intermediate(F_GT[z][p]/F_max_GT) for p in range(P)] for z in range(Z)]
q_GT = [[m.Intermediate(Q_GT[z][p]/Q_max_GT) for p in range(P)] for z in range(Z)]
Q_RB = [[m.Intermediate(eta_RB*Q_GT_RB[z][p]*delta_RB[z][p]) for p in range(P)] for z in range(Z)]
F_HB = [[m.Intermediate(eta_HB*Q_HB[z][p]*delta_HB[z][p]) for p in range(P)] for z in range(Z)]
Q_RS = [[m.Intermediate(eta_RS*K_RS[z][p]*delta_RS[z][p]) for p in range(P)] for z in range(Z)]
E_RE = [[m.Intermediate(eta_RE*K_RE[z][p]*delta_RE[z][p]) for p in range(P)] for z in range(Z)]
F_Gas = [[m.Intermediate(F_GT[z][p] + eta_HB*Q_HB[z][p]*delta_HB[z][p]) for p in range(P)] for z in range(Z)]
Cc = m.Intermediate(R*(C_GT + C_RB + C_HB + C_RS + C_RE))
Cr_z = [m.Intermediate(m.sum([C_p_Gas[p]*F_Gas[z][p] + C_p_Elec[p]*E_purch[z][p] for p in range(P)])) for z in range(Z)]
Cr = m.Intermediate(m.sum([Cr_z[z]*T_z[z] for z in range(Z)]))
# ______Equations_______
m.Equation([e_min_GT*delta_GT[z][p] <= e_GT[z][p] for z in range(Z) for p in range(P)])
m.Equation([e_GT[z][p] <= 1*delta_GT[z][p] for z in range(Z) for p in range(P)])
m.Equation([f_GT [z][p]== a*delta_GT[z][p] + b*e_GT[z][p] for z in range(Z) for p in range(P)])
m.Equation([q_GT [z][p]== c*delta_GT[z][p] + d*e_GT[z][p] for z in range(Z) for p in range(P)])
m.Equation([E_purch[z][p] + E_GT[z][p] == E_RE[z][p] + E_d[z][p] for z in range(Z) for p in range(P)])
m.Equation([Q_GT[z][p] == Q_disp[z][p] + Q_GT_RB[z][p] for z in range(Z) for p in range(P)])
m.Equation([Q_RB[z][p] + Q_HB[z][p] == Q_RS[z][p] + H_d[z][p] for z in range(Z) for p in range(P)])
m.Equation([K_RS[z][p] + K_RE[z][p] == K_d[z][p] for z in range(Z) for p in range(P)])
m.Equation([Q_disp[z][p] <= alpha*Q_GT[z][p] for z in range(Z) for p in range(P)])
# ______Objective_______
m.Obj(Cc + Cr)
#_____Solve Problem_____
#m.open_folder()
m.solve()
The problem solves with 1.6 sec
of solver time.
--------- APM Model Size ------------
Each time step contains
Objects : 13
Constants : 20
Variables : 5209
Intermediates: 2320
Connections : 313
Equations : 5213
Residuals : 2893
Number of state variables: 5209
Number of total equations: - 2905
Number of slack variables: - 864
---------------------------------------
Degrees of freedom : 1440
----------------------------------------------
Steady State Optimization with APOPT Solver
----------------------------------------------
Iter: 1 I: 0 Tm: 1.53 NLPi: 3 Dpth: 0 Lvs: 0 Obj: 2.69E+04 Gap: 0.00E+00
Successful solution
---------------------------------------------------
Solver : APOPT (v1.0)
Solution time : 1.59730000000854 sec
Objective : 26890.6404951639
Successful solution
---------------------------------------------------
来源:https://stackoverflow.com/questions/59103401/python-gekko-minlp-optimization-of-energy-system-how-to-build-intermediates-tha