Linear optimization with python

Linear optimization with python

Linear programming is the technique used to maximize or minimize a function. The idea is to optimize a complex function by best representing them with linear relationships. In simpler terms, we try to optimize (to maximize or minimize) a function denoted in linear terms and bounded by linear constraints.

Use case — Profit Maximization

Let’s Consider a manufacture company having two product, A and B and sell them with the profit of 4 and 5 Rs. Each product processed on two machines, X and Y. Type A product takes 2 minutes on X and 2 minutes on Y. Type B requires 2 minutes on X and 3 minutes on Y. The machine X is available not more than 5 hours and 30 min a day. Machine Y is available for 8 hours a day

Step 1: Let’s consider x1 is number of product A and x2 is number of product B

Step 2: For product A 4 Rs. is profit and for product B 5 Rs. is profit. So total profit is:

                        Z = 4x1 + 5x2 

Step 3: Now lets add a constraints. As Machine X is only available for 4 hours and 30 min and 2 minutes processing is required for product A and 2 minutes processing is requires for product B so total processing required is:

                        2x1 + 2x2 <= 330

Similarly, machine Y is only available for 8 hours and 2 minutes processing is required for product A and 3 minutes processing is requires for product B so total processing required is:

                        2x1 + 3x2 <= 480

Also number of production for each A and B is always grater than or equal to 0 so:

                        x1,x2 >= 0

So, now our problem is:

                        Maximize Z = 4x1 + 5x2

with consrtaints:

                         2x1 + 2x2 <= 330
                         2x1 + 3x2 <= 480
                         x1, x2 >= 0

Now let’s see how we can use pulp in python to solve this linear optimization problem

First, We import the PuLP package.

Then, We define our problem by giving a suitable name, also specifying that our aim is to maximize the objective function.

After that we define LpVariable to hold the variables of objective functions. The next arguments specifies the lower and upper bound of the defined variable. As per 3rd constraint, its non-negative, hence made second argument as 0 and 3rd as None (in-place of infinity). The last argument says if the required output is discrete or continuous.

Finally denotes the objective function in terms of defined variables, along with a short description. and add constraints in terms of variables.

In [1]:
# import PuLP
from pulp import *

# Create the 'prob' variable to contain the problem data
prob = LpProblem("The Product maximization", LpMaximize)

# Create problem variables
x1=LpVariable("Product_1_units",0,None,LpInteger)
x2=LpVariable("Product_2_units",0, None, LpInteger)

# The objective function is added to 'prob' first
prob += 4*x1 + 5*x2, "Profit to be maximized"
# The two constraints are entered
prob += 2*x1 + 2*x2 <= 330, "Machine X constraint"
prob += 2*x1 + 3*x2 <= 480, "Machine Y constraint"

# The problem data is written to an .lp file
prob.writeLP("Productmax.lp")

# The problem is solved using PuLP's choice of Solver
prob.solve()
Out[1]:
1
In [2]:
# The status of the solution is printed to the screen
print("Status:", LpStatus[prob.status])

for v in prob.variables():
    print(v.name, "=", v.varValue)

# The optimised objective function value is printed to the screen
print("Total units can to be manufactured = ", value(prob.objective))
Status: Optimal
Product_1_units = 15.0
Product_2_units = 150.0
Total units can to be manufactured =  810.0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.