1

I'm using pulp for a linear optimization problem. I want the subject to be rounded to 100. How should I do it.

I saw the answer to this one, But I don't know how to defined a integer variable I in this case. Use mod function in a constraint using Python Pulp

Thank you!!!

hhd
  • 25
  • 1
  • 4

1 Answers1

2

Consider a simple ILP which consist of an objective function and constraints on the variables:

min x1 + x2
s.t.
x1 + x2 >= 50
x1 >= 0 
x2 >= 0

To enforce your condition you can add 2 variables y and z and 2 constraints:

  1. y >= x1 + x2
  2. y == 100 * j for some j >= 1

and change your objective function in min y.

In code:

Original formulation

x1 = pulp.LpVariable('x1',lowBound=0,cat=pulp.LpContinuous)
x2 = pulp.LpVariable('x2',lowBound=0,cat=pulp.LpContinuous)

prob1 = pulp.LpProblem('example1',pulp.LpMinimize)
# obj
prob1+= 5*x1 + 10*x2
# constraints
prob1+= x1 + x2 >= 50

prob1.solve() 
print(pulp.value(prob1.objective)) #250

Converted one

y = pulp.LpVariable('y',lowBound=0, cat=pulp.LpContinuous)
z = pulp.LpVariable('z',lowBound=1, cat=pulp.LpInteger)

prob2 = pulp.LpProblem('example2',pulp.LpMinimize)
# obj
prob2+= y
# constraints
prob2+= y >= 5*x1 + 10*x2
prob2+= y == 100 * z
prob2+= x1 + x2 >= 50

prob2.solve()
print(pulp.value(prob2.objective)) #300
abc
  • 11,579
  • 2
  • 26
  • 51
  • Thanks a lot. Can you also take a look at this one:https://stackoverflow.com/questions/57949190/how-to-apply-conditional-constraints-to-python-pulp-function – hhd Sep 17 '19 at 13:40
  • What would be the approach for applying a condition that each LP variable should be a multiple of 100? I am trying to solve a supply chain problem where the quantity of each part (i.e. LP variable) should be multiple of 100. Kindly help – Sri Pallavi Mar 25 '20 at 12:28