【问题标题】:Pyomo: defining a variable using a maximum of other variablesPyomo:使用最多其他变量定义一个变量
【发布时间】:2021-02-04 19:34:19
【问题描述】:

如果这很简单,请提前道歉——我怀疑是这样——因为我已经搜索过,但没有找到这样的例子!

我正在 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

谢谢。

更新

所以我现在按照建议添加了修改目标函数,并修改了约束代码如下:

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

【问题讨论】:

    标签: pyomo


    【解决方案1】:

    max() 不是线性函数,因此您将无法在约束内使用它。所以pyomo 在您尝试进行约束时会很生气。如果您只想在此处捕获最高边际成本,您可以轻松地重新制定您的 SystemShortRunMarginalCost` 变量,使其大于您希望它捕获的最大值,然后在目标函数中最小化它。

    【讨论】:

      【解决方案2】:

      我通过对 Pyomo 结果进行后处理来计算边际 SRMC 和平均成本输出来解决了这个问题。我最初的问题是试图通过在 Pyomo 问题本身中创建新变量来获得这些输出,但这被证明具有挑战性,如上所述。我仍然不能 100% 确定这是否可能,但是我的 VBA 代码(我用来将 JSON 输出从 Pyomo 转换为 CSV 格式)中的后处理工作得很好!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-13
        • 2018-05-06
        • 1970-01-01
        • 2021-12-23
        • 2011-02-26
        • 2016-04-06
        • 1970-01-01
        相关资源
        最近更新 更多