在使用Python Pulp的约束中使用mod函数 [英] Use mod function in a constraint using Python Pulp

查看:206
本文介绍了在使用Python Pulp的约束中使用mod函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我正在尝试使用mod(),round()和int()的下一个表达式,但由于它们不支持LpAffineExpression,因此无法正常工作.

I am trying the next expressions using mod(), round() and int() but none works because they don't support LpAffineExpression.

probl + = lpSum(如果h [2] == b],则varSKU中[h的hs为[vars [h])%100 == 0

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

probl + = lpSum(如果h [2] == b],则在varSKU中为[vars [h] for h [2] == int(lpSum(如果h [2] = = b])/100)

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],如果h [2] == b])/100 == round(lpSum(如果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.

谢谢!

推荐答案

一种相当简单的方法:

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

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

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!

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

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

这篇关于在使用Python Pulp的约束中使用mod函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆