【问题标题】:Python pulp constraintPython纸浆约束
【发布时间】:2017-04-09 04:31:34
【问题描述】:

我正在尝试使用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 但是它似乎并没有解决我的问题。任何帮助表示赞赏。

【问题讨论】:

  • Idk 纸浆,但我认为即使你的第一行也是错误的,因为它编译为类似:for week in (range(14,52), i in I.index),所以你的 for 循环只是迭代 2 个元素,第一个是 @ 987654327@ generator(也许这是问题所在),第二个是 bool 语句(如 [1,2,3]==True 中的 1)

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


【解决方案1】:

或者段的评论看起来正确,使用的语法应该是

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2017-03-31
    • 1970-01-01
    相关资源
    最近更新 更多