简体   繁体   English

Python纸浆约束

[英]Python pulp constraint

I am trying to add a constraint to linear programming problem in python using pulp library. 我正在尝试使用pulp库为python中的线性编程问题添加约束。 I tried code below. 我在下面尝试了代码。

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

where I and J have following indexes 我和J有以下索引

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

The error I get is SyntaxError: Generator expression must be parenthesized if not sole argument . 我得到的错误是SyntaxError: Generator expression must be parenthesized if not sole argument I have researched this link Generator expression must be parenthesized if not sole argument however it didn't seem to resolve my issue. 我研究了此链接生成器表达式,如果不是唯一的参数,必须将其括在括号中,但是它似乎无法解决我的问题。 Any help is appreciated. 任何帮助表示赞赏。

Or Duan's comment looks correct the syntax used should be 还是段的评论看起来正确,使用的语法应该是

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

but in this case it will be much faster if you use the lpSum() function 但是在这种情况下,如果使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM