【发布时间】:2023-01-27 02:12:26
【问题描述】:
我正在尝试优化一个由 2 个电池组成的能源系统,这些电池应该在发送信号(能量请求)时提供能量。 我在 Pyomo 中创建了一个抽象模型来表示我的问题,到目前为止我设法让它工作,但我的问题是我的数据将根据我的优化结果不断变化。例如,如果接收到信号并且电池提供一些能量,则充电状态 (SoC) 将降低(因为电量较少)。我希望能够更新此值,以便在下一次优化(当连续信号进入时)使用真正的 SoC 解决我的问题。 另一种表达方式是:有没有办法使用数据帧作为我的 Pyomo 优化的输入参数?
这是我的代码。我的集合称为 ASSETS,因为从技术上讲,我将拥有多种不同类型的资产(即经典的锂电池和可能的氢储存)。
# iterative1.py
from pyomo.environ import *
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
## CREATING MODEL, SET AND PARAM
model = AbstractModel()
# Sets of PTU for which the model is being created for
# model.PTU = Set()
model.ASSETS = Set()
# Set up the param
model.MinPower = Param(model.ASSETS)
model.MaxPower = Param(model.ASSETS)
model.Capacity = Param(model.ASSETS)
model.SoC = Param(model.ASSETS)
model.P_rated = Param(model.ASSETS)
# DATA FROM the EMS csv
FR = 20 #requet of power
# model.SoC = 0.9
P_rated = 1 #how much the asset is already in use during the request of power
# Decision variable
# model.Psh = Var(model.PTU, within=Reals)
model.Psh = Var(model.ASSETS, within=Reals)
# Objective Function
def objective_rule(model):
return FR - sum(model.Psh[i] for i in model.ASSETS)
model.PowerProvided = Objective(rule=objective_rule, sense=minimize)
# Constraints
# defining the rules
def MinPowerRated_rule(model,i): # Min rated power limit
return - model.MaxPower[i] <= model.Psh[i]
def MaxPowerRated_rule(model,i): # Max rated power limit
return model.Psh[i] <= model.MaxPower[i]
# def PowerRated_rule(model,i):
# return model.MinPower[i] <= model.Psh[i] <= model.MaxPower[i]
def MaxCapacityLimits_rule(model,i): #Checks that the power flex is within the limits of the storage (discharge limit)
return model.Psh[i] <= model.Capacity[i]*model.SoC[i]/4
def MinCapacityLimits_rule(model,i): #Checks that the power flex is within the limits of the storage (charge limit)
return model.Psh[i] >= - model.Capacity[i]*model.SoC[i]/4
def MaxPowerAvailable_rule(model,i):
return model.Psh[i] <= model.MaxPower[i] - P_rated
# return model.Psh[i] <= model.MaxPower[i] - model.P_rated[i]
def MinPowerAvailable_rule(model,i):
return model.Psh[i] >= - (model.MaxPower[i] - P_rated)
# return model.Psh[i] >= - (model.MaxPower[i] - model.P_rated[i])
# activating the constraints
model.MaxPowerRated = Constraint(model.ASSETS, rule=MaxPowerRated_rule)
model.MinPowerRated = Constraint(model.ASSETS, rule=MinPowerRated_rule)
model.MaxCapacityLimits = Constraint(model.ASSETS, rule=MaxCapacityLimits_rule)
model.MinCapacityLimits = Constraint(model.ASSETS, rule=MinCapacityLimits_rule)
model.MaxPowerAvailable = Constraint(model.ASSETS, rule=MaxPowerAvailable_rule)
model.MinPowerAvailable = Constraint(model.ASSETS, rule=MinPowerAvailable_rule)
#create model instance
data = DataPortal() #DataPortal handles the .dat file
data.load(filename="abstract.dat", model=model)
instance = model.create_instance(data)
opt = SolverFactory('glpk')
opt.solve(instance)
我正在使用以下 .dat 文件来获取约束和目标函数的参数。
ASSETS := 1 2;
param MinPower :=
1 0
2 0;
param MaxPower :=
1 15
2 15;
param Capacity :=
1 30
2 30;
param SoC :=
1 0.9
2 0.9;
我试图用每次优化后都会更新的数据框来更改 SoC,但不幸的是我遇到了错误。
【问题讨论】:
标签: python-3.x optimization pyomo