Introduction to Linear Optimization
Parag Verma
12th July, 2022
Introduction
The goal of every enterprise or individual is to maximize performance.Now performance can be measured by various metrics such as profit, sales, reduction in cost, weight loss etc. These are things that are performed continuously day in day out. Hence it is important to understand the mathematical model that helps achieve these specific goals.In this blog we will start with a very simple example of Linear Optimization. We are calling this Linear Optimization as Objective function and constraints are both linear in nature(Dont get afraid !!!. We will cover everything).
We will be using the lpSolve library to solve the problem.
package.name<-c("dplyr","lpSolve")
for(i in package.name){
if(!require(i,character.only = T)){
install.packages(i)
}
library(i,character.only = T)
}
Step 1: The Problem
Suppose a company wants to maximize the profit for two products A and B which are sold at $25 and $20 respectively.
There are 1800 resource units available every day and product A requires 20 units while B requires 12 units.
Both of these products require a production time of 4 minutes and the total available working hours are 8 in a day.
What should be the production quantity for each of the products to maximize profits?
Step 2: Defining the Objective Function
Let X1 be the unit of Product A sold in the market
Let X2 be the unit of Product B sold in the market.
Hence the objective function will become Max(Sales) = 25X1 + 20X2
X1 and X2 are the decision variables
Step 3: Defining the Raw Material constraint
1800 units of raw material is available to produce products on any given day.Product A requires 20 units and Product B requires 12 units.Hence the equation will be:
20X1 + 12X2 <= 1800
Step 4: Defining the Time constraint__
Each product requires 4 minutes to produce.The total hours available in a day are 8 hours. Hence:
4X1 + 4X2 <= 8*60
Step 5: Defining the objective function in R
objective.in<-c(25,20)
Step 6: Creating the Constraint Matrices
Const.mat<-matrix(c(20,12,4,4),byrow = T,nrow = 2)
colnames(Const.mat)<-c("X1","X2")
Const.mat
X1 X2
[1,] 20 12
[2,] 4 4
Time and raw material constraints
time.constraint<-8*60
material_constraint<-1800
Const.rhs<-c(material_constraint,time.constraint)
Const.rhs
[1] 1800 480
Equality/Inequality constraints
Const.dir<-c("<=","<=")
Const.dir
[1] "<=" "<="
Step 7: Identifying the Optimal Solution
Optimal_solution<-lp(direction="max",objective.in,Const.mat,Const.dir,Const.rhs)
Optimal_solution
Success: the objective function is 2625
Max Sales
Optimal_solution$solution
[1] 45 75
The total maximum sales is 2625 and for that the 45 units of product A and 75 units of Product should be sold