【问题标题】:Is is possible to have a ever changing parameter for a Pyomo optimization (Python)?是否可以为 Pyomo 优化(Python)设置不断变化的参数?
【发布时间】: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


    【解决方案1】:

    我会建议几件事......

    1. 切换到 pyomo 中的 ConcreteModel。它们更容易处理,您可以使用基本的 Python 来读取不同类型的数据(如果需要)或者只是将数据编码到程序中。与尝试更改您已加载的 AbstractModel 中的某些内容相比,它更容易处理并且更具适应性。

    2. 您的模型现在是“瞬时”或静态的。它不代表时间的流逝,这很好。因此,如果我对您的理解是正确的,那么输入数据的某些部分将会发生变化,并且您想在不依赖于先前解决方案的情况下重新解决。因此,作为概念验证,您可以 (a) 制作一个具体模型,(b) 让它运行并对其进行 QA,(c) 将其置于循环内以求解,并在循环外更改输入到参数并重新求解。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-23
      • 2017-09-04
      • 1970-01-01
      • 2012-05-30
      • 2022-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多