【问题标题】:Python PULP Unsupported Operand Type(s) TypeErrorPython PULP 不支持的操作数类型 TypeError
【发布时间】:2019-06-12 22:55:41
【问题描述】:

我正在尝试在 PULP 中设置一些特定的约束,并且觉得我缺少一些简单的东西。所有的问题都是相同的 TypeError,在 'generator' 和 int 或连续之间显示 UNSUPPORTED OPERAND。

我尝试了各种解决方案,提供了我所拥有的代码,但不起作用。

YPER = 365
HE = 24

yearlyhours = [(i,j) for i in range(YPER) for j in range(HE)]

YAHL = pulp.LpVariable.dicts('YAHL', yearlyhours, lowBound=0, cat='Continuous')
YALL = pulp.LpVariable.dicts('YALL', yearlyhours, lowBound=0, cat='Continuous')
YAHLINT = pulp.LpVariable.dicts('YAHLINT', yearlyhours, lowBound=0, cat='Integer')
YAHLBIN = pulp.LpVariable.dicts('YAHLBIN', yearlyhours, lowBound=0, cat='Binary')

model += pulp.lpSum([YAHLINT[(i,j)] for i in range(YPER) for j in range(HE) if j >= 7 and j <= 22]) == (YAHL[i][j] for i in range(YPER) for j in range(HE) if j >= 7 and j <= 22) / 25

model += pulp.lpSum([YAHL[(i,j)] for i in range(YPER) for j in range(HE) if j >= 7 and j <= 22]) >= 0 * (YAHLBIN[i][j] for i in range(YPER) for j in range(HE) if j >= 7 and j <= 22)

TypeError: /: 'generator' 和 'int' 的操作数类型不受支持

TypeError: *: 'int' 和 'generator' 的操作数类型不受支持

【问题讨论】:

    标签: python optimization pulp


    【解决方案1】:

    您在此处使用generator expression

    (YAHL[i][j] for i in range(YPER) for j in range(HE) if j >= 7 and j <= 22)
    

    我猜这不是你真正想要的。

    你应该根据你的目标改变

    例如,

    == pulp.lpsum([YAHL[i][j] for i in range(YPER) for j in range(HE) if j >= 7 and j <= 22])
    

    【讨论】:

      猜你喜欢
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 2020-02-29
      • 2011-03-08
      • 2016-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多