[英]Piecewise objective functions using Pyomo
我目前正在嘗試使用 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' object has no attribute 'lb' 。我認為這是指 model.Y 是索引的事實。
誰能解釋如何設置問題?
由於其中一個變量被索引,您需要將索引集作為第一個參數提供給 Piecewise。 例如, Piecewise(times,model.X,model.Y,...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.