【问题标题】:Use mod function in a constraint using Python Pulp使用 Python Pulp 在约束中使用 mod 函数
【发布时间】:2017-12-21 16:39:46
【问题描述】:

我正在编写一个 LpProblem,我需要创建一个约束,其中一些变量的总和是 100...100、200、300...的倍数...

我正在尝试使用 mod()、round() 和 int() 的下一个表达式,但没有一个有效,因为它们不支持 LpAffineExpression。

probl += lpSum([vars[h] for h in varSKU if h[2] == b]) % 100 == 0

probl += lpSum([vars[h] for h in varSKU if h[2] == b]) / 100 == int(lpSum([vars[h] for h in varSKU if h[2] = = b]) / 100)

probl += lpSum([vars[h] for h in varSKU if h[2] == b]) / 100 == round(lpSum([vars[h] for h in varSKU if h[2] = = b]) / 100)

你能给我一些写这个约束的想法吗?

谢谢!

【问题讨论】:

    标签: python mathematical-optimization pulp mixed-integer-programming


    【解决方案1】:

    一种相当简单的方法:

    • 引入一个整数变量I
    • 将约束构建为:probl += lpSum([vars[h] for h in varSKU if h[2] == b]) == I*100
    • (根据需要约束I:例如I >= 1I <= N

    请记住:当有多个约束且 100 的倍数不一定相同时,每个约束都需要一个辅助变量 I_x

    (并且:您不能在纸浆或任何其他 LP 建模系统(round、int、mod、ceil、...)中使用 python 的运算符一般!您必须接受那些建模系统允许的规则/形式:在这种情况下 -> LpAffineExpression)

    【讨论】:

    • 整数变量I应该放在模型目标函数probl的什么位置?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    • 2018-05-24
    • 1970-01-01
    相关资源
    最近更新 更多