我正在尝试使用pyomo和glpk求解器优化微电网。这是我的约束之一:
https://drive.google.com/file/d/1_iskeKjYPBPicJDFnZl1QIo275sEq3f9/view?usp=sharing
我是这样用python写的:
data = pd.read_csv('Data.csv')
load = data['L2']
model = pyo.ConcreteModel()
model.i = pyo.RangeSet(0, 23) #i
model.P_w = pyo.Var(model.i, domain=pyo.NonNegativeReals) #P-wind
model.P_pv = pyo.Var(model.i, domain=pyo.NonNegativeReals) #P-PV
model.P_fc = pyo.Var(model.i, domain=pyo.NonNegativeReals) #P-Fuel Cells
model.P_c = pyo.Var(model.i, domain=pyo.NonNegativeReals) #P-Charged
model.P_d = pyo.Var(model.i, domain=pyo.NonNegativeReals) #P_Discharged
model.UE = pyo.Var(model.i, domain=pyo.NonNegativeReals) #Undelivered Energy
model.EG = pyo.Var(model.i, domain=pyo.NonNegativeReals) #Excess Generated
def constraint1(model,i):
return sum(model.P_w[i] + model.P_pv[i]+ model.P_fc[i] + model.P_d[i] + model.UE[i] for i in model.i) == sum(load[i] + model.P_c[i] + model.EG[i] for i in model.i)
model.constraint1 = pyo.Constraint(model.i, rule=constraint1)
请原谅缩进中的错误。然而,我得到的值如下:(P_w [0] + P_w [1] + . . + P_w [23]) + (P_pv [0] + P_pv [1 ]+..)+....+( 问题[0]+ [1 ]+...) = ( 负载[0][1 +负载 ]+..) +...
而我想要的结果应该是:P_w [0] + P_pv [0] + P_fc [0] + . .+问题[0]=负载[0]+ P_c[0] +如[0]P_w [1] + P_pv [1] + P_fc [1] + . .+UE[1] = load[1]+P_c[1]+EG[1]
我还试着写:
sum(model.P_w[i] + model.P_pv[i]+ model.P_fc[i] + model.P_d[i] + model.UE[i] - load[i] - model.P_c[i] -model.EG[i] for i in model.i) == 0
但是答案是一样的。我该怎么表述呢?提前感谢:)
这里不完全清楚您想要什么。您的链接数学表达式是无意义的/没有正确表达。
看来你想为model.i中的每个i做一个等价约束。但是你在求和,所以只要把sum()
语句从约束的两边去掉。您已经设置了model.constraint1()
语句,以便为该集合中的每个i
创建单独的约束。所以:
def constraint1(model, i):
return model.P_w[i] + model.P_pv[i]+ model.P_fc[i] + model.P_d[i] + model.UE[i] ==
load[i] + model.P_c[i] + model.EG[i]