Python 中的优化:'Var' 对象不可迭代

Optimization in Python: 'Var' object is not iterable

我希望这不是重复的,但我似乎找不到这个问题的具体答案。我是 Python 的新手,所以它可能很明显,但我似乎找不到我的错误。

所以问题是:我正在使用 gurobi 来优化网络流模型。但是我在制定约束时遇到了一些麻烦,因为我必须迭代 2 个变量(我想这就是问题所在)。

首先是代码:

from gurobipy import *
import optimization

def beer(P, S, W, SP, g, d, b, c, m, n):

    # Create new model
    m = Model("Beer-Flow")

    # Create variables
    x = { (i,j,p) : m.addVar(name = "x[%s,%s,%s]" % (i,j,p)) for i,j in W for p in P }
    y = { (i,j) : m.addVar(name = "y[%s,%s]" % (i,j)) for i,j in W }

    # Integrate variables
    m.update()

    # Set objective
    m.setObjective(quicksum(y[i,j] * d[i,j] * c for (i,j) in W ), GRB.MINIMIZE)

    # Add constraints
    for i in S:
        m.addConstr((quicksum(x[i2,j,p])for p in P for (i2,j) in W if i2 == i ) - (quicksum(x[i2,j,p]for p in P for (i2,j) in W if j == i)) == b[i,p])

    for i,j in W:
        m.addConstr(m * y[i,j] >= (quicksum(x[i,j,p] * w[p] for p in P)))
        m.addConstr(n * y[i,j] >= (quicksum(x[i,j,p] for p in P)))

    return m

# Fill sets and parameters
P, g = multidict({"full" : 12, "empty" : 4 })
S = {"Brauerei","München","Stuttgart","Göttingen","Bielefeld","Magdeburg" }
W, d = multidict({("Brauerei", "München") : 558, ("Brauerei", "Stuttgart") : 437, ("Brauerei","Göttingen") : 142,
                  ("Brauerei", "Bielefeld") : 45, ("Brauerei", "Magdeburg") : 290, ("München", "Brauerei") : 558,                           
                  ("München", "Stuttgart") : 232, ("München", "Göttingen") : 500, ("München", "Bielefeld") : 600,
                  ("München", "Magdeburg") : 523, ("Stuttgart", "Brauerei") : 437, ("Stuttgart", "München") : 232,
                  ("Stuttgart", "Göttingen") : 408, ("Stuttgart", "Bielefeld") : 492, ("Stuttgart", "Magdeburg") : 572,
                  ("Göttingen", "Brauerei") : 142, ("Göttingen", "München") : 500, ("Göttingen", "Stuttgart") : 408,
                  ("Göttingen", "Bielefeld") : 180, ("Göttingen", "Magdeburg") : 197, ("Bielefeld", "Brauerei") : 45,
                  ("Bielefeld", "München") : 600, ("Bielefeld", "Stuttgart") : 492, ("Bielefeld", "Göttingen") : 180,
                  ("Bielefeld", "Magdeburg") : 254, ("Magdeburg", "Brauerei") : 290, ("Magedeburg", "München") : 523,
                  ("Magdeburg", "Stuttgart") : 572, ("Magdeburg", "Göttingen") : 197, ("Magdeburg", "Bielefeld") : 254})      
SP, b = multidict({("München", "full") : 1840, ("Brauerei", "empty") : -1700, ("Stuttgart", "full") : 1400, ("Stuttgart", "empty") : -1550,
                   ("Göttingen","full") : 380, ("Göttingen", "empty") : - 400, ("Bielefeld", "full") : 840, ("Bielefeld", "empty") : -800,
                   ("Magdeburg", "full") : 600, ("Magdeburg", "empty") : -500, ("Brauerei", "full") : -5600, ("Brauerei", "empty") : 4950})
c = 1
m = 24000
n = 2000


# Create model for given sets and parameters
m = beer(P, S, W, SP, g, d, b, c, m, n)

# Run optimization and print results
m.optimize()
optimization.print_result(m)

无效的部分是:

for i in S:
            m.addConstr((quicksum(x[i2,j,p])for p in P for (i2,j) in W if i2 == i ) - (quicksum(x[i2,j,p]for p in P for (i2,j) in W if j == i)) == b[i,p])

错误是:'Var'类型不可迭代 例如,在 Java 中,我可以通过 p 和 i 的两个循环来完成它(在 Python 中尝试过,没有用),但我不知道如何用 [= 解决这个问题28=].

提前致谢

看起来像一个错误的地方:

m.addConstr((quicksum(x[i2,j,p])for p in P for (i2,j) in W if i2 == i ) - (quicksum(x[i2,j,p]for p in P for (i2,j) in W if j == i)) == b[i,p])
                               ^ Closes the quicksum call on a single value, not the whole genexpr

quicksum 在同一查询中的其他用法:

m.addConstr((quicksum(x[i2,j,p])for p in P for (i2,j) in W if i2 == i ) - (quicksum(x[i2,j,p]for p in P for (i2,j) in W if j == i)) == b[i,p])
                                                                                             ^ Note no paren, so it's quicksum-ing the whole generator expression