更新
忙于電池存盤的優化,我在嘗試基于 36 小時的回圈優化模型時遇到了一個問題,例如,運行一整年的資料。
通過這樣做,我重新初始化了在每一步都很重要的變數,從而損害了模型。如何提取變數的最后一個值并將其用作下一次迭代的第一個值?這是一個簡單的問題:
#creation of list to store last variable values:
df1=[]
df2=[]
df3=[]
# Loop
for i in range (0,3)
step = 36
first_model_hour = step * i
last_model_hour = (step * (i 1)) - 1
model = ConcreteModel()
model.T = Set(doc='hour of year',initialize=df.index.tolist())
model.A = Var(model.T, NonNegativeReals)
model.B = Var(model.T, NonNegativeReals)
model.C = Var(model.T, NonNegativeReals)
def constraints_x(model,t)
for t == 0
return model.C == 100
elif t == first_model_hour
return model.C[t] == model.A[from previous loop] model.B[from previous loop] model.C[from previous loop]
else
return model.C[t] == model.A[t-1] model.B[t-1] model.C[t-1]
model.constraint = Constraint(model.T, rule=constraint_x)
solver = SolverFactory('cbc')
solver.solve(model)
df1.append(model.S[last_model_hour])
df2.append(model.B[last_model_hour])
df3.append(model.C[last_model_hour])
是否可以從 pyomo 檢索變數的最后一個值,以將其用作下一個回圈的初始化,因此不會隨著時間的推移失去電池的連續充電狀態?
uj5u.com熱心網友回復:
這是一個完整的模型,它實作了我認為你想做的事情;這是從先前的迭代中結轉一個值。這建立在先前的答案/示例之上。
# rolling time horizon
import pyomo.environ as pyo
periods = 3
num_segments = 4
starting_value = 2.0
solver = pyo.SolverFactory('cbc')
for period in range(periods):
segments = list(range(period*num_segments, (period 1) * num_segments)) # the time segments in this per
model = pyo.ConcreteModel()
model.S = pyo.Set(initialize=segments)
model.X = pyo.Var(model.S, domain=pyo.NonNegativeReals)
# assign the first value and fix it
model.X[model.S.first()] = starting_value
model.X[model.S.first()].fix()
# dummy constraint to constrain each value to 1 greater than previous.
def C1(model, s):
if s == model.S.first():
return pyo.Constraint.Skip
return model.X[s] <= model.X[s-1] 1
model.C1 = pyo.Constraint(model.S, rule=C1)
# obj: maximize X
model.obj = pyo.Objective(expr=pyo.summation(model.X), sense=pyo.maximize)
# solve
result = solver.solve(model)
assert(result.Solver()['Termination condition'].value == 'optimal') # a little insurance
# print(result)
# model.display()
starting_value = pyo.value(model.X[model.S.last()]) 1
# rolling printout of results...
print(f'From iteration {period}:')
model.X.display()
產量:
From iteration 0:
X : Size=4, Index=S
Key : Lower : Value : Upper : Fixed : Stale : Domain
0 : 0 : 2.0 : None : True : True : NonNegativeReals
1 : 0 : 3.0 : None : False : False : NonNegativeReals
2 : 0 : 4.0 : None : False : False : NonNegativeReals
3 : 0 : 5.0 : None : False : False : NonNegativeReals
From iteration 1:
X : Size=4, Index=S
Key : Lower : Value : Upper : Fixed : Stale : Domain
4 : 0 : 6.0 : None : True : True : NonNegativeReals
5 : 0 : 7.0 : None : False : False : NonNegativeReals
6 : 0 : 8.0 : None : False : False : NonNegativeReals
7 : 0 : 9.0 : None : False : False : NonNegativeReals
From iteration 2:
X : Size=4, Index=S
Key : Lower : Value : Upper : Fixed : Stale : Domain
8 : 0 : 10.0 : None : True : True : NonNegativeReals
9 : 0 : 11.0 : None : False : False : NonNegativeReals
10 : 0 : 12.0 : None : False : False : NonNegativeReals
11 : 0 : 13.0 : None : False : False : NonNegativeReals
uj5u.com熱心網友回復:
這是一篇很長的帖子,我認為您可以將其縮減為一個更簡單的示例。我想您是在問是否/如何可以從某個已知值(在您的情況下,來自先前運行的某個值)設定特定變數的值(在您的情況下是第一個索引的變數)。
您可以通過簡單地為變數分配一個值來做到這一點,然后“修復”該運行的值,然后繼續解決......
In [11]: import pyomo.environ as pyo
In [12]: model = pyo.ConcreteModel()
In [13]: model.S = pyo.Set(initialize=range(3))
In [14]: model.X = pyo.Var(model.S, domain=pyo.NonNegativeReals)
In [15]: model.X[0]=3.2
In [16]: model.X[0].fix()
In [17]: model.X.pprint()
X : Size=3, Index=S
Key : Lower : Value : Upper : Fixed : Stale : Domain
0 : 0 : 3.2 : None : True : False : NonNegativeReals
1 : 0 : None : None : False : True : NonNegativeReals
2 : 0 : None : None : False : True : NonNegativeReals
對你的模型的一些看法......解決一個超過約 9000 個時間步的優化模型有點過頭了,即使你像你一樣通過切碎它來攻擊它。您可以嘗試其他一些事情,例如將時間間隔增加到 4-6 小時,或者,也許更真實地假設在某些感興趣的時期內,系統將以大致相同的狀態啟動/停止(相同的電池狀態)所以只需解決一個特定季節或時間段的一周,您認為系統會以不同的方式運行并說電池在 80% 時啟動/停止......只是幾個想法......
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/438104.html
下一篇:JAVA:如何比較串列中的子類
