Using Python
Install Anaconda:
Please install Anaconda and Jupytor Notebook
Install Python Packages using following syntax:
pip install numpy
pip install PuLP
Refer the following links for more details:
https://pypi.org/project/numpy/
https://pypi.org/project/PuLP/
Example 3: Maximization Problem
Max Z = 20X1+60X2
S.t.
30X1+20X2 ≤ 2700
5X1+10X2 ≤ 850
X1+X2 ≤ 95
X1, X2 ≥ 0
The python syntax is as follows:
# import the library pulp as p
import pulp as p
# Create a LP Maximization problem
Lp_prob = p.LpProblem('Problem', p.LpMaximize)
# Create problem Variables
X1 = p.LpVariable("X1", lowBound = 0) # Create a variable X1 >= 0
X2 = p.LpVariable("X2", lowBound = 0) # Create a variable X2 >= 0
# Objective Function
Lp_prob += 20 * X1 + 60 * X2
# Constraints:
Lp_prob += 30 * X1 + 20 * X2 <= 2700
Lp_prob += 5*X1 + 10*X2 <= 850
Lp_prob += X1 + X2 <= 95
# Display the problem
print(Lp_prob)
status = Lp_prob.solve() # Solver
print(p.LpStatus[status]) # The solution status
# Printing the final solution
print(p.value(X1), p.value(X2), p.value(Lp_prob.objective))
Solution is:
Problem: MAXIMIZE 20*X1 + 60*X2 + 0 SUBJECT TO _C1: 30 X1 + 20 X2 <= 2700 _C2: 5 X1 + 10 X2 <= 850 _C3: X1 + X2 <= 95 VARIABLES X1 Continuous X2 Continuous Optimal 0.0 85.0 5100.0
Min Z = 20X1+60X2
S.t.
30X1+20X2 ≥ 2700
5X1+10X2 ≥ 850
X1+X2 ≥ 95
X1, X2 ≥ 0
The python syntax is as follows:
# Create a LP Minimization problem
Lp_prob = p.LpProblem('Problem', p.LpMinimize)
# Create problem Variables
X1 = p.LpVariable("X1", lowBound = 0) # Create a variable X1 >= 0
X2 = p.LpVariable("X2", lowBound = 0) # Create a variable X2 >= 0
# Objective Function
Lp_prob += 20 * X1 + 60 * X2
# Constraints:
Lp_prob += 30 * X1 + 20 * X2 >= 2700
Lp_prob += 5*X1 + 10*X2 >= 850
Lp_prob += X1 + X2 >= 95
# Display the problem
print(Lp_prob)
status = Lp_prob.solve() # Solver
print(p.LpStatus[status]) # The solution status
# Printing the final solution
print(p.value(X1), p.value(X2), p.value(Lp_prob.objective))
Solution is:
Problem: MINIMIZE 20*X1 + 60*X2 + 0 SUBJECT TO _C1: 30 X1 + 20 X2 >= 2700 _C2: 5 X1 + 10 X2 >= 850 _C3: X1 + X2 >= 95 VARIABLES X1 Continuous X2 Continuous Optimal 170.0 0.0 3400.0
Sensitivity Analysis Using Python
import pulp
import pandas as pd
D = pulp.LpVariable('D', lowBound=0)
S = pulp.LpVariable('S', lowBound=0)
profit = pulp.LpProblem('maximum
profit', pulp.LpMaximize)
profit += 10*S + 9*D, 'Objective
Function'
profit += 7/10*S + D <= 630,
'Constraint for cutting and dying'
profit += 0.5*S + 5/6*D <=600,
'Constraint for sewing'
profit += S + 2/3*D<=708, 'Constraint
for finishing'
profit += 1/10*S + 1/4*D<=135,
'Constraint for I&P'
profit.solve()
for variable in profit.variables():
print(variable.name, '=', variable.varValue)
print('Maximum Profit
:',pulp.value(profit.objective))
#Sensitivity Analysis
'''
cname = name of the constraint
cinfo = info about the constraint such
as shadow price and slack variable
'''
SA = [{'Constraint Name ': cname, 'Slack
Values':cinfo.slack, 'Shadow price': cinfo.pi} for cname, cinfo in
profit.constraints.items()]
df = pd.DataFrame(SA)
print(df)
Solution is:
D = 252.0
S = 540.0
Maximum Profit : 7668.0
Constraint Name Slack
Values Shadow price
0
Constraint_for_cutting_and_dying
-0.0 4.3750
1
Constraint_for_sewing
120.0 -0.0000
2
Constraint_for_finishing
-0.0 6.9375
3
Constraint_for_I&P
18.0 -0.0000
Using R
install.packages("lpSolve")
Example 1: Maximization Problem
Max
Z = 20X1+60X2
S.t.
30X1+20X2
≤ 2700
5X1+10X2
≤ 850
X1+X2
≤ 95
X1, X2 ≥ 0
library(lpSolve)
obj.fun<-c(20,60)
constr<-matrix(c(30,20,5,10,1,1),ncol=2,byrow=TRUE)
constr.dir<-c("<=","<=","<=")
rhs<-c(2700,850,95)
prod.sol<-lp("max",obj.fun,constr,constr.dir,rhs,compute.sens=TRUE)
prod.sol$solution
lp("max", obj.fun, constr, constr.dir, rhs)
lp("max", obj.fun, constr, constr.dir, rhs, compute.sens=TRUE)$duals
SolutionVariables: X1 = 0; X2 = 85
Min Z = 20X1+60X2
S.t.
30X1+20X2
≥ 2700
5X1+10X2
≥ 850
X1+X2
≥ 95
X1, X2
≥ 0
The syntax for R Software:
library(lpSolve)
obj.fun<-c(20,60)
constr<-matrix(c(30,20,5,10,1,1),ncol=2,byrow=TRUE)
constr.dir<-c(">=",">=",">=")
rhs<-c(2700,850,95)
prod.sol<-lp("min",obj.fun,constr,constr.dir,rhs,compute.sens=TRUE)
prod.sol$solution
lp("min", obj.fun, constr, constr.dir, rhs)
lp("min", obj.fun, constr, constr.dir, rhs, compute.sens=TRUE)$duals
SolutionVariables: X1 = 170; X2 = 0
The objective function is 3400
Shadow Price: 0 4 0 Reduced Cost: 0 20
Good. Hope sensitivity ananlysis is possible using R
ReplyDeleteThank you Sir...
DeleteSensitivity analysis will add soon.
Very informative. Please keep on writing such blogs!
ReplyDeleteThank you Abhinav.
DeleteI will add other important Operations Research topics also.
Thank You sir
ReplyDeleteWelcome...
ReplyDeleteHappy to see your posts. Keep enlarging this learning space - Rajesh
ReplyDeleteThank you Sir...
DeleteI will try to add more topics...
Useful Information
ReplyDeleteThank you Sir..
ReplyDeleteVery informative sir :)
ReplyDeleteThank you Dipshi...
Deleteit is very useful & informative ..it is a great work Sir
ReplyDelete