【问题标题】:Piecewise objective functions using Pyomo使用 Pyomo 的分段目标函数
【发布时间】:2021-08-04 17:09:43
【问题描述】:

我目前正在尝试使用 Pyomo 解决电池调度问题,即给定需求、太阳能发电量和从电网购买的价格以及卖回电网的价格,电池应该在何时以及多少(dis )/充电。

我是 Pyomo 的新手,我尝试使用以下代码。 '''

import pyomo.environ as pyomo
import numpy as np
import matplotlib.pyplot as plt 
import pandas as pd


# A  piecewise example
# We can bound the X with min and max
# Xmin = -1, Xmax = 1
#
#
#        / Y * SP,    ,  0 <= Y <= 1
# X(Y) = | 
#        \ Y * P      , -1 <= Y >= 0

# We consider a flat price for purchasing electricity

df = pd.read_csv('optimal_dispatch_flatprice.csv').iloc[:,1:]

P = df.iloc[:,2] #Price to buy (fixed)
S = df.iloc[:,1] #Solar output
L = df.iloc[:,0] #Demand (load)
SP = df.iloc[:,4] #Price to sell (fixed)

T = len(df)
#Z : charge of battery at time t (how much is in the battery)
Zmin = 0.0
Zmax = 12

#Qt = amount the battery (dis)/charges at time t
Qmin = -5.0
Qmax = 5.0


RANGE_POINTS = {-1.0:-2.4, 0.0:0.0, 1.0:13.46}
def f(model,x):
    return RANGE_POINTS[x]


model = pyomo.ConcreteModel()

model.Y = pyomo.Var(times, domain=pyomo.Reals)
model.X = pyomo.Var()

times = range(T)
times_plus_1 = range(T+1)

# Decisions variables

model.Q = pyomo.Var(times, domain=pyomo.Reals) # how much to (dis)/charge
model.Z = pyomo.Var(times_plus_1, domain=pyomo.NonNegativeReals) # SoB

# constraints
model.cons = pyomo.ConstraintList()
model.cons.add(model.Z[0] == 0)


for t in times:
    model.cons.add(pyomo.inequality(Qmin, model.Q[t], Qmax))
    model.cons.add(pyomo.inequality(Zmin, model.Z[t], Zmax))
    model.cons.add(model.Z[t+1] == model.Z[t] - model.Q[t])
    model.cons.add(model.Y[t] == L[t]- S[t] - model.Q[t])

model.cons = pyomo.Piecewise(model.X,model.Y, # range and domain variables
                      pw_pts=[-1,0,1] ,
                      pw_constr_type='EQ',
                      f_rule=f)

model.cost = pyomo.Objective(expr = model.X, sense=pyomo.minimize)

'''

我收到错误“'IndexedVar' 对象没有属性 'lb'。 我认为这是指 model.Y 是索引的事实。

谁能解释一下如何解决这个问题?

【问题讨论】:

    标签: python optimization pyomo


    【解决方案1】:

    由于其中一个变量被索引,您需要将索引集作为第一个参数提供给 Piecewise。例如,Piecewise(times,model.X,model.Y,...

    【讨论】:

    • 不幸的是,它不起作用。问题似乎与model.Y有关。事实上,它是索引的,分段的方法似乎不喜欢它。
    • 你能澄清一下吗?您是否遇到了新错误,或者问题解决方案不是您所期望的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 2021-05-27
    • 2021-07-29
    • 2017-02-15
    • 2019-07-10
    • 2021-08-27
    • 1970-01-01
    相关资源
    最近更新 更多