Python纸浆约束

时间:2017-04-09 04:31:35

标签: python pandas mathematical-optimization linear-programming pulp

我正在尝试使用pulp库在python中为线性编程问题添加约束。我试过下面的代码。

for week in range(14,52), i in I.index:
    k = week
    model += sum(x[(i, j, week, B)] for week in range(k, k+13), 
                                        j in J.index) <= 1

其中I和J有以下索引

I.index = ['A','B','C']
J.index = [1,2,3]

我得到的错误是SyntaxError: Generator expression must be parenthesized if not sole argument。我研究了这个链接 Generator expression must be parenthesized if not sole argument 但它似乎没有解决我的问题。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

或段的评论看起来正确使用的语法

for week in range(14,52), i in I.index:
    k = week
    model += sum(x[(i, j, week, B)] for week in range(k, k+13)
                                    for j in J.index) <= 1

但在这种情况下,如果使用lpSum()函数

,速度会快得多
for week in range(14,52), i in I.index:
    k = week
    model += lpSum(x[(i, j, week, B)] for week in range(k, k+13)
                                    for j in J.index) <= 1