Pyomo输出

2024-10-01 13:29:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用pyomo包来实现一个优化问题。我正在尝试pyomo在线文档中提供的一个示例问题。但是,当我试图解决它时,我遇到了错误。在

使用的python代码:

from __future__ import division
from pyomo.environ import *

model = AbstractModel()

model.Nodes = Set()
model.Arcs = Set(dimen=2)

model.Flow = Var(model.Arcs, domain=NonNegativeReals)
model.FlowCost = Param(model.Arcs)
model.Demand = Param(model.Nodes)
model.Supply = Param(model.Nodes)

def Obj_rule(model):
    return summation(model.FlowCost, model.Flow)

model.Obj = Objective(rule=Obj_rule, sense=minimize)


def FlowBalance_rule(model, node):
    return model.Supply[node] \
        + sum(model.Flow[i, node] for i in model.Nodes if (i,node) in model.Arcs) \
        - model.Demand[node] \
        - sum(model.Flow[node, j] for j in model.Nodes if (j,node) in model.Arcs) \
        == 0

model.FlowBalance = Constraint(model.Nodes, rule=FlowBalance_rule)

数据文件是:

^{pr2}$

当我试图解决这个问题时,我得到了以下错误。在

[    0.00] Setting up Pyomo environment
[    0.00] Applying Pyomo preprocessing actions
[    0.01] Creating model
ERROR: Rule failed when generating expression for constraint FlowBalance with index CityB:
        KeyError: "Error accessing indexed component: Index '('CityB', 'CityC')' is not valid for array component 'Flow'"
ERROR: Constructing component 'FlowBalance' from data=None failed:
        KeyError: "Error accessing indexed component: Index '('CityB', 'CityC')' is not valid for array component 'Flow'"
ERROR: Unexpected exception while running model test.py:
        Error accessing indexed component: Index '('CityB', 'CityC')' is not valid for array component 'Flow'

Tags: infromnodeobjformodelparamflow