无法对 LpVariable 应用除法

问题描述

大家好,

我对线性规划问题还是个新手,在尝试设置成本时出现以下错误 使用“除法”运算符的变量。它使用乘法 (*) 运算符顺利运行。

line_cmn_var= LpVariable.dicts('md_line',line_keys,None,'Integer')


cost= lpSum(line_cmn_var[(l,c)] / md_line.loc[l,c] for c in cmn for l in lines )


md_model= LpProblem('production',LpMinimize)
md_model += cost

在定义成本时出现以下错误 错误 cost= lpSum(line_cmn_var[(l,c] for c in cmn for l in lines )

TypeError: unsupported operand type(s) for /: 'LpVariable' and 'int'

Thanks

解决方法

试试:

cost= lpSum(line_cmn_var[(l,c)] * (1.0 / md_line.loc[l,c]) for c in cmn for l in lines )