简体   繁体   English

使用 Pyomo 的分段目标函数

[英]Piecewise objective functions using Pyomo

I'm currently trying to use Pyomo to solve a battery dispatch problem, ie Given demand, solar generation and price to buy from the grid and a price to sell back to the grid, when and how much should the battery (dis)/charge.我目前正在尝试使用 Pyomo 解决电池调度问题,即给定需求、太阳能发电量和从电网购买的价格以及卖回电网的价格,电池(dis)/充电的时间和数量.

I am new to Pyomo and I have tried to use the following code.我是 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)

''' '''

I get the error " 'IndexedVar' object has no attribute 'lb' . I think this is referring to the fact that model.Y is index with times.我收到错误“ 'IndexedVar' object has no attribute 'lb' 。我认为这是指 model.Y 是索引的事实。

Can anyone explain how to set the problem up?谁能解释如何设置问题?

Since one of the variables is indexed, you need to provide the index set as the first argument to Piecewise.由于其中一个变量被索引,您需要将索引集作为第一个参数提供给 Piecewise。 Eg, Piecewise(times,model.X,model.Y,...例如, Piecewise(times,model.X,model.Y,...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM