问题描述
我目前正在Python GEKKO中实现MINLP优化问题,以确定三代能源系统的最佳运行策略.当我将代表日不同时段的所有时间的能源需求视为输入数据时,基本上我所有的决策变量,中间物等都是二维数组.我怀疑2D中间体的声明是我的问题.现在,我使用列表推导来声明2D中间体,但似乎python无法使用这些中间体.此外,错误此稳态IMODE仅允许标量值..
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.
每当我使用GEKKO m.Array函数时,如下所示: 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))
它说,不能调用GEKKO对象m.Intermediate.
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.
这是完整的代码:
"""
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()
推荐答案
2D列表定义需要额外的方括号.这将给出一个具有3行4列的2D列表.
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]]
如果省略内括号,则为一维长度为12的列表.
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]
当列表中的每个元素都是Gekko Intermediate
时,它也适用.
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)]
这篇关于能源系统的Python GEKKO MINLP优化:如何构建2D阵列的中间体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!