简体   繁体   English

在使用Python Pulp的约束中使用mod函数

[英]Use mod function in a constraint using Python Pulp

I am writing a LpProblem and I need to create a constraint where the sum of some variables is multiples of 100... 100, 200, 300... 我正在编写LpProblem,我需要创建一个约束,其中某些变量的总和是100 ... 100、200、300 ...的倍数。

I am trying the next expressions using mod(), round() and int() but none works because they don't support LpAffineExpression. 我正在尝试使用mod(),round()和int()的下一个表达式,但由于它们不支持LpAffineExpression,因此它们均无效。

probl += lpSum([vars[h] for h in varSKU if h[2] == b]) % 100 == 0 probl + = lpSum([hvarsKU中[h的vars [h],如果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([如果h [2] == b],则为[varSKU中h的vars [h])/ 100 == int(lpSum([如果h [2] == b,则为varSKU中h的vars [h] )/ 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) probl + = lpSum([如果h [2] == b],则[varsKU中的h为varS [h])/ 100 ==圆(lpSum(如果h [2] == b,则为varSKU中[h]为h中的h )/ 100)

Can you give me some ideas for write this constraint. 你能给我一些写这个约束的想法吗?

Thank you! 谢谢!

One fairly simple approach: 一种相当简单的方法:

  • introduce an integer-variable I 引入整数变量I
  • build your constraint as: probl += lpSum([vars[h] for h in varSKU if h[2] == b]) == I*100 将约束构建为: probl += lpSum([vars[h] for h in varSKU if h[2] == b]) == I*100
  • (constrain I as needed: eg I >= 1 ; I <= N ) (根据需要约束I :例如I >= 1I <= N

Keep in mind: when having multiple constraints and the multiples of 100 are not necessarily the same for your constraints, you will need one auxiliary variable I_x for each constraint! 请记住:当有多个约束并且100的倍数不一定与您的约束相同时,每个约束将需要一个辅助变量I_x

(And: you can't use python's operators in general within pulp or any other LP-modelling sytem (round, int, mod, ceil, ...)! You have to accept the rules/form those modelling-systems allow: in this case -> LpAffineExpression ) (而且: 通常,您不能在纸浆或任何其他LP建模系统(round,int,mod,ceil等)中使用python的运算符!您必须接受那些建模系统允许的规则/表格:这种情况-> LpAffineExpression

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM