问题
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