python - 在 Pyomo 中获取决策变量以正确优化的问题
问题描述
我正在使用 pyomo 解决一个简单的多周期优化问题。该模型的目标是根据该小时的火花价差(电价 - 燃气价格 * 热费 + 可变成本)确定发电厂应该在哪些时间开启或关闭。火花传播可以是负数,表示工厂应该关闭,也可以是正数,表示工厂应该运行。
目前的结果表明,尽管火花传播为负,但工厂刚刚启动并运行。
考虑到那个小时的火花传播,我怎样才能让植物在每个时间步长打开和关闭?
我确信这是一个相当简单的解决方案,但我对 pyomo 和优化问题非常陌生,因此非常感谢任何指导和帮助。
gas_price = [2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81]
power_price = [26.24,23.8,21.94,20.4,21.2,19.98,19.34,18.83,19.19,18.48,21,21.77,23.45,26.53,29.85,31.8,28.7]
priceDict = dict(enumerate(power_price))
gasDict = dict(enumerate(gas_price))
m = en.ConcreteModel()
m.Time = en.RangeSet(0, len(power_price)-1)
m.powerPrice = en.Param(m.Time, initialize=priceDict)
m.gasPrice = en.Param(m.Time, initialize=gasDict)
m.generation = en.Var(m.Time, bounds=(0,800),
initialize=0)
m.spark = en.Var(m.Time,initialize=0)
m.heatRate = en.Var(m.Time,initialize=7)
m.vom = en.Var(m.Time,initialize=2)
m.max_gen = en.Param(initialize=800)
def Obj_fn(m):
return sum((m.spark[i]*m.generation[i]) for i in m.Time)
m.total_cost = en.Objective(rule=Obj_fn,sense=en.maximize)
# 7 is the heat rate of the plant
def spark_rule(m,i):
return (m.spark[i] == m.powerPrice[i]-(m.gasPrice[i]*7+m.vom[i]))
m.hourly_spark = en.Constraint(m.Time,rule=spark_rule)
def generation_rule(m,i):
return (0<=m.generation[i]<=m.max_gen)
m.t_generation_rule = en.Constraint(m.Time, rule=generation_rule)
opt = SolverFactory("clp",executable='C:\\clp.exe')
results = opt.solve(m)
目前模型的输出为:
Time Generation Spark Spread
1 0 6.57
2 800 4.13
3 800 2.27
4 800 0.73
5 800 1.53
6 800 0.31
7 800 -0.33
8 800 -0.84
9 800 -0.48
10 800 -1.19
11 800 1.33
12 800 2.1
13 800 3.78
14 800 6.86
15 800 10.18
16 800 12.13
17 800 9.03
解决方案
我在这里可能错了,但我认为您实际上是想将heatRate
and定义vom
为参数,而不是变量。
这导致了一个奇怪的问题,因为只要“火花”价格为正,工厂自然会使用其最大功率,而只要火花价格为负,工厂就会自然而然地使用其最大功率。我想您稍后会添加更多约束。
如果heatRate
和vom
是固定的,那么您可以通过以下方式重新定义问题:
from pyomo import environ as pe
gas_price = [2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81,2.81]
power_price = [26.24,23.8,21.94,20.4,21.2,19.98,19.34,18.83,19.19,18.48,21,21.77,23.45,26.53,29.85,31.8,28.7]
priceDict = dict(enumerate(power_price))
gasDict = dict(enumerate(gas_price))
m = pe.ConcreteModel()
m.Time = pe.RangeSet(0, len(power_price)-1)
# this are all input parameters
m.powerPrice = pe.Param(m.Time, initialize=priceDict)
m.gasPrice = pe.Param(m.Time, initialize=gasDict)
m.vom = pe.Param(default=7)
m.heatRate = pe.Param(default=2)
m.maxGen = pe.Param(default=800)
# this is a "dependent" parameter
m.spark = pe.Param(m.Time,
initialize = lambda m,t: m.powerPrice[t]-(m.gasPrice[t]*7+m.vom)
)
# this is the only variable
m.generation = pe.Var(m.Time,
initialize=0,
bounds = (0, m.maxGen)
)
def Obj_fn(m):
return sum((m.spark[t]*m.generation[t]) for t in m.Time)
m.total_cost = pe.Objective(rule=Obj_fn,sense=pe.maximize)
推荐阅读
- java - 如何将嵌套列表转换为二维数组
- python - 根据条件组合熊猫行
- python - 如何在 scipy.integrate.RK45 中输入时间步长
- angularjs - 在 Angular.js 上的 HTML5 Canvas 中的背景图像上绘制一个矩形
- c# - 将图像保存到数据库中的重复项
- c++ - 如何在 C++ 中声明 std::bind 的 std::invoke_result?
- java - 如何链接不同类的两个对象?
- algorithm - 我如何从数学上计算这个算法的时间复杂度?
- flutter - 可访问性不会在颤动中读取一些文本如何解决?
- android - 借助 Android 在 Tensorflow 上创建一个简单的神经网络