Use mod function in a constraint using Python Pulp

岁酱吖の 提交于 2019-12-10 12:16:39

问题


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...

I am trying the next expressions using mod(), round() and int() but none works because they don't support 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)

Can you give me some ideas for write this constraint.

Thank you!


回答1:


One fairly simple approach:

  • introduce an integer-variable I
  • build your constraint as: probl += lpSum([vars[h] for h in varSKU if h[2] == b]) == I*100
  • (constrain I as needed: e.g. I >= 1; I <= 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!

(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)



来源:https://stackoverflow.com/questions/47929215/use-mod-function-in-a-constraint-using-python-pulp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!