Pyomo:使用最多其他变量定义一个变量
Pyomo: defining a variable using a maximum of other variables
如果这很简单,请提前致歉 - 我怀疑它很简单 - 因为我搜索过但未能找到这方面的示例!
我正在 Pyomo 中构建一个能源系统调度模型,并且有这个 运行 的一个版本。我正在定义一个新变量“SystemShortRunMarginalCost”,它应该被定义为“ActiveShortRunMarginalCostByGenerator”的最大值,如下:
def SystemShortRunMarginalCost_rule(model,h):
max(model.ActiveShortRunMarginalCostByGenerator[g,h] for g in model.GeneratorName) == SystemShortRunMarginalCost[h]
model.SystemShortRunMarginalCostHourly = Constraint(model.Hour, rule=SystemShortRunMarginalCost_rule)
我在这里缺少一些基本语法吗?我收到以下错误消息:
ERROR: Rule failed when generating expression for constraint
SystemShortRunMarginalCostHourly with index 1: NameError: name
'SystemShortRunMarginalCost' is not defined
ERROR: Constructing component 'SystemShortRunMarginalCostHourly' from
data=None failed:
NameError: name 'SystemShortRunMarginalCost' is not defined
[ 0.10] Pyomo Finished
ERROR: Unexpected exception while running model:
name 'SystemShortRunMarginalCost' is not defined
谢谢。
已更新
所以我现在已经按照提示修改了objective函数,修改了约束代码如下:
def SystemShortRunMarginalCost_rule(model,g,h):
return SystemShortRunMarginalCost[h] >= model.ActiveShortRunMarginalCostByGenerator[g,h]
model.SystemShortRunMarginalCostHourly = Constraint(model.GeneratorName, model.Hour, rule=SystemShortRunMarginalCost_rule)
我现在得到一个不同的错误,如下:
ERROR: Rule failed when generating expression for constraint
SystemShortRunMarginalCostHourly with index ('Wind1', 1): NameError: name
'SystemShortRunMarginalCost' is not defined
ERROR: Constructing component 'SystemShortRunMarginalCostHourly' from
data=None failed:
NameError: name 'SystemShortRunMarginalCost' is not defined
[ 0.11] Pyomo Finished
ERROR: Unexpected exception while running model:
name 'SystemShortRunMarginalCost' is not defined
max()
不是线性函数,因此您无法在约束内使用它。因此,当您尝试进行约束时,pyomo
会呕吐。如果您只想在这里捕获最高的边际成本,您可以轻松地重新制定,使您的 SystemShortRunMarginalCost` 变量大于您希望它捕获最大值的每个事物,然后在 objective 函数中将其最小化。
我通过 post 解决了这个问题 - 处理 Pyomo 结果以计算边际 SRMC 和平均成本输出。我最初的问题是试图通过在 Pyomo 问题本身中创建新变量来获取这些输出,但事实证明这具有挑战性,如上所述。我仍然不能 100% 确定这是否可行,但是 post-在我的 VBA 代码中进行处理(我用它来将 Pyomo 的 JSON 输出转换为CSV 格式)工作正常!
如果这很简单,请提前致歉 - 我怀疑它很简单 - 因为我搜索过但未能找到这方面的示例!
我正在 Pyomo 中构建一个能源系统调度模型,并且有这个 运行 的一个版本。我正在定义一个新变量“SystemShortRunMarginalCost”,它应该被定义为“ActiveShortRunMarginalCostByGenerator”的最大值,如下:
def SystemShortRunMarginalCost_rule(model,h):
max(model.ActiveShortRunMarginalCostByGenerator[g,h] for g in model.GeneratorName) == SystemShortRunMarginalCost[h]
model.SystemShortRunMarginalCostHourly = Constraint(model.Hour, rule=SystemShortRunMarginalCost_rule)
我在这里缺少一些基本语法吗?我收到以下错误消息:
ERROR: Rule failed when generating expression for constraint
SystemShortRunMarginalCostHourly with index 1: NameError: name
'SystemShortRunMarginalCost' is not defined
ERROR: Constructing component 'SystemShortRunMarginalCostHourly' from
data=None failed:
NameError: name 'SystemShortRunMarginalCost' is not defined
[ 0.10] Pyomo Finished
ERROR: Unexpected exception while running model:
name 'SystemShortRunMarginalCost' is not defined
谢谢。
已更新
所以我现在已经按照提示修改了objective函数,修改了约束代码如下:
def SystemShortRunMarginalCost_rule(model,g,h):
return SystemShortRunMarginalCost[h] >= model.ActiveShortRunMarginalCostByGenerator[g,h]
model.SystemShortRunMarginalCostHourly = Constraint(model.GeneratorName, model.Hour, rule=SystemShortRunMarginalCost_rule)
我现在得到一个不同的错误,如下:
ERROR: Rule failed when generating expression for constraint
SystemShortRunMarginalCostHourly with index ('Wind1', 1): NameError: name
'SystemShortRunMarginalCost' is not defined
ERROR: Constructing component 'SystemShortRunMarginalCostHourly' from
data=None failed:
NameError: name 'SystemShortRunMarginalCost' is not defined
[ 0.11] Pyomo Finished
ERROR: Unexpected exception while running model:
name 'SystemShortRunMarginalCost' is not defined
max()
不是线性函数,因此您无法在约束内使用它。因此,当您尝试进行约束时,pyomo
会呕吐。如果您只想在这里捕获最高的边际成本,您可以轻松地重新制定,使您的 SystemShortRunMarginalCost` 变量大于您希望它捕获最大值的每个事物,然后在 objective 函数中将其最小化。
我通过 post 解决了这个问题 - 处理 Pyomo 结果以计算边际 SRMC 和平均成本输出。我最初的问题是试图通过在 Pyomo 问题本身中创建新变量来获取这些输出,但事实证明这具有挑战性,如上所述。我仍然不能 100% 确定这是否可行,但是 post-在我的 VBA 代码中进行处理(我用它来将 Pyomo 的 JSON 输出转换为CSV 格式)工作正常!