Optimization In Real World: Maximize the Profit — Python

Uday Rao
4 min readMay 30, 2021

--

Optimization is an important and fascinating area of management science and operations research. Optimization lies at the heart of every machine learning model. As Data Scientists, we should use the Linear Programming Skill in the field of operational research. The full source code at https://github.com/raouday79/optimization_model

Every industry looks to maximize its profit, this can be done by minimizing its Raw Material Cost, Production Cost, and Production Capacity Utilization. We are going to build the optimization model to maximize the Profit.

Overview: Each optimization problem consists of three elements:

  1. Decision Variables: Describe our choice that is under our control.
  2. Objective Function: Describe a criterion that we wish to minimize eg. Cost or Maximize eg. Profit
  3. Constraints: Describe the limitation that restricts our choices for decision variables.

We are going to use Google OR for optimization.

Problem 1 :

Problem 1

Here, The company requires a production plan of P1, P2, and P3. So our decision variable will be p1, p2, and p3.

from ortools.linear_solver import pywraplp
import numpy as np
#Initilize the Solver
#We are using the Mixed integer Programming
#Variable can take only integer value
solver = pywraplp.Solver.CreateSolver('SCIP')
infinity = solver.infinity()

We have imported the Google OR linear_solver. Created the solver using the SCIP. That is used for Mixed Integer Problem. As we have required product in integer. The Next step will be creating the decision variable.

#Creating the decision 
p1 = solver.IntVar(0, infinity, 'product1')
p2 = solver.IntVar(0, infinity, 'product2')
p3 = solver.IntVar(0, infinity, 'product3')

p1, p2, and p3 are variables indicating product1, product2, and product3 respectively. The above problem statement is given the minimum amount of p1,p2, and p3 to be produced. This will be constrained for optimization.

#Minimum Production Demand Constrain
solver.Add(p1 >=100)
solver.Add(p2 >=200)
solver.Add(p3 >=150)

There is a requirement of M1 and M2 Raw Material for each of the products. The availability of M1, M2 are 500 and 400. The proportion of raw material required is given in the above table. The production plan will be constrained by the Raw Material availability. We can convert into the constrain in solver.

#Raw Material Constrain
cons1 = [(p1/2),p2,p3]
solver.Add(solver.Sum(cons1)<=500)
cons2 = [(2*p1),(p2/2),(p3/5)]
solver.Add(solver.Sum(cons2)<=400)

We can add constrain by using the solver add function. here the sum of the list should be less than or equal to 500 for M1. Similarly, for M2 it will be ≤400

Objective Function: The objective is to maximize the profit by the production plan. The Profit of P1, P2, and P3 are ₹2, ₹5, and ₹4 respectively. The amount of product produce will give the amount of profit. We can multiply the variable by the profit amount associated with it.

#DEfining the objective function
objective_terms = [(2*p1),5*p2,4*p3]
solver.Maximize(solver.Sum(objective_terms))

The next step will be calling the solver to solve the model and give output.

Wow!!! We got the Production Plan by maximizing the Profit and fulfill all the constraints. The profit Amount is ₹2300. This is the optimized production plan.

Problem 2:

Problem 2

In my previous post, we have used the Forecasting technique to predict future demand. Suppose the forecasted value for part1, part2, and part3 is 6, 5, and 4 units respectively. This will be the minimum quantity of each part to produce.

Step 1: Decision variable: In this, we have to give the part production plan. Then this will be our decision variable here. Lets x1, x2, and x3 represent the parts a,b, and c. Import the required library and create the solver object.

from ortools.linear_solver import pywraplp
import numpy as np
solver = pywraplp.Solver.CreateSolver('SCIP')
infinity = solver.infinity()
#Declaring the decision variable
x1 = solver.IntVar(0, infinity, 'part1')
x2 = solver.IntVar(0, infinity, 'part2')
x3 = solver.IntVar(0, infinity, 'part3')

There are two costs involved i.e Casting cost and Machine cost, that we need to subtract from the selling price. So, the Net profit amount will be Profit = Selling Price-(Casting Cost+Machine Cost). Next, will be multiplying profit by each variable and maximize it.

#Storing the vaiable in List
variable=[x1,x2,x3]
#Selling Price
selling_price = [46,55,75]
#raw Material Cost
raw_material_cost = [31,35,55]
#Manufacturing Cost
manufacturing_cost = [(100/25)+(150/25)+(150/40), (100/40)+(150/20)+(150/30), (100/25)+(150/20)+(150/40)]
#Creating objective list
obj_list =[]
for i in range(0,len(selling_price)):
obj_list.append((selling_price[i]-raw_material_cost[i]-manufacturing_cost[i])*variable[i])
#Maximization of profit, calling maximize function
solver.Maximize(solver.Sum(obj_list))

The capacity of Drilling, Shaping, and Polishing of each part is given in the above table. The capacity of each process should be up to 100% utilization only. This we can formulate by adding all the parts up to 1.

# Constrain for Drilling
cons1 = [(x1/25),(x2/40),(x3/25)]
# Constrain for Shaping
cons2 = [(x1/25),(x2/20),(x3/20)]
# Constrain for Polishing
cons3 = [(x1/40),(x2/30),(x3/40)]
#Adding the Constrain to solver with utilization limit upto 1
solver.Add(solver.Sum(cons1)<=1)
solver.Add(solver.Sum(cons2)<=1)
solver.Add(solver.Sum(cons2)<=1)

Finally, we will call the solver to solve the model.

Output 2

Conclusion: Now that we have built an optimization model, we would share the plan with the production team for maximum revenue generation. Optimization is crucial for investigating complex issues and solving the problem. Many practical problems can be modeled and solved with mathematical optimization techniques.

Source Code: https://github.com/raouday79/optimization_model

Thank You!!!

Keep Learning…Keep Exploring.

--

--

Uday Rao
Uday Rao

Written by Uday Rao

Data Scientist | Operation Research Scientist | Software Engineer

No responses yet