簡體   English   中英

在 pyomo 中使用索引集

[英]Use indexed sets in pyomo

我有一個來自 pyomo 網站的流量計算問題。 我更改了一些設置,因為我需要為不同的 model 測試一些東西。但是,更改后的版本沒有運行,我也不知道為什么。 代碼如下:

from pyomo.environ import AbstractModel, Param, minimize, Var, NonNegativeReals,\
    Objective, Constraint, SolverFactory, Set, summation, RangeSet

model = AbstractModel()

## Connections
# pipes
model.Pipes = Set()

## Nodes
# all nodes
model.Nodes = Set()
# Nodes or pipes that going out, e.g. from node a going out to b, c, d
model.NodesOut = Set(model.Nodes)
# Nodes or pipes that going in, e.g. in node a going e, f
model.NodesIn = Set(model.Nodes)

model.Flow = Var(model.Pipes, domain=NonNegativeReals)
model.FlowCost = Param(model.Pipes)

model.Demand = Param(model.Nodes)
model.Supply = Param(model.Nodes)

def Obj_rule(m):
    return summation(m.FlowCost, m.Flow)
model.Obj = Objective(rule=Obj_rule, sense=minimize)

def FlowBalance_rule(m, node):
    return m.Supply[node] \
        + sum(m.Flow[m.NodesIn[node][pipe]] for pipe in m.NodesIn[node]) \
        - m.Demand[node] \
        - sum(m.Flow[m.NodesIn[node][pipe]] for pipe in m.NodesOut[node]) \
        == 0
model.FlowBalance = Constraint(model.Nodes, rule=FlowBalance_rule)

dict_data = {
    None: {
        'Pipes': {None: ['AB', 'AC', 'CB']},
        'Nodes': {
            None: ['CityA', 'CityB', 'CityC']
        },
        'NodesIn': {
            'CityA': [],
            'CityB': ['AB', 'CB'],
            'CityC': ['AC',]
        },
        'NodesOut': {
            'CityA': ['AB', 'AC'],
            'CityB': [],
            'CityC': ['CB',]
        },
        'Demand': {
            'CityA': 0,
            'CityB': 1,
            'CityC': 1,
        },
        'FlowCost': {
            'AB': 1.4,
            'AC': 2.7,
            'CB': 1.6,
        },
        'Supply': {
            'CityA': 2,
            'CityB': 0,
            'CityC': 0,
        }
    }
}

我收到以下錯誤:IndexError:NodesIn[CityA] 索引必須是整數,而不是 str。 我明白這個錯誤,但我不明白為什么不能使用字符串。 Pyomo 解決這個 pipe 流問題如下:

from pyomo.environ import AbstractModel, Param, minimize, Var, NonNegativeReals,\
    Objective, Constraint, SolverFactory, Set, summation, RangeSet

model = AbstractModel()

## Connections
# # all connections
model.Arcs = Set(dimen=2)

## Nodes
# all nodes
model.Nodes = Set()
# Nodes or pipes that going out, e.g. from node a going out to b, c, d
model.NodesOut = Set(model.Nodes)
# Nodes or pipes that going in, e.g. in node a going e, f
model.NodesIn = Set(model.Nodes)

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(m):
    return summation(m.FlowCost, m.Flow)
model.Obj = Objective(rule=Obj_rule, sense=minimize)

def FlowBalance_rule(m, node):
    return m.Supply[node] \
        + sum(m.Flow[i, node] for i in m.NodesIn[node]) \
        - m.Demand[node] \
        - sum(m.Flow[node, j] for j in m.NodesOut[node]) \
        == 0
model.FlowBalance = Constraint(model.Nodes, rule=FlowBalance_rule)

dict_data = {
    None: {
        'Arcs': {None: [('CityA', 'CityB'), ('CityA', 'CityC'), ('CityC', 'CityB')]},
        'Nodes': {
            None: ['CityA', 'CityB', 'CityC']
        },
        'NodesIn': {
            'CityA': [],
            'CityB': ['CityA', 'CityC'],
            'CityC': ['CityA',]
        },
        'NodesOut': {
            'CityA': ['CityB', 'CityC'],
            'CityB': [],
            'CityC': ['CityB',]
        },
        'Demand': {
            'CityA': 0,
            'CityB': 1,
            'CityC': 1,
        },
        'FlowCost': {
            ('CityA', 'CityB'): 1.4,
            ('CityA', 'CityC'): 2.7,
            ('CityC', 'CityB'): 1.6,
        },
        'Supply': {
            'CityA': 2,
            'CityB': 0,
            'CityC': 0,
        }
    }
}

對於更復雜的問題,我需要使用代碼的第一個版本(使用索引集 m.NodesIn)。

我已經解決了代碼 I 的問題。FlowBalance_rule 必須重新表述如下:

def FlowBalance_rule(m, node):
    return m.Supply[node] \
        + sum(m.Flow[pipe] for pipe in m.NodesIn[node]) \
        - m.Demand[node] \
        - sum(m.Flow[pipe] for pipe in m.NodesOut[node]) \
        == 0
model.FlowBalance = Constraint(model.Nodes, rule=FlowBalance_rule)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM