使用时间序列参数的Python优化Pyomo
我正在优化一种能源系统,该能源系统从两种天然气和氢来提供能源为一周内(每小时时间步长)内需求变化的最终用户。
我使用PYOMO和IPOPT求解器对正常工作的系统进行建模。我在两个单独的列中有两个在CSV文件中的入口参数(需求和氢)的列表(正如我所说,有一个小时的时间步长)。
我想为每个时间步骤优化系统,并生成OF并计算每个时间步长的变量(所有行的每小时)。错误是
索引'(1,'ng')'对于索引组件'x'
无效
我认为将函数用于每个变量和约束“ def”,但我无法成功获得结果。
如果您可以看一下代码,并告诉我如何重复每个时间步骤的优化,我将不胜感激。
import pyomo.environ as pyo
from pyomo.opt import SolverFactory
from pyomo.environ import *
import pandas as pd
import matplotlib as mp
import matplotlib.pyplot as plt
from pyomo.dae import *
import numpy as np
data=pd.read_csv('H2_Demand_Week.csv')
date_numbers = data['Time']
y = data['Demand_MWh']
y2 = data['H2_Curtailed_10MWh']
m = pyo.ConcreteModel()
Nd=len(data)
cost_NG = 0.066 #the NG price is 0.066 €/kWh in Ireland in March 2022
cost_CW = 25 #curtailed wind cost is 25 €/MWh
cost_Storage = 470 #€/MWh
cost_H2_out = 0.044 #€/nm3
cost_H2_in = 0.5 #€/nm3
WI_NG = 48 #the natural gas Wobbe Index MJ/m3
WI_H2 = 41 #the hydrogen Wobbe Index MJ/m3
H2_perc=0.05
yd = data['Demand_MWh']
yh = data['H2_Curtailed_10MWh']
T = range(len(data))
data.replace([np.nan], 0, inplace=True)
#sets and paramenters
#m.setInv = pyo.Set(initialize=['NG','H','S'])
m.NG = pyo.Var(T, bounds=(0,None), initialize=1)
m.H = pyo.Var(T, bounds=(0,None), initialize=1)
m.S = pyo.Var(T, bounds=(0,None), initialize=1)
#constraints
#m.C1 = pyo.Constraint(expr = pyo.summation(m.X) == Demand)
for i in range(1,Nd):
m.C1 = pyo.Constraint(expr = m.NG[i] + m.H[i] ==yd[i])
for i in range(1,Nd):
m.C2 = pyo.Constraint(expr = m.H[i] + m.S[i] == yh[i])
for i in range(1,Nd):
m.C3 = pyo.Constraint(expr = m.H[i]/(m.NG[i] + m.H[i])<= H2_perc) #hydrogen concentration
#objective function
#m.obj = pyo.Objective(expr = m.X['NG']*0.066+m.X['H']*0.5+m.X['S']*cost_Storage, sense=pyo.minimize)
for i in range(1,Nd):
m.obj = pyo.Objective(expr = m.S[i]*cost_Storage, sense=pyo.minimize)
#begin = time.time()
#solve
opt = SolverFactory('ipopt', executable='C:\\ipopt.exe')
#opt = SolverFactory('couenne', executable='C:\\My_Files\\_H2_HYDROGEN_THE_GREEN\\ThisPC_MODELLING\\PYTHON-MODEL\\ENERGY SYSTEM OPTIMISATION\RHODE\\Couenne-0.3.2-win32-msvc9\\Couenne-0.3.2-win32-msvc9\\bin\\couenne.exe')
#opt.options['tol']=1e-3
#opt.solve(m)
m.results = opt.solve(m)
#detalT = time.time() - begin
#print
m.pprint()
print('\n\nStorage:',pyo.value(m.S))
print('\n\nOF Storage cost €:',pyo.value(m.obj))
I am optimising an energy system that supplies energy from two sources of natural gas and hydrogen to end-users with varying demand in a week period (in hourly time steps).
I used Pyomo and the ipopt solver to model the system which is working properly. I have two lists of inlet parameters (demand and hydrogen) in my CSV file in two separate columns (and 700 rows as I said there is an hourly time step).
I want to optimise the system for each time step and generate the OF and calculate the variables per time step (per hour for all rows). The error is
Index '(1, 'NG')' is not valid for indexed component 'X'
I was thinking to use the function for each variable and constraint "def" but I couldn't succeed to get the results.
I would appreciate it if you could take look at the code and let me know how can I repeat the optimisation for each time step.
import pyomo.environ as pyo
from pyomo.opt import SolverFactory
from pyomo.environ import *
import pandas as pd
import matplotlib as mp
import matplotlib.pyplot as plt
from pyomo.dae import *
import numpy as np
data=pd.read_csv('H2_Demand_Week.csv')
date_numbers = data['Time']
y = data['Demand_MWh']
y2 = data['H2_Curtailed_10MWh']
m = pyo.ConcreteModel()
Nd=len(data)
cost_NG = 0.066 #the NG price is 0.066 €/kWh in Ireland in March 2022
cost_CW = 25 #curtailed wind cost is 25 €/MWh
cost_Storage = 470 #€/MWh
cost_H2_out = 0.044 #€/nm3
cost_H2_in = 0.5 #€/nm3
WI_NG = 48 #the natural gas Wobbe Index MJ/m3
WI_H2 = 41 #the hydrogen Wobbe Index MJ/m3
H2_perc=0.05
yd = data['Demand_MWh']
yh = data['H2_Curtailed_10MWh']
T = range(len(data))
data.replace([np.nan], 0, inplace=True)
#sets and paramenters
#m.setInv = pyo.Set(initialize=['NG','H','S'])
m.NG = pyo.Var(T, bounds=(0,None), initialize=1)
m.H = pyo.Var(T, bounds=(0,None), initialize=1)
m.S = pyo.Var(T, bounds=(0,None), initialize=1)
#constraints
#m.C1 = pyo.Constraint(expr = pyo.summation(m.X) == Demand)
for i in range(1,Nd):
m.C1 = pyo.Constraint(expr = m.NG[i] + m.H[i] ==yd[i])
for i in range(1,Nd):
m.C2 = pyo.Constraint(expr = m.H[i] + m.S[i] == yh[i])
for i in range(1,Nd):
m.C3 = pyo.Constraint(expr = m.H[i]/(m.NG[i] + m.H[i])<= H2_perc) #hydrogen concentration
#objective function
#m.obj = pyo.Objective(expr = m.X['NG']*0.066+m.X['H']*0.5+m.X['S']*cost_Storage, sense=pyo.minimize)
for i in range(1,Nd):
m.obj = pyo.Objective(expr = m.S[i]*cost_Storage, sense=pyo.minimize)
#begin = time.time()
#solve
opt = SolverFactory('ipopt', executable='C:\\ipopt.exe')
#opt = SolverFactory('couenne', executable='C:\\My_Files\\_H2_HYDROGEN_THE_GREEN\\ThisPC_MODELLING\\PYTHON-MODEL\\ENERGY SYSTEM OPTIMISATION\RHODE\\Couenne-0.3.2-win32-msvc9\\Couenne-0.3.2-win32-msvc9\\bin\\couenne.exe')
#opt.options['tol']=1e-3
#opt.solve(m)
m.results = opt.solve(m)
#detalT = time.time() - begin
#print
m.pprint()
print('\n\nStorage:',pyo.value(m.S))
print('\n\nOF Storage cost €:',pyo.value(m.obj))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论