Pyomo 初学者:当模型表达式相互调用时,如何避免 pyomo 中的递归错误?

问题描述 投票:0回答:0

我正在尝试建立一个最大化电池利润的模型。作为模型的一部分,定义了以下两个量


The actual energy delivered $E_{del,m}$ at minute $m$ when the asset is activated during a frequency deviation differs from the expected energy $E_{\exp,m}$ because the battery can deliver energy based on the state of charge limits ($SOC_{min}$,$SOC_{max}$) and the state of charge at minute $m$, $SOC_{m}$. The actual energy delivered is then calculated as follows: 

\begin{align*} E_{del,m} = \begin{cases} {\max \left ({E_{\exp,m}, \frac {SOC_{\min } - SOC_{m} }{100} \cdot E_{rated} }\right)} &~ {P_{av,m} \ge 0} \\ {\min \left ({E_{\exp,m}, \frac {SOC_{\max } - SOC_{m} }{100} \cdot E_{rated} }\right)} &~ {P_{av,m} < 0} \end{cases}, \\\tag{9}\end{align*}

The state of charge for minute $m+1$ is calculated as follows: 

\begin{equation*} SOC_{m+1} = SOC_{m} + 100 \cdot \frac {E_{del,m} }{E_{rated} }\tag{10}\end{equation*} 

这就是我尝试定义模型的方式:

#Calculation of SOC for minute m
def calculate_SOC_m (model, m):
    if m == model.m.first():
        return SOC_init
    else:
        return model.SOC_m[m-1] + (100 * model.E_del_m[m-1]/ E_rated)
model.SOC_m = pyo.Expression (model.m, rule=calculate_SOC_m)

#Calculation of actual delivered energy in minute m based on SOC 
def calculate_E_del_m(model, m):
    if P_av_m[m]>= 0:
        return max(model.E_exp_m[m], ((SOC_min - model.SOC_m[m])/100) * E_rated)
    else:
        return min(model.E_exp_m[m], ((SOC_max - model.SOC_m[m])/100) * E_rated)
model.E_del_m = pyo.Expression (model.m, rule=calculate_E_del_m)

由于表达式间接相互调用,遇到了 Pyomo Execption 错误。有没有办法访问由“m”索引的每个变量的实时计算?

linear-programming pyomo
© www.soinside.com 2019 - 2024. All rights reserved.