简体   繁体   English

Pyomo输出错误

[英]Pyomo output error

I am using pyomo package for implementing an optimization problem. 我正在使用pyomo包来实现优化问题。 I was trying one of the example problems provided in pyomo online documentation. 我正在尝试pyomo在线文档中提供的示例问题之一。 But, I am getting error when I was trying to solve it. 但是,当我尝试解决它时出现错误。

The python code used: 使用的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)

And, the data file is: 并且,数据文件是:

set Nodes := CityA CityB CityC ;
set Arcs :=
CityA CityB
CityA CityC
CityC CityB
;
param : FlowCost :=
CityA CityB 1.4
CityA CityC 2.7
CityC CityB 1.6
;
param Demand :=
CityA 0
CityB 1
CityC 1
;
param Supply :=
CityA 2
CityB 0
CityC 0
;

When I try to solve this, I am getting the following error. 当我尝试解决此问题时,出现以下错误。

[    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'

You have the following typo in FlowBalance_rule 您在FlowBalance_rule有以下错字

sum(model.Flow[node, j] for j in model.Nodes if (j,node) in model.Arcs)

where the indexing model.Flow[node,j] and the conditional if (j,node) in model.Arcs are causing the error. 其中model.Flow[node,j]model.Flow[node,j]的条件if (j,node) in model.Arcs导致错误。

I'm assuming that you want to flip the order of the conditional tuple, the following works 我假设您想翻转条件元组的顺序,以下工作

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 (node,j) in model.Arcs) \
        == 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM