在 networkx 方法上定义 Pyomo 集的问题

问题描述 投票:0回答:0

我正在使用 Networkx 包处理有向图,我需要的是在优化模型上使用其前辈的方法。假设存在一个只有 12 个节点的有向图。问题如下:

G = nx.DiGraph([(1, 2), (1, 3),(2, 4), (2, 5), (2, 6),(3, 6), (3, 7),(4, 9),(5, 9),(6, 8),(7, 11),(8, 9), (8, 10),(9, 12),(10, 12),(11, 10),])
number_of_station = 4
tasks = numpy.array(G.nodes())predecessors = numpy.array(G.predecessors)
I = range(tasks)B = range(number_of_station)m.I = Set(initialize= I)m.B = Set(initialize= B)m.PAIRS = Set(initialize = m.I * m.I, dimen=2, filter=lambda m, i, j: (i,j) in predecessors)
m.x = Var(m.I, m.B, domain=Boolean, name= "x", bounds=(0, 1))m.c3 = Constraint(m.PAIRS, rule=lambda m, i, j: sum(m.x[i,b]*b for b in m.B) <= sum(m.x[j,b]*b for b in m.B))

当我运行模型时,它没有显示任何抛出,但结果不正确。我猜它来自上面 PAIR 集 (!) 的定义以及它如何用于约束 3。我想知道是否有人可以指导我解决这个问题。

此外,我尝试了另一种形式的优先关系,但求解器返回以下问题:

S= [[1, 2], [1, 3],[2, 4], [2, 5], [2, 6],[3, 6], [3, 7],[4, 9],[5, 9],[6, 8],[7, 11],[8, 9], [8, 10],[9, 12],[10, 12],[11, 10],]
m.c3 = ConstraintList()for (i,j) in S:m.c3.add(sum(m.x[i,b]*b for b in m.B) <= sum(m.x[j,b]*b for b in m.B))

the issue: KeyError: "Index '(12, 0)' is not valid for indexed component 'x'"

pyomo
© www.soinside.com 2019 - 2024. All rights reserved.