TypeError: unsupported operand type(s) for +: 'generator' and 'generator'

痞子三分冷 提交于 2019-12-06 07:22:46

(quicksum(item_rev) for item_rev in expr_rev) evaluates to a generator expression.

If the one line for loop is inside the parenthesis - (...) - you get a generator object. Here's a small example to illustrate what I mean:

>>> (x for x in range(5)) # shorthand for creating generators
<generator object <genexpr> at 0xb74308ec>

See docs for more info.

It seems you're trying to pass individual items from given lists to quicksum, but instead you're creating generators, unintentionally.

To fix this error, directly pass the objects to quicksum:

m.setObjective(
    quicksum(expr_rev) - (quicksum(expr_pc) + quicksum(expr_sc)),
    GRB.MAXIMIZE
)

UPDATE:

There also seems to be a problem at

expr_sc.append(quicksum(r_jk[(j, k)]*x[(i, j, k)]) for i in I)

Change that line like this:

expr_sc.append(quicksum(r_jk[(j, k)] * x[(i, j, k)] for i in I))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!