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

3
  • What do you mean by 'rounded to 100'? Could you provide an example? Commented Jun 13, 2019 at 15:48
  • Like I want results be 100 multiplier, if it returns 90 I wanted to be 100 Commented Jun 13, 2019 at 15:58
  • Can you share the code snippet and share where it causes the error ? Commented Jun 13, 2019 at 16:58

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. Can you also take a look at this one:stackoverflow.com/questions/57949190/…
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.