微电网电池调度的约束优化
Constrained Optimization of battery scheduling in microgrid
给定电力消耗、太阳能电池板发电、价格(所有在给定时间 t)等输入,我们有一个电池,我们想评估它在任何给定时间应该(放电)/充电多少时间。
问题可以表述如下:
Pt = price of electricity at time t
Lt = consumption of electricity at time t
Zt = charge of battery at time t (how much is in the battery)
St = Electricity generated from solar generator at time t
Qt = amount the battery (dis)/charges at time t
我们要优化的功能是
Ct = Pt *(Lt - St - Qt)
这旨在最大限度地减少购买的电量
具有以下限制条件:
Lt - St - Qt >= 0 (our demand has to be non-negative)
Qmin <= Qt <= Qmax ( the battery can only (dis)/charge between certain values at any given time)
Zmin <= Zt <= Zmax. (the battery has to be within its capacity, i.e. you can't discharge more than the battery holders, and you can charge more than the battery can hold)
Zt+1 = Zt + Qt+1 ( this means that the battery level at the next time step is equal to the battery level at the previous time step plus the amount that was (dis)/charged from the battery)
我遇到的问题是如何在python (Scipy) 中表述问题,特别是更新电池电量。
我知道存在其他库(Pyomo、Pulp),欢迎提供解决方案。
根据我的经验(线性/MIP)优化对于此类应用程序是一种有效的方法。在我看来(意见,是的),Pyomo 是一个很棒的工具:
- 写在Python
- 整体设计很棒
- 它具有其他建模语言(AMPL、GAMS...)的大多数共同特征
- 它具有适用于大多数求解器的简单界面
- 维护得很好(查看 Github 页面)
文档内容非常丰富,位于此处:
https://pyomo.readthedocs.io/en/latest/index.html
您可以在此处找到更多 material:
https://pyomo.readthedocs.io/en/latest/tutorial_examples.html
此外,this 是对 Pyomo 的广泛介绍的 link,它深入到相当高级的主题,例如随机优化和 bi-level 问题。
最后,您的案例唯一的具体问题是您可能希望将损失应用于电池的充电和放电。请注意,为充电和放电定义两个自变量(均为 non-negative)可能是个好主意,这样您就可以将电池的能量平衡写为约束条件 link将时间 t
的能源状态 (SOE) 与时间 t+1
.
的 SOE 进行比较
祝你好运!
你很幸运,Giorgio 的回答激励我学习 pyomo(我主要使用 PULP),所以利用你的问题来确保我理解所有界面。我会 post 放在这里,这样我以后可以自己再次找到它:
import pyomo.environ as pyomo
import numpy as np
# create model
m = pyomo.ConcreteModel()
# Problem DATA
T = 24
Zmin = 0.0
Zmax = 2.0
Qmin = -1.0
Qmax = 1.0
# Generate prices, solar output and load signals
np.random.seed(42)
P = np.random.rand(T)*5.0
S = np.random.rand(T)
L = np.random.rand(T)*2.0
# Indexes
times = range(T)
times_plus_1 = range(T+1)
# Decisions variables
m.Q = pyomo.Var(times, domain=pyomo.Reals)
m.Z = pyomo.Var(times_plus_1, domain=pyomo.NonNegativeReals)
# objective
cost = sum(P[t]*(L[t] - S[t] - m.Q[t]) for t in times)
m.cost = pyomo.Objective(expr = cost, sense=pyomo.minimize)
# constraints
m.cons = pyomo.ConstraintList()
m.cons.add(m.Z[0] == 0.5*(Zmin + Zmax))
for t in times:
m.cons.add(pyomo.inequality(Qmin, m.Q[t], Qmax))
m.cons.add(pyomo.inequality(Zmin, m.Z[t], Zmax))
m.cons.add(m.Z[t+1] == m.Z[t] - m.Q[t])
m.cons.add(L[t] - S[t] - m.Q[t] >= 0)
# solve
solver = pyomo.SolverFactory('cbc')
solver.solve(m)
# display results
print("Total cost =", m.cost(), ".")
for v in m.component_objects(pyomo.Var, active=True):
print ("Variable component object",v)
print ("Type of component object: ", str(type(v))[1:-1]) # Stripping <> for nbconvert
varobject = getattr(m, str(v))
print ("Type of object accessed via getattr: ", str(type(varobject))[1:-1])
for index in varobject:
print (" ", index, varobject[index].value)
给定电力消耗、太阳能电池板发电、价格(所有在给定时间 t)等输入,我们有一个电池,我们想评估它在任何给定时间应该(放电)/充电多少时间。 问题可以表述如下:
Pt = price of electricity at time t
Lt = consumption of electricity at time t
Zt = charge of battery at time t (how much is in the battery)
St = Electricity generated from solar generator at time t
Qt = amount the battery (dis)/charges at time t
我们要优化的功能是
Ct = Pt *(Lt - St - Qt)
这旨在最大限度地减少购买的电量
具有以下限制条件:
Lt - St - Qt >= 0 (our demand has to be non-negative)
Qmin <= Qt <= Qmax ( the battery can only (dis)/charge between certain values at any given time)
Zmin <= Zt <= Zmax. (the battery has to be within its capacity, i.e. you can't discharge more than the battery holders, and you can charge more than the battery can hold)
Zt+1 = Zt + Qt+1 ( this means that the battery level at the next time step is equal to the battery level at the previous time step plus the amount that was (dis)/charged from the battery)
我遇到的问题是如何在python (Scipy) 中表述问题,特别是更新电池电量。
我知道存在其他库(Pyomo、Pulp),欢迎提供解决方案。
根据我的经验(线性/MIP)优化对于此类应用程序是一种有效的方法。在我看来(意见,是的),Pyomo 是一个很棒的工具:
- 写在Python
- 整体设计很棒
- 它具有其他建模语言(AMPL、GAMS...)的大多数共同特征
- 它具有适用于大多数求解器的简单界面
- 维护得很好(查看 Github 页面)
文档内容非常丰富,位于此处: https://pyomo.readthedocs.io/en/latest/index.html
您可以在此处找到更多 material: https://pyomo.readthedocs.io/en/latest/tutorial_examples.html
此外,this 是对 Pyomo 的广泛介绍的 link,它深入到相当高级的主题,例如随机优化和 bi-level 问题。
最后,您的案例唯一的具体问题是您可能希望将损失应用于电池的充电和放电。请注意,为充电和放电定义两个自变量(均为 non-negative)可能是个好主意,这样您就可以将电池的能量平衡写为约束条件 link将时间 t
的能源状态 (SOE) 与时间 t+1
.
祝你好运!
你很幸运,Giorgio 的回答激励我学习 pyomo(我主要使用 PULP),所以利用你的问题来确保我理解所有界面。我会 post 放在这里,这样我以后可以自己再次找到它:
import pyomo.environ as pyomo
import numpy as np
# create model
m = pyomo.ConcreteModel()
# Problem DATA
T = 24
Zmin = 0.0
Zmax = 2.0
Qmin = -1.0
Qmax = 1.0
# Generate prices, solar output and load signals
np.random.seed(42)
P = np.random.rand(T)*5.0
S = np.random.rand(T)
L = np.random.rand(T)*2.0
# Indexes
times = range(T)
times_plus_1 = range(T+1)
# Decisions variables
m.Q = pyomo.Var(times, domain=pyomo.Reals)
m.Z = pyomo.Var(times_plus_1, domain=pyomo.NonNegativeReals)
# objective
cost = sum(P[t]*(L[t] - S[t] - m.Q[t]) for t in times)
m.cost = pyomo.Objective(expr = cost, sense=pyomo.minimize)
# constraints
m.cons = pyomo.ConstraintList()
m.cons.add(m.Z[0] == 0.5*(Zmin + Zmax))
for t in times:
m.cons.add(pyomo.inequality(Qmin, m.Q[t], Qmax))
m.cons.add(pyomo.inequality(Zmin, m.Z[t], Zmax))
m.cons.add(m.Z[t+1] == m.Z[t] - m.Q[t])
m.cons.add(L[t] - S[t] - m.Q[t] >= 0)
# solve
solver = pyomo.SolverFactory('cbc')
solver.solve(m)
# display results
print("Total cost =", m.cost(), ".")
for v in m.component_objects(pyomo.Var, active=True):
print ("Variable component object",v)
print ("Type of component object: ", str(type(v))[1:-1]) # Stripping <> for nbconvert
varobject = getattr(m, str(v))
print ("Type of object accessed via getattr: ", str(type(varobject))[1:-1])
for index in varobject:
print (" ", index, varobject[index].value)