0

我正在尝试写这样的东西(这是约束的一部分):

sum(a in ArcsOut[po]) F[a][p] for all p=(o,d) 成对。

我的代码是:

我有一组节点:

nodes = ["uno","dos","tres","cuatro","cinco","seis"]

及其所有可能对的集合

pairs = [(i,j) for i in nodes for j in nodes if i!=j]

我有一套ARCS。另外,我定义了集合 ArcsOut[i],它包含 ARCS 中的所有弧,并且它的第一个组件是 i。

二进制变量 F[a][p]:

F = [[mdl.binary_var_dict(ARCS, name= "F")] for p in pairs]

也就是说,对于成对的每个 p,都有一个长度为弧数的二进制向量。

在这种情况下,我试图写出开头的总结:

mdl.sum([(F[a][p]) for a in ArcsOut[p[0]]] for p in pairs)

但是,我自然会出错。

完整代码:

nodes = ["uno","dos","tres","cuatro","cinco","seis"]

EDGES = [("uno", "dos"),
     ("uno", "tres"), 
     ("dos", "tres"), 
     ("dos", "cuatro"), 
     ("tres", "cuatro"), 
     ("tres", "cinco"), 
     ("cuatro", "cinco"), 
     ("cuatro", "seis"), 
     ("cinco", "seis")]

INVERSES =[(j,i) for (i,j) in EDGES]

ARCS = EDGES + INVERSES

ArcsOut = defaultdict(list)
for k, v in ARCS:
    ArcsOut[k].append((k,v))


pairs = [(i,j) for i in nodes for j in nodes if i!=j]


costY = [2, 3, 2.2, 3, 2.5, 1.3]


costX = [1.7, 2.7, 2.1, 3, 2.6, 1.7, 2.8, 2.4, 1.9]

max = 25


d = [9, 26, 19, 13, 12, 11, 14, 26, 7, 18, 30, 19, 30, 24, 8, 21, 9,
         11, 22, 16, 14, 14, 8, 9, 20, 26, 1, 22, 24, 13]


mdl = CpoModel()


Y = mdl.binary_var_dict(nodes, name = "Y") 

X = mdl.binary_var_dict(EDGES, name = "X") 

Z = mdl.binary_var_dict(pairs, name = "Z") 

F = [[mdl.binary_var_dict(ARCS, name= "F")] for p in pairs]


mdl.part1 = mdl.sum([(costX[i]*X[e]) for i, e in enumerate(EDGES)])
mdl.part2 = mdl.sum([(costY[i]*Y[e]) for i, e in enumerate(nodes)])
mdl.add(mdl.part1 + mdl.part2 <= max)

prod = [(d[i]*Z[p]) for i, p in enumerate(pairs)]
obj = mdl.sum(prod)
mdl.add(mdl.maximize(obj))

mdl.add(aX[e]<=Y[e[0]] for e in EDGES)
mdl.add(X[e]<=Y[e[1]] for e in EDGES)

我对下一个约束有疑问:

mdl.add(mdl.sum([(F[p][a])  for a in ArcsOut[p[0]]]) == Z[p]  for p in pairs)

我该怎么写?提前致谢!

4

0 回答 0